summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/settings/site_settings/site_data.js
blob: b8657741880ff287e851a0005288ae567fbe1a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview
 * 'site-data' handles showing the local storage summary list for all sites.
 */

Polymer({
  is: 'site-data',

  behaviors: [SiteSettingsBehavior, WebUIListenerBehavior],

  properties: {
    /**
     * A summary list of all sites and how many entities each contain.
     * @type {Array<CookieDataSummaryItem>}
     */
    sites: Array,

    /**
     * The cookie tree with the details needed to display individual sites and
     * their contained data.
     * @type {!settings.CookieTreeNode}
     */
    treeNodes_: Object,

    /**
     * Keeps track of how many outstanding requests for more data there are.
     */
    requests_: Number,

    /**
     * The current filter applied to the cookie data list.
     */
    filterString_: {
      type: String,
      value: '',
    }
  },

  ready: function() {
    this.addWebUIListener('onTreeItemRemoved',
        this.onTreeItemRemoved_.bind(this));
    this.treeNodes_ = new settings.CookieTreeNode(null);
    // Start the initial request.
    this.reloadCookies_();
  },

  /**
   * Reloads the whole cookie list.
   * @private
   */
  reloadCookies_: function() {
    this.browserProxy.reloadCookies().then(function(list) {
      this.loadChildren_(list);
    }.bind(this));
  },

  /**
   * A filter function for the list.
   * @param {!CookieDataSummaryItem} item The item to possibly filter out.
   * @return {!boolean} Whether to show the item.
   * @private
   */
  showItem_: function(item) {
    if (this.filterString_.length == 0)
      return true;
    return item.site.indexOf(this.filterString_) > -1;
  },

  /** @private */
  onSearchChanged_: function(e) {
    this.filterString_ = e.detail;
    this.$.list.render();
  },

  /** @private */
  isRemoveButtonVisible_: function(sites, renderedItemCount) {
    return renderedItemCount != 0;
  },

  /**
   * Returns the string to use for the Remove label.
   * @return {!string} filterString The current filter string.
   * @private
   */
  computeRemoveLabel_: function(filterString) {
    if (filterString.length == 0)
      return loadTimeData.getString('siteSettingsCookieRemoveAll');
    return loadTimeData.getString('siteSettingsCookieRemoveAllShown');
  },

  /**
   * Called when the cookie list is ready to be shown.
   * @param {!CookieList} list The cookie list to show.
   * @private
   */
  loadChildren_: function(list) {
    var parentId = list.id;
    var data = list.children;

    if (parentId == null) {
      // New root being added, clear the list and add the nodes.
      this.sites = [];
      this.requests_ = 0;
      this.treeNodes_.addChildNodes(this.treeNodes_, data);
    } else {
      this.treeNodes_.populateChildNodes(parentId, this.treeNodes_, data);
    }

    for (var i = 0; i < data.length; ++i) {
      var prefix = parentId == null ? '' : parentId + ', ';
      if (data[i].hasChildren) {
        ++this.requests_;
        this.browserProxy.loadCookieChildren(
            prefix + data[i].id).then(function(list) {
          --this.requests_;
          this.loadChildren_(list);
        }.bind(this));
      }
    }

    if (this.requests_ == 0)
      this.sites = this.treeNodes_.getSummaryList();

    // If this reaches below zero then we're forgetting to increase the
    // outstanding request count and the summary list won't be updated at the
    // end.
    assert(this.requests_ >= 0);
  },

  /**
   * Called when a single item has been removed (not during delete all).
   * @param {!CookieRemovePacket} args The details about what to remove.
   */
  onTreeItemRemoved_: function(args) {
    this.treeNodes_.removeByParentId(args.id, args.start, args.count);
    this.sites = this.treeNodes_.getSummaryList();
  },

  /**
   * Deletes all site data for a given site.
   * @param {!{model: !{item: CookieDataSummaryItem}}} event
   * @private
   */
  onDeleteSite_: function(event) {
    this.browserProxy.removeCookie(event.model.item.id);
  },

  /**
   * Deletes site data for multiple sites.
   * @private
   */
  onDeleteMultipleSites_: function() {
    if (this.filterString_.length == 0) {
      this.browserProxy.removeAllCookies().then(function(list) {
        this.loadChildren_(list);
      }.bind(this));
    } else {
      var items = this.$.list.items;
      for (var i = 0; i < items.length; ++i) {
        if (this.showItem_(items[i]))
          this.browserProxy.removeCookie(items[i].id);
      }
    }
  },

  /**
   * @param {!{model: !{item: CookieDataSummaryItem}}} event
   * @private
   */
  onSiteTap_: function(event) {
    var dialog = document.createElement('site-data-details-dialog');
    dialog.category = this.category;
    this.shadowRoot.appendChild(dialog);

    var node = this.treeNodes_.fetchNodeById(event.model.item.id, false);
    dialog.open(node);

    dialog.addEventListener('close', function(event) {
      dialog.remove();
    });
  },
});