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.
6 * @fileoverview Polymer element for displaying a summary of network states
7 * by type: Ethernet, WiFi, Cellular, WiMAX, and VPN.
11 /** @typedef {chrome.networkingPrivate.DeviceStateProperties} */
12 var DeviceStateProperties;
16 * Ethernet: (DeviceStateProperties|undefined),
17 * WiFi: (DeviceStateProperties|undefined),
18 * Cellular: (DeviceStateProperties|undefined),
19 * WiMAX: (DeviceStateProperties|undefined),
20 * VPN: (DeviceStateProperties|undefined)
23 var DeviceStateObject;
27 * Ethernet: (?CrOnc.NetworkStateProperties|undefined),
28 * WiFi: (?CrOnc.NetworkStateProperties|undefined),
29 * Cellular: (?CrOnc.NetworkStateProperties|undefined),
30 * WiMAX: (?CrOnc.NetworkStateProperties|undefined),
31 * VPN: (?CrOnc.NetworkStateProperties|undefined)
34 var NetworkStateObject;
38 * Ethernet: (Array<CrOnc.NetworkStateProperties>|undefined),
39 * WiFi: (Array<CrOnc.NetworkStateProperties>|undefined),
40 * Cellular: (Array<CrOnc.NetworkStateProperties>|undefined),
41 * WiMAX: (Array<CrOnc.NetworkStateProperties>|undefined),
42 * VPN: (Array<CrOnc.NetworkStateProperties>|undefined)
45 var NetworkStateListObject;
47 /** @const {!Array<string>} */
48 var NETWORK_TYPES = ['Ethernet', 'WiFi', 'Cellular', 'WiMAX', 'VPN'];
51 is: 'network-summary',
55 * The device state for each network device type.
56 * @type {DeviceStateObject}
60 value: function() { return {}; },
64 * Network state data for each network type.
65 * @type {NetworkStateObject}
69 value: function() { return {}; },
73 * List of network state data for each network type.
74 * @type {NetworkStateListObject}
78 value: function() { return {}; },
83 * Listener function for chrome.networkingPrivate.onNetworkListChanged event.
84 * @type {?function(!Array<string>)}
87 networkListChangedListener_: null,
90 * Listener function for chrome.networkingPrivate.onDeviceStateListChanged
92 * @type {?function(!Array<string>)}
95 deviceStateListChangedListener_: null,
98 * Listener function for chrome.networkingPrivate.onNetworksChanged event.
99 * @type {?function(!Array<string>)}
102 networksChangedListener_: null,
105 * Dictionary of GUIDs identifying primary (active) networks for each type.
112 attached: function() {
113 this.networkIds_ = {};
115 this.getNetworkLists_();
117 this.networkListChangedListener_ =
118 this.onNetworkListChangedEvent_.bind(this);
119 chrome.networkingPrivate.onNetworkListChanged.addListener(
120 this.networkListChangedListener_);
122 this.deviceStateListChangedListener_ =
123 this.onDeviceStateListChangedEvent_.bind(this);
124 chrome.networkingPrivate.onDeviceStateListChanged.addListener(
125 this.deviceStateListChangedListener_);
127 this.networksChangedListener_ = this.onNetworksChangedEvent_.bind(this);
128 chrome.networkingPrivate.onNetworksChanged.addListener(
129 this.networksChangedListener_);
133 detached: function() {
134 chrome.networkingPrivate.onNetworkListChanged.removeListener(
135 this.networkListChangedListener_);
137 chrome.networkingPrivate.onDeviceStateListChanged.removeListener(
138 this.deviceStateListChangedListener_);
140 chrome.networkingPrivate.onNetworksChanged.removeListener(
141 this.networksChangedListener_);
145 * Event triggered when the WiFi network-summary-item is expanded.
146 * @param {!{detail: {expanded: boolean, type: string}}} event
149 onWiFiExpanded_: function(event) {
150 this.getNetworkStates_(); // Get the latest network states (only).
151 chrome.networkingPrivate.requestNetworkScan();
155 * Event triggered when a network-summary-item is selected.
156 * @param {!{detail: !CrOnc.NetworkStateProperties}} event
159 onSelected_: function(event) {
160 var state = event.detail;
161 if (state.ConnectionState == CrOnc.ConnectionState.NOT_CONNECTED) {
162 this.connectToNetwork_(state);
165 MoreRouting.navigateTo('internet-detail', {guid: state.GUID});
169 * Event triggered when the enabled state of a network-summary-item is
171 * @param {!{detail: {enabled: boolean, type: string}}} event
174 onDeviceEnabledToggled_: function(event) {
175 if (event.detail.enabled)
176 chrome.networkingPrivate.enableNetworkType(event.detail.type);
178 chrome.networkingPrivate.disableNetworkType(event.detail.type);
182 * networkingPrivate.onNetworkListChanged event callback.
185 onNetworkListChangedEvent_: function() { this.getNetworkLists_(); },
188 * networkingPrivate.onDeviceStateListChanged event callback.
191 onDeviceStateListChangedEvent_: function() { this.getNetworkLists_(); },
194 * networkingPrivate.onNetworksChanged event callback.
195 * @param {!Array<string>} networkIds The list of changed network GUIDs.
198 onNetworksChangedEvent_: function(networkIds) {
199 networkIds.forEach(function(id) {
200 if (id in this.networkIds_) {
201 chrome.networkingPrivate.getState(id,
202 this.getStateCallback_.bind(this));
208 * Handles UI requests to connect to a network.
209 * TODO(stevenjb): Handle Cellular activation, etc.
210 * @param {!CrOnc.NetworkStateProperties} state The network state.
213 connectToNetwork_: function(state) {
214 chrome.networkingPrivate.startConnect(state.GUID);
218 * Requests the list of device states and network states from Chrome.
219 * Updates deviceStates, networkStates, and networkStateLists once the
220 * results are returned from Chrome.
223 getNetworkLists_: function() {
224 // First get the device states.
225 chrome.networkingPrivate.getDeviceStates(
227 this.getDeviceStatesCallback_(states);
228 // Second get the network states.
229 this.getNetworkStates_();
234 * Requests the list of network states from Chrome. Updates networkStates and
235 * networkStateLists once the results are returned from Chrome.
238 getNetworkStates_: function() {
244 chrome.networkingPrivate.getNetworks(
245 filter, this.getNetworksCallback_.bind(this));
249 * networkingPrivate.getDeviceStates callback.
250 * @param {!Array<!DeviceStateProperties>} states The state properties for all
254 getDeviceStatesCallback_: function(states) {
255 /** @type {!DeviceStateObject} */ var newStates = {};
256 states.forEach(function(state) { newStates[state.Type] = state; });
257 this.deviceStates = newStates;
261 * networkingPrivate.getNetworksState callback.
262 * @param {!Array<!CrOnc.NetworkStateProperties>} states The state properties
263 * for all visible networks.
266 getNetworksCallback_: function(states) {
267 // Clear any current networks.
268 this.networkIds_ = {};
270 // Get the first (active) state for each type.
272 /** @type {!NetworkStateListObject} */ var networkStateLists = {
279 states.forEach(function(state) {
280 var type = state.Type;
281 if (!foundTypes[type]) {
282 foundTypes[type] = true;
283 this.updateNetworkState_(type, state);
285 networkStateLists[type].push(state);
288 // Set any types not found to a default value or null.
289 NETWORK_TYPES.forEach(function(type) {
290 if (!foundTypes[type]) {
291 /** @type {CrOnc.NetworkStateProperties} */ var defaultState = null;
292 if (this.deviceStates[type])
293 defaultState = { GUID: '', Type: type };
294 this.updateNetworkState_(type, defaultState);
298 this.networkStateLists = networkStateLists;
300 // Create a VPN entry in deviceStates if there are any VPN networks.
301 if (networkStateLists.VPN && networkStateLists.VPN.length > 0) {
302 var vpn = { Type: 'VPN', State: 'Enabled' };
303 this.set('deviceStates.VPN', vpn);
308 * networkingPrivate.getState callback.
309 * @param {!CrOnc.NetworkStateProperties} state The network state properties.
312 getStateCallback_: function(state) {
314 if (!this.networkIds_[id])
316 this.updateNetworkState_(state.Type, state);
320 * Sets 'networkStates[type]' which will update the network-list-item
321 * associated with 'type'.
322 * @param {string} type The network type.
323 * @param {?CrOnc.NetworkStateProperties} state The state properties for the
324 * network to associate with |type|. May be null if there are no networks
328 updateNetworkState_: function(type, state) {
329 this.set('networkStates.' + type, state);
331 this.networkIds_[state.GUID] = true;