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>} */
57 is
: 'network-summary',
61 * The device state for each network device type.
62 * @type {DeviceStateObject}
66 value: function() { return {}; },
70 * Network state data for each network type.
71 * @type {NetworkStateObject}
75 value: function() { return {}; },
79 * List of network state data for each network type.
80 * @type {NetworkStateListObject}
84 value: function() { return {}; },
89 * Listener function for chrome.networkingPrivate.onNetworkListChanged event.
90 * @type {?function(!Array<string>)}
93 networkListChangedListener_
: null,
96 * Listener function for chrome.networkingPrivate.onDeviceStateListChanged
98 * @type {?function(!Array<string>)}
101 deviceStateListChangedListener_
: null,
104 * Listener function for chrome.networkingPrivate.onNetworksChanged event.
105 * @type {?function(!Array<string>)}
108 networksChangedListener_
: null,
111 * Dictionary of GUIDs identifying primary (active) networks for each type.
118 attached: function() {
119 this.networkIds_
= {};
121 this.getNetworkLists_();
123 this.networkListChangedListener_
=
124 this.onNetworkListChangedEvent_
.bind(this);
125 chrome
.networkingPrivate
.onNetworkListChanged
.addListener(
126 this.networkListChangedListener_
);
128 this.deviceStateListChangedListener_
=
129 this.onDeviceStateListChangedEvent_
.bind(this);
130 chrome
.networkingPrivate
.onDeviceStateListChanged
.addListener(
131 this.deviceStateListChangedListener_
);
133 this.networksChangedListener_
= this.onNetworksChangedEvent_
.bind(this);
134 chrome
.networkingPrivate
.onNetworksChanged
.addListener(
135 this.networksChangedListener_
);
139 detached: function() {
140 chrome
.networkingPrivate
.onNetworkListChanged
.removeListener(
141 this.networkListChangedListener_
);
143 chrome
.networkingPrivate
.onDeviceStateListChanged
.removeListener(
144 this.deviceStateListChangedListener_
);
146 chrome
.networkingPrivate
.onNetworksChanged
.removeListener(
147 this.networksChangedListener_
);
151 * Event triggered when the WiFi network-summary-item is expanded.
152 * @param {!{detail: {expanded: boolean, type: string}}} event
155 onWiFiExpanded_: function(event
) {
156 this.getNetworkStates_(); // Get the latest network states (only).
157 if (event
.detail
.expanded
)
158 chrome
.networkingPrivate
.requestNetworkScan();
162 * Event triggered when a network-summary-item is selected.
163 * @param {!{detail: !CrOnc.NetworkStateProperties}} event
166 onSelected_: function(event
) {
167 var state
= event
.detail
;
168 if (state
.ConnectionState
== CrOnc
.ConnectionState
.NOT_CONNECTED
) {
169 this.connectToNetwork_(state
);
172 MoreRouting
.navigateTo('internet-detail', {guid
: state
.GUID
});
176 * Event triggered when the enabled state of a network-summary-item is
178 * @param {!{detail: {enabled: boolean, type: string}}} event
181 onDeviceEnabledToggled_: function(event
) {
182 if (event
.detail
.enabled
)
183 chrome
.networkingPrivate
.enableNetworkType(event
.detail
.type
);
185 chrome
.networkingPrivate
.disableNetworkType(event
.detail
.type
);
189 * networkingPrivate.onNetworkListChanged event callback.
192 onNetworkListChangedEvent_: function() { this.getNetworkLists_(); },
195 * networkingPrivate.onDeviceStateListChanged event callback.
198 onDeviceStateListChangedEvent_: function() { this.getNetworkLists_(); },
201 * networkingPrivate.onNetworksChanged event callback.
202 * @param {!Array<string>} networkIds The list of changed network GUIDs.
205 onNetworksChangedEvent_: function(networkIds
) {
206 networkIds
.forEach(function(id
) {
207 if (id
in this.networkIds_
) {
208 chrome
.networkingPrivate
.getState(
211 // Async call, ensure id still exists.
212 if (!this.networkIds_
[id
])
215 this.networkIds_
[id
] = undefined;
218 this.updateNetworkState_(state
.Type
, state
);
225 * Handles UI requests to connect to a network.
226 * TODO(stevenjb): Handle Cellular activation, etc.
227 * @param {!CrOnc.NetworkStateProperties} state The network state.
230 connectToNetwork_: function(state
) {
231 chrome
.networkingPrivate
.startConnect(state
.GUID
);
235 * Requests the list of device states and network states from Chrome.
236 * Updates deviceStates, networkStates, and networkStateLists once the
237 * results are returned from Chrome.
240 getNetworkLists_: function() {
241 // First get the device states.
242 chrome
.networkingPrivate
.getDeviceStates(
244 this.getDeviceStatesCallback_(states
);
245 // Second get the network states.
246 this.getNetworkStates_();
251 * Requests the list of network states from Chrome. Updates networkStates and
252 * networkStateLists once the results are returned from Chrome.
255 getNetworkStates_: function() {
261 chrome
.networkingPrivate
.getNetworks(
262 filter
, this.getNetworksCallback_
.bind(this));
266 * networkingPrivate.getDeviceStates callback.
267 * @param {!Array<!DeviceStateProperties>} states The state properties for all
271 getDeviceStatesCallback_: function(states
) {
272 /** @type {!DeviceStateObject} */ var newStates
= {};
273 states
.forEach(function(state
) { newStates
[state
.Type
] = state
; });
274 this.deviceStates
= newStates
;
278 * networkingPrivate.getNetworksState callback.
279 * @param {!Array<!CrOnc.NetworkStateProperties>} states The state properties
280 * for all visible networks.
283 getNetworksCallback_: function(states
) {
284 // Clear any current networks.
285 this.networkIds_
= {};
287 // Track the first (active) state for each type.
290 // Complete list of states by type.
291 /** @type {!NetworkStateListObject} */ var networkStateLists
= {
299 states
.forEach(function(state
) {
300 var type
= state
.Type
;
301 if (!foundTypes
[type
]) {
302 foundTypes
[type
] = true;
303 this.updateNetworkState_(type
, state
);
305 networkStateLists
[type
].push(state
);
308 // Set any types with a deviceState and no network to a default state,
309 // and any types not found to null.
310 NETWORK_TYPES
.forEach(function(type
) {
311 if (!foundTypes
[type
]) {
312 /** @type {CrOnc.NetworkStateProperties} */ var defaultState
= null;
313 if (this.deviceStates
[type
])
314 defaultState
= { GUID
: '', Type
: type
};
315 this.updateNetworkState_(type
, defaultState
);
319 this.networkStateLists
= networkStateLists
;
321 // Create a VPN entry in deviceStates if there are any VPN networks.
322 if (networkStateLists
.VPN
&& networkStateLists
.VPN
.length
> 0) {
323 var vpn
= { Type
: CrOnc
.Type
.VPN
, State
: 'Enabled' };
324 this.set('deviceStates.VPN', vpn
);
329 * Sets 'networkStates[type]' which will update the network-list-item
330 * associated with 'type'.
331 * @param {string} type The network type.
332 * @param {?CrOnc.NetworkStateProperties} state The state properties for the
333 * network to associate with |type|. May be null if there are no networks
337 updateNetworkState_: function(type
, state
) {
338 this.set('networkStates.' + type
, state
);
340 this.networkIds_
[state
.GUID
] = true;