summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/settings/site_settings/website_usage_private_api.js
blob: 17540e1d8765b8f744cd47ab9b06e6ba1307ece0 (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
// 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.

(function() {
'use strict';

Polymer({
  is: 'website-usage-private-api',

  properties: {
    /**
     * The amount of data used by the given website.
     */
    websiteDataUsage: {
      type: String,
      notify: true,
    },

    /**
     * The type of data used by the given website.
     */
    websiteStorageType: {
      type: Number,
      notify: true,
    },
  },

  /** @override */
  attached: function() {
    settings.WebsiteUsagePrivateApi.websiteUsagePolymerInstance = this;
  },

  /** @param {string} host */
  fetchUsageTotal: function(host) {
    settings.WebsiteUsagePrivateApi.fetchUsageTotal(host);
  },

  /**
   * @param {string} origin
   * @param {number} type
   */
  clearUsage: function(origin, type) {
    settings.WebsiteUsagePrivateApi.clearUsage(origin, type);
  },

  /** @param {string} origin */
  notifyUsageDeleted: function(origin) {
    this.fire('usage-deleted', {origin: origin});
  },
});
})();

cr.define('settings.WebsiteUsagePrivateApi', function() {
  /**
   * @type {Object} An instance of the polymer object defined above.
   * All data will be set here.
   */
  const websiteUsagePolymerInstance = null;

  /**
   * @type {string} The host for which the usage total is being fetched.
   */
  let hostName;

  /**
   * Encapsulates the calls between JS and C++ to fetch how much storage the
   * host is using.
   * Will update the data in |websiteUsagePolymerInstance|.
   */
  const fetchUsageTotal = function(host) {
    const instance =
        settings.WebsiteUsagePrivateApi.websiteUsagePolymerInstance;
    if (instance != null)
      instance.websiteDataUsage = '';

    hostName = host;
    chrome.send('fetchUsageTotal', [host]);
  };

  /**
   * Callback for when the usage total is known.
   * @param {string} host The host that the usage was fetched for.
   * @param {string} usage The string showing how much data the given host
   *     is using.
   * @param {number} type The storage type.
   */
  const returnUsageTotal = function(host, usage, type) {
    const instance =
        settings.WebsiteUsagePrivateApi.websiteUsagePolymerInstance;
    if (instance == null)
      return;

    if (hostName == host) {
      instance.websiteDataUsage = usage;
      instance.websiteStorageType = type;
    }
  };

  /**
   * Deletes the storage being used for a given origin.
   * @param {string} origin The origin to delete storage for.
   * @param {number} type The type of storage to delete.
   */
  const clearUsage = function(origin, type) {
    chrome.send('clearUsage', [origin, type]);
  };

  /**
   * Callback for when the usage has been cleared.
   * @param {string} origin The origin that the usage was fetched for.
   */
  const onUsageCleared = function(origin) {
    const instance =
        settings.WebsiteUsagePrivateApi.websiteUsagePolymerInstance;
    if (instance == null)
      return;

    instance.notifyUsageDeleted(origin);
  };

  return {
    websiteUsagePolymerInstance: websiteUsagePolymerInstance,
    fetchUsageTotal: fetchUsageTotal,
    returnUsageTotal: returnUsageTotal,
    clearUsage: clearUsage,
    onUsageCleared: onUsageCleared,
  };
});