Chromium Code Reviews| 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 | 9 |
| 10 /** @typedef {chrome.networkingPrivate.DeviceStateProperties} */ | 10 /** @typedef {chrome.networkingPrivate.DeviceStateProperties} */ |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 CrOnc.Type.CELLULAR, | 52 CrOnc.Type.CELLULAR, |
| 53 CrOnc.Type.WI_MAX, | 53 CrOnc.Type.WI_MAX, |
| 54 CrOnc.Type.VPN | 54 CrOnc.Type.VPN |
| 55 ]; | 55 ]; |
| 56 | 56 |
| 57 Polymer({ | 57 Polymer({ |
| 58 is: 'network-summary', | 58 is: 'network-summary', |
| 59 | 59 |
| 60 properties: { | 60 properties: { |
| 61 /** | 61 /** |
| 62 * Highest priority connected network or null. | |
| 63 * @type {?CrOnc.NetworkStateProperties} | |
| 64 */ | |
| 65 defaultNetwork: { | |
| 66 type: Object, | |
| 67 value: null, | |
| 68 notify: true | |
| 69 }, | |
| 70 | |
| 71 /** | |
| 62 * The device state for each network device type. | 72 * The device state for each network device type. |
| 63 * @type {DeviceStateObject} | 73 * @type {DeviceStateObject} |
| 64 */ | 74 */ |
| 65 deviceStates: { | 75 deviceStates: { |
| 66 type: Object, | 76 type: Object, |
| 67 value: function() { return {}; }, | 77 value: function() { return {}; }, |
| 68 }, | 78 }, |
| 69 | 79 |
| 70 /** | 80 /** |
| 71 * Network state data for each network type. | 81 * Network state data for each network type. |
| 72 * @type {NetworkStateObject} | 82 * @type {NetworkStateObject} |
| 73 */ | 83 */ |
| 74 networkStates: { | 84 networkStates: { |
| 75 type: Object, | 85 type: Object, |
| 76 value: function() { return {}; }, | 86 value: function() { return {}; }, |
| 77 }, | 87 }, |
| 78 | 88 |
| 79 /** | 89 /** |
| 80 * List of network state data for each network type. | 90 * List of network state data for each network type. |
| 81 * @type {NetworkStateListObject} | 91 * @type {NetworkStateListObject} |
| 82 */ | 92 */ |
| 83 networkStateLists: { | 93 networkStateLists: { |
| 84 type: Object, | 94 type: Object, |
| 85 value: function() { return {}; }, | 95 value: function() { return {}; }, |
| 86 } | 96 }, |
| 87 }, | 97 }, |
| 88 | 98 |
| 89 /** | 99 /** |
| 90 * Listener function for chrome.networkingPrivate.onNetworkListChanged event. | 100 * Listener function for chrome.networkingPrivate.onNetworkListChanged event. |
| 91 * @type {function(!Array<string>)} | 101 * @type {function(!Array<string>)} |
| 92 * @private | 102 * @private |
| 93 */ | 103 */ |
| 94 networkListChangedListener_: function() {}, | 104 networkListChangedListener_: function() {}, |
| 95 | 105 |
| 96 /** | 106 /** |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 chrome.networkingPrivate.onNetworksChanged.removeListener( | 157 chrome.networkingPrivate.onNetworksChanged.removeListener( |
| 148 this.networksChangedListener_); | 158 this.networksChangedListener_); |
| 149 }, | 159 }, |
| 150 | 160 |
| 151 /** | 161 /** |
| 152 * Event triggered when the WiFi network-summary-item is expanded. | 162 * Event triggered when the WiFi network-summary-item is expanded. |
| 153 * @param {!{detail: {expanded: boolean, type: string}}} event | 163 * @param {!{detail: {expanded: boolean, type: string}}} event |
| 154 * @private | 164 * @private |
| 155 */ | 165 */ |
| 156 onWiFiExpanded_: function(event) { | 166 onWiFiExpanded_: function(event) { |
| 157 this.getNetworkStates_(); // Get the latest network states (only). | 167 // Get the latest network states (only). |
| 168 this.getNetworkStates_(null); | |
| 158 if (event.detail.expanded) | 169 if (event.detail.expanded) |
| 159 chrome.networkingPrivate.requestNetworkScan(); | 170 chrome.networkingPrivate.requestNetworkScan(); |
| 160 }, | 171 }, |
| 161 | 172 |
| 162 /** | 173 /** |
| 163 * Event triggered when a network-summary-item is selected. | 174 * Event triggered when a network-summary-item is selected. |
| 164 * @param {!{detail: !CrOnc.NetworkStateProperties}} event | 175 * @param {!{detail: !CrOnc.NetworkStateProperties}} event |
| 165 * @private | 176 * @private |
| 166 */ | 177 */ |
| 167 onSelected_: function(event) { | 178 onSelected_: function(event) { |
| 168 var state = event.detail; | 179 var state = event.detail; |
| 169 if (state.ConnectionState == CrOnc.ConnectionState.NOT_CONNECTED) { | 180 if (this.canConnect_(state)) { |
| 170 this.connectToNetwork_(state); | 181 this.connectToNetwork_(state); |
| 171 return; | 182 return; |
| 172 } | 183 } |
| 173 this.fire('show-detail', state); | 184 this.fire('show-detail', state); |
| 174 }, | 185 }, |
| 175 | 186 |
| 176 /** | 187 /** |
| 177 * Event triggered when the enabled state of a network-summary-item is | 188 * Event triggered when the enabled state of a network-summary-item is |
| 178 * toggled. | 189 * toggled. |
| 179 * @param {!{detail: {enabled: boolean, | 190 * @param {!{detail: {enabled: boolean, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 this.networkIds_[id] = undefined; | 236 this.networkIds_[id] = undefined; |
| 226 return; | 237 return; |
| 227 } | 238 } |
| 228 this.updateNetworkState_(state.Type, state); | 239 this.updateNetworkState_(state.Type, state); |
| 229 }.bind(this)); | 240 }.bind(this)); |
| 230 } | 241 } |
| 231 }, this); | 242 }, this); |
| 232 }, | 243 }, |
| 233 | 244 |
| 234 /** | 245 /** |
| 246 * Determines whether or not a network state can be connected to. | |
| 247 * @param {!CrOnc.NetworkStateProperties} state The network state. | |
| 248 * @private | |
| 249 */ | |
| 250 canConnect_: function(state) { | |
| 251 if (state.Type == CrOnc.Type.ETHERNET || | |
| 252 state.Type == CrOnc.Type.VPN && !this.defaultNetwork) { | |
| 253 return false; | |
| 254 } | |
| 255 return state.ConnectionState == CrOnc.ConnectionState.NOT_CONNECTED; | |
| 256 }, | |
| 257 | |
| 258 /** | |
| 235 * Handles UI requests to connect to a network. | 259 * Handles UI requests to connect to a network. |
| 236 * TODO(stevenjb): Handle Cellular activation, etc. | 260 * TODO(stevenjb): Handle Cellular activation, etc. |
| 237 * @param {!CrOnc.NetworkStateProperties} state The network state. | 261 * @param {!CrOnc.NetworkStateProperties} state The network state. |
| 238 * @private | 262 * @private |
| 239 */ | 263 */ |
| 240 connectToNetwork_: function(state) { | 264 connectToNetwork_: function(state) { |
| 241 chrome.networkingPrivate.startConnect(state.GUID, function() { | 265 chrome.networkingPrivate.startConnect(state.GUID, function() { |
| 242 if (chrome.runtime.lastError && | 266 if (chrome.runtime.lastError && |
| 243 chrome.runtime.lastError != 'connecting') { | 267 chrome.runtime.lastError.message != 'connecting') { |
| 244 console.error('Unexpected networkingPrivate.startConnect error:', | 268 console.error('Unexpected networkingPrivate.startConnect error:', |
| 245 chrome.runtime.lastError); | 269 chrome.runtime.lastError, 'For:', state.GUID); |
| 246 } | 270 } |
| 247 }); | 271 }); |
| 248 }, | 272 }, |
| 249 | 273 |
| 250 /** | 274 /** |
| 251 * Requests the list of device states and network states from Chrome. | 275 * Requests the list of device states and network states from Chrome. |
| 252 * Updates deviceStates, networkStates, and networkStateLists once the | 276 * Updates deviceStates, networkStates, and networkStateLists once the |
| 253 * results are returned from Chrome. | 277 * results are returned from Chrome. |
| 254 * @private | 278 * @private |
| 255 */ | 279 */ |
| 256 getNetworkLists_: function() { | 280 getNetworkLists_: function() { |
| 257 // First get the device states. | 281 // First get the device states. |
| 258 chrome.networkingPrivate.getDeviceStates( | 282 chrome.networkingPrivate.getDeviceStates( |
| 259 function(states) { | 283 function(deviceStates) { |
| 260 this.getDeviceStatesCallback_(states); | |
| 261 // Second get the network states. | 284 // Second get the network states. |
| 262 this.getNetworkStates_(); | 285 this.getNetworkStates_(deviceStates); |
| 263 }.bind(this)); | 286 }.bind(this)); |
| 264 }, | 287 }, |
| 265 | 288 |
| 266 /** | 289 /** |
| 267 * Requests the list of network states from Chrome. Updates networkStates and | 290 * Requests the list of network states from Chrome. Updates networkStates and |
| 268 * networkStateLists once the results are returned from Chrome. | 291 * networkStateLists once the results are returned from Chrome. |
| 292 * @param {?Array<!DeviceStateProperties>} deviceStates The state properties | |
| 293 * for all available devices. | |
| 269 * @private | 294 * @private |
| 270 */ | 295 */ |
| 271 getNetworkStates_: function() { | 296 getNetworkStates_: function(deviceStates) { |
|
michaelpg
2015/10/26 20:41:42
Maybe make this opt_deviceStates (@param {Array<!D
stevenjb
2015/10/27 16:49:50
Done.
| |
| 272 var filter = { | 297 var filter = { |
| 273 networkType: chrome.networkingPrivate.NetworkType.ALL, | 298 networkType: chrome.networkingPrivate.NetworkType.ALL, |
| 274 visible: true, | 299 visible: true, |
| 275 configured: false | 300 configured: false |
| 276 }; | 301 }; |
| 277 chrome.networkingPrivate.getNetworks( | 302 var self = this; |
|
michaelpg
2015/10/26 20:41:41
we mostly/only use .bind(this) instead of var foo
stevenjb
2015/10/27 16:49:50
Done.
| |
| 278 filter, this.getNetworksCallback_.bind(this)); | 303 chrome.networkingPrivate.getNetworks(filter, function(networkStates) { |
| 279 }, | 304 self.getNetworksCallback_(deviceStates, networkStates); |
| 280 | 305 }); |
| 281 /** | |
| 282 * networkingPrivate.getDeviceStates callback. | |
| 283 * @param {!Array<!DeviceStateProperties>} states The state properties for all | |
| 284 * available devices. | |
| 285 * @private | |
| 286 */ | |
| 287 getDeviceStatesCallback_: function(states) { | |
| 288 var newStates = /** @type {!DeviceStateObject} */({}); | |
| 289 states.forEach(function(state) { newStates[state.Type] = state; }); | |
| 290 this.deviceStates = newStates; | |
| 291 }, | 306 }, |
| 292 | 307 |
| 293 /** | 308 /** |
| 294 * networkingPrivate.getNetworksState callback. | 309 * networkingPrivate.getNetworksState callback. |
| 295 * @param {!Array<!CrOnc.NetworkStateProperties>} states The state properties | 310 * @param {?Array<!DeviceStateProperties>} deviceStates The state properties |
| 296 * for all visible networks. | 311 * for all available devices. |
| 312 * @param {!Array<!CrOnc.NetworkStateProperties>} networkStates The state | |
| 313 * properties for all visible networks. | |
| 297 * @private | 314 * @private |
| 298 */ | 315 */ |
| 299 getNetworksCallback_: function(states) { | 316 getNetworksCallback_: function(deviceStates, networkStates) { |
| 317 var newDeviceStates; | |
| 318 if (deviceStates) { | |
| 319 newDeviceStates = /** @type {!DeviceStateObject} */({}); | |
| 320 deviceStates.forEach(function(state) { | |
| 321 newDeviceStates[state.Type] = state; | |
| 322 }); | |
| 323 } else { | |
| 324 newDeviceStates = this.deviceStates; | |
| 325 } | |
| 326 | |
| 300 // Clear any current networks. | 327 // Clear any current networks. |
| 301 this.networkIds_ = {}; | 328 this.networkIds_ = {}; |
| 302 | 329 |
| 303 // Track the first (active) state for each type. | 330 // Track the first (active) state for each type. |
| 304 var foundTypes = {}; | 331 var foundTypes = {}; |
| 305 | 332 |
| 306 // Complete list of states by type. | 333 // Complete list of states by type. |
| 307 /** @type {!NetworkStateListObject} */ var networkStateLists = { | 334 /** @type {!NetworkStateListObject} */ var networkStateLists = { |
| 308 Ethernet: [], | 335 Ethernet: [], |
| 309 WiFi: [], | 336 WiFi: [], |
| 310 Cellular: [], | 337 Cellular: [], |
| 311 WiMAX: [], | 338 WiMAX: [], |
| 312 VPN: [] | 339 VPN: [] |
| 313 }; | 340 }; |
| 314 | 341 |
| 315 states.forEach(function(state) { | 342 var firstConnectedNetwork = null; |
| 343 networkStates.forEach(function(state) { | |
| 316 var type = state.Type; | 344 var type = state.Type; |
| 317 if (!foundTypes[type]) { | 345 if (!foundTypes[type]) { |
| 318 foundTypes[type] = true; | 346 foundTypes[type] = true; |
| 319 this.updateNetworkState_(type, state); | 347 this.updateNetworkState_(type, state); |
| 348 if (!firstConnectedNetwork && state.Type != CrOnc.Type.VPN && | |
| 349 state.ConnectionState == CrOnc.ConnectionState.CONNECTED) { | |
| 350 firstConnectedNetwork = state; | |
| 351 } | |
| 320 } | 352 } |
| 321 networkStateLists[type].push(state); | 353 networkStateLists[type].push(state); |
| 322 }, this); | 354 }, this); |
| 323 | 355 |
| 356 this.defaultNetwork = firstConnectedNetwork; | |
| 357 | |
| 324 // Set any types with a deviceState and no network to a default state, | 358 // Set any types with a deviceState and no network to a default state, |
| 325 // and any types not found to undefined. | 359 // and any types not found to undefined. |
| 326 NETWORK_TYPES.forEach(function(type) { | 360 NETWORK_TYPES.forEach(function(type) { |
| 327 if (!foundTypes[type]) { | 361 if (!foundTypes[type]) { |
| 328 var defaultState = undefined; | 362 var defaultState = undefined; |
| 329 if (this.deviceStates[type]) | 363 if (newDeviceStates[type]) |
| 330 defaultState = {GUID: '', Type: type}; | 364 defaultState = {GUID: '', Type: type}; |
| 331 this.updateNetworkState_(type, defaultState); | 365 this.updateNetworkState_(type, defaultState); |
| 332 } | 366 } |
| 333 }, this); | 367 }, this); |
| 334 | 368 |
| 335 this.networkStateLists = networkStateLists; | 369 this.networkStateLists = networkStateLists; |
| 336 | 370 |
| 337 // Create a VPN entry in deviceStates if there are any VPN networks. | 371 // Create a VPN entry in deviceStates if there are any VPN networks. |
| 338 if (networkStateLists.VPN && networkStateLists.VPN.length > 0) { | 372 if (networkStateLists.VPN && networkStateLists.VPN.length > 0) { |
| 339 var vpn = {Type: CrOnc.Type.VPN, State: 'Enabled'}; | 373 newDeviceStates.VPN = /** @type {DeviceStateProperties} */ ({ |
| 340 this.set('deviceStates.VPN', vpn); | 374 Type: CrOnc.Type.VPN, |
| 375 State: chrome.networkingPrivate.DeviceStateType.ENABLED | |
| 376 }); | |
| 341 } | 377 } |
| 378 | |
| 379 this.deviceStates = newDeviceStates; | |
| 342 }, | 380 }, |
| 343 | 381 |
| 344 /** | 382 /** |
| 345 * Sets 'networkStates[type]' which will update the cr-network-list-item | 383 * Sets 'networkStates[type]' which will update the cr-network-list-item |
| 346 * associated with 'type'. | 384 * associated with 'type'. |
| 347 * @param {string} type The network type. | 385 * @param {string} type The network type. |
| 348 * @param {!CrOnc.NetworkStateProperties|undefined} state The state properties | 386 * @param {!CrOnc.NetworkStateProperties|undefined} state The state properties |
| 349 * for the network to associate with |type|. May be undefined if there are | 387 * for the network to associate with |type|. May be undefined if there are |
| 350 * no networks matching |type|. | 388 * no networks matching |type|. |
| 351 * @private | 389 * @private |
| 352 */ | 390 */ |
| 353 updateNetworkState_: function(type, state) { | 391 updateNetworkState_: function(type, state) { |
| 354 this.set('networkStates.' + type, state); | 392 this.set('networkStates.' + type, state); |
| 355 if (state) | 393 if (state) |
| 356 this.networkIds_[state.GUID] = true; | 394 this.networkIds_[state.GUID] = true; |
| 357 }, | 395 }, |
| 358 }); | 396 }); |
| 359 })(); | 397 })(); |
| OLD | NEW |