| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 /** |
| 6 * @fileoverview Polymer element for displaying informaiton about WiFi, |
| 7 * WiMAX, or virtual networks. |
| 8 */ |
| 9 |
| 10 /** @typedef {chrome.networkingPrivate.DeviceStateProperties} */ |
| 11 var DeviceStateProperties; |
| 12 |
| 13 Polymer({ |
| 14 is: 'settings-internet-subpage', |
| 15 |
| 16 behaviors: [ |
| 17 CrPolicyNetworkBehavior, |
| 18 settings.RouteObserverBehavior, |
| 19 I18nBehavior, |
| 20 ], |
| 21 |
| 22 properties: { |
| 23 /** |
| 24 * Highest priority connected network or null. Set by network-summary. |
| 25 * @type {?CrOnc.NetworkStateProperties|undefined} |
| 26 */ |
| 27 defaultNetwork: Object, |
| 28 |
| 29 /** |
| 30 * Device state for the network type. |
| 31 * @type {?DeviceStateProperties|undefined} |
| 32 */ |
| 33 deviceState: Object, |
| 34 |
| 35 /** @type {!chrome.networkingPrivate.GlobalPolicy|undefined} */ |
| 36 globalPolicy: Object, |
| 37 |
| 38 /** |
| 39 * List of third party VPN providers. |
| 40 * @type |
| 41 * {!Array<!chrome.networkingPrivate.ThirdPartyVPNProperties>|undefined} |
| 42 */ |
| 43 thirdPartyVpnProviders: Array, |
| 44 |
| 45 /** |
| 46 * Interface for networkingPrivate calls, passed from internet_page. |
| 47 * @type {!NetworkingPrivate} |
| 48 */ |
| 49 networkingPrivate: Object, |
| 50 |
| 51 showSpinner: { |
| 52 type: Boolean, |
| 53 notify: true, |
| 54 value: false, |
| 55 }, |
| 56 |
| 57 /** |
| 58 * List of all network state data for the network type. |
| 59 * @private {!Array<!CrOnc.NetworkStateProperties>} |
| 60 */ |
| 61 networkStateList_: { |
| 62 type: Array, |
| 63 value: function() { |
| 64 return []; |
| 65 }, |
| 66 }, |
| 67 |
| 68 /** |
| 69 * Dictionary of lists of network states for third party VPNs. |
| 70 * @private {!Object<!Array<!CrOnc.NetworkStateProperties>>} |
| 71 */ |
| 72 thirdPartyVpns_: { |
| 73 type: Object, |
| 74 value: function() { |
| 75 return {}; |
| 76 }, |
| 77 }, |
| 78 }, |
| 79 |
| 80 observers: ['updateScanning_(networkingPrivate, deviceState)'], |
| 81 |
| 82 /** @private {number|null} */ |
| 83 scanIntervalId_: null, |
| 84 |
| 85 /** |
| 86 * Listener function for chrome.networkingPrivate.onNetworkListChanged event. |
| 87 * @type {?function(!Array<string>)} |
| 88 * @private |
| 89 */ |
| 90 networkListChangedListener_: null, |
| 91 |
| 92 /** |
| 93 * settings.RouteObserverBehavior |
| 94 * @param {!settings.Route} route |
| 95 * @protected |
| 96 */ |
| 97 currentRouteChanged: function(route) { |
| 98 if (route != settings.Route.INTERNET_NETWORKS) { |
| 99 this.stopScanning_(); |
| 100 return; |
| 101 } |
| 102 // Clear any stale data. |
| 103 this.networkStateList_ = []; |
| 104 this.thirdPartyVpns_ = {}; |
| 105 // Request the list of networks and start scanning of necessary. |
| 106 this.getNetworkStateList_(); |
| 107 this.updateScanning_(); |
| 108 }, |
| 109 |
| 110 /** override */ |
| 111 attached: function() { |
| 112 this.scanIntervalId_ = null; |
| 113 |
| 114 this.networkListChangedListener_ = this.networkListChangedListener_ || |
| 115 this.onNetworkListChangedEvent_.bind(this); |
| 116 this.networkingPrivate.onNetworkListChanged.addListener( |
| 117 this.networkListChangedListener_); |
| 118 }, |
| 119 |
| 120 /** override */ |
| 121 detached: function() { |
| 122 this.stopScanning_(); |
| 123 this.networkingPrivate.onNetworkListChanged.removeListener( |
| 124 assert(this.networkListChangedListener_)); |
| 125 }, |
| 126 |
| 127 /** @private */ |
| 128 updateScanning_: function() { |
| 129 if (!this.deviceState) |
| 130 return; |
| 131 if (this.deviceState.Type != CrOnc.Type.WI_FI) { |
| 132 // deviceState probably changed, re-request networks. |
| 133 this.getNetworkStateList_(); |
| 134 return; |
| 135 } |
| 136 this.showSpinner = !!this.deviceState.Scanning; |
| 137 this.startScanning_(); |
| 138 }, |
| 139 |
| 140 /** @private */ |
| 141 startScanning_: function() { |
| 142 if (this.scanIntervalId_ != null) |
| 143 return; |
| 144 /** @const */ var INTERVAL_MS = 10 * 1000; |
| 145 this.networkingPrivate.requestNetworkScan(); |
| 146 this.scanIntervalId_ = window.setInterval(function() { |
| 147 this.networkingPrivate.requestNetworkScan(); |
| 148 }.bind(this), INTERVAL_MS); |
| 149 }, |
| 150 |
| 151 /** @private */ |
| 152 stopScanning_: function() { |
| 153 if (this.scanIntervalId_ == null) |
| 154 return; |
| 155 window.clearInterval(this.scanIntervalId_); |
| 156 this.scanIntervalId_ = null; |
| 157 }, |
| 158 |
| 159 /** |
| 160 * networkingPrivate.onNetworkListChanged event callback. |
| 161 * @private |
| 162 */ |
| 163 onNetworkListChangedEvent_: function() { |
| 164 this.getNetworkStateList_(); |
| 165 }, |
| 166 |
| 167 /** @private */ |
| 168 getNetworkStateList_: function() { |
| 169 if (!this.deviceState) |
| 170 return; |
| 171 var filter = { |
| 172 networkType: this.deviceState.Type, |
| 173 visible: true, |
| 174 configured: false |
| 175 }; |
| 176 this.networkingPrivate.getNetworks(filter, function(networkStates) { |
| 177 if (this.deviceState.Type != CrOnc.Type.VPN) { |
| 178 this.networkStateList_ = networkStates; |
| 179 return; |
| 180 } |
| 181 // For VPNs, separate out third party VPNs. |
| 182 var networkStateList = []; |
| 183 var thirdPartyVpns = {}; |
| 184 for (var i = 0; i < networkStates.length; ++i) { |
| 185 var state = networkStates[i]; |
| 186 var providerType = state.VPN && state.VPN.ThirdPartyVPN && |
| 187 state.VPN.ThirdPartyVPN.ProviderName; |
| 188 if (providerType) { |
| 189 thirdPartyVpns[providerType] = thirdPartyVpns[providerType] || []; |
| 190 thirdPartyVpns[providerType].push(state); |
| 191 } else { |
| 192 networkStateList.push(state); |
| 193 } |
| 194 } |
| 195 this.networkStateList_ = networkStateList; |
| 196 this.thirdPartyVpns_ = thirdPartyVpns; |
| 197 }.bind(this)); |
| 198 }, |
| 199 |
| 200 /** |
| 201 * @param {!DeviceStateProperties|undefined} deviceState |
| 202 * @return {boolean} Whether or not the device state is enabled. |
| 203 * @private |
| 204 */ |
| 205 deviceIsEnabled_: function(deviceState) { |
| 206 return !!deviceState && |
| 207 deviceState.State == chrome.networkingPrivate.DeviceStateType.ENABLED; |
| 208 }, |
| 209 |
| 210 /** |
| 211 * @param {!DeviceStateProperties|undefined} deviceState |
| 212 * @param {string} onstr |
| 213 * @param {string} offstr |
| 214 * @return {string} |
| 215 * @private |
| 216 */ |
| 217 getOffOnString_: function(deviceState, onstr, offstr) { |
| 218 return this.deviceIsEnabled_(deviceState) ? onstr : offstr; |
| 219 }, |
| 220 |
| 221 /** |
| 222 * @param {?DeviceStateProperties} deviceState |
| 223 * @return {boolean} |
| 224 * @private |
| 225 */ |
| 226 enableToggleIsVisible_: function(deviceState) { |
| 227 return !!deviceState && deviceState.Type != CrOnc.Type.ETHERNET && |
| 228 deviceState.Type != CrOnc.Type.VPN; |
| 229 }, |
| 230 |
| 231 /** |
| 232 * @param {?DeviceStateProperties} deviceState |
| 233 * @return {boolean} |
| 234 * @private |
| 235 */ |
| 236 enableToggleIsEnabled_: function(deviceState) { |
| 237 return !!deviceState && |
| 238 deviceState.State != |
| 239 chrome.networkingPrivate.DeviceStateType.PROHIBITED; |
| 240 }, |
| 241 |
| 242 /** |
| 243 * @param {!DeviceStateProperties} deviceState |
| 244 * @return {string} |
| 245 * @private |
| 246 */ |
| 247 getToggleA11yString_: function(deviceState) { |
| 248 if (!this.enableToggleIsVisible_(deviceState)) |
| 249 return ''; |
| 250 switch (deviceState.Type) { |
| 251 case CrOnc.Type.CELLULAR: |
| 252 return this.i18n('internetToggleMobileA11yLabel'); |
| 253 case CrOnc.Type.WI_FI: |
| 254 return this.i18n('internetToggleWiFiA11yLabel'); |
| 255 case CrOnc.Type.WI_MAX: |
| 256 return this.i18n('internetToggleWiMAXA11yLabel'); |
| 257 } |
| 258 assertNotReached(); |
| 259 return ''; |
| 260 }, |
| 261 |
| 262 /** |
| 263 * @param {!chrome.networkingPrivate.ThirdPartyVPNProperties} vpnState |
| 264 * @return {string} |
| 265 * @private |
| 266 */ |
| 267 getAddThirdPartyVpnA11yString: function(vpnState) { |
| 268 return this.i18n('internetAddThirdPartyVPN', vpnState.ProviderName); |
| 269 }, |
| 270 |
| 271 /** |
| 272 * @param {!chrome.networkingPrivate.GlobalPolicy} globalPolicy |
| 273 * @return {boolean} |
| 274 * @private |
| 275 */ |
| 276 allowAddConnection_: function(globalPolicy) { |
| 277 return globalPolicy && !globalPolicy.AllowOnlyPolicyNetworksToConnect; |
| 278 }, |
| 279 |
| 280 /** |
| 281 * @param {!DeviceStateProperties} deviceState |
| 282 * @param {!chrome.networkingPrivate.GlobalPolicy} globalPolicy |
| 283 * @return {boolean} |
| 284 * @private |
| 285 */ |
| 286 showAddButton_: function(deviceState, globalPolicy) { |
| 287 if (!deviceState || deviceState.Type != CrOnc.Type.WI_FI) |
| 288 return false; |
| 289 return this.allowAddConnection_(globalPolicy); |
| 290 }, |
| 291 |
| 292 /** @private */ |
| 293 onAddButtonTap_: function() { |
| 294 chrome.send('addNetwork', [this.deviceState.Type]); |
| 295 }, |
| 296 |
| 297 /** |
| 298 * @param {!{model: |
| 299 * !{item: !chrome.networkingPrivate.ThirdPartyVPNProperties}, |
| 300 * }} event |
| 301 * @private |
| 302 */ |
| 303 onAddThirdPartyVpnTap_: function(event) { |
| 304 var provider = event.model.item; |
| 305 chrome.send('addNetwork', [CrOnc.Type.VPN, provider.ExtensionID]); |
| 306 }, |
| 307 |
| 308 /** |
| 309 * @param {!DeviceStateProperties} deviceState |
| 310 * @return {boolean} |
| 311 * @private |
| 312 */ |
| 313 knownNetworksIsVisible_: function(deviceState) { |
| 314 return deviceState && deviceState.Type == CrOnc.Type.WI_FI; |
| 315 }, |
| 316 |
| 317 /** |
| 318 * Event triggered when the known networks button is tapped. |
| 319 * @private |
| 320 */ |
| 321 onKnownNetworksTap_: function() { |
| 322 this.fire('show-known-networks', {type: CrOnc.Type.WI_FI}); |
| 323 }, |
| 324 |
| 325 /** |
| 326 * Event triggered when the enable button is toggled. |
| 327 * @param {!Event} event |
| 328 * @private |
| 329 */ |
| 330 onDeviceEnabledTap_: function(event) { |
| 331 assert(this.deviceState); |
| 332 var deviceIsEnabled = this.deviceIsEnabled_(this.deviceState); |
| 333 var type = this.deviceState ? this.deviceState.Type : ''; |
| 334 this.fire( |
| 335 'device-enabled-toggled', {enabled: !deviceIsEnabled, type: type}); |
| 336 // Make sure this does not propagate to onDetailsTap_. |
| 337 event.stopPropagation(); |
| 338 }, |
| 339 |
| 340 /** |
| 341 * @param {!Object<!Array<!CrOnc.NetworkStateProperties>>} thirdPartyVpns |
| 342 * @param {!chrome.networkingPrivate.ThirdPartyVPNProperties} vpnState |
| 343 * @return {!Array<!CrOnc.NetworkStateProperties>} |
| 344 * @private |
| 345 */ |
| 346 getThirdPartyVpnNetworks_: function(thirdPartyVpns, vpnState) { |
| 347 return thirdPartyVpns[vpnState.ProviderName] || []; |
| 348 }, |
| 349 |
| 350 /** |
| 351 * @param {!Object<!Array<!CrOnc.NetworkStateProperties>>} thirdPartyVpns |
| 352 * @param {!chrome.networkingPrivate.ThirdPartyVPNProperties} vpnState |
| 353 * @return {boolean} |
| 354 * @private |
| 355 */ |
| 356 haveThirdPartyVpnNetwork_: function(thirdPartyVpns, vpnState) { |
| 357 var list = this.getThirdPartyVpnNetworks_(thirdPartyVpns, vpnState); |
| 358 return !!list.length; |
| 359 }, |
| 360 |
| 361 /** |
| 362 * Event triggered when a network list item is selected. |
| 363 * @param {!{target: HTMLElement, detail: !CrOnc.NetworkStateProperties}} e |
| 364 * @private |
| 365 */ |
| 366 onNetworkSelected_: function(e) { |
| 367 assert(this.globalPolicy); |
| 368 assert(this.defaultNetwork !== undefined); |
| 369 var state = e.detail; |
| 370 e.target.blur(); |
| 371 if (this.canConnect_(state, this.globalPolicy, this.defaultNetwork)) { |
| 372 this.connectToNetwork_(state); |
| 373 return; |
| 374 } |
| 375 this.fire('show-detail', state); |
| 376 }, |
| 377 |
| 378 /** |
| 379 * Determines whether or not a network state can be connected to. |
| 380 * @param {!CrOnc.NetworkStateProperties} state The network state. |
| 381 * @param {!chrome.networkingPrivate.GlobalPolicy} globalPolicy |
| 382 * @param {?CrOnc.NetworkStateProperties} defaultNetwork |
| 383 * @private |
| 384 */ |
| 385 canConnect_: function(state, globalPolicy, defaultNetwork) { |
| 386 if (state.Type == CrOnc.Type.WI_FI && globalPolicy && |
| 387 globalPolicy.AllowOnlyPolicyNetworksToConnect && |
| 388 !this.isPolicySource(state.Source)) { |
| 389 return false; |
| 390 } |
| 391 if (state.Type == CrOnc.Type.VPN && |
| 392 (!defaultNetwork || |
| 393 defaultNetwork.ConnectionState != CrOnc.ConnectionState.CONNECTED)) { |
| 394 return false; |
| 395 } |
| 396 return state.ConnectionState == CrOnc.ConnectionState.NOT_CONNECTED; |
| 397 }, |
| 398 |
| 399 /** |
| 400 * Handles UI requests to connect to a network. |
| 401 * TODO(stevenjb): Handle Cellular activation, etc. |
| 402 * @param {!CrOnc.NetworkStateProperties} state The network state. |
| 403 * @private |
| 404 */ |
| 405 connectToNetwork_: function(state) { |
| 406 this.networkingPrivate.startConnect(state.GUID, function() { |
| 407 if (chrome.runtime.lastError) { |
| 408 var message = chrome.runtime.lastError.message; |
| 409 if (message != 'connecting') { |
| 410 console.error( |
| 411 'Unexpected networkingPrivate.startConnect error: ' + message + |
| 412 'For: ' + state.GUID); |
| 413 } |
| 414 } |
| 415 }); |
| 416 }, |
| 417 |
| 418 /** |
| 419 * @param {*} lhs |
| 420 * @param {*} rhs |
| 421 * @return {boolean} |
| 422 */ |
| 423 isEqual_: function(lhs, rhs) { |
| 424 return lhs === rhs; |
| 425 }, |
| 426 }); |
| OLD | NEW |