summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/settings/site_settings/website_usage_private_api.js
blob: 214b3ff9259806e678376ec6aba0f45588bec551 (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
// 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 number of cookies used by the given website.
     */
    websiteCookieUsage: {
      type: String,
      notify: true,
    },
  },

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

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

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

  /** @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.
   */
  const returnUsageTotal = function(host, usage, cookies) {
    const instance =
        settings.WebsiteUsagePrivateApi.websiteUsagePolymerInstance;
    if (instance == null) {
      return;
    }

    if (hostName == host) {
      instance.websiteDataUsage = usage;
      instance.websiteCookieUsage = cookies;
    }
  };

  /**
   * Deletes the storage being used for a given origin.
   * @param {string} origin The origin to delete storage for.
   */
  const clearUsage = function(origin) {
    chrome.send('clearUsage', [origin]);
    const instance =
        settings.WebsiteUsagePrivateApi.websiteUsagePolymerInstance;
    if (instance == null) {
      return;
    }

    instance.notifyUsageDeleted(origin);
  };

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