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 | 10 |
11 cr.exportPath('cr.onc'); | 11 cr.exportPath('cr.onc'); |
12 | 12 |
13 cr.define('cr.onc', function() { | 13 cr.define('cr.onc', function() { |
14 'use strict'; | 14 'use strict'; |
15 | 15 |
16 /** | 16 /** |
17 * @constructor | 17 * @constructor |
18 */ | 18 */ |
19 function OncData(data) { | 19 function OncData(data) { |
20 this.data_ = data; | 20 this.data_ = data; |
21 } | 21 } |
22 | 22 |
23 OncData.prototype = { | 23 OncData.prototype = { |
24 /** @return {string} The GUID of the network. */ | 24 /** @return {string} The GUID of the network. */ |
25 guid: function() { return this.data_['GUID']; }, | 25 guid: function() { |
| 26 return this.data_['GUID']; |
| 27 }, |
26 | 28 |
27 /** | 29 /** |
28 * Returns either a managed property dictionary or an unmanaged value. | 30 * Returns either a managed property dictionary or an unmanaged value. |
29 * @param {string} key The property key. | 31 * @param {string} key The property key. |
30 * @return {?} The property value or dictionary if it exists, otherwise | 32 * @return {?} The property value or dictionary if it exists, otherwise |
31 * undefined. | 33 * undefined. |
32 */ | 34 */ |
33 getManagedProperty: function(key) { | 35 getManagedProperty: function(key) { |
34 var data = this.data_; | 36 var data = this.data_; |
35 while (true) { | 37 while (true) { |
(...skipping 20 matching lines...) Expand all Loading... |
56 while (true) { | 58 while (true) { |
57 var index = key.indexOf('.'); | 59 var index = key.indexOf('.'); |
58 if (index < 0) | 60 if (index < 0) |
59 break; | 61 break; |
60 var keyComponent = key.substr(0, index); | 62 var keyComponent = key.substr(0, index); |
61 if (!(keyComponent in data)) | 63 if (!(keyComponent in data)) |
62 data[keyComponent] = {}; | 64 data[keyComponent] = {}; |
63 data = data[keyComponent]; | 65 data = data[keyComponent]; |
64 key = key.substr(index + 1); | 66 key = key.substr(index + 1); |
65 } | 67 } |
66 if (!(key in data) || | 68 if (!(key in data) || (typeof data[key] != 'object') || |
67 (typeof data[key] != 'object') || | |
68 (!('Active' in data[key]) && !('Effective' in data[key]))) { | 69 (!('Active' in data[key]) && !('Effective' in data[key]))) { |
69 data[key] = value; | 70 data[key] = value; |
70 } else { | 71 } else { |
71 var effective = data[key]['Effective']; | 72 var effective = data[key]['Effective']; |
72 assert(effective != 'UserPolicy' || data[key]['UserEditable']); | 73 assert(effective != 'UserPolicy' || data[key]['UserEditable']); |
73 assert(effective != 'DevicePolicy' || data[key]['DeviceEditable']); | 74 assert(effective != 'DevicePolicy' || data[key]['DeviceEditable']); |
74 // For now, just update the active value. TODO(stevenjb): Eventually we | 75 // For now, just update the active value. TODO(stevenjb): Eventually we |
75 // should update the 'UserSetting' and 'Effective' properties correctly | 76 // should update the 'UserSetting' and 'Effective' properties correctly |
76 // and send that back to Chrome. | 77 // and send that back to Chrome. |
77 data[key]['Active'] = value; | 78 data[key]['Active'] = value; |
78 } | 79 } |
79 }, | 80 }, |
80 | 81 |
81 /** | 82 /** |
82 * Gets the active value of a property. | 83 * Gets the active value of a property. |
83 * @param {string} key The property key. | 84 * @param {string} key The property key. |
84 * @return {?} The property value or undefined. | 85 * @return {?} The property value or undefined. |
85 */ | 86 */ |
86 getActiveValue: function(key) { | 87 getActiveValue: function(key) { |
87 var property = this.getManagedProperty(key); | 88 var property = this.getManagedProperty(key); |
88 if (Array.isArray(property) || typeof property != 'object') | 89 if (Array.isArray(property) || typeof property != 'object') |
89 return property; | 90 return property; |
90 // Otherwise get the Active value (default behavior). | 91 // Otherwise get the Active value (default behavior). |
91 if ('Active' in property) | 92 if ('Active' in property) |
92 return property['Active']; | 93 return property['Active']; |
93 // If no Active value is defined, return the effective value if present. | 94 // If no Active value is defined, return the effective value if present. |
94 var effective = this.getEffectiveValueFromProperty_( | 95 var effective = this.getEffectiveValueFromProperty_( |
95 /** @type {Object} */(property)); | 96 /** @type {Object} */ (property)); |
96 if (effective != undefined) | 97 if (effective != undefined) |
97 return effective; | 98 return effective; |
98 // Otherwise this is an Object but not a Managed one. | 99 // Otherwise this is an Object but not a Managed one. |
99 return property; | 100 return property; |
100 }, | 101 }, |
101 | 102 |
102 /** | 103 /** |
103 * Gets the translated ONC value from the result of getActiveValue() using | 104 * Gets the translated ONC value from the result of getActiveValue() using |
104 * loadTimeData. If no translation exists, returns the untranslated value. | 105 * loadTimeData. If no translation exists, returns the untranslated value. |
105 * @param {string} key The property key. | 106 * @param {string} key The property key. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 }, | 183 }, |
183 | 184 |
184 /** | 185 /** |
185 * Returns the complete ONC dictionary. | 186 * Returns the complete ONC dictionary. |
186 */ | 187 */ |
187 getData: function() { | 188 getData: function() { |
188 return this.data_; | 189 return this.data_; |
189 } | 190 } |
190 }; | 191 }; |
191 | 192 |
192 return { | 193 return {OncData: OncData}; |
193 OncData: OncData | |
194 }; | |
195 }); | 194 }); |
OLD | NEW |