OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 a summary of network states | 6 * @fileoverview Polymer element for displaying a summary of network states |
7 * by type: Ethernet, WiFi, Cellular, WiMAX, and VPN. | 7 * by type: Ethernet, WiFi, Cellular, WiMAX, and VPN. |
8 */ | 8 */ |
9 (function() { | |
10 | |
11 /** @typedef {chrome.networkingPrivate.DeviceStateProperties} */ | |
12 var DeviceStateProperties; | |
13 | |
14 /** @typedef {chrome.networkingPrivate.NetworkStateProperties} */ | |
15 var NetworkStateProperties; | |
16 | |
17 /** | |
18 * @typedef {{ | |
19 * Ethernet: (DeviceStateProperties|undefined), | |
20 * WiFi: (DeviceStateProperties|undefined), | |
21 * Cellular: (DeviceStateProperties|undefined), | |
22 * WiMAX: (DeviceStateProperties|undefined), | |
23 * VPN: (DeviceStateProperties|undefined) | |
24 * }} | |
25 */ | |
26 var DeviceStateObject; | |
27 | |
28 /** | |
29 * @typedef {{ | |
30 * Ethernet: (CrOncDataElement|undefined), | |
31 * WiFi: (CrOncDataElement|undefined), | |
32 * Cellular: (CrOncDataElement|undefined), | |
33 * WiMAX: (CrOncDataElement|undefined), | |
34 * VPN: (CrOncDataElement|undefined) | |
35 * }} | |
36 */ | |
37 var NetworkStateObject; | |
38 | |
39 /** | |
40 * @typedef {{ | |
41 * Ethernet: (Array<CrOncDataElement>|undefined), | |
42 * WiFi: (Array<CrOncDataElement>|undefined), | |
43 * Cellular: (Array<CrOncDataElement>|undefined), | |
44 * WiMAX: (Array<CrOncDataElement>|undefined), | |
45 * VPN: (Array<CrOncDataElement>|undefined) | |
46 * }} | |
47 */ | |
48 var NetworkStateListObject; | |
49 | |
50 /** @const {!Array<string>} */ var | |
51 NETWORK_TYPES = ['Ethernet', 'WiFi', 'Cellular', 'WiMAX', 'VPN']; | |
52 | |
9 Polymer('cr-network-summary', { | 53 Polymer('cr-network-summary', { |
10 publish: { | 54 publish: { |
11 /** | 55 /** |
56 * The device state for each network device type. | |
57 * | |
58 * @attribute deviceStates | |
59 * @type {?DeviceStateObject} | |
60 * @default null | |
61 */ | |
62 deviceStates: null, | |
63 | |
64 /** | |
12 * Network state data for each network type. | 65 * Network state data for each network type. |
13 * | 66 * |
14 * @attribute networkStates | 67 * @attribute networkStates |
15 * @type {{ | 68 * @type {?NetworkStateObject} |
16 * Ethernet: (CrOncDataElement|undefined), | 69 * @default null |
17 * WiFi: (CrOncDataElement|undefined), | |
18 * Cellular: (CrOncDataElement|undefined), | |
19 * WiMAX: (CrOncDataElement|undefined), | |
20 * VPN: (CrOncDataElement|undefined) | |
21 * }} | |
22 * @default {} | |
23 */ | 70 */ |
24 networkStates: null, | 71 networkStates: null, |
72 | |
73 /** | |
74 * List of network state data for each network type. | |
75 * | |
76 * @attribute networkStateLists | |
77 * @type {?NetworkStateListObject} | |
78 * @default null | |
79 */ | |
80 networkStateLists: null, | |
25 }, | 81 }, |
26 | 82 |
27 /** | 83 /** |
28 * Listener function for chrome.networkingPrivate.onNetworkListChanged event. | 84 * Listener function for chrome.networkingPrivate.onNetworkListChanged event. |
29 * @type {function(!Array<string>)} | 85 * @type {?function(!Array<string>)} |
30 * @private | 86 * @private |
31 */ | 87 */ |
32 listChangedListener_: null, | 88 networkListChangedListener_: null, |
89 | |
90 /** | |
91 * Listener function for chrome.networkingPrivate.onDeviceStateListChanged | |
92 * event. | |
93 * @type {?function(!Array<string>)} | |
94 * @private | |
95 */ | |
96 deviceStateListChangedListener_: null, | |
33 | 97 |
34 /** | 98 /** |
35 * Listener function for chrome.networkingPrivate.onNetworksChanged event. | 99 * Listener function for chrome.networkingPrivate.onNetworksChanged event. |
36 * @type {function(!Array<string>)} | 100 * @type {?function(!Array<string>)} |
37 * @private | 101 * @private |
38 */ | 102 */ |
39 networksChangedListener_: null, | 103 networksChangedListener_: null, |
40 | 104 |
41 /** | 105 /** |
42 * Dictionary of GUIDs identifying primary (active) networks for each type. | 106 * Dictionary of GUIDs identifying primary (active) networks for each type. |
43 * @type {Object} | 107 * @type {?Object} |
44 * @private | 108 * @private |
45 */ | 109 */ |
46 networkIds_: {}, | 110 networkIds_: null, |
47 | 111 |
48 /** @override */ | 112 /** @override */ |
49 created: function() { | 113 created: function() { |
114 this.deviceStates = {}; | |
50 this.networkStates = {}; | 115 this.networkStates = {}; |
116 this.networkStateLists = {}; | |
117 this.networkIds_ = {}; | |
51 }, | 118 }, |
52 | 119 |
53 /** @override */ | 120 /** @override */ |
54 attached: function() { | 121 attached: function() { |
55 this.getNetworks_(); | 122 this.getNetworkLists_(); |
56 | 123 |
57 this.listChangedListener_ = this.onNetworkListChangedEvent_.bind(this); | 124 this.networkListChangedListener_ = |
125 this.onNetworkListChangedEvent_.bind(this); | |
58 chrome.networkingPrivate.onNetworkListChanged.addListener( | 126 chrome.networkingPrivate.onNetworkListChanged.addListener( |
59 this.listChangedListener_); | 127 this.networkListChangedListener_); |
128 | |
129 this.deviceStateListChangedListener_ = | |
130 this.onDeviceStateListChangedEvent_.bind(this); | |
131 chrome.networkingPrivate.onDeviceStateListChanged.addListener( | |
132 this.deviceStateListChangedListener_); | |
60 | 133 |
61 this.networksChangedListener_ = this.onNetworksChangedEvent_.bind(this); | 134 this.networksChangedListener_ = this.onNetworksChangedEvent_.bind(this); |
62 chrome.networkingPrivate.onNetworksChanged.addListener( | 135 chrome.networkingPrivate.onNetworksChanged.addListener( |
63 this.networksChangedListener_); | 136 this.networksChangedListener_); |
64 }, | 137 }, |
65 | 138 |
66 /** @override */ | 139 /** @override */ |
67 detached: function() { | 140 detached: function() { |
68 chrome.networkingPrivate.onNetworkListChanged.removeListener( | 141 chrome.networkingPrivate.onNetworkListChanged.removeListener( |
69 this.listChangedListener_); | 142 this.networkListChangedListener_); |
143 | |
144 chrome.networkingPrivate.onDeviceStateListChanged.removeListener( | |
145 this.deviceStateListChangedListener_); | |
70 | 146 |
71 chrome.networkingPrivate.onNetworksChanged.removeListener( | 147 chrome.networkingPrivate.onNetworksChanged.removeListener( |
72 this.networksChangedListener_); | 148 this.networksChangedListener_); |
73 }, | 149 }, |
74 | 150 |
75 /** | 151 /** |
152 * @param {string} deviceState The state of the device. | |
153 * @return {boolean} Whether or not an item for the device type is visible. | |
76 * @private | 154 * @private |
77 */ | 155 */ |
78 onNetworkListChangedEvent_: function() { | 156 itemIsVisible_: function(deviceState) { return !!deviceState; }, |
79 this.getNetworks_(); | 157 |
158 /** | |
159 * Event triggered when the WiFi cr-network-summary-item is expanded. | |
160 * @param {!{detail: {expanded: boolean, type: string}}} event | |
161 * @private | |
162 */ | |
163 onWiFiExpanded_: function(event) { | |
164 this.getNetworkStates_(); // Get the latest network states (only). | |
165 chrome.networkingPrivate.requestNetworkScan(); | |
80 }, | 166 }, |
81 | 167 |
82 /** | 168 /** |
83 * @param {!Array<string>} networkIds The list of changed network GUIDs. | 169 * Event triggered when a cr-network-summary-item is selected. |
170 * @param {!{detail: !CrOncDataElement}} event | |
171 * @private | |
172 */ | |
173 onSelected_: function(event) { | |
174 var onc = event.detail; | |
175 if (onc.disconnected()) { | |
176 this.connectToNetwork_(onc); | |
177 return; | |
178 } | |
179 // TODO(stevenjb): Show details for connected or unconfigured networks. | |
180 }, | |
181 | |
182 /** | |
183 * Event triggered when the enabled state of a cr-network-summary-item is | |
184 * toggled. | |
185 * @param {!{detail: {enabled: boolean, type: string}}} event | |
186 * @private | |
187 */ | |
188 onToggleEnabled_: function(event) { | |
189 if (event.detail.enabled) | |
190 chrome.networkingPrivate.enableNetworkType(event.detail.type); | |
191 else | |
192 chrome.networkingPrivate.disableNetworkType(event.detail.type); | |
193 }, | |
194 | |
195 /** | |
196 * networkingPrivate.onNetworkListChanged event callback. | |
197 * @private | |
198 */ | |
199 onNetworkListChangedEvent_: function() { this.getNetworkLists_(); }, | |
200 | |
201 /** | |
202 * networkingPrivate.onDeviceStateListChanged event callback. | |
203 * @private | |
204 */ | |
205 onDeviceStateListChangedEvent_: function() { this.getNetworkLists_(); }, | |
206 | |
207 /** | |
208 * networkingPrivate.onNetworksChanged event callback. | |
209 * @param {Array<string>} networkIds The list of changed network GUIDs. | |
84 * @private | 210 * @private |
85 */ | 211 */ |
86 onNetworksChangedEvent_: function(networkIds) { | 212 onNetworksChangedEvent_: function(networkIds) { |
87 networkIds.forEach(function(id) { | 213 networkIds.forEach(function(id) { |
88 if (id in this.networkIds_) { | 214 if (id in this.networkIds_) { |
89 chrome.networkingPrivate.getState(id, | 215 chrome.networkingPrivate.getState(id, |
90 this.getStateCallback_.bind(this)); | 216 this.getStateCallback_.bind(this)); |
91 } | 217 } |
92 }, this); | 218 }, this); |
93 }, | 219 }, |
94 | 220 |
95 /** @private */ | 221 /** |
96 getNetworks_: function() { | 222 * Handles UI requests to connect to a network. |
223 * TODO(stevenjb): Handle Cellular activation, etc. | |
224 * @param {!CrOncDataElement} state The network state. | |
225 * @private | |
226 */ | |
227 connectToNetwork_: function(state) { | |
228 chrome.networkingPrivate.startConnect(state.data.GUID); | |
229 }, | |
230 | |
231 /** | |
232 * Requests the list of device states and network states from Chrome. | |
233 * Updates deviceStates, networkStates, and networkStateLists once the | |
234 * results are returned from Chrome. | |
235 * @private | |
236 */ | |
237 getNetworkLists_: function() { | |
238 // First get the device states. | |
239 chrome.networkingPrivate.getDeviceStates( | |
240 function(states) { | |
241 this.getDeviceStatesCallback_(states); | |
242 // Second get the network states. | |
243 this.getNetworkStates_(); | |
244 }.bind(this)); | |
245 }, | |
246 | |
247 /** | |
248 * Requests the list of network states from Chrome. Updates networkStates, and | |
michaelpg
2015/04/24 19:35:37
Remove the comma, so it's clear the callback does
stevenjb
2015/04/24 21:18:09
Done.
| |
249 * networkStateLists once the results are returned from Chrome. | |
250 * @private | |
251 */ | |
252 getNetworkStates_: function() { | |
97 var filter = { | 253 var filter = { |
98 networkType: 'All', | 254 networkType: 'All', |
99 visible: true, | 255 visible: true, |
100 configured: false | 256 configured: false |
101 }; | 257 }; |
102 chrome.networkingPrivate.getNetworks(filter, | 258 chrome.networkingPrivate.getNetworks( |
103 this.getNetworksCallback_.bind(this)); | 259 filter, this.getNetworksCallback_.bind(this)); |
michaelpg
2015/04/24 19:35:37
nit 4 spaces
stevenjb
2015/04/24 21:18:09
Done.
| |
104 }, | 260 }, |
105 | 261 |
106 /** | 262 /** |
107 * @param {!Array<!chrome.networkingPrivate.NetworkStateProperties>} states | 263 * networkingPrivate.getDeviceStates callback. |
108 * The state properties for all networks. | 264 * @param {!Array<!DeviceStateProperties>} states The state properties for all |
265 * available devices. | |
266 * @private | |
267 */ | |
268 getDeviceStatesCallback_: function(states) { | |
269 states.forEach(function(state) { | |
270 this.deviceStates[state.Type] = state; | |
271 }, this); | |
272 }, | |
273 | |
274 /** | |
275 * networkingPrivate.getNetworksState callback. | |
276 * @param {!Array<!NetworkStateProperties>} states The state properties for | |
277 * all visible networks. | |
109 * @private | 278 * @private |
110 */ | 279 */ |
111 getNetworksCallback_: function(states) { | 280 getNetworksCallback_: function(states) { |
112 // Clear all active networks. | 281 // Clear any current networks. |
113 this.networkIds_ = {}; | 282 this.networkIds_ = {}; |
114 | 283 |
115 // Get the first (active) state for each type. | 284 // Get the first (active) state for each type. |
116 var foundTypes = {}; | 285 var foundTypes = {}; |
286 /** @type {!NetworkStateListObject} */ var oncNetworks = { | |
287 Ethernet: [], | |
288 WiFi: [], | |
289 Cellular: [], | |
290 WiMAX: [], | |
291 VPN: [] | |
292 }; | |
117 states.forEach(function(state) { | 293 states.forEach(function(state) { |
118 var type = state.Type; | 294 var type = state.Type; |
119 if (!foundTypes[type]) { | 295 if (!foundTypes[type]) { |
120 foundTypes[type] = true; | 296 foundTypes[type] = true; |
121 this.updateNetworkState_(type, state); | 297 this.updateNetworkState_(type, state); |
122 } | 298 } |
299 oncNetworks[type].push(CrOncDataElement.create(state)); | |
123 }, this); | 300 }, this); |
124 | 301 |
125 // Set any types not found to null. TODO(stevenjb): Support types that are | 302 // Set any types not found to null. |
126 // disabled but available with no active network. | 303 NETWORK_TYPES.forEach(function(type) { |
127 var types = ['Ethernet', 'WiFi', 'Cellular', 'WiMAX', 'VPN']; | |
128 types.forEach(function(type) { | |
129 if (!foundTypes[type]) | 304 if (!foundTypes[type]) |
130 this.updateNetworkState_(type, null); | 305 this.updateNetworkState_(type, null); |
131 }, this); | 306 }, this); |
307 | |
308 // Set the network list for each type. | |
309 NETWORK_TYPES.forEach(function(type) { | |
310 this.networkStateLists[type] = oncNetworks[type]; | |
311 }, this); | |
312 | |
313 // Create a VPN entry in deviceStates if there are any VPN networks. | |
314 if (this.networkStateLists.VPN && this.networkStateLists.VPN.length > 0) | |
315 this.deviceStates.VPN = { Type: 'VPN', State: 'Enabled' }; | |
132 }, | 316 }, |
133 | 317 |
134 /** | 318 /** |
135 * @param {!chrome.networkingPrivate.NetworkStateProperties} state The state | 319 * networkingPrivate.getState callback. |
136 * properties for the network. | 320 * @param {!NetworkStateProperties} state The network state properties. |
137 * @private | 321 * @private |
138 */ | 322 */ |
139 getStateCallback_: function(state) { | 323 getStateCallback_: function(state) { |
140 var id = state.GUID; | 324 var id = state.GUID; |
141 if (!this.networkIds_[id]) | 325 if (!this.networkIds_[id]) |
142 return; | 326 return; |
143 this.updateNetworkState_(state.Type, state); | 327 this.updateNetworkState_(state.Type, state); |
144 }, | 328 }, |
145 | 329 |
146 /** | 330 /** |
147 * Creates a CrOncDataElement from the network state (if not null) for 'type'. | 331 * Creates a CrOncDataElement from the network state (if not null) for 'type'. |
148 * Sets 'networkStates[type]' which will update the cr-network-list-item | 332 * Sets 'networkStates[type]' which will update the cr-network-list-item |
149 * associated with 'type'. | 333 * associated with 'type'. |
150 * @param {string} type The network type. | 334 * @param {string} type The network type. |
151 * @param {chrome.networkingPrivate.NetworkStateProperties} state The state | 335 * @param {?NetworkStateProperties} state The state properties for the network |
152 * properties for the network to associate with |type|. May be null if | 336 * to associate with |type|. May be null if there are no networks matching |
153 * there are no networks matching |type|. | 337 * |type|. |
154 * @private | 338 * @private |
155 */ | 339 */ |
156 updateNetworkState_: function(type, state) { | 340 updateNetworkState_: function(type, state) { |
157 this.networkStates[type] = state ? CrOncDataElement.create(state) : null; | 341 this.networkStates[type] = state ? CrOncDataElement.create(state) : null; |
158 if (state) | 342 if (state) |
159 this.networkIds_[state.GUID] = true; | 343 this.networkIds_[state.GUID] = true; |
160 }, | 344 }, |
161 }); | 345 }); |
346 })(); | |
OLD | NEW |