1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 * @fileoverview Polymer element for displaying a summary of network states
7 * by type: Ethernet, WiFi, Cellular, WiMAX, and VPN.
10 /** @typedef {chrome.networkingPrivate.DeviceStateProperties} */
11 var DeviceStateProperties;
15 * Ethernet: (DeviceStateProperties|undefined),
16 * WiFi: (DeviceStateProperties|undefined),
17 * Cellular: (DeviceStateProperties|undefined),
18 * WiMAX: (DeviceStateProperties|undefined),
19 * VPN: (DeviceStateProperties|undefined)
22 var DeviceStateObject;
26 * Ethernet: (?CrOnc.NetworkStateProperties|undefined),
27 * WiFi: (?CrOnc.NetworkStateProperties|undefined),
28 * Cellular: (?CrOnc.NetworkStateProperties|undefined),
29 * WiMAX: (?CrOnc.NetworkStateProperties|undefined),
30 * VPN: (?CrOnc.NetworkStateProperties|undefined)
33 var NetworkStateObject;
37 * Ethernet: (Array<CrOnc.NetworkStateProperties>|undefined),
38 * WiFi: (Array<CrOnc.NetworkStateProperties>|undefined),
39 * Cellular: (Array<CrOnc.NetworkStateProperties>|undefined),
40 * WiMAX: (Array<CrOnc.NetworkStateProperties>|undefined),
41 * VPN: (Array<CrOnc.NetworkStateProperties>|undefined)
44 var NetworkStateListObject;
48 /** @const {!Array<chrome.networkingPrivate.NetworkType>} */
58 is: 'network-summary',
62 * The device state for each network device type.
63 * @type {DeviceStateObject}
67 value: function() { return {}; },
71 * Network state data for each network type.
72 * @type {NetworkStateObject}
76 value: function() { return {}; },
80 * List of network state data for each network type.
81 * @type {NetworkStateListObject}
85 value: function() { return {}; },
90 * Listener function for chrome.networkingPrivate.onNetworkListChanged event.
91 * @type {function(!Array<string>)}
94 networkListChangedListener_: function() {},
97 * Listener function for chrome.networkingPrivate.onDeviceStateListChanged
99 * @type {function(!Array<string>)}
102 deviceStateListChangedListener_: function() {},
105 * Listener function for chrome.networkingPrivate.onNetworksChanged event.
106 * @type {function(!Array<string>)}
109 networksChangedListener_: function() {},
112 * Dictionary of GUIDs identifying primary (active) networks for each type.
119 attached: function() {
120 this.networkIds_ = {};
122 this.getNetworkLists_();
124 this.networkListChangedListener_ =
125 this.onNetworkListChangedEvent_.bind(this);
126 chrome.networkingPrivate.onNetworkListChanged.addListener(
127 this.networkListChangedListener_);
129 this.deviceStateListChangedListener_ =
130 this.onDeviceStateListChangedEvent_.bind(this);
131 chrome.networkingPrivate.onDeviceStateListChanged.addListener(
132 this.deviceStateListChangedListener_);
134 this.networksChangedListener_ = this.onNetworksChangedEvent_.bind(this);
135 chrome.networkingPrivate.onNetworksChanged.addListener(
136 this.networksChangedListener_);
140 detached: function() {
141 chrome.networkingPrivate.onNetworkListChanged.removeListener(
142 this.networkListChangedListener_);
144 chrome.networkingPrivate.onDeviceStateListChanged.removeListener(
145 this.deviceStateListChangedListener_);
147 chrome.networkingPrivate.onNetworksChanged.removeListener(
148 this.networksChangedListener_);
152 * Event triggered when the WiFi network-summary-item is expanded.
153 * @param {!{detail: {expanded: boolean, type: string}}} event
156 onWiFiExpanded_: function(event) {
157 this.getNetworkStates_(); // Get the latest network states (only).
158 if (event.detail.expanded)
159 chrome.networkingPrivate.requestNetworkScan();
163 * Event triggered when a network-summary-item is selected.
164 * @param {!{detail: !CrOnc.NetworkStateProperties}} event
167 onSelected_: function(event) {
168 var state = event.detail;
169 if (state.ConnectionState == CrOnc.ConnectionState.NOT_CONNECTED) {
170 this.connectToNetwork_(state);
173 this.fire('show-detail', state);
177 * Event triggered when the enabled state of a network-summary-item is
179 * @param {!{detail: {enabled: boolean,
180 * type: chrome.networkingPrivate.NetworkType}}} event
183 onDeviceEnabledToggled_: function(event) {
184 if (event.detail.enabled)
185 chrome.networkingPrivate.enableNetworkType(event.detail.type);
187 chrome.networkingPrivate.disableNetworkType(event.detail.type);
191 * networkingPrivate.onNetworkListChanged event callback.
194 onNetworkListChangedEvent_: function() { this.getNetworkLists_(); },
197 * networkingPrivate.onDeviceStateListChanged event callback.
200 onDeviceStateListChangedEvent_: function() { this.getNetworkLists_(); },
203 * networkingPrivate.onNetworksChanged event callback.
204 * @param {!Array<string>} networkIds The list of changed network GUIDs.
207 onNetworksChangedEvent_: function(networkIds) {
208 networkIds.forEach(function(id) {
209 if (id in this.networkIds_) {
210 chrome.networkingPrivate.getState(
213 if (chrome.runtime.lastError) {
214 if (chrome.runtime.lastError != 'Error.NetworkUnavailable') {
215 console.error('Unexpected networkingPrivate.getState error:',
216 chrome.runtime.lastError);
220 // Async call, ensure id still exists.
221 if (!this.networkIds_[id])
224 this.networkIds_[id] = undefined;
227 this.updateNetworkState_(state.Type, state);
234 * Handles UI requests to connect to a network.
235 * TODO(stevenjb): Handle Cellular activation, etc.
236 * @param {!CrOnc.NetworkStateProperties} state The network state.
239 connectToNetwork_: function(state) {
240 chrome.networkingPrivate.startConnect(state.GUID, function() {
241 if (chrome.runtime.lastError &&
242 chrome.runtime.lastError != 'connecting') {
243 console.error('Unexpected networkingPrivate.startConnect error:',
244 chrome.runtime.lastError);
250 * Requests the list of device states and network states from Chrome.
251 * Updates deviceStates, networkStates, and networkStateLists once the
252 * results are returned from Chrome.
255 getNetworkLists_: function() {
256 // First get the device states.
257 chrome.networkingPrivate.getDeviceStates(
259 this.getDeviceStatesCallback_(states);
260 // Second get the network states.
261 this.getNetworkStates_();
266 * Requests the list of network states from Chrome. Updates networkStates and
267 * networkStateLists once the results are returned from Chrome.
270 getNetworkStates_: function() {
272 networkType: chrome.networkingPrivate.NetworkType.ALL,
276 chrome.networkingPrivate.getNetworks(
277 filter, this.getNetworksCallback_.bind(this));
281 * networkingPrivate.getDeviceStates callback.
282 * @param {!Array<!DeviceStateProperties>} states The state properties for all
286 getDeviceStatesCallback_: function(states) {
287 var newStates = /** @type {!DeviceStateObject} */({});
288 states.forEach(function(state) { newStates[state.Type] = state; });
289 this.deviceStates = newStates;
293 * networkingPrivate.getNetworksState callback.
294 * @param {!Array<!CrOnc.NetworkStateProperties>} states The state properties
295 * for all visible networks.
298 getNetworksCallback_: function(states) {
299 // Clear any current networks.
300 this.networkIds_ = {};
302 // Track the first (active) state for each type.
305 // Complete list of states by type.
306 /** @type {!NetworkStateListObject} */ var networkStateLists = {
314 states.forEach(function(state) {
315 var type = state.Type;
316 if (!foundTypes[type]) {
317 foundTypes[type] = true;
318 this.updateNetworkState_(type, state);
320 networkStateLists[type].push(state);
323 // Set any types with a deviceState and no network to a default state,
324 // and any types not found to null.
325 NETWORK_TYPES.forEach(function(type) {
326 if (!foundTypes[type]) {
327 var defaultState = /** @type {?CrOnc.NetworkStateProperties} */(null);
328 if (this.deviceStates[type])
329 defaultState = {GUID: '', Type: type};
330 this.updateNetworkState_(type, defaultState);
334 this.networkStateLists = networkStateLists;
336 // Create a VPN entry in deviceStates if there are any VPN networks.
337 if (networkStateLists.VPN && networkStateLists.VPN.length > 0) {
338 var vpn = {Type: CrOnc.Type.VPN, State: 'Enabled'};
339 this.set('deviceStates.VPN', vpn);
344 * Sets 'networkStates[type]' which will update the cr-network-list-item
345 * associated with 'type'.
346 * @param {string} type The network type.
347 * @param {?CrOnc.NetworkStateProperties} state The state properties for the
348 * network to associate with |type|. May be null if there are no networks
352 updateNetworkState_: function(type, state) {
353 this.set('networkStates.' + type, state);
355 this.networkIds_[state.GUID] = true;