Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / settings / internet_page / network_summary.js
blob67b30436be756e31add4893a409b14940ffb896f
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.
5 /**
6  * @fileoverview Polymer element for displaying a summary of network states
7  * by type: Ethernet, WiFi, Cellular, WiMAX, and VPN.
8  */
10 /** @typedef {chrome.networkingPrivate.DeviceStateProperties} */
11 var DeviceStateProperties;
13 /**
14  * @typedef {{
15  *   Ethernet: (DeviceStateProperties|undefined),
16  *   WiFi: (DeviceStateProperties|undefined),
17  *   Cellular: (DeviceStateProperties|undefined),
18  *   WiMAX: (DeviceStateProperties|undefined),
19  *   VPN: (DeviceStateProperties|undefined)
20  * }}
21  */
22 var DeviceStateObject;
24 /**
25  * @typedef {{
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)
31  * }}
32  */
33 var NetworkStateObject;
35 /**
36  * @typedef {{
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)
42  * }}
43  */
44 var NetworkStateListObject;
46 (function() {
48 /** @const {!Array<chrome.networkingPrivate.NetworkType>} */
49 var NETWORK_TYPES = [
50   CrOnc.Type.ETHERNET,
51   CrOnc.Type.WI_FI,
52   CrOnc.Type.CELLULAR,
53   CrOnc.Type.WI_MAX,
54   CrOnc.Type.VPN
57 Polymer({
58   is: 'network-summary',
60   properties: {
61     /**
62      * The device state for each network device type.
63      * @type {DeviceStateObject}
64      */
65     deviceStates: {
66       type: Object,
67       value: function() { return {}; },
68     },
70     /**
71      * Network state data for each network type.
72      * @type {NetworkStateObject}
73      */
74     networkStates: {
75       type: Object,
76       value: function() { return {}; },
77     },
79     /**
80      * List of network state data for each network type.
81      * @type {NetworkStateListObject}
82      */
83     networkStateLists: {
84       type: Object,
85       value: function() { return {}; },
86     }
87   },
89   /**
90    * Listener function for chrome.networkingPrivate.onNetworkListChanged event.
91    * @type {function(!Array<string>)}
92    * @private
93    */
94   networkListChangedListener_: function() {},
96   /**
97    * Listener function for chrome.networkingPrivate.onDeviceStateListChanged
98    * event.
99    * @type {function(!Array<string>)}
100    * @private
101    */
102   deviceStateListChangedListener_: function() {},
104   /**
105    * Listener function for chrome.networkingPrivate.onNetworksChanged event.
106    * @type {function(!Array<string>)}
107    * @private
108    */
109   networksChangedListener_: function() {},
111   /**
112    * Dictionary of GUIDs identifying primary (active) networks for each type.
113    * @type {?Object}
114    * @private
115    */
116   networkIds_: null,
118   /** @override */
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_);
137   },
139   /** @override */
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_);
149   },
151   /**
152    * Event triggered when the WiFi network-summary-item is expanded.
153    * @param {!{detail: {expanded: boolean, type: string}}} event
154    * @private
155    */
156   onWiFiExpanded_: function(event) {
157     this.getNetworkStates_();  // Get the latest network states (only).
158     if (event.detail.expanded)
159       chrome.networkingPrivate.requestNetworkScan();
160   },
162   /**
163    * Event triggered when a network-summary-item is selected.
164    * @param {!{detail: !CrOnc.NetworkStateProperties}} event
165    * @private
166    */
167   onSelected_: function(event) {
168     var state = event.detail;
169     if (state.ConnectionState == CrOnc.ConnectionState.NOT_CONNECTED) {
170       this.connectToNetwork_(state);
171       return;
172     }
173     this.fire('show-detail', state);
174   },
176   /**
177    * Event triggered when the enabled state of a network-summary-item is
178    * toggled.
179    * @param {!{detail: {enabled: boolean,
180    *                    type: chrome.networkingPrivate.NetworkType}}} event
181    * @private
182    */
183   onDeviceEnabledToggled_: function(event) {
184     if (event.detail.enabled)
185       chrome.networkingPrivate.enableNetworkType(event.detail.type);
186     else
187       chrome.networkingPrivate.disableNetworkType(event.detail.type);
188   },
190   /**
191    * networkingPrivate.onNetworkListChanged event callback.
192    * @private
193    */
194   onNetworkListChangedEvent_: function() { this.getNetworkLists_(); },
196   /**
197    * networkingPrivate.onDeviceStateListChanged event callback.
198    * @private
199    */
200   onDeviceStateListChangedEvent_: function() { this.getNetworkLists_(); },
202   /**
203    * networkingPrivate.onNetworksChanged event callback.
204    * @param {!Array<string>} networkIds The list of changed network GUIDs.
205    * @private
206    */
207   onNetworksChangedEvent_: function(networkIds) {
208     networkIds.forEach(function(id) {
209       if (id in this.networkIds_) {
210         chrome.networkingPrivate.getState(
211             id,
212             function(state) {
213               if (chrome.runtime.lastError) {
214                 if (chrome.runtime.lastError != 'Error.NetworkUnavailable') {
215                   console.error('Unexpected networkingPrivate.getState error:',
216                                 chrome.runtime.lastError);
217                 }
218                 return;
219               }
220               // Async call, ensure id still exists.
221               if (!this.networkIds_[id])
222                 return;
223               if (!state) {
224                 this.networkIds_[id] = undefined;
225                 return;
226               }
227               this.updateNetworkState_(state.Type, state);
228             }.bind(this));
229       }
230     }, this);
231   },
233   /**
234    * Handles UI requests to connect to a network.
235    * TODO(stevenjb): Handle Cellular activation, etc.
236    * @param {!CrOnc.NetworkStateProperties} state The network state.
237    * @private
238    */
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);
245       }
246     });
247   },
249   /**
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.
253    * @private
254    */
255   getNetworkLists_: function() {
256     // First get the device states.
257     chrome.networkingPrivate.getDeviceStates(
258       function(states) {
259         this.getDeviceStatesCallback_(states);
260         // Second get the network states.
261         this.getNetworkStates_();
262       }.bind(this));
263   },
265   /**
266    * Requests the list of network states from Chrome. Updates networkStates and
267    * networkStateLists once the results are returned from Chrome.
268    * @private
269    */
270   getNetworkStates_: function() {
271     var filter = {
272       networkType: chrome.networkingPrivate.NetworkType.ALL,
273       visible: true,
274       configured: false
275     };
276     chrome.networkingPrivate.getNetworks(
277         filter, this.getNetworksCallback_.bind(this));
278   },
280   /**
281    * networkingPrivate.getDeviceStates callback.
282    * @param {!Array<!DeviceStateProperties>} states The state properties for all
283    *     available devices.
284    * @private
285    */
286   getDeviceStatesCallback_: function(states) {
287     var newStates = /** @type {!DeviceStateObject} */({});
288     states.forEach(function(state) { newStates[state.Type] = state; });
289     this.deviceStates = newStates;
290   },
292   /**
293    * networkingPrivate.getNetworksState callback.
294    * @param {!Array<!CrOnc.NetworkStateProperties>} states The state properties
295    *     for all visible networks.
296    * @private
297    */
298   getNetworksCallback_: function(states) {
299     // Clear any current networks.
300     this.networkIds_ = {};
302     // Track the first (active) state for each type.
303     var foundTypes = {};
305     // Complete list of states by type.
306     /** @type {!NetworkStateListObject} */ var networkStateLists = {
307       Ethernet: [],
308       WiFi: [],
309       Cellular: [],
310       WiMAX: [],
311       VPN: []
312     };
314     states.forEach(function(state) {
315       var type = state.Type;
316       if (!foundTypes[type]) {
317         foundTypes[type] = true;
318         this.updateNetworkState_(type, state);
319       }
320       networkStateLists[type].push(state);
321     }, this);
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);
331       }
332     }, this);
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);
340     }
341   },
343   /**
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
349    *     matching |type|.
350    * @private
351    */
352   updateNetworkState_: function(type, state) {
353     this.set('networkStates.' + type, state);
354     if (state)
355       this.networkIds_[state.GUID] = true;
356   },
358 })();