OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
armansito
2014/09/03 21:42:02
nit: I think we omit the "(c)" bit now.
stevenjb
2014/09/04 15:34:11
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @fileoverview ONC Data support class. Wraps a dictionary object containing | |
7 * ONC managed or unmanaged dictionaries. Supports nested dictionaries, | |
8 * e.g. data.getManagedProperty('VPN.Type'). | |
9 */ | |
10 cr.define('cr.onc', function() { | |
11 'use strict'; | |
12 | |
13 function OncData(data) { | |
14 this.data_ = data; | |
15 // For convenience set 'type' to the active 'Type' value. | |
16 this.type = this.getActiveValue('Type'); | |
17 } | |
18 | |
19 OncData.prototype = { | |
20 | |
21 /** | |
22 * Returns either a managed property dictionary or an unmanaged value. | |
23 * @param {string} key The property key. | |
24 * @return {*} The property value or dictionary if it exists, otherwise | |
25 * undefined. | |
26 */ | |
27 getManagedProperty: function(key) { | |
28 var data = this.data_; | |
29 while (true) { | |
30 var index = key.indexOf('.'); | |
31 if (index < 0) | |
32 break; | |
33 var keyComponent = key.substr(0, index); | |
34 if (!(keyComponent in data)) | |
35 return undefined; | |
36 data = data[keyComponent]; | |
37 key = key.substr(index + 1); | |
38 } | |
39 return data[key]; | |
40 }, | |
41 | |
42 /** | |
43 * Sets the value of a property. Currently only supports unmanaged | |
44 * properties. | |
45 * @param {string} key The property key. | |
46 * @param {string} value The property value to set. | |
47 */ | |
48 setManagedProperty: function(key, value) { | |
49 var data = this.data_; | |
50 while (true) { | |
51 var index = key.indexOf('.'); | |
52 if (index < 0) | |
53 break; | |
54 var keyComponent = key.substr(0, index); | |
55 if (!(keyComponent in data)) | |
56 data[keyComponent] = {}; | |
57 data = data[keyComponent]; | |
58 key = key.substr(index + 1); | |
59 } | |
60 if (!(key in data) || | |
61 (typeof data[key] != 'object') || | |
62 (!('Active' in data[key]) && !('Effective' in data[key]))) { | |
63 data[key] = value; | |
64 } else { | |
65 var effective = data[key]['Effective']; | |
66 assert(effective != 'UserPolicy' || data[key]['UserEditable']); | |
67 assert(effective != 'DevicePolicy' || data[key]['DeviceEditable']); | |
68 // For now, just uupdate the active value. TODO(stevenjb): Eventually we | |
69 // should update the 'UserSetting' and 'Effective' properties correctly | |
70 // and send that back to Chrome. | |
71 data[key]['Active'] = value; | |
72 } | |
73 }, | |
74 | |
75 /** | |
76 * Gets the active value of a property. | |
77 * @param {string} key The property key. | |
78 * @return {*} The property value or undefined. | |
79 */ | |
80 getActiveValue: function(key) { | |
81 var property = this.getManagedProperty(key); | |
82 if (Array.isArray(property) || typeof property != 'object') | |
83 return property; | |
84 // Otherwise get the Active value (defalt behavior). | |
85 if ('Active' in property) | |
86 return property['Active']; | |
87 // If no Active value is defined, return the effective value if present. | |
88 var effective = this.getEffectiveValueFromProperty_(property); | |
89 if (effective != undefined) | |
90 return effective; | |
91 // Otherwise this is an Object but not a Managed one. | |
92 return property; | |
93 }, | |
94 | |
95 /** | |
96 * Gets the translated ONC value from the result of getActiveValue() using | |
97 * loadTimeData. If no translation exists, returns the untranslated value. | |
98 * @param {string} key The property key. | |
99 * @return {*} The translation if available or the value if not. | |
100 */ | |
101 getTranslatedValue: function(key) { | |
102 var value = this.getActiveValue(key); | |
103 if (typeof value != 'string') | |
104 return value; | |
105 var oncString = 'Onc' + key + value; | |
106 // Handle special cases | |
107 if (key == 'Name' && this.type == 'Ethernet') | |
108 return loadTimeData.getString('ethernetName'); | |
109 if (key == 'VPN.Type' && value == 'L2TP-IPsec') { | |
110 var auth = this.getActiveValue('VPN.IPsec.AuthenticationType'); | |
111 if (auth != undefined) | |
112 oncString += auth; | |
113 } | |
114 oncString = oncString.replace(/\./g, '-'); | |
115 if (loadTimeData.valueExists(oncString)) | |
116 return loadTimeData.getString(oncString); | |
117 return value; | |
118 }, | |
119 | |
120 /** | |
121 * Gets the recommended value of a property. | |
122 * @param {string} key The property key. | |
123 * @return {*} The property value or undefined. | |
124 */ | |
125 getRecommendedValue: function(key) { | |
126 var property = this.getManagedProperty(key); | |
127 if (Array.isArray(property) || typeof property != 'object') | |
128 return undefined; | |
129 if (property['UserEditable']) | |
130 return property['UserPolicy']; | |
131 if (property['DeviceEditable']) | |
132 return property['DevicePolicy']; | |
133 // No value recommended by policy. | |
134 return undefined; | |
135 }, | |
136 | |
137 /** | |
138 * Get the effective value from a Managed property ONC dictionary. | |
139 * @param {object} property The managed property ONC dictionary. | |
140 * @return {*} The effective value or undefined. | |
141 * @private | |
142 */ | |
143 getEffectiveValueFromProperty_: function(property) { | |
144 if ('Effective' in property) { | |
145 var effective = property.Effective; | |
146 if (effective in property) | |
147 return property[effective]; | |
148 } | |
149 return undefined; | |
150 } | |
151 }; | |
152 | |
153 return { | |
154 OncData: OncData | |
155 }; | |
156 }); | |
OLD | NEW |