summaryrefslogtreecommitdiff
path: root/chromium/ui/webui/resources/cr_components/chromeos/network/network_select.js
blob: 5c385f28b6528ad00db407f8a93b5e5684c11b1c (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// 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 Polymer element wrapping network-list including the
 * networkConfig mojo API calls to populate it.
 */

(function() {

const mojom = chromeos.networkConfig.mojom;

Polymer({
  is: 'network-select',

  behaviors: [NetworkListenerBehavior],

  properties: {
    /**
     * Show all buttons in list items.
     */
    showButtons: {
      type: Boolean,
      value: false,
      reflectToAttribute: true,
    },

    /**
     * The list of custom items to display after the list of networks.
     * See NetworkList for details.
     * @type {!Array<NetworkList.CustomItemState>}
     */
    customItems: {
      type: Array,
      value() {
        return [];
      },
    },

    /** Whether to show technology badges on mobile network icons. */
    showTechnologyBadge: {type: Boolean, value: true},

    /**
     * Whether to show a progress indicator at the top of the network list while
     * a scan (e.g., for nearby Wi-Fi networks) is in progress.
     */
    showScanProgress: {type: Boolean, value: false},

    /** Whether cellular activation is unavailable in the current context. */
    activationUnavailable: Boolean,

    /**
     * List of all network state data for all visible networks.
     * @private {!Array<!OncMojo.NetworkStateProperties>}
     */
    networkStateList_: {
      type: Array,
      value() {
        return [];
      },
    },

    /**
     * Whether a network scan is currently in progress.
     * @private
     */
    isScanOngoing_: {type: Boolean, value: false},

    /**
     * The cellular DeviceState, or undefined if there is no Cellular device.
     * @private {!OncMojo.DeviceStateProperties|undefined} deviceState
     */
    cellularDeviceState_: Object,
  },

  /** @type {!OncMojo.NetworkStateProperties|undefined} */
  defaultNetworkState_: undefined,


  /** @private {number|null} */
  scanIntervalId_: null,

  /** @private {?chromeos.networkConfig.mojom.CrosNetworkConfigRemote} */
  networkConfig_: null,

  /** @override */
  created() {
    this.networkConfig_ = network_config.MojoInterfaceProviderImpl.getInstance()
                              .getMojoServiceRemote();
  },

  /** @override */
  attached() {
    this.refreshNetworks();

    const INTERVAL_MS = 10 * 1000;
    // Request only WiFi network scans. Tether and Cellular scans are not useful
    // here. Cellular scans are disruptive and should only be triggered by
    // explicit user action.
    const kWiFi = chromeos.networkConfig.mojom.NetworkType.kWiFi;
    this.networkConfig_.requestNetworkScan(kWiFi);
    this.scanIntervalId_ = window.setInterval(function() {
      this.networkConfig_.requestNetworkScan(kWiFi);
    }.bind(this), INTERVAL_MS);
  },

  /** @override */
  detached() {
    if (this.scanIntervalId_ !== null) {
      window.clearInterval(this.scanIntervalId_);
    }
  },

  /**
   * Returns network list object for testing.
   */
  getNetworkListForTest() {
    return this.$.networkList.$$('#networkList');
  },

  /**
   * Returns network list item object for testing.
   */
  getNetworkListItemByNameForTest(name) {
    const networkList = this.$.networkList.$$('#networkList');
    assert(networkList);
    for (const network of networkList.children) {
      if (network.is === 'network-list-item' &&
          network.$$('#divText').children[0].innerText === name) {
        return network.shadowRoot.getElementById('divOuter');
      }
    }
    return null;
  },

  focus() {
    this.$.networkList.focus();
  },

  /** CrosNetworkConfigObserver impl */
  onNetworkStateListChanged() {
    this.refreshNetworks();
  },

  /** CrosNetworkConfigObserver impl */
  onDeviceStateListChanged() {
    this.refreshNetworks();
  },

  /**
   * Requests the device and network states. May be called externally to force a
   * refresh and list update (e.g. when the element is shown).
   */
  refreshNetworks() {
    this.networkConfig_.getDeviceStateList().then(response => {
      this.onGetDeviceStates_(response.result);
    });
  },

  /**
   * Returns default network if it is present.
   * @return {!OncMojo.NetworkStateProperties|undefined}
   */
  getDefaultNetwork() {
    let defaultNetwork;
    for (let i = 0; i < this.networkStateList_.length; ++i) {
      const state = this.networkStateList_[i];
      if (OncMojo.connectionStateIsConnected(state.connectionState)) {
        defaultNetwork = state;
        break;
      }
      if (state.connectionState === mojom.ConnectionStateType.kConnecting &&
          !defaultNetwork) {
        defaultNetwork = state;
        // Do not break here in case a non WiFi network is connecting but a
        // WiFi network is connected.
      } else if (state.type === mojom.NetworkType.kWiFi) {
        break;  // Non connecting or connected WiFI networks are always last.
      }
    }
    return defaultNetwork;
  },

  /**
   * Returns network with specified GUID if it is available.
   * @param {string} guid of network.
   * @return {!OncMojo.NetworkStateProperties|undefined}
   */
  getNetwork(guid) {
    return this.networkStateList_.find(function(network) {
      return network.guid === guid;
    });
  },

  /**
   * @param {!Array<!OncMojo.DeviceStateProperties>} deviceStates
   * @private
   */
  onGetDeviceStates_(deviceStates) {
    this.isScanOngoing_ =
        deviceStates.some((deviceState) => !!deviceState.scanning);

    const filter = {
      filter: chromeos.networkConfig.mojom.FilterType.kVisible,
      networkType: mojom.NetworkType.kAll,
      limit: chromeos.networkConfig.mojom.NO_LIMIT,
    };
    this.networkConfig_.getNetworkStateList(filter).then(response => {
      this.onGetNetworkStateList_(deviceStates, response.result);
    });
  },

  /**
   * @param {!Array<!OncMojo.DeviceStateProperties>} deviceStates
   * @param {!Array<!OncMojo.NetworkStateProperties>} networkStates
   * @private
   */
  onGetNetworkStateList_(deviceStates, networkStates) {
    this.cellularDeviceState_ = deviceStates.find(function(device) {
      return device.type === mojom.NetworkType.kCellular;
    });
    if (this.cellularDeviceState_) {
      this.ensureCellularNetwork_(networkStates);
    }
    this.networkStateList_ = networkStates;
    this.fire('network-list-changed', networkStates);

    const defaultNetwork = this.getDefaultNetwork();

    if ((!defaultNetwork && !this.defaultNetworkState_) ||
        (defaultNetwork && this.defaultNetworkState_ &&
         defaultNetwork.guid === this.defaultNetworkState_.guid &&
         defaultNetwork.connectionState ===
             this.defaultNetworkState_.connectionState)) {
      return;  // No change to network or ConnectionState
    }
    this.defaultNetworkState_ = defaultNetwork ?
        /** @type {!OncMojo.NetworkStateProperties|undefined} */ (
            Object.assign({}, defaultNetwork)) :
        undefined;
    // Note: event.detail will be {} if defaultNetwork is undefined.
    this.fire('default-network-changed', defaultNetwork);
  },

  /**
   * Modifies |networkStates| to include a cellular network if one is required
   * but does not exist.
   * @param {!Array<!OncMojo.NetworkStateProperties>} networkStates
   * @private
   */
  ensureCellularNetwork_(networkStates) {
    if (networkStates.find(function(network) {
          return network.type === mojom.NetworkType.kCellular;
        })) {
      return;
    }
    const deviceState = this.cellularDeviceState_.deviceState;
    if (deviceState === mojom.DeviceStateType.kDisabled ||
        deviceState === mojom.DeviceStateType.kProhibited) {
      return;  // No Cellular network
    }

    // Note: the default connectionState is kNotConnected.
    // TODO(khorimoto): Maybe set an 'initializing' CellularState property if
    // the device state is initializing, see TODO in network_list_item.js.

    // Insert the Cellular network after the Ethernet network if it exists.
    const idx = (networkStates.length > 0 &&
                 networkStates[0].type === mojom.NetworkType.kEthernet) ?
        1 :
        0;
    networkStates.splice(
        idx, 0, OncMojo.getDefaultNetworkState(mojom.NetworkType.kCellular));
  },

  /**
   * Event triggered when a network-list-item is selected.
   * @param {!{target: HTMLElement, detail: !OncMojo.NetworkStateProperties}} e
   * @private
   */
  onNetworkListItemSelected_(e) {
    const state = e.detail;
    e.target.blur();

    this.fire('network-item-selected', state);
  },
});
})();