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
3 // found in the LICENSE file.
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').
10 cr
.define('cr.onc', function() {
16 function OncData(data
) {
23 * Returns either a managed property dictionary or an unmanaged value.
24 * @param {string} key The property key.
25 * @return {*} The property value or dictionary if it exists, otherwise
28 getManagedProperty: function(key
) {
29 var data
= this.data_
;
31 var index
= key
.indexOf('.');
34 var keyComponent
= key
.substr(0, index
);
35 if (!(keyComponent
in data
))
37 data
= data
[keyComponent
];
38 key
= key
.substr(index
+ 1);
44 * Sets the value of a property. Currently only supports unmanaged
46 * @param {string} key The property key.
47 * @param {string} value The property value to set.
49 setManagedProperty: function(key
, value
) {
50 var data
= this.data_
;
52 var index
= key
.indexOf('.');
55 var keyComponent
= key
.substr(0, index
);
56 if (!(keyComponent
in data
))
57 data
[keyComponent
] = {};
58 data
= data
[keyComponent
];
59 key
= key
.substr(index
+ 1);
62 (typeof data
[key
] != 'object') ||
63 (!('Active' in data
[key
]) && !('Effective' in data
[key
]))) {
66 var effective
= data
[key
]['Effective'];
67 assert(effective
!= 'UserPolicy' || data
[key
]['UserEditable']);
68 assert(effective
!= 'DevicePolicy' || data
[key
]['DeviceEditable']);
69 // For now, just update the active value. TODO(stevenjb): Eventually we
70 // should update the 'UserSetting' and 'Effective' properties correctly
71 // and send that back to Chrome.
72 data
[key
]['Active'] = value
;
77 * Gets the active value of a property.
78 * @param {string} key The property key.
79 * @return {*} The property value or undefined.
81 getActiveValue: function(key
) {
82 var property
= this.getManagedProperty(key
);
83 if (Array
.isArray(property
) || typeof property
!= 'object')
85 // Otherwise get the Active value (default behavior).
86 if ('Active' in property
)
87 return property
['Active'];
88 // If no Active value is defined, return the effective value if present.
89 var effective
= this.getEffectiveValueFromProperty_(property
);
90 if (effective
!= undefined)
92 // Otherwise this is an Object but not a Managed one.
97 * Gets the translated ONC value from the result of getActiveValue() using
98 * loadTimeData. If no translation exists, returns the untranslated value.
99 * @param {string} key The property key.
100 * @return {*} The translation if available or the value if not.
102 getTranslatedValue: function(key
) {
103 var value
= this.getActiveValue(key
);
104 if (typeof value
!= 'string')
106 var oncString
= 'Onc' + key
+ value
;
107 // Handle special cases
108 if (key
== 'Name' && this.getActiveValue('Type') == 'Ethernet')
109 return loadTimeData
.getString('ethernetName');
110 if (key
== 'VPN.Type' && value
== 'L2TP-IPsec') {
111 var auth
= this.getActiveValue('VPN.IPsec.AuthenticationType');
112 if (auth
!= undefined)
115 oncString
= oncString
.replace(/\./g, '-');
116 if (loadTimeData
.valueExists(oncString
))
117 return loadTimeData
.getString(oncString
);
122 * Gets the recommended value of a property.
123 * @param {string} key The property key.
124 * @return {*} The property value or undefined.
126 getRecommendedValue: function(key
) {
127 var property
= this.getManagedProperty(key
);
128 if (Array
.isArray(property
) || typeof property
!= 'object')
130 if (property
['UserEditable'])
131 return property
['UserPolicy'];
132 if (property
['DeviceEditable'])
133 return property
['DevicePolicy'];
134 // No value recommended by policy.
139 * Returns the Source of this configuration. If undefined returns 'None'.
140 * @return {string} The configuration source: 'None', 'User', 'Device',
141 * 'UserPolicy', or 'DevicePolicy'.
143 getSource: function() {
144 var source
= this.getActiveValue('Source');
145 if (source
== undefined)
151 * Returns the WiFi security type (defaults to 'None').
152 * @return {string} The security type.
154 getWiFiSecurity: function() {
155 var security
= this.getActiveValue('WiFi.Security');
156 if (security
== undefined)
162 * Get the effective value from a Managed property ONC dictionary.
163 * @param {Object} property The managed property ONC dictionary.
164 * @return {*} The effective value or undefined.
167 getEffectiveValueFromProperty_: function(property
) {
168 if ('Effective' in property
) {
169 var effective
= property
.Effective
;
170 if (effective
in property
)
171 return property
[effective
];