OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 ONC Data support class. Wraps a dictionary object containing | 6 * @fileoverview ONC Data support class. Wraps a dictionary object containing |
7 * ONC managed or unmanaged dictionaries. Supports nested dictionaries, | 7 * ONC managed or unmanaged dictionaries. Supports nested dictionaries, |
8 * e.g. data.getManagedProperty('VPN.Type'). | 8 * e.g. data.getManagedProperty('VPN.Type'). |
9 */ | 9 */ |
| 10 |
| 11 cr.exportPath('cr.onc'); |
| 12 |
| 13 /** |
| 14 * @typedef {(Object|Array|string|undefined)} |
| 15 */ |
| 16 cr.onc.OncValue; |
| 17 |
10 cr.define('cr.onc', function() { | 18 cr.define('cr.onc', function() { |
11 'use strict'; | 19 'use strict'; |
12 | 20 |
13 /** | 21 /** |
14 * @constructor | 22 * @constructor |
15 */ | 23 */ |
16 function OncData(data) { | 24 function OncData(data) { |
17 this.data_ = data; | 25 this.data_ = data; |
18 } | 26 } |
19 | 27 |
20 OncData.prototype = { | 28 OncData.prototype = { |
21 | 29 |
22 /** | 30 /** |
23 * Returns either a managed property dictionary or an unmanaged value. | 31 * Returns either a managed property dictionary or an unmanaged value. |
24 * @param {string} key The property key. | 32 * @param {string} key The property key. |
25 * @return {*} The property value or dictionary if it exists, otherwise | 33 * @return {cr.onc.OncValue} The property value or dictionary if it exists, |
26 * undefined. | 34 * otherwise undefined. |
27 */ | 35 */ |
28 getManagedProperty: function(key) { | 36 getManagedProperty: function(key) { |
29 var data = this.data_; | 37 var data = this.data_; |
30 while (true) { | 38 while (true) { |
31 var index = key.indexOf('.'); | 39 var index = key.indexOf('.'); |
32 if (index < 0) | 40 if (index < 0) |
33 break; | 41 break; |
34 var keyComponent = key.substr(0, index); | 42 var keyComponent = key.substr(0, index); |
35 if (!(keyComponent in data)) | 43 if (!(keyComponent in data)) |
36 return undefined; | 44 return undefined; |
37 data = data[keyComponent]; | 45 data = data[keyComponent]; |
38 key = key.substr(index + 1); | 46 key = key.substr(index + 1); |
39 } | 47 } |
40 return data[key]; | 48 return data[key]; |
41 }, | 49 }, |
42 | 50 |
43 /** | 51 /** |
44 * Sets the value of a property. Currently only supports unmanaged | 52 * Sets the value of a property. Currently only supports unmanaged |
45 * properties. | 53 * properties. |
46 * @param {string} key The property key. | 54 * @param {string} key The property key. |
47 * @param {string} value The property value to set. | 55 * @param {Object} value The property value to set. |
48 */ | 56 */ |
49 setManagedProperty: function(key, value) { | 57 setManagedProperty: function(key, value) { |
50 var data = this.data_; | 58 var data = this.data_; |
51 while (true) { | 59 while (true) { |
52 var index = key.indexOf('.'); | 60 var index = key.indexOf('.'); |
53 if (index < 0) | 61 if (index < 0) |
54 break; | 62 break; |
55 var keyComponent = key.substr(0, index); | 63 var keyComponent = key.substr(0, index); |
56 if (!(keyComponent in data)) | 64 if (!(keyComponent in data)) |
57 data[keyComponent] = {}; | 65 data[keyComponent] = {}; |
(...skipping 11 matching lines...) Expand all Loading... |
69 // For now, just update the active value. TODO(stevenjb): Eventually we | 77 // For now, just update the active value. TODO(stevenjb): Eventually we |
70 // should update the 'UserSetting' and 'Effective' properties correctly | 78 // should update the 'UserSetting' and 'Effective' properties correctly |
71 // and send that back to Chrome. | 79 // and send that back to Chrome. |
72 data[key]['Active'] = value; | 80 data[key]['Active'] = value; |
73 } | 81 } |
74 }, | 82 }, |
75 | 83 |
76 /** | 84 /** |
77 * Gets the active value of a property. | 85 * Gets the active value of a property. |
78 * @param {string} key The property key. | 86 * @param {string} key The property key. |
79 * @return {*} The property value or undefined. | 87 * @return {cr.onc.OncValue} The property value or undefined. |
80 */ | 88 */ |
81 getActiveValue: function(key) { | 89 getActiveValue: function(key) { |
82 var property = this.getManagedProperty(key); | 90 var property = this.getManagedProperty(key); |
83 if (Array.isArray(property) || typeof property != 'object') | 91 if (Array.isArray(property) || typeof property != 'object') |
84 return property; | 92 return property; |
85 // Otherwise get the Active value (default behavior). | 93 // Otherwise get the Active value (default behavior). |
86 if ('Active' in property) | 94 if ('Active' in property) |
87 return property['Active']; | 95 return property['Active']; |
88 // If no Active value is defined, return the effective value if present. | 96 // If no Active value is defined, return the effective value if present. |
89 var effective = this.getEffectiveValueFromProperty_(property); | 97 var effective = this.getEffectiveValueFromProperty_( |
| 98 /** @type {Object} */(property)); |
90 if (effective != undefined) | 99 if (effective != undefined) |
91 return effective; | 100 return effective; |
92 // Otherwise this is an Object but not a Managed one. | 101 // Otherwise this is an Object but not a Managed one. |
93 return property; | 102 return property; |
94 }, | 103 }, |
95 | 104 |
96 /** | 105 /** |
97 * Gets the translated ONC value from the result of getActiveValue() using | 106 * Gets the translated ONC value from the result of getActiveValue() using |
98 * loadTimeData. If no translation exists, returns the untranslated value. | 107 * loadTimeData. If no translation exists, returns the untranslated value. |
99 * @param {string} key The property key. | 108 * @param {string} key The property key. |
100 * @return {*} The translation if available or the value if not. | 109 * @return {cr.onc.OncValue} The translation if available or the value if |
| 110 * not. |
101 */ | 111 */ |
102 getTranslatedValue: function(key) { | 112 getTranslatedValue: function(key) { |
103 var value = this.getActiveValue(key); | 113 var value = this.getActiveValue(key); |
104 if (typeof value != 'string') | 114 if (typeof value != 'string') |
105 return value; | 115 return value; |
106 var oncString = 'Onc' + key + value; | 116 var oncString = 'Onc' + key + value; |
107 // Handle special cases | 117 // Handle special cases |
108 if (key == 'Name' && this.getActiveValue('Type') == 'Ethernet') | 118 if (key == 'Name' && this.getActiveValue('Type') == 'Ethernet') |
109 return loadTimeData.getString('ethernetName'); | 119 return loadTimeData.getString('ethernetName'); |
110 if (key == 'VPN.Type' && value == 'L2TP-IPsec') { | 120 if (key == 'VPN.Type' && value == 'L2TP-IPsec') { |
111 var auth = this.getActiveValue('VPN.IPsec.AuthenticationType'); | 121 var auth = this.getActiveValue('VPN.IPsec.AuthenticationType'); |
112 if (auth != undefined) | 122 if (auth != undefined) |
113 oncString += auth; | 123 oncString += auth; |
114 } | 124 } |
115 oncString = oncString.replace(/\./g, '-'); | 125 oncString = oncString.replace(/\./g, '-'); |
116 if (loadTimeData.valueExists(oncString)) | 126 if (loadTimeData.valueExists(oncString)) |
117 return loadTimeData.getString(oncString); | 127 return loadTimeData.getString(oncString); |
118 return value; | 128 return value; |
119 }, | 129 }, |
120 | 130 |
121 /** | 131 /** |
122 * Gets the recommended value of a property. | 132 * Gets the recommended value of a property. |
123 * @param {string} key The property key. | 133 * @param {string} key The property key. |
124 * @return {*} The property value or undefined. | 134 * @return {cr.onc.OncValue} The property value or undefined. |
125 */ | 135 */ |
126 getRecommendedValue: function(key) { | 136 getRecommendedValue: function(key) { |
127 var property = this.getManagedProperty(key); | 137 var property = this.getManagedProperty(key); |
128 if (Array.isArray(property) || typeof property != 'object') | 138 if (Array.isArray(property) || typeof property != 'object') |
129 return undefined; | 139 return undefined; |
130 if (property['UserEditable']) | 140 if (property['UserEditable']) |
131 return property['UserPolicy']; | 141 return property['UserPolicy']; |
132 if (property['DeviceEditable']) | 142 if (property['DeviceEditable']) |
133 return property['DevicePolicy']; | 143 return property['DevicePolicy']; |
134 // No value recommended by policy. | 144 // No value recommended by policy. |
(...skipping 19 matching lines...) Expand all Loading... |
154 getWiFiSecurity: function() { | 164 getWiFiSecurity: function() { |
155 var security = this.getActiveValue('WiFi.Security'); | 165 var security = this.getActiveValue('WiFi.Security'); |
156 if (security == undefined) | 166 if (security == undefined) |
157 return 'None'; | 167 return 'None'; |
158 return security; | 168 return security; |
159 }, | 169 }, |
160 | 170 |
161 /** | 171 /** |
162 * Get the effective value from a Managed property ONC dictionary. | 172 * Get the effective value from a Managed property ONC dictionary. |
163 * @param {Object} property The managed property ONC dictionary. | 173 * @param {Object} property The managed property ONC dictionary. |
164 * @return {*} The effective value or undefined. | 174 * @return {cr.onc.OncValue} The effective value or undefined. |
165 * @private | 175 * @private |
166 */ | 176 */ |
167 getEffectiveValueFromProperty_: function(property) { | 177 getEffectiveValueFromProperty_: function(property) { |
168 if ('Effective' in property) { | 178 if ('Effective' in property) { |
169 var effective = property.Effective; | 179 var effective = property.Effective; |
170 if (effective in property) | 180 if (effective in property) |
171 return property[effective]; | 181 return property[effective]; |
172 } | 182 } |
173 return undefined; | 183 return undefined; |
174 } | 184 } |
175 }; | 185 }; |
176 | 186 |
177 return { | 187 return { |
178 OncData: OncData | 188 OncData: OncData |
179 }; | 189 }; |
180 }); | 190 }); |
OLD | NEW |