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 rendering network icons based on ONC | 6 * @fileoverview Polymer element for rendering network icons based on ONC |
7 * state properties. | 7 * state properties. |
8 */ | 8 */ |
9 | 9 |
10 /** | 10 /** |
11 * @typedef {{ | 11 * @typedef {{ |
12 * showBadges: boolean, | 12 * showBadges: boolean, |
13 * showDisconnected: boolean, | 13 * showDisconnected: boolean, |
14 * strength: number | 14 * strength: number |
15 * }} | 15 * }} |
16 */ | 16 */ |
17 var NetworkIconParamType; | 17 var NetworkIconParamType; |
18 | 18 |
19 (function() { | 19 (function() { |
20 /** @const {string} */ var RESOURCE_IMAGE_BASE = | 20 /** @const {string} */ var RESOURCE_IMAGE_BASE = |
21 'chrome://resources/cr_elements/network/'; | 21 'chrome://resources/cr_elements/network/'; |
22 | 22 |
23 /** @const {string} */ var RESOURCE_IMAGE_EXT = '.png'; | 23 /** @const {string} */ var RESOURCE_IMAGE_EXT = '.png'; |
24 | 24 |
25 /** | 25 /** |
26 * Gets the icon type from the network type. This allows multiple types | 26 * Gets the icon type from the network type. This allows multiple types |
27 * (i.e. Cellular, WiMAX) to map to the same icon type (i.e. mobile). | 27 * (i.e. Cellular, WiMAX) to map to the same icon type (i.e. mobile). |
28 * @param {chrome.networkingPrivate.NetworkType} networkType | 28 * @param {chrome.networkingPrivate.NetworkType} networkType |
29 * @return {string} The icon type: ethernet, wifi, mobile, or vpn. | 29 * @return {string} The icon type: ethernet, wifi, mobile, or vpn. |
30 */ | 30 */ |
31 function getIconTypeFromNetworkType(networkType) { | 31 function getIconTypeFromNetworkType(networkType) { |
32 if (!networkType || networkType == CrOnc.Type.ETHERNET) | 32 if (!networkType || networkType == CrOnc.Type.ETHERNET) |
| 33 return 'ethernet'; |
| 34 else if (networkType == CrOnc.Type.WI_FI) |
| 35 return 'wifi'; |
| 36 else if (networkType == CrOnc.Type.CELLULAR) |
| 37 return 'mobile'; |
| 38 else if (networkType == CrOnc.Type.WI_MAX) |
| 39 return 'mobile'; |
| 40 else if (networkType == CrOnc.Type.VPN) |
| 41 return 'vpn'; |
| 42 |
| 43 console.error('Unrecognized network type for icon: ' + networkType); |
33 return 'ethernet'; | 44 return 'ethernet'; |
34 else if (networkType == CrOnc.Type.WI_FI) | 45 } |
35 return 'wifi'; | |
36 else if (networkType == CrOnc.Type.CELLULAR) | |
37 return 'mobile'; | |
38 else if (networkType == CrOnc.Type.WI_MAX) | |
39 return 'mobile'; | |
40 else if (networkType == CrOnc.Type.VPN) | |
41 return 'vpn'; | |
42 | |
43 console.error('Unrecognized network type for icon: ' + networkType); | |
44 return 'ethernet'; | |
45 } | |
46 | |
47 /** | |
48 * Polymer class definition for 'cr-network-icon'. | |
49 */ | |
50 Polymer({ | |
51 is: 'cr-network-icon', | |
52 | |
53 properties: { | |
54 /** | |
55 * If set, the ONC properties will be used to display the icon. This may | |
56 * either be the complete set of NetworkProperties or the subset of | |
57 * NetworkStateProperties. | |
58 * @type {!CrOnc.NetworkProperties|!CrOnc.NetworkStateProperties|undefined} | |
59 */ | |
60 networkState: { | |
61 type: Object, | |
62 observer: 'networkStateChanged_', | |
63 }, | |
64 | |
65 /** | |
66 * If set, the ONC network type will be used to display the icon. | |
67 * @type {?chrome.networkingPrivate.NetworkType} | |
68 */ | |
69 networkType: { | |
70 type: String, | |
71 value: null, | |
72 observer: 'networkTypeChanged_', | |
73 }, | |
74 | |
75 /** | |
76 * If true, the icon is part of a list of networks and may be displayed | |
77 * differently, e.g. the disconnected image will never be shown for | |
78 * list items. | |
79 */ | |
80 isListItem: { | |
81 type: Boolean, | |
82 value: false, | |
83 observer: 'isListItemChanged_', | |
84 }, | |
85 | |
86 /** The icon type to use for the base image of the icon. */ | |
87 iconType_: { | |
88 type: String, | |
89 value: 'ethernet', | |
90 }, | |
91 | |
92 /** Set to true to show a badge for roaming networks. */ | |
93 roaming_: { | |
94 type: Boolean, | |
95 value: false, | |
96 }, | |
97 | |
98 /** Set to true to show a badge for secure networks. */ | |
99 secure_: { | |
100 type: Boolean, | |
101 value: false, | |
102 }, | |
103 | |
104 /** Set to the name of a technology to show show a badge. */ | |
105 technology_: { | |
106 type: String, | |
107 value: '', | |
108 }, | |
109 }, | |
110 | 46 |
111 /** | 47 /** |
112 * Polymer networkState changed method. Updates the icon based on the | 48 * Polymer class definition for 'cr-network-icon'. |
113 * network state. | |
114 * @private | |
115 */ | 49 */ |
116 networkStateChanged_: function() { | 50 Polymer({ |
117 if (!this.networkState) | 51 is: 'cr-network-icon', |
118 return; | 52 |
119 | 53 properties: { |
120 this.networkType = null; | 54 /** |
121 this.iconType_ = getIconTypeFromNetworkType(this.networkState.Type); | 55 * If set, the ONC properties will be used to display the icon. This may |
122 var strength = CrOnc.getSignalStrength(this.networkState); | 56 * either be the complete set of NetworkProperties or the subset of |
123 var params = /** @type {NetworkIconParamType} */ { | 57 * NetworkStateProperties. |
124 showBadges: true, | 58 * @type |
125 showDisconnected: !this.isListItem, | 59 * {!CrOnc.NetworkProperties|!CrOnc.NetworkStateProperties|undefined} |
126 strength: strength | 60 */ |
127 }; | 61 networkState: { |
128 this.setIcon_(params); | 62 type: Object, |
129 }, | 63 observer: 'networkStateChanged_', |
130 | 64 }, |
131 /** | 65 |
132 * Polymer networkType changed method. Updates the icon based on the type | 66 /** |
133 * of network, showing a disconnected icon where appropriate and no badges. | 67 * If set, the ONC network type will be used to display the icon. |
134 * @private | 68 * @type {?chrome.networkingPrivate.NetworkType} |
135 */ | 69 */ |
136 networkTypeChanged_: function() { | 70 networkType: { |
137 if (!this.networkType) | 71 type: String, |
138 return; | 72 value: null, |
139 | 73 observer: 'networkTypeChanged_', |
140 this.networkState = undefined; | 74 }, |
141 this.iconType_ = getIconTypeFromNetworkType(this.networkType); | 75 |
142 var params = /** @type {NetworkIconParamType} */ { | 76 /** |
143 showBadges: false, | 77 * If true, the icon is part of a list of networks and may be displayed |
144 showDisconnected: true, | 78 * differently, e.g. the disconnected image will never be shown for |
145 strength: 0, | 79 * list items. |
146 }; | 80 */ |
147 this.setIcon_(params); | 81 isListItem: { |
148 }, | 82 type: Boolean, |
149 | 83 value: false, |
150 /** | 84 observer: 'isListItemChanged_', |
151 * Polymer isListItem changed method. | 85 }, |
152 * @private | 86 |
153 */ | 87 /** The icon type to use for the base image of the icon. */ |
154 isListItemChanged_: function() { | 88 iconType_: { |
155 if (this.networkState) | 89 type: String, |
156 this.networkStateChanged_(); | 90 value: 'ethernet', |
157 else if (this.networkType) | 91 }, |
158 this.networkTypeChanged_(); | 92 |
159 }, | 93 /** Set to true to show a badge for roaming networks. */ |
160 | 94 roaming_: { |
161 /** | 95 type: Boolean, |
| 96 value: false, |
| 97 }, |
| 98 |
| 99 /** Set to true to show a badge for secure networks. */ |
| 100 secure_: { |
| 101 type: Boolean, |
| 102 value: false, |
| 103 }, |
| 104 |
| 105 /** Set to the name of a technology to show show a badge. */ |
| 106 technology_: { |
| 107 type: String, |
| 108 value: '', |
| 109 }, |
| 110 }, |
| 111 |
| 112 /** |
| 113 * Polymer networkState changed method. Updates the icon based on the |
| 114 * network state. |
| 115 * @private |
| 116 */ |
| 117 networkStateChanged_: function() { |
| 118 if (!this.networkState) |
| 119 return; |
| 120 |
| 121 this.networkType = null; |
| 122 this.iconType_ = getIconTypeFromNetworkType(this.networkState.Type); |
| 123 var strength = CrOnc.getSignalStrength(this.networkState); |
| 124 var params = /** @type {NetworkIconParamType} */ { |
| 125 showBadges: true, |
| 126 showDisconnected: !this.isListItem, |
| 127 strength: strength |
| 128 }; |
| 129 this.setIcon_(params); |
| 130 }, |
| 131 |
| 132 /** |
| 133 * Polymer networkType changed method. Updates the icon based on the type |
| 134 * of network, showing a disconnected icon where appropriate and no badges. |
| 135 * @private |
| 136 */ |
| 137 networkTypeChanged_: function() { |
| 138 if (!this.networkType) |
| 139 return; |
| 140 |
| 141 this.networkState = undefined; |
| 142 this.iconType_ = getIconTypeFromNetworkType(this.networkType); |
| 143 var params = /** @type {NetworkIconParamType} */ { |
| 144 showBadges: false, |
| 145 showDisconnected: true, |
| 146 strength: 0, |
| 147 }; |
| 148 this.setIcon_(params); |
| 149 }, |
| 150 |
| 151 /** |
| 152 * Polymer isListItem changed method. |
| 153 * @private |
| 154 */ |
| 155 isListItemChanged_: function() { |
| 156 if (this.networkState) |
| 157 this.networkStateChanged_(); |
| 158 else if (this.networkType) |
| 159 this.networkTypeChanged_(); |
| 160 }, |
| 161 |
| 162 /** |
162 * Returns the url for an image based on identifier |id|. | 163 * Returns the url for an image based on identifier |id|. |
163 * @param {string} id The identifier describing the image. | 164 * @param {string} id The identifier describing the image. |
164 * @return {string} The url to use for the image 'src' property. | 165 * @return {string} The url to use for the image 'src' property. |
165 * @private | 166 * @private |
166 */ | 167 */ |
167 toImageSrc_: function(id) { | 168 toImageSrc_: function(id) { |
168 return id ? RESOURCE_IMAGE_BASE + id + RESOURCE_IMAGE_EXT : ''; | 169 return id ? RESOURCE_IMAGE_BASE + id + RESOURCE_IMAGE_EXT : ''; |
169 }, | 170 }, |
170 | 171 |
171 /** | 172 /** |
172 * Returns the url for a badge image based on identifier |id|. | 173 * Returns the url for a badge image based on identifier |id|. |
173 * @param {string} id The identifier describing the badge. | 174 * @param {string} id The identifier describing the badge. |
174 * @return {string} The url to use for the badge image 'src' property. | 175 * @return {string} The url to use for the badge image 'src' property. |
175 * @private | 176 * @private |
176 */ | 177 */ |
177 toBadgeImageSrc_: function(id) { | 178 toBadgeImageSrc_: function(id) { |
178 return id ? this.toImageSrc_('badge_' + id) : ''; | 179 return id ? this.toImageSrc_('badge_' + id) : ''; |
179 }, | 180 }, |
180 | 181 |
181 /** | 182 /** |
182 * Sets the icon and badge based on the current state and |strength|. | 183 * Sets the icon and badge based on the current state and |strength|. |
183 * @param {!NetworkIconParamType} params Set of params describing the icon. | 184 * @param {!NetworkIconParamType} params Set of params describing the icon. |
184 * @private | 185 * @private |
185 */ | 186 */ |
186 setIcon_: function(params) { | 187 setIcon_: function(params) { |
187 var icon = this.$.icon; | 188 var icon = this.$.icon; |
188 | 189 |
189 var multiLevel = (this.iconType_ == 'wifi' || this.iconType_ == 'mobile'); | 190 var multiLevel = (this.iconType_ == 'wifi' || this.iconType_ == 'mobile'); |
190 | 191 |
191 if (this.networkState && multiLevel) { | 192 if (this.networkState && multiLevel) { |
192 this.setMultiLevelIcon_(params); | 193 this.setMultiLevelIcon_(params); |
193 } else { | 194 } else { |
194 icon.classList.toggle('multi-level', multiLevel); | 195 icon.classList.toggle('multi-level', multiLevel); |
195 icon.classList.toggle('level0', multiLevel); | 196 icon.classList.toggle('level0', multiLevel); |
196 icon.classList.toggle('level1', false); | 197 icon.classList.toggle('level1', false); |
197 icon.classList.toggle('level2', false); | 198 icon.classList.toggle('level2', false); |
198 icon.classList.toggle('level3', false); | 199 icon.classList.toggle('level3', false); |
199 icon.classList.toggle('level4', false); | 200 icon.classList.toggle('level4', false); |
200 } | |
201 | |
202 this.setIconBadges_(params); | |
203 }, | |
204 | |
205 /** | |
206 * Toggles icon classes based on strength and connecting properties. | |
207 * |this.networkState| is expected to be specified. | |
208 * @param {!NetworkIconParamType} params Set of params describing the icon. | |
209 * @private | |
210 */ | |
211 setMultiLevelIcon_: function(params) { | |
212 // Set the strength or connecting properties. | |
213 var networkState = this.networkState; | |
214 | |
215 var connectionState = networkState.ConnectionState; | |
216 var connecting = false; | |
217 var strength = -1; | |
218 if (connectionState == CrOnc.ConnectionState.CONNECTING) { | |
219 strength = 0; | |
220 connecting = true; | |
221 } else if ( | |
222 connectionState == CrOnc.ConnectionState.CONNECTED || | |
223 !params.showDisconnected) { | |
224 strength = params.strength || 0; | |
225 } | |
226 | |
227 var icon = this.$.icon; | |
228 icon.classList.toggle('multi-level', true); | |
229 icon.classList.toggle('connecting', connecting); | |
230 icon.classList.toggle('level0', strength < 0); | |
231 icon.classList.toggle('level1', strength >= 0 && strength <= 25); | |
232 icon.classList.toggle('level2', strength > 25 && strength <= 50); | |
233 icon.classList.toggle('level3', strength > 50 && strength <= 75); | |
234 icon.classList.toggle('level4', strength > 75); | |
235 }, | |
236 | |
237 /** | |
238 * Sets the icon badge visibility properties: roaming, secure, technology. | |
239 * @param {!NetworkIconParamType} params Set of params describing the icon. | |
240 * @private | |
241 */ | |
242 setIconBadges_: function(params) { | |
243 var networkState = this.networkState; | |
244 | |
245 var type = (params.showBadges && networkState) ? networkState.Type : ''; | |
246 if (type == CrOnc.Type.WI_FI) { | |
247 this.roaming_ = false; | |
248 var security = networkState.WiFi ? networkState.WiFi.Security : ''; | |
249 this.secure_ = !!security && security != 'None'; | |
250 this.technology_ = ''; | |
251 } else if (type == CrOnc.Type.WI_MAX) { | |
252 this.roaming_ = false; | |
253 this.secure_ = false; | |
254 this.technology_ = '4g'; | |
255 } else if (type == CrOnc.Type.CELLULAR && networkState.Cellular) { | |
256 this.roaming_ = | |
257 networkState.Cellular.RoamingState == CrOnc.RoamingState.ROAMING; | |
258 this.secure_ = false; | |
259 var oncTechnology = networkState.Cellular.NetworkTechnology; | |
260 switch (oncTechnology) { | |
261 case CrOnc.NetworkTechnology.CDMA1XRTT: | |
262 this.technology_ = '1x'; | |
263 break; | |
264 case CrOnc.NetworkTechnology.EDGE: | |
265 this.technology_ = 'edge'; | |
266 break; | |
267 case CrOnc.NetworkTechnology.EVDO: | |
268 this.technology_ = 'evdo'; | |
269 break; | |
270 case CrOnc.NetworkTechnology.GPRS: | |
271 case CrOnc.NetworkTechnology.GSM: | |
272 this.technology_ = 'gsm'; | |
273 break; | |
274 case CrOnc.NetworkTechnology.HSPA: | |
275 this.technology_ = 'hspa'; | |
276 break; | |
277 case CrOnc.NetworkTechnology.HSPA_PLUS: | |
278 this.technology_ = 'hspa_plus'; | |
279 break; | |
280 case CrOnc.NetworkTechnology.LTE: | |
281 this.technology_ = 'lte'; | |
282 break; | |
283 case CrOnc.NetworkTechnology.LTE_ADVANCED: | |
284 this.technology_ = 'lte_advanced'; | |
285 break; | |
286 case CrOnc.NetworkTechnology.UMTS: | |
287 this.technology_ = '3g'; | |
288 break; | |
289 } | 201 } |
290 } else { | 202 |
291 this.roaming_ = false; | 203 this.setIconBadges_(params); |
292 this.secure_ = false; | 204 }, |
293 this.technology_ = ''; | 205 |
294 } | 206 /** |
295 }, | 207 * Toggles icon classes based on strength and connecting properties. |
296 }); | 208 * |this.networkState| is expected to be specified. |
| 209 * @param {!NetworkIconParamType} params Set of params describing the icon. |
| 210 * @private |
| 211 */ |
| 212 setMultiLevelIcon_: function(params) { |
| 213 // Set the strength or connecting properties. |
| 214 var networkState = this.networkState; |
| 215 |
| 216 var connectionState = networkState.ConnectionState; |
| 217 var connecting = false; |
| 218 var strength = -1; |
| 219 if (connectionState == CrOnc.ConnectionState.CONNECTING) { |
| 220 strength = 0; |
| 221 connecting = true; |
| 222 } else if ( |
| 223 connectionState == CrOnc.ConnectionState.CONNECTED || |
| 224 !params.showDisconnected) { |
| 225 strength = params.strength || 0; |
| 226 } |
| 227 |
| 228 var icon = this.$.icon; |
| 229 icon.classList.toggle('multi-level', true); |
| 230 icon.classList.toggle('connecting', connecting); |
| 231 icon.classList.toggle('level0', strength < 0); |
| 232 icon.classList.toggle('level1', strength >= 0 && strength <= 25); |
| 233 icon.classList.toggle('level2', strength > 25 && strength <= 50); |
| 234 icon.classList.toggle('level3', strength > 50 && strength <= 75); |
| 235 icon.classList.toggle('level4', strength > 75); |
| 236 }, |
| 237 |
| 238 /** |
| 239 * Sets the icon badge visibility properties: roaming, secure, technology. |
| 240 * @param {!NetworkIconParamType} params Set of params describing the icon. |
| 241 * @private |
| 242 */ |
| 243 setIconBadges_: function(params) { |
| 244 var networkState = this.networkState; |
| 245 |
| 246 var type = (params.showBadges && networkState) ? networkState.Type : ''; |
| 247 if (type == CrOnc.Type.WI_FI) { |
| 248 this.roaming_ = false; |
| 249 var security = networkState.WiFi ? networkState.WiFi.Security : ''; |
| 250 this.secure_ = !!security && security != 'None'; |
| 251 this.technology_ = ''; |
| 252 } else if (type == CrOnc.Type.WI_MAX) { |
| 253 this.roaming_ = false; |
| 254 this.secure_ = false; |
| 255 this.technology_ = '4g'; |
| 256 } else if (type == CrOnc.Type.CELLULAR && networkState.Cellular) { |
| 257 this.roaming_ = |
| 258 networkState.Cellular.RoamingState == CrOnc.RoamingState.ROAMING; |
| 259 this.secure_ = false; |
| 260 var oncTechnology = networkState.Cellular.NetworkTechnology; |
| 261 switch (oncTechnology) { |
| 262 case CrOnc.NetworkTechnology.CDMA1XRTT: |
| 263 this.technology_ = '1x'; |
| 264 break; |
| 265 case CrOnc.NetworkTechnology.EDGE: |
| 266 this.technology_ = 'edge'; |
| 267 break; |
| 268 case CrOnc.NetworkTechnology.EVDO: |
| 269 this.technology_ = 'evdo'; |
| 270 break; |
| 271 case CrOnc.NetworkTechnology.GPRS: |
| 272 case CrOnc.NetworkTechnology.GSM: |
| 273 this.technology_ = 'gsm'; |
| 274 break; |
| 275 case CrOnc.NetworkTechnology.HSPA: |
| 276 this.technology_ = 'hspa'; |
| 277 break; |
| 278 case CrOnc.NetworkTechnology.HSPA_PLUS: |
| 279 this.technology_ = 'hspa_plus'; |
| 280 break; |
| 281 case CrOnc.NetworkTechnology.LTE: |
| 282 this.technology_ = 'lte'; |
| 283 break; |
| 284 case CrOnc.NetworkTechnology.LTE_ADVANCED: |
| 285 this.technology_ = 'lte_advanced'; |
| 286 break; |
| 287 case CrOnc.NetworkTechnology.UMTS: |
| 288 this.technology_ = '3g'; |
| 289 break; |
| 290 } |
| 291 } else { |
| 292 this.roaming_ = false; |
| 293 this.secure_ = false; |
| 294 this.technology_ = ''; |
| 295 } |
| 296 }, |
| 297 }); |
297 })(); | 298 })(); |
OLD | NEW |