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 NETWORK_TYPES = | |
Jeremy Klein
2015/04/23 18:21:15
nit: I think this would read cleaner like:
/** @c
stevenjb
2015/04/24 01:25:25
Done.
| |
51 ['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_: {}, |
47 | 111 |
48 /** @override */ | 112 /** @override */ |
49 created: function() { | 113 created: function() { |
114 this.deviceStates = {}; | |
50 this.networkStates = {}; | 115 this.networkStates = {}; |
116 this.networkStateLists = {}; | |
51 }, | 117 }, |
52 | 118 |
53 /** @override */ | 119 /** @override */ |
54 attached: function() { | 120 attached: function() { |
55 this.getNetworks_(); | 121 this.getNetworks_(); |
56 | 122 |
57 this.listChangedListener_ = this.onNetworkListChangedEvent_.bind(this); | 123 this.networkListChangedListener_ = |
124 this.onNetworkListChangedEvent_.bind(this); | |
58 chrome.networkingPrivate.onNetworkListChanged.addListener( | 125 chrome.networkingPrivate.onNetworkListChanged.addListener( |
59 this.listChangedListener_); | 126 this.networkListChangedListener_); |
127 | |
128 this.deviceStateListChangedListener_ = | |
129 this.onDeviceStateListChangedEvent_.bind(this); | |
130 chrome.networkingPrivate.onDeviceStateListChanged.addListener( | |
131 this.deviceStateListChangedListener_); | |
60 | 132 |
61 this.networksChangedListener_ = this.onNetworksChangedEvent_.bind(this); | 133 this.networksChangedListener_ = this.onNetworksChangedEvent_.bind(this); |
62 chrome.networkingPrivate.onNetworksChanged.addListener( | 134 chrome.networkingPrivate.onNetworksChanged.addListener( |
63 this.networksChangedListener_); | 135 this.networksChangedListener_); |
64 }, | 136 }, |
65 | 137 |
66 /** @override */ | 138 /** @override */ |
67 detached: function() { | 139 detached: function() { |
68 chrome.networkingPrivate.onNetworkListChanged.removeListener( | 140 chrome.networkingPrivate.onNetworkListChanged.removeListener( |
69 this.listChangedListener_); | 141 this.networkListChangedListener_); |
142 | |
143 chrome.networkingPrivate.onDeviceStateListChanged.removeListener( | |
144 this.deviceStateListChangedListener_); | |
70 | 145 |
71 chrome.networkingPrivate.onNetworksChanged.removeListener( | 146 chrome.networkingPrivate.onNetworksChanged.removeListener( |
72 this.networksChangedListener_); | 147 this.networksChangedListener_); |
73 }, | 148 }, |
74 | 149 |
75 /** | 150 /** |
151 * @param {string} deviceState The state of the device. | |
152 * @return {boolean} Whether or not an item for the device type is visible. | |
76 * @private | 153 * @private |
77 */ | 154 */ |
78 onNetworkListChangedEvent_: function() { | 155 itemIsVisible: function(deviceState) { return !!deviceState; }, |
Jeremy Klein
2015/04/23 18:21:14
Trailing _.
stevenjb
2015/04/24 01:25:25
I haven't use a trailing _ in methods that are use
Jeremy Klein
2015/04/24 02:55:28
This is part of the google style guide and I think
stevenjb
2015/04/24 19:03:18
Done.
| |
79 this.getNetworks_(); | 156 |
157 /** | |
158 * Event triggered when a cr-network-summary-item is expanded. | |
159 * @param {{detail: {expanded: boolean, type: string}}} event | |
160 * @private | |
161 */ | |
162 onExpanded: function(event) { | |
Jeremy Klein
2015/04/23 18:21:15
Trailing _.
stevenjb
2015/04/24 01:25:25
Acknowledged.
| |
163 if (event.detail.type == 'WiFi') | |
Jeremy Klein
2015/04/23 18:21:14
Why is this needed if this function is only ever c
stevenjb
2015/04/24 01:25:25
In the future we may do things for other types, bu
| |
164 chrome.networkingPrivate.requestNetworkScan(); | |
80 }, | 165 }, |
81 | 166 |
82 /** | 167 /** |
168 * Event triggered when a cr-network-summary-item is selected. | |
169 * @param {{detail: CrOncDataElement}} event | |
michaelpg
2015/04/23 02:16:29
!CrOncDataElement
stevenjb
2015/04/24 01:25:25
Done.
| |
170 * @private | |
171 */ | |
172 onSelected: function(event) { | |
Jeremy Klein
2015/04/23 18:21:14
Trailing _.
stevenjb
2015/04/24 01:25:25
Acknowledged.
| |
173 var onc = event.detail; | |
174 if (onc.disconnected()) { | |
175 this.connectToNetwork_(onc); | |
176 return; | |
177 } | |
178 // TODO(stevenjb): Show details for connected or unconfigured networks. | |
179 }, | |
180 | |
181 /** | |
182 * Event triggered when the enabled state of a cr-network-summary-item is | |
183 * toggled. | |
184 * @param {{detail: {enabled: boolean, type: string}}} event | |
185 */ | |
186 onToggleEnabled: function(event) { | |
Jeremy Klein
2015/04/23 18:21:14
@private?
Also nit: maybe just onToggled since th
stevenjb
2015/04/24 01:25:25
Done.
Well, it's toggling the "enabled" state (vs
michaelpg
2015/04/24 19:35:37
I think it's confusing too -- it implies the event
stevenjb
2015/04/24 21:18:07
OK, I made this as explicit as I felt I reasonably
| |
187 if (event.detail.enabled) | |
188 chrome.networkingPrivate.enableNetworkType(event.detail.type); | |
189 else | |
190 chrome.networkingPrivate.disableNetworkType(event.detail.type); | |
191 }, | |
192 | |
193 /** | |
194 * networkingPrivate.onNetworkListChanged event callback. | |
195 * @private | |
196 */ | |
197 onNetworkListChangedEvent_: function() { this.getNetworks_(); }, | |
Jeremy Klein
2015/04/23 18:21:15
It doesn't seem like this function and onDeviceSta
stevenjb
2015/04/24 01:25:25
While currently true, I kind of prefer to keep the
Jeremy Klein
2015/04/24 02:55:28
I still don't agree with this, but won't block on
stevenjb
2015/04/24 19:03:18
Acknowledged.
| |
198 | |
199 /** | |
200 * networkingPrivate.onDeviceStateListChanged event callback. | |
201 * @private | |
202 */ | |
203 onDeviceStateListChangedEvent_: function() { this.getNetworks_(); }, | |
204 | |
205 /** | |
206 * networkingPrivate.onNetworksChanged event callback. | |
83 * @param {!Array<string>} networkIds The list of changed network GUIDs. | 207 * @param {!Array<string>} networkIds The list of changed network GUIDs. |
84 * @private | 208 * @private |
85 */ | 209 */ |
86 onNetworksChangedEvent_: function(networkIds) { | 210 onNetworksChangedEvent_: function(networkIds) { |
87 networkIds.forEach(function(id) { | 211 networkIds.forEach(function(id) { |
88 if (id in this.networkIds_) { | 212 if (id in this.networkIds_) { |
89 chrome.networkingPrivate.getState(id, | 213 chrome.networkingPrivate.getState(id, |
90 this.getStateCallback_.bind(this)); | 214 this.getStateCallback_.bind(this)); |
91 } | 215 } |
92 }, this); | 216 }, this); |
93 }, | 217 }, |
94 | 218 |
95 /** @private */ | 219 /** |
96 getNetworks_: function() { | 220 * Handles UI requests to connect to a network. |
97 var filter = { | 221 * TODO(stevenjb): Handle Cellular activation, etc. |
98 networkType: 'All', | 222 * @param {CrOncDataElement} state The network state. |
Jeremy Klein
2015/04/23 18:21:14
!CrOncDataElement
stevenjb
2015/04/24 01:25:25
Done.
| |
99 visible: true, | 223 * @private |
100 configured: false | 224 */ |
101 }; | 225 connectToNetwork_: function(state) { |
102 chrome.networkingPrivate.getNetworks(filter, | 226 chrome.networkingPrivate.startConnect(state.data.GUID); |
103 this.getNetworksCallback_.bind(this)); | |
104 }, | 227 }, |
105 | 228 |
106 /** | 229 /** |
107 * @param {!Array<!chrome.networkingPrivate.NetworkStateProperties>} states | 230 * Requests the list of device states and network states from Chrome. |
108 * The state properties for all networks. | 231 * Updates deviceStates, networkStates, and networkStateLists once the |
232 * results are returned from Chrome. | |
233 * @private | |
234 */ | |
235 getNetworks_: function() { | |
236 // First get the device states. | |
237 chrome.networkingPrivate.getDeviceStates( | |
238 function(states) { | |
239 this.getDeviceStatesCallback_(states); | |
240 var filter = { | |
241 networkType: 'All', | |
242 visible: true, | |
243 configured: false | |
244 }; | |
245 // Second get the network states. | |
246 chrome.networkingPrivate.getNetworks( | |
247 filter, this.getNetworksCallback_.bind(this)); | |
248 }.bind(this)); | |
249 }, | |
250 | |
251 /** | |
252 * networkingPrivate.getDeviceStates callback. | |
253 * @param {!Array<!DeviceStateProperties>} states The state properties for all | |
254 * available devices. | |
255 * @private | |
256 */ | |
257 getDeviceStatesCallback_: function(states) { | |
258 states.forEach(function(state) { | |
259 this.deviceStates[state.Type] = state; | |
260 }, this); | |
261 }, | |
262 | |
263 /** | |
264 * networkingPrivate.getNetworksState callback. | |
265 * @param {!Array<!NetworkStateProperties>} states The state properties for | |
266 * all visible networks. | |
109 * @private | 267 * @private |
110 */ | 268 */ |
111 getNetworksCallback_: function(states) { | 269 getNetworksCallback_: function(states) { |
112 // Clear all active networks. | 270 // Clear any current networks. |
113 this.networkIds_ = {}; | 271 this.networkIds_ = {}; |
114 | 272 |
115 // Get the first (active) state for each type. | 273 // Get the first (active) state for each type. |
116 var foundTypes = {}; | 274 var foundTypes = {}; |
275 /** @type {NetworkStateListObject} */ var oncNetworks = { | |
276 Ethernet: [], | |
277 WiFi: [], | |
278 Cellular: [], | |
279 WiMAX: [], | |
280 VPN: [] | |
281 }; | |
117 states.forEach(function(state) { | 282 states.forEach(function(state) { |
118 var type = state.Type; | 283 var type = state.Type; |
119 if (!foundTypes[type]) { | 284 if (!foundTypes[type]) { |
120 foundTypes[type] = true; | 285 foundTypes[type] = true; |
121 this.updateNetworkState_(type, state); | 286 this.updateNetworkState_(type, state); |
122 } | 287 } |
288 oncNetworks[type].push(CrOncDataElement.create(state)); | |
123 }, this); | 289 }, this); |
124 | 290 |
125 // Set any types not found to null. TODO(stevenjb): Support types that are | 291 // Set any types not found to null. |
126 // disabled but available with no active network. | 292 NETWORK_TYPES.forEach(function(type) { |
127 var types = ['Ethernet', 'WiFi', 'Cellular', 'WiMAX', 'VPN']; | |
128 types.forEach(function(type) { | |
129 if (!foundTypes[type]) | 293 if (!foundTypes[type]) |
130 this.updateNetworkState_(type, null); | 294 this.updateNetworkState_(type, null); |
131 }, this); | 295 }, this); |
296 | |
297 // Set the network list for each type. | |
298 NETWORK_TYPES.forEach(function(type) { | |
299 this.networkStateLists[type] = oncNetworks[type]; | |
300 }, this); | |
301 | |
302 // Create a VPN entry in deviceStates if there are any VPN networks. | |
303 if (this.networkStateLists.VPN && this.networkStateLists.VPN.length > 0) | |
304 this.deviceStates.VPN = { Type: 'VPN', State: 'Enabled' }; | |
132 }, | 305 }, |
133 | 306 |
134 /** | 307 /** |
135 * @param {!chrome.networkingPrivate.NetworkStateProperties} state The state | 308 * networkingPrivate.getState callback. |
136 * properties for the network. | 309 * @param {!NetworkStateProperties} state The network state properties. |
137 * @private | 310 * @private |
138 */ | 311 */ |
139 getStateCallback_: function(state) { | 312 getStateCallback_: function(state) { |
140 var id = state.GUID; | 313 var id = state.GUID; |
141 if (!this.networkIds_[id]) | 314 if (!this.networkIds_[id]) |
142 return; | 315 return; |
143 this.updateNetworkState_(state.Type, state); | 316 this.updateNetworkState_(state.Type, state); |
144 }, | 317 }, |
145 | 318 |
146 /** | 319 /** |
147 * Creates a CrOncDataElement from the network state (if not null) for 'type'. | 320 * Creates a CrOncDataElement from the network state (if not null) for 'type'. |
148 * Sets 'networkStates[type]' which will update the cr-network-list-item | 321 * Sets 'networkStates[type]' which will update the cr-network-list-item |
149 * associated with 'type'. | 322 * associated with 'type'. |
150 * @param {string} type The network type. | 323 * @param {string} type The network type. |
151 * @param {chrome.networkingPrivate.NetworkStateProperties} state The state | 324 * @param {NetworkStateProperties} state The state properties for the network |
152 * properties for the network to associate with |type|. May be null if | 325 * to associate with |type|. May be null if there are no networks matching |
153 * there are no networks matching |type|. | 326 * |type|. |
154 * @private | 327 * @private |
155 */ | 328 */ |
156 updateNetworkState_: function(type, state) { | 329 updateNetworkState_: function(type, state) { |
157 this.networkStates[type] = state ? CrOncDataElement.create(state) : null; | 330 this.networkStates[type] = state ? CrOncDataElement.create(state) : null; |
158 if (state) | 331 if (state) |
159 this.networkIds_[state.GUID] = true; | 332 this.networkIds_[state.GUID] = true; |
160 }, | 333 }, |
161 }); | 334 }); |
335 })(); | |
OLD | NEW |