summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/settings/site_settings/chooser_exception_list.js
blob: 79c7539ca337cdc0f26d88dd22f1d695020eb9ed (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
// Copyright 2018 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
 * 'chooser-exception-list' shows a list of chooser exceptions for a given
 * chooser type.
 */
Polymer({
  is: 'chooser-exception-list',

  behaviors: [
    I18nBehavior,
    ListPropertyUpdateBehavior,
    SiteSettingsBehavior,
    WebUIListenerBehavior,
  ],

  properties: {
    /**
     * Array of chooser exceptions to display in the widget.
     * @type {!Array<ChooserException>}
     */
    chooserExceptions: {
      type: Array,
      value: function() {
        return [];
      },
    },

    /**
     * The string ID of the chooser type that this element is displaying data
     * for.
     * See site_settings/constants.js for possible values.
     * @type {!settings.ChooserType}
     */
    chooserType: {
      observer: 'chooserTypeChanged_',
      type: String,
      value: settings.ChooserType.NONE,
    },

    /** @private */
    emptyListMessage_: {
      type: String,
      value: '',
    },

    /** @private */
    hasIncognito_: Boolean,

    /** @private */
    tooltipText_: String,
  },

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

  /** @override */
  attached: function() {
    this.addWebUIListener(
        'contentSettingChooserPermissionChanged',
        this.objectWithinChooserTypeChanged_.bind(this));
    this.addWebUIListener(
        'onIncognitoStatusChanged', this.onIncognitoStatusChanged_.bind(this));
    this.browserProxy.updateIncognitoStatus();
  },

  /**
   * Called when a chooser exception changes permission and updates the element
   * if |category| is equal to the settings category of this element.
   * @param {settings.ContentSettingsTypes} category The content settings type
   *     that represents this permission category.
   * @param {settings.ChooserType} chooserType The content settings type that
   *     represents the chooser data for this permission.
   * @private
   */
  objectWithinChooserTypeChanged_: function(category, chooserType) {
    if (category === this.category && chooserType === this.chooserType) {
      this.chooserTypeChanged_();
    }
  },

  /**
   * Called for each chooser-exception-list when incognito is enabled or
   * disabled. Only called on change (opening N incognito windows only fires one
   * message). Another message is sent when the *last* incognito window closes.
   * @private
   */
  onIncognitoStatusChanged_: function(hasIncognito) {
    this.hasIncognito_ = hasIncognito;
    this.populateList_();
  },

  /**
   * Configures the visibility of the widget and shows the list.
   * @private
   */
  chooserTypeChanged_: function() {
    if (this.chooserType == settings.ChooserType.NONE) {
      return;
    }

    // Set the message to display when the exception list is empty.
    switch (this.chooserType) {
      case settings.ChooserType.USB_DEVICES:
        this.emptyListMessage_ = this.i18n('noUsbDevicesFound');
        break;
      case settings.ChooserType.SERIAL_PORTS:
        this.emptyListMessage_ = this.i18n('noSerialPortsFound');
        break;
      default:
        this.emptyListMessage_ = '';
    }

    this.populateList_();
  },

  /**
   * Returns true if there are any chooser exceptions for this chooser type.
   * @return {boolean}
   * @private
   */
  hasExceptions_: function() {
    return this.chooserExceptions.length > 0;
  },

  /**
   * Need to use a common tooltip since the tooltip in the entry is cut off from
   * the iron-list.
   * @param{!CustomEvent<!{target: HTMLElement, text: string}>} e
   * @private
   */
  onShowTooltip_: function(e) {
    this.tooltipText_ = e.detail.text;
    const target = e.detail.target;
    // paper-tooltip normally determines the target from the |for| property,
    // which is a selector. Here paper-tooltip is being reused by multiple
    // potential targets.
    this.$.tooltip.target = target;
    const hide = () => {
      this.$.tooltip.hide();
      target.removeEventListener('mouseleave', hide);
      target.removeEventListener('blur', hide);
      target.removeEventListener('tap', hide);
      this.$.tooltip.removeEventListener('mouseenter', hide);
    };
    target.addEventListener('mouseleave', hide);
    target.addEventListener('blur', hide);
    target.addEventListener('tap', hide);
    this.$.tooltip.addEventListener('mouseenter', hide);
    this.$.tooltip.show();
  },

  /**
   * Populate the chooser exception list for display.
   * @private
   */
  populateList_: function() {
    this.browserProxy_.getChooserExceptionList(this.chooserType)
        .then(exceptionList => this.processExceptions_(exceptionList));
  },

  /**
   * Process the chooser exception list returned from the native layer.
   * @param {!Array<RawChooserException>} exceptionList
   * @private
   */
  processExceptions_: function(exceptionList) {
    const exceptions = exceptionList.map(exception => {
      const sites = exception.sites.map(this.expandSiteException);
      return Object.assign(exception, {sites});
    });

    if (!this.updateList(
            'chooserExceptions', x => x.displayName, exceptions,
            true /* uidBasedUpdate */)) {
      // The chooser objects have not been changed, so check if their site
      // permissions have changed. The |exceptions| and |this.chooserExceptions|
      // arrays should be the same length.
      const siteUidGetter = x => x.origin + x.embeddingOrigin + x.incognito;
      exceptions.forEach((exception, index) => {
        const propertyPath = 'chooserExceptions.' + index + '.sites';
        this.updateList(propertyPath, siteUidGetter, exception.sites);
      }, this);
    }
  },
});