| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Polymer element for displaying a summary of network states | 6 * @fileoverview Polymer element for displaying a summary of network states |
| 7 * by type: Ethernet, WiFi, Cellular, WiMAX, and VPN. | 7 * by type: Ethernet, WiFi, Cellular, WiMAX, and VPN. |
| 8 */ | 8 */ |
| 9 (function() { |
| 10 |
| 11 /** @typedef {chrome.networkingPrivate.DeviceStateProperties} */ |
| 12 var DeviceStateProperties; |
| 13 |
| 14 /** @typedef {chrome.networkingPrivate.NetworkStateProperties} */ |
| 15 var NetworkStateProperties; |
| 16 |
| 17 /** |
| 18 * @typedef {{ |
| 19 * Ethernet: (DeviceStateProperties|undefined), |
| 20 * WiFi: (DeviceStateProperties|undefined), |
| 21 * Cellular: (DeviceStateProperties|undefined), |
| 22 * WiMAX: (DeviceStateProperties|undefined), |
| 23 * VPN: (DeviceStateProperties|undefined) |
| 24 * }} |
| 25 */ |
| 26 var DeviceStateObject; |
| 27 |
| 28 /** |
| 29 * @typedef {{ |
| 30 * Ethernet: (CrOncDataElement|undefined), |
| 31 * WiFi: (CrOncDataElement|undefined), |
| 32 * Cellular: (CrOncDataElement|undefined), |
| 33 * WiMAX: (CrOncDataElement|undefined), |
| 34 * VPN: (CrOncDataElement|undefined) |
| 35 * }} |
| 36 */ |
| 37 var NetworkStateObject; |
| 38 |
| 39 /** |
| 40 * @typedef {{ |
| 41 * Ethernet: (Array<CrOncDataElement>|undefined), |
| 42 * WiFi: (Array<CrOncDataElement>|undefined), |
| 43 * Cellular: (Array<CrOncDataElement>|undefined), |
| 44 * WiMAX: (Array<CrOncDataElement>|undefined), |
| 45 * VPN: (Array<CrOncDataElement>|undefined) |
| 46 * }} |
| 47 */ |
| 48 var NetworkStateListObject; |
| 49 |
| 50 /** @const {!Array<string>} */ |
| 51 var NETWORK_TYPES = ['Ethernet', 'WiFi', 'Cellular', 'WiMAX', 'VPN']; |
| 52 |
| 9 Polymer('cr-network-summary', { | 53 Polymer('cr-network-summary', { |
| 10 publish: { | 54 publish: { |
| 11 /** | 55 /** |
| 56 * The device state for each network device type. |
| 57 * |
| 58 * @attribute deviceStates |
| 59 * @type {?DeviceStateObject} |
| 60 * @default null |
| 61 */ |
| 62 deviceStates: null, |
| 63 |
| 64 /** |
| 12 * Network state data for each network type. | 65 * Network state data for each network type. |
| 13 * | 66 * |
| 14 * @attribute networkStates | 67 * @attribute networkStates |
| 15 * @type {{ | 68 * @type {?NetworkStateObject} |
| 16 * Ethernet: (CrOncDataElement|undefined), | 69 * @default null |
| 17 * WiFi: (CrOncDataElement|undefined), | |
| 18 * Cellular: (CrOncDataElement|undefined), | |
| 19 * WiMAX: (CrOncDataElement|undefined), | |
| 20 * VPN: (CrOncDataElement|undefined) | |
| 21 * }} | |
| 22 * @default {} | |
| 23 */ | 70 */ |
| 24 networkStates: null, | 71 networkStates: null, |
| 72 |
| 73 /** |
| 74 * List of network state data for each network type. |
| 75 * |
| 76 * @attribute networkStateLists |
| 77 * @type {?NetworkStateListObject} |
| 78 * @default null |
| 79 */ |
| 80 networkStateLists: null, |
| 25 }, | 81 }, |
| 26 | 82 |
| 27 /** | 83 /** |
| 28 * Listener function for chrome.networkingPrivate.onNetworkListChanged event. | 84 * Listener function for chrome.networkingPrivate.onNetworkListChanged event. |
| 29 * @type {function(!Array<string>)} | 85 * @type {?function(!Array<string>)} |
| 30 * @private | 86 * @private |
| 31 */ | 87 */ |
| 32 listChangedListener_: null, | 88 networkListChangedListener_: null, |
| 89 |
| 90 /** |
| 91 * Listener function for chrome.networkingPrivate.onDeviceStateListChanged |
| 92 * event. |
| 93 * @type {?function(!Array<string>)} |
| 94 * @private |
| 95 */ |
| 96 deviceStateListChangedListener_: null, |
| 33 | 97 |
| 34 /** | 98 /** |
| 35 * Listener function for chrome.networkingPrivate.onNetworksChanged event. | 99 * Listener function for chrome.networkingPrivate.onNetworksChanged event. |
| 36 * @type {function(!Array<string>)} | 100 * @type {?function(!Array<string>)} |
| 37 * @private | 101 * @private |
| 38 */ | 102 */ |
| 39 networksChangedListener_: null, | 103 networksChangedListener_: null, |
| 40 | 104 |
| 41 /** | 105 /** |
| 42 * Dictionary of GUIDs identifying primary (active) networks for each type. | 106 * Dictionary of GUIDs identifying primary (active) networks for each type. |
| 43 * @type {Object} | 107 * @type {?Object} |
| 44 * @private | 108 * @private |
| 45 */ | 109 */ |
| 46 networkIds_: {}, | 110 networkIds_: null, |
| 47 | 111 |
| 48 /** @override */ | 112 /** @override */ |
| 49 created: function() { | 113 created: function() { |
| 114 this.deviceStates = {}; |
| 50 this.networkStates = {}; | 115 this.networkStates = {}; |
| 116 this.networkStateLists = {}; |
| 117 this.networkIds_ = {}; |
| 51 }, | 118 }, |
| 52 | 119 |
| 53 /** @override */ | 120 /** @override */ |
| 54 attached: function() { | 121 attached: function() { |
| 55 this.getNetworks_(); | 122 this.getNetworkLists_(); |
| 56 | 123 |
| 57 this.listChangedListener_ = this.onNetworkListChangedEvent_.bind(this); | 124 this.networkListChangedListener_ = |
| 125 this.onNetworkListChangedEvent_.bind(this); |
| 58 chrome.networkingPrivate.onNetworkListChanged.addListener( | 126 chrome.networkingPrivate.onNetworkListChanged.addListener( |
| 59 this.listChangedListener_); | 127 this.networkListChangedListener_); |
| 128 |
| 129 this.deviceStateListChangedListener_ = |
| 130 this.onDeviceStateListChangedEvent_.bind(this); |
| 131 chrome.networkingPrivate.onDeviceStateListChanged.addListener( |
| 132 this.deviceStateListChangedListener_); |
| 60 | 133 |
| 61 this.networksChangedListener_ = this.onNetworksChangedEvent_.bind(this); | 134 this.networksChangedListener_ = this.onNetworksChangedEvent_.bind(this); |
| 62 chrome.networkingPrivate.onNetworksChanged.addListener( | 135 chrome.networkingPrivate.onNetworksChanged.addListener( |
| 63 this.networksChangedListener_); | 136 this.networksChangedListener_); |
| 64 }, | 137 }, |
| 65 | 138 |
| 66 /** @override */ | 139 /** @override */ |
| 67 detached: function() { | 140 detached: function() { |
| 68 chrome.networkingPrivate.onNetworkListChanged.removeListener( | 141 chrome.networkingPrivate.onNetworkListChanged.removeListener( |
| 69 this.listChangedListener_); | 142 this.networkListChangedListener_); |
| 143 |
| 144 chrome.networkingPrivate.onDeviceStateListChanged.removeListener( |
| 145 this.deviceStateListChangedListener_); |
| 70 | 146 |
| 71 chrome.networkingPrivate.onNetworksChanged.removeListener( | 147 chrome.networkingPrivate.onNetworksChanged.removeListener( |
| 72 this.networksChangedListener_); | 148 this.networksChangedListener_); |
| 73 }, | 149 }, |
| 74 | 150 |
| 75 /** | 151 /** |
| 152 * Event triggered when the WiFi cr-network-summary-item is expanded. |
| 153 * @param {!{detail: {expanded: boolean, type: string}}} event |
| 76 * @private | 154 * @private |
| 77 */ | 155 */ |
| 78 onNetworkListChangedEvent_: function() { | 156 onWiFiExpanded_: function(event) { |
| 79 this.getNetworks_(); | 157 this.getNetworkStates_(); // Get the latest network states (only). |
| 158 chrome.networkingPrivate.requestNetworkScan(); |
| 80 }, | 159 }, |
| 81 | 160 |
| 82 /** | 161 /** |
| 162 * Event triggered when a cr-network-summary-item is selected. |
| 163 * @param {!{detail: !CrOncDataElement}} event |
| 164 * @private |
| 165 */ |
| 166 onSelected_: function(event) { |
| 167 var onc = event.detail; |
| 168 if (onc.disconnected()) { |
| 169 this.connectToNetwork_(onc); |
| 170 return; |
| 171 } |
| 172 // TODO(stevenjb): Show details for connected or unconfigured networks. |
| 173 }, |
| 174 |
| 175 /** |
| 176 * Event triggered when the enabled state of a cr-network-summary-item is |
| 177 * toggled. |
| 178 * @param {!{detail: {enabled: boolean, type: string}}} event |
| 179 * @private |
| 180 */ |
| 181 onDeviceEnabledToggled_: function(event) { |
| 182 if (event.detail.enabled) |
| 183 chrome.networkingPrivate.enableNetworkType(event.detail.type); |
| 184 else |
| 185 chrome.networkingPrivate.disableNetworkType(event.detail.type); |
| 186 }, |
| 187 |
| 188 /** |
| 189 * networkingPrivate.onNetworkListChanged event callback. |
| 190 * @private |
| 191 */ |
| 192 onNetworkListChangedEvent_: function() { this.getNetworkLists_(); }, |
| 193 |
| 194 /** |
| 195 * networkingPrivate.onDeviceStateListChanged event callback. |
| 196 * @private |
| 197 */ |
| 198 onDeviceStateListChangedEvent_: function() { this.getNetworkLists_(); }, |
| 199 |
| 200 /** |
| 201 * networkingPrivate.onNetworksChanged event callback. |
| 83 * @param {!Array<string>} networkIds The list of changed network GUIDs. | 202 * @param {!Array<string>} networkIds The list of changed network GUIDs. |
| 84 * @private | 203 * @private |
| 85 */ | 204 */ |
| 86 onNetworksChangedEvent_: function(networkIds) { | 205 onNetworksChangedEvent_: function(networkIds) { |
| 87 networkIds.forEach(function(id) { | 206 networkIds.forEach(function(id) { |
| 88 if (id in this.networkIds_) { | 207 if (id in this.networkIds_) { |
| 89 chrome.networkingPrivate.getState(id, | 208 chrome.networkingPrivate.getState(id, |
| 90 this.getStateCallback_.bind(this)); | 209 this.getStateCallback_.bind(this)); |
| 91 } | 210 } |
| 92 }, this); | 211 }, this); |
| 93 }, | 212 }, |
| 94 | 213 |
| 95 /** @private */ | 214 /** |
| 96 getNetworks_: function() { | 215 * Handles UI requests to connect to a network. |
| 216 * TODO(stevenjb): Handle Cellular activation, etc. |
| 217 * @param {!CrOncDataElement} state The network state. |
| 218 * @private |
| 219 */ |
| 220 connectToNetwork_: function(state) { |
| 221 chrome.networkingPrivate.startConnect(state.data.GUID); |
| 222 }, |
| 223 |
| 224 /** |
| 225 * Requests the list of device states and network states from Chrome. |
| 226 * Updates deviceStates, networkStates, and networkStateLists once the |
| 227 * results are returned from Chrome. |
| 228 * @private |
| 229 */ |
| 230 getNetworkLists_: function() { |
| 231 // First get the device states. |
| 232 chrome.networkingPrivate.getDeviceStates( |
| 233 function(states) { |
| 234 this.getDeviceStatesCallback_(states); |
| 235 // Second get the network states. |
| 236 this.getNetworkStates_(); |
| 237 }.bind(this)); |
| 238 }, |
| 239 |
| 240 /** |
| 241 * Requests the list of network states from Chrome. Updates networkStates and |
| 242 * networkStateLists once the results are returned from Chrome. |
| 243 * @private |
| 244 */ |
| 245 getNetworkStates_: function() { |
| 97 var filter = { | 246 var filter = { |
| 98 networkType: 'All', | 247 networkType: 'All', |
| 99 visible: true, | 248 visible: true, |
| 100 configured: false | 249 configured: false |
| 101 }; | 250 }; |
| 102 chrome.networkingPrivate.getNetworks(filter, | 251 chrome.networkingPrivate.getNetworks( |
| 103 this.getNetworksCallback_.bind(this)); | 252 filter, this.getNetworksCallback_.bind(this)); |
| 104 }, | 253 }, |
| 105 | 254 |
| 106 /** | 255 /** |
| 107 * @param {!Array<!chrome.networkingPrivate.NetworkStateProperties>} states | 256 * networkingPrivate.getDeviceStates callback. |
| 108 * The state properties for all networks. | 257 * @param {!Array<!DeviceStateProperties>} states The state properties for all |
| 258 * available devices. |
| 259 * @private |
| 260 */ |
| 261 getDeviceStatesCallback_: function(states) { |
| 262 /** @type {!DeviceStateObject} */ var newStates = {}; |
| 263 states.forEach(function(state) { newStates[state.Type] = state; }); |
| 264 this.deviceStates = newStates; |
| 265 }, |
| 266 |
| 267 /** |
| 268 * networkingPrivate.getNetworksState callback. |
| 269 * @param {!Array<!NetworkStateProperties>} states The state properties for |
| 270 * all visible networks. |
| 109 * @private | 271 * @private |
| 110 */ | 272 */ |
| 111 getNetworksCallback_: function(states) { | 273 getNetworksCallback_: function(states) { |
| 112 // Clear all active networks. | 274 // Clear any current networks. |
| 113 this.networkIds_ = {}; | 275 this.networkIds_ = {}; |
| 114 | 276 |
| 115 // Get the first (active) state for each type. | 277 // Get the first (active) state for each type. |
| 116 var foundTypes = {}; | 278 var foundTypes = {}; |
| 279 /** @type {!NetworkStateListObject} */ var oncNetworks = { |
| 280 Ethernet: [], |
| 281 WiFi: [], |
| 282 Cellular: [], |
| 283 WiMAX: [], |
| 284 VPN: [] |
| 285 }; |
| 117 states.forEach(function(state) { | 286 states.forEach(function(state) { |
| 118 var type = state.Type; | 287 var type = state.Type; |
| 119 if (!foundTypes[type]) { | 288 if (!foundTypes[type]) { |
| 120 foundTypes[type] = true; | 289 foundTypes[type] = true; |
| 121 this.updateNetworkState_(type, state); | 290 this.updateNetworkState_(type, state); |
| 122 } | 291 } |
| 292 oncNetworks[type].push(CrOncDataElement.create(state)); |
| 123 }, this); | 293 }, this); |
| 124 | 294 |
| 125 // Set any types not found to null. TODO(stevenjb): Support types that are | 295 // Set any types not found to a default value or null. |
| 126 // disabled but available with no active network. | 296 NETWORK_TYPES.forEach(function(type) { |
| 127 var types = ['Ethernet', 'WiFi', 'Cellular', 'WiMAX', 'VPN']; | 297 if (!foundTypes[type]) { |
| 128 types.forEach(function(type) { | 298 /** @type {NetworkStateProperties} */ var defaultState = null; |
| 129 if (!foundTypes[type]) | 299 if (this.deviceStates[type]) |
| 130 this.updateNetworkState_(type, null); | 300 defaultState = { GUID: '', Type: 'WiFi' }; |
| 301 this.updateNetworkState_(type, defaultState); |
| 302 } |
| 131 }, this); | 303 }, this); |
| 304 |
| 305 // Set the network list for each type. |
| 306 NETWORK_TYPES.forEach(function(type) { |
| 307 this.networkStateLists[type] = oncNetworks[type]; |
| 308 }, this); |
| 309 |
| 310 // Create a VPN entry in deviceStates if there are any VPN networks. |
| 311 if (this.networkStateLists.VPN && this.networkStateLists.VPN.length > 0) |
| 312 this.deviceStates.VPN = { Type: 'VPN', State: 'Enabled' }; |
| 132 }, | 313 }, |
| 133 | 314 |
| 134 /** | 315 /** |
| 135 * @param {!chrome.networkingPrivate.NetworkStateProperties} state The state | 316 * networkingPrivate.getState callback. |
| 136 * properties for the network. | 317 * @param {!NetworkStateProperties} state The network state properties. |
| 137 * @private | 318 * @private |
| 138 */ | 319 */ |
| 139 getStateCallback_: function(state) { | 320 getStateCallback_: function(state) { |
| 140 var id = state.GUID; | 321 var id = state.GUID; |
| 141 if (!this.networkIds_[id]) | 322 if (!this.networkIds_[id]) |
| 142 return; | 323 return; |
| 143 this.updateNetworkState_(state.Type, state); | 324 this.updateNetworkState_(state.Type, state); |
| 144 }, | 325 }, |
| 145 | 326 |
| 146 /** | 327 /** |
| 147 * Creates a CrOncDataElement from the network state (if not null) for 'type'. | 328 * Creates a CrOncDataElement from the network state (if not null) for 'type'. |
| 148 * Sets 'networkStates[type]' which will update the cr-network-list-item | 329 * Sets 'networkStates[type]' which will update the cr-network-list-item |
| 149 * associated with 'type'. | 330 * associated with 'type'. |
| 150 * @param {string} type The network type. | 331 * @param {string} type The network type. |
| 151 * @param {chrome.networkingPrivate.NetworkStateProperties} state The state | 332 * @param {?NetworkStateProperties} state The state properties for the network |
| 152 * properties for the network to associate with |type|. May be null if | 333 * to associate with |type|. May be null if there are no networks matching |
| 153 * there are no networks matching |type|. | 334 * |type|. |
| 154 * @private | 335 * @private |
| 155 */ | 336 */ |
| 156 updateNetworkState_: function(type, state) { | 337 updateNetworkState_: function(type, state) { |
| 157 this.networkStates[type] = state ? CrOncDataElement.create(state) : null; | 338 this.networkStates[type] = state ? CrOncDataElement.create(state) : null; |
| 158 if (state) | 339 if (state) |
| 159 this.networkIds_[state.GUID] = true; | 340 this.networkIds_[state.GUID] = true; |
| 160 }, | 341 }, |
| 161 }); | 342 }); |
| 343 })(); |
| OLD | NEW |