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.
7 * 'cr-settings-internet-detail' is the settings subpage containing details
10 * @group Chrome Settings Elements
11 * @element cr-settings-internet-detail
15 is: 'cr-settings-internet-detail-page',
25 value: 'internet-detail',
38 * Whether the page is a subpage.
46 * Title for the page header and navigation menu.
51 return loadTimeData.getString('internetDetailPageTitle');
56 * Reflects the selected settings page. We use this to extract guid from
57 * window.location.href when this page is navigated to. This is a
58 * workaround for a bug in the 1.0 version of more-routing where
59 * selected-params='{{params}}' is not correctly setting params in
60 * settings_main.html. TODO(stevenjb): Remove once more-routing is fixed.
65 observer: 'selectedPageChanged_'
69 * Name of the 'core-icon' to show. TODO(stevenjb): Update this with the
70 * icon for the active internet connection.
74 value: 'settings-ethernet',
79 * The network GUID to display details for.
84 observer: 'guidChanged_',
88 * The current state for the network matching |guid|.
89 * @type {?CrOnc.NetworkStateProperties}
94 observer: 'networkStateChanged_'
98 * The network AutoConnect state.
103 observer: 'autoConnectChanged_'
107 * The network preferred state.
112 observer: 'preferNetworkChanged_'
116 * The network IP Address.
125 * Listener function for chrome.networkingPrivate.onNetworksChanged event.
126 * @type {?function(!Array<string>)}
129 networksChangedListener_: null,
132 attached: function() {
133 this.networksChangedListener_ = this.onNetworksChangedEvent_.bind(this);
134 chrome.networkingPrivate.onNetworksChanged.addListener(
135 this.networksChangedListener_);
139 detached: function() {
140 chrome.networkingPrivate.onNetworksChanged.removeListener(
141 this.networksChangedListener_);
145 * Polymer guid changed method.
147 guidChanged_: function() {
150 this.getNetworkDetails_();
154 * Polymer guid changed method. TODO(stevenjb): Remove, see TODO above.
156 selectedPageChanged_: function() {
157 if ((this.selectedPage && this.selectedPage.PAGE_ID) != this.PAGE_ID)
159 var href = window.location.href;
160 var idx = href.lastIndexOf('/');
161 var guid = href.slice(idx + 1);
166 * Polymer networkState changed method.
168 networkStateChanged_: function() {
169 if (!this.networkState)
172 // Update autoConnect if it has changed. Default value is false.
174 CrOnc.getActiveTypeValue(this.networkState, 'AutoConnect') || false;
175 if (autoConnect != this.autoConnect)
176 this.autoConnect = autoConnect;
178 // Update preferNetwork if it has changed. Default value is false.
179 var preferNetwork = this.networkState.Priority > 0;
180 if (preferNetwork != this.preferNetwork)
181 this.preferNetwork = preferNetwork;
183 // Set the IPAddress property to the IPV4 Address.
184 var ipv4 = CrOnc.getIPConfigForType(this.networkState, CrOnc.IPType.IPV4);
185 this.IPAddress = (ipv4 && ipv4.IPAddress) || '';
189 * Polymer autoConnect changed method.
191 autoConnectChanged_: function() {
192 if (!this.networkState || !this.guid)
194 var onc = {Type: this.networkState.Type};
195 CrOnc.setTypeProperty(onc, 'AutoConnect', this.autoConnect);
196 this.setNetworkProperties_(onc);
200 * Polymer preferNetwork changed method.
202 preferNetworkChanged_: function() {
203 if (!this.networkState || !this.guid)
205 var priority = this.preferNetwork ? 1 : 0;
206 this.setNetworkProperties_(
207 {Type: this.networkState.Type, Priority: priority});
211 * networkingPrivate.onNetworksChanged event callback.
212 * @param {!Array<string>} networkIds The list of changed network GUIDs.
215 onNetworksChangedEvent_: function(networkIds) {
216 if (networkIds.indexOf(this.guid) != -1)
217 this.getNetworkDetails_();
221 * Calls networkingPrivate.getState for this.guid.
224 getNetworkDetails_: function() {
227 chrome.networkingPrivate.getProperties(
228 this.guid, this.getPropertiesCallback_.bind(this));
232 * networkingPrivate.getProperties callback.
233 * @param {!CrOnc.NetworkStateProperties} state The network state properties.
236 getPropertiesCallback_: function(state) {
237 this.networkState = state;
241 * @param {!chrome.networkingPrivate.NetworkConfigProperties} onc The ONC
242 * network properties.
245 setNetworkProperties_: function(onc) {
248 chrome.networkingPrivate.setProperties(this.guid, onc, function() {
249 if (chrome.runtime.lastError) {
250 // An error typically indicates invalid input; request the properties
251 // to update any invalid fields.
252 this.getNetworkDetails_();
258 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
259 * @return {string} The text to display for the network name.
262 getStateName_: function(state) {
263 return (state && state.Name) || '';
267 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
268 * @return {string} The text to display for the network name.
271 getStateClass_: function(state) {
272 return this.isConnectedState_(state) ? 'connected' : '';
276 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
277 * @return {string} The text to display for the network connection state.
280 getStateText_: function(state) {
281 // TODO(stevenjb): Localize.
282 return (state && state.ConnectionState) || '';
286 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
287 * @return {boolean} True if the state is connected.
290 isConnectedState_: function(state) {
291 return state && state.ConnectionState == CrOnc.ConnectionState.CONNECTED;
295 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
296 * @return {boolean} Whether or not the network can be connected.
299 canConnect_: function(state) {
300 return state && state.Type != 'Ethernet' &&
301 state.ConnectionState == CrOnc.ConnectionState.NOT_CONNECTED;
305 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
306 * @return {boolean} Whether or not the network can be disconnected.
309 canDisconnect_: function(state) {
310 return state && state.Type != 'Ethernet' &&
311 state.ConnectionState != CrOnc.ConnectionState.NOT_CONNECTED;
315 * Callback when the Connect button is clicked.
318 onConnectClicked_: function() {
319 chrome.networkingPrivate.startConnect(this.guid);
323 * Callback when the Disconnect button is clicked.
326 onDisconnectClicked_: function() {
327 chrome.networkingPrivate.startDisconnect(this.guid);
331 * Event triggered when the apnlist element changes.
332 * @param {!{detail: { field: string, value: CrOnc.APNProperties}}} event
333 * The network-apnlist change event.
336 onApnChange_: function(event) {
337 if (event.detail.field != 'APN')
339 this.setNetworkProperties_({Cellular: {APN: event.detail.value}});
343 * Event triggered when the IP Config or NameServers element changes.
344 * @param {!{detail: {field: string,
345 * value: string|CrOnc.IPConfigProperties}}} event
346 * The network-ip-config or network-nameservers change event.
349 onIPConfigChange_: function(event) {
350 if (!this.networkState)
353 var field = event.detail.field;
354 var value = event.detail.value;
356 // Set just the IP Config properties that need to change.
358 if (field == 'IPAddressConfigType') {
359 if (onc.IPAddressConfigType == value)
361 onc.IPAddressConfigType = value;
362 } else if (field == 'NameServersConfigType') {
363 if (onc.NameServersConfigType == value)
365 onc.NameServersConfigType = value;
366 } else if (field == 'StaticIPConfig') {
367 if (onc.IPAddressConfigType == 'Static' && onc.StaticIPConfig) {
369 for (var key in value) {
370 if (onc.StaticIPConfig[key] != value[key]) {
378 onc.IPAddressConfigType = 'Static';
379 onc.StaticIPConfig = onc.StaticIPConfig || {};
381 onc.StaticIPConfig[key] = value[key];
382 } else if (field == 'NameServers') {
383 if (onc.NameServersConfigType == 'Static' && onc.StaticIPConfig &&
384 onc.StaticIPConfig.NameServers == value) {
387 onc.NameServersConfigType = 'Static';
388 onc.StaticIPConfig = onc.StaticIPConfig || {};
389 onc.StaticIPConfig.NameServers = value;
391 console.error('Unexpected change field: ' + field);
394 // setValidStaticIPConfig will fill in any other properties from
395 // networkState. This is necessary since we update IP Address and
396 // NameServers independently.
397 CrOnc.setValidStaticIPConfig(onc, this.networkState);
398 this.setNetworkProperties_(onc);
402 * Event triggered when the Proxy configuration element changes.
403 * @param {!{detail: {field: string, value: CrOnc.ProxySettings}}} event
404 * The network-proxy change event.
407 onProxyChange_: function(event) {
408 var field = event.detail.field;
409 var value = event.detail.value;
410 if (field != 'ProxySettings')
412 this.setNetworkProperties_({ProxySettings: value});
416 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
417 * @return {boolean} True if the shared message should be shown.
420 showShared_: function(state) {
422 (state.Source == 'Device' || state.Source == 'DevicePolicy');
426 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
427 * @return {boolean} True if the AutoConnect checkbox should be shown.
430 showAutoConnect_: function(state) {
431 return state && state.Type != 'Ethernet';
435 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
436 * @return {boolean} True if the prefer network checkbox should be shown.
439 showPreferNetwork_: function(state) {
440 return state && state.Type != 'Ethernet';
444 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
445 * @return {!Array<string>} The fields to display in the info section.
448 getInfoFields_: function(state) {
449 /** @type {!Array<string>} */ var fields = [];
453 if (state.Type == 'Cellular') {
454 fields.push('Cellular.ActivationState',
455 'Cellular.RoamingState',
456 'RestrictedConnectivity',
457 'Cellular.ServingOperator.Name');
459 if (state.Type == 'VPN') {
460 fields.push('VPN.Host', 'VPN.Type');
461 if (state.VPN.Type == 'OpenVPN')
462 fields.push('VPN.OpenVPN.Username');
463 else if (state.VPN.Type == 'L2TP-IPsec')
464 fields.push('VPN.L2TP.Username');
465 // TODO(stevenjb): ThirdPartyVPN
467 if (state.Type == 'WiFi')
468 fields.push('RestrictedConnectivity');
469 if (state.Type == 'WiMAX') {
470 fields.push('RestrictedConnectivity', 'WiMAX.EAP.Identity');
476 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
477 * @return {!Array<string>} The fields to display in the Advanced section.
480 getAdvancedFields_: function(state) {
481 /** @type {!Array<string>} */ var fields = [];
484 fields.push('MacAddress');
485 if (state.Type == 'Cellular') {
486 fields.push('Cellular.Carrier',
488 'Cellular.NetworkTechnology',
489 'Cellular.ServingOperator.Code');
491 if (state.Type == 'WiFi') {
492 fields.push('WiFi.SSID',
495 'WiFi.SignalStrength',
498 if (state.Type == 'WiMAX')
499 fields.push('WiFi.SignalStrength');
504 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
505 * @return {!Array<string>} The fields to display in the device section.
508 getDeviceFields_: function(state) {
509 /** @type {!Array<string>} */ var fields = [];
512 if (state.Type == 'Cellular') {
513 fields.push('Cellular.HomeProvider.Name',
514 'Cellular.HomeProvider.Country',
515 'Cellular.HomeProvider.Code',
516 'Cellular.Manufacturer',
518 'Cellular.FirmwareRevision',
519 'Cellular.HardwareRevision',
527 'Cellular.PRLVersion');
533 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
534 * @return {boolean} True if there are any advanced fields to display.
537 hasAdvancedOrDeviceFields_: function(state) {
538 return this.getAdvancedFields_(state).length > 0 || this.hasDeviceFields_();
542 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
543 * @return {boolean} True if there are any device fields to display.
546 hasDeviceFields_: function(state) {
547 var fields = this.getDeviceFields_(state);
548 return fields.length > 0;
552 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
553 * @return {boolean} True if the network section should be shown.
556 hasNetworkSection_: function(state) {
557 return state && state.Type != 'VPN';
561 * @param {?CrOnc.NetworkStateProperties} state The network state properties.
562 * @param {string} type The network type.
563 * @return {boolean} True if the network type matches 'type'.
566 isType_: function(state, type) {
567 return state && (state.Type == type);