summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/settings/site_settings/site_settings_behavior.js
blob: 545c49afcba8677282cb4423fcd244bce4b31e94 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2015 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 Behavior common to Site Settings classes.
 */


/**
 * The source information on site exceptions doesn't exactly match the
 * controlledBy values.
 * TODO(dschuyler): Can they be unified (and this dictionary removed)?
 * @type {!Object}
 */
const kControlledByLookup = {
  'extension': chrome.settingsPrivate.ControlledBy.EXTENSION,
  'HostedApp': chrome.settingsPrivate.ControlledBy.EXTENSION,
  'platform_app': chrome.settingsPrivate.ControlledBy.EXTENSION,
  'policy': chrome.settingsPrivate.ControlledBy.USER_POLICY,
};


/** @polymerBehavior */
const SiteSettingsBehaviorImpl = {
  properties: {
    /**
     * The string ID of the category this element is displaying data for.
     * See site_settings/constants.js for possible values.
     * @type {!settings.ContentSettingsTypes}
     */
    category: String,

    /**
     * A cached list of ContentSettingsTypes with a standard allow-block-ask
     * pattern that are currently enabled for use. This property is the same
     * across all elements with SiteSettingsBehavior ('static').
     * @type {Array<settings.ContentSettingsTypes>}
     * @private
     */
    contentTypes_: {
      type: Array,
      value: [],
    },

    /**
     * The browser proxy used to retrieve and change information about site
     * settings categories and the sites within.
     * @type {settings.SiteSettingsPrefsBrowserProxy}
     */
    browserProxy: Object,
  },

  /** @override */
  created: function() {
    this.browserProxy =
        settings.SiteSettingsPrefsBrowserProxyImpl.getInstance();
  },

  /** @override */
  ready: function() {
    this.ContentSetting = settings.ContentSetting;
  },

  /**
   * Ensures the URL has a scheme (assumes http if omitted).
   * @param {string} url The URL with or without a scheme.
   * @return {string} The URL with a scheme, or an empty string.
   */
  ensureUrlHasScheme: function(url) {
    if (url.length == 0) {
      return url;
    }
    return url.includes('://') ? url : 'http://' + url;
  },

  /**
   * Removes redundant ports, such as port 80 for http and 443 for https.
   * @param {string} url The URL to sanitize.
   * @return {string} The URL without redundant ports, if any.
   */
  sanitizePort: function(url) {
    const urlWithScheme = this.ensureUrlHasScheme(url);
    if (urlWithScheme.startsWith('https://') &&
        urlWithScheme.endsWith(':443')) {
      return url.slice(0, -4);
    }
    if (urlWithScheme.startsWith('http://') && urlWithScheme.endsWith(':80')) {
      return url.slice(0, -3);
    }
    return url;
  },

  /**
   * Returns true if the passed content setting is considered 'enabled'.
   * @param {string} setting
   * @return {boolean}
   * @protected
   */
  computeIsSettingEnabled: function(setting) {
    return setting != settings.ContentSetting.BLOCK;
  },

  /**
   * Converts a string origin/pattern to a URL.
   * @param {string} originOrPattern The origin/pattern to convert to URL.
   * @return {URL} The URL to return (or null if origin is not a valid URL).
   * @protected
   */
  toUrl: function(originOrPattern) {
    if (originOrPattern.length == 0) {
      return null;
    }
    // TODO(finnur): Hmm, it would probably be better to ensure scheme on the
    //     JS/C++ boundary.
    // TODO(dschuyler): I agree. This filtering should be done in one go, rather
    // that during the sort. The URL generation should be wrapped in a try/catch
    // as well.
    originOrPattern = originOrPattern.replace('*://', '');
    originOrPattern = originOrPattern.replace('[*.]', '');
    return new URL(this.ensureUrlHasScheme(originOrPattern));
  },

  /**
   * Convert an exception (received from the C++ handler) to a full
   * SiteException.
   * @param {!RawSiteException} exception The raw site exception from C++.
   * @return {!SiteException} The expanded (full) SiteException.
   * @protected
   */
  expandSiteException: function(exception) {
    const origin = exception.origin;
    const embeddingOrigin = exception.embeddingOrigin;

    // TODO(patricialor): |exception.source| should be one of the values defined
    // in |settings.SiteSettingSource|.
    let enforcement = /** @type {?chrome.settingsPrivate.Enforcement} */ (null);
    if (exception.source == 'extension' || exception.source == 'HostedApp' ||
        exception.source == 'platform_app' || exception.source == 'policy') {
      enforcement = chrome.settingsPrivate.Enforcement.ENFORCED;
    }

    const controlledBy = /** @type {!chrome.settingsPrivate.ControlledBy} */ (
        kControlledByLookup[exception.source] ||
        chrome.settingsPrivate.ControlledBy.PRIMARY_USER);

    return {
      category: this.category,
      origin: origin,
      displayName: exception.displayName,
      embeddingOrigin: embeddingOrigin,
      incognito: exception.incognito,
      setting: exception.setting,
      enforcement: enforcement,
      controlledBy: controlledBy,
    };
  },

  /**
   * Returns list of categories for each setting.ContentSettingsTypes that are
   * currently enabled.
   * @return {!Array<!settings.ContentSettingsTypes>}
   */
  getCategoryList: function() {
    if (this.contentTypes_.length == 0) {
      for (const typeName in settings.ContentSettingsTypes) {
        const contentType = settings.ContentSettingsTypes[typeName];
        // <if expr="not chromeos">
        if (contentType == settings.ContentSettingsTypes.PROTECTED_CONTENT) {
          continue;
        }
        // </if>
        // Some categories store their data in a custom way.
        if (contentType == settings.ContentSettingsTypes.COOKIES ||
            contentType == settings.ContentSettingsTypes.PROTOCOL_HANDLERS ||
            contentType == settings.ContentSettingsTypes.ZOOM_LEVELS) {
          continue;
        }
        this.contentTypes_.push(contentType);
      }
    }

    const addOrRemoveSettingWithFlag = (type, flag) => {
      if (loadTimeData.getBoolean(flag)) {
        if (!this.contentTypes_.includes(type)) {
          this.contentTypes_.push(type);
        }
      } else {
        if (this.contentTypes_.includes(type)) {
          this.contentTypes_.splice(this.contentTypes_.indexOf(type), 1);
        }
      }
    };
    // These categories are gated behind flags.
    addOrRemoveSettingWithFlag(
        settings.ContentSettingsTypes.SERIAL_PORTS,
        'enableExperimentalWebPlatformFeatures');
    addOrRemoveSettingWithFlag(
        settings.ContentSettingsTypes.BLUETOOTH_SCANNING,
        'enableExperimentalWebPlatformFeatures');
    addOrRemoveSettingWithFlag(
        settings.ContentSettingsTypes.ADS,
        'enableSafeBrowsingSubresourceFilter');
    addOrRemoveSettingWithFlag(
        settings.ContentSettingsTypes.PAYMENT_HANDLER,
        'enablePaymentHandlerContentSetting');
    addOrRemoveSettingWithFlag(
        settings.ContentSettingsTypes.NATIVE_FILE_SYSTEM_WRITE,
        'enableNativeFileSystemWriteContentSetting');
    addOrRemoveSettingWithFlag(
        settings.ContentSettingsTypes.MIXEDSCRIPT,
        'enableInsecureContentContentSetting');
    return this.contentTypes_.slice(0);
  },

};

/** @polymerBehavior */
const SiteSettingsBehavior = [SiteSettingsBehaviorImpl];