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').
11 cr
.exportPath('cr.onc');
13 cr
.define('cr.onc', function() {
19 function OncData(data
) {
24 /** @return {string} The GUID of the network. */
25 guid: function() { return this.data_
['GUID']; },
28 * Returns either a managed property dictionary or an unmanaged value.
29 * @param {string} key The property key.
30 * @return {?} The property value or dictionary if it exists, otherwise
33 getManagedProperty: function(key
) {
34 var data
= this.data_
;
36 var index
= key
.indexOf('.');
39 var keyComponent
= key
.substr(0, index
);
40 if (!(keyComponent
in data
))
42 data
= data
[keyComponent
];
43 key
= key
.substr(index
+ 1);
49 * Sets the value of a property. Currently only supports unmanaged
51 * @param {string} key The property key.
52 * @param {?} value The property value to set.
54 setProperty: function(key
, value
) {
55 var data
= this.data_
;
57 var index
= key
.indexOf('.');
60 var keyComponent
= key
.substr(0, index
);
61 if (!(keyComponent
in data
))
62 data
[keyComponent
] = {};
63 data
= data
[keyComponent
];
64 key
= key
.substr(index
+ 1);
67 (typeof data
[key
] != 'object') ||
68 (!('Active' in data
[key
]) && !('Effective' in data
[key
]))) {
71 var effective
= data
[key
]['Effective'];
72 assert(effective
!= 'UserPolicy' || data
[key
]['UserEditable']);
73 assert(effective
!= 'DevicePolicy' || data
[key
]['DeviceEditable']);
74 // For now, just update the active value. TODO(stevenjb): Eventually we
75 // should update the 'UserSetting' and 'Effective' properties correctly
76 // and send that back to Chrome.
77 data
[key
]['Active'] = value
;
82 * Gets the active value of a property.
83 * @param {string} key The property key.
84 * @return {?} The property value or undefined.
86 getActiveValue: function(key
) {
87 var property
= this.getManagedProperty(key
);
88 if (Array
.isArray(property
) || typeof property
!= 'object')
90 // Otherwise get the Active value (default behavior).
91 if ('Active' in property
)
92 return property
['Active'];
93 // If no Active value is defined, return the effective value if present.
94 var effective
= this.getEffectiveValueFromProperty_(
95 /** @type {Object} */(property
));
96 if (effective
!= undefined)
98 // Otherwise this is an Object but not a Managed one.
103 * Gets the translated ONC value from the result of getActiveValue() using
104 * loadTimeData. If no translation exists, returns the untranslated value.
105 * @param {string} key The property key.
106 * @return {?} The translation if available or the value if not.
108 getTranslatedValue: function(key
) {
109 var value
= this.getActiveValue(key
);
110 if (typeof value
!= 'string')
112 var oncString
= 'Onc' + key
+ value
;
113 // Handle special cases
114 if (key
== 'Name' && this.getActiveValue('Type') == 'Ethernet')
115 return loadTimeData
.getString('ethernetName');
116 if (key
== 'VPN.Type' && value
== 'L2TP-IPsec') {
117 var auth
= this.getActiveValue('VPN.IPsec.AuthenticationType');
118 if (auth
!= undefined)
121 oncString
= oncString
.replace(/\./g, '-');
122 if (loadTimeData
.valueExists(oncString
))
123 return loadTimeData
.getString(oncString
);
128 * Gets the recommended value of a property.
129 * @param {string} key The property key.
130 * @return {?} The property value or undefined.
132 getRecommendedValue: function(key
) {
133 var property
= this.getManagedProperty(key
);
134 if (Array
.isArray(property
) || typeof property
!= 'object')
136 if (property
['UserEditable'])
137 return property
['UserPolicy'];
138 if (property
['DeviceEditable'])
139 return property
['DevicePolicy'];
140 // No value recommended by policy.
145 * Returns the Source of this configuration. If undefined returns 'None'.
146 * @return {string} The configuration source: 'None', 'User', 'Device',
147 * 'UserPolicy', or 'DevicePolicy'.
149 getSource: function() {
150 var source
= this.getActiveValue('Source');
151 if (source
== undefined)
153 assert(typeof source
== 'string');
158 * Returns the WiFi security type (defaults to 'None').
159 * @return {string} The security type.
161 getWiFiSecurity: function() {
162 var security
= this.getActiveValue('WiFi.Security');
163 if (security
== undefined)
165 assert(typeof security
== 'string');
170 * Get the effective value from a Managed property ONC dictionary.
171 * @param {Object} property The managed property ONC dictionary.
172 * @return {?} The effective value or undefined.
175 getEffectiveValueFromProperty_: function(property
) {
176 if ('Effective' in property
) {
177 var effective
= property
.Effective
;
178 if (effective
in property
)
179 return property
[effective
];
185 * Returns the complete ONC dictionary.
187 getData: function() {