Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: chrome/browser/resources/settings/internet_page/internet_subpage.js

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

Powered by Google App Engine
This is Rietveld 408576698