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) {
26 * Returns either a managed property dictionary or an unmanaged value.
27 * @param {string} key The property key.
28 * @return {?} The property value or dictionary if it exists, otherwise
31 getManagedProperty: function(key) {
32 var data = this.data_;
34 var index = key.indexOf('.');
37 var keyComponent = key.substr(0, index);
38 if (!(keyComponent in data))
40 data = data[keyComponent];
41 key = key.substr(index + 1);
47 * Sets the value of a property. Currently only supports unmanaged
49 * @param {string} key The property key.
50 * @param {?} value The property value to set.
52 setProperty: function(key, value) {
53 var data = this.data_;
55 var index = key.indexOf('.');
58 var keyComponent = key.substr(0, index);
59 if (!(keyComponent in data))
60 data[keyComponent] = {};
61 data = data[keyComponent];
62 key = key.substr(index + 1);
65 (typeof data[key] != 'object') ||
66 (!('Active' in data[key]) && !('Effective' in data[key]))) {
69 var effective = data[key]['Effective'];
70 assert(effective != 'UserPolicy' || data[key]['UserEditable']);
71 assert(effective != 'DevicePolicy' || data[key]['DeviceEditable']);
72 // For now, just update the active value. TODO(stevenjb): Eventually we
73 // should update the 'UserSetting' and 'Effective' properties correctly
74 // and send that back to Chrome.
75 data[key]['Active'] = value;
80 * Gets the active value of a property.
81 * @param {string} key The property key.
82 * @return {?} The property value or undefined.
84 getActiveValue: function(key) {
85 var property = this.getManagedProperty(key);
86 if (Array.isArray(property) || typeof property != 'object')
88 // Otherwise get the Active value (default behavior).
89 if ('Active' in property)
90 return property['Active'];
91 // If no Active value is defined, return the effective value if present.
92 var effective = this.getEffectiveValueFromProperty_(
93 /** @type {Object} */(property));
94 if (effective != undefined)
96 // Otherwise this is an Object but not a Managed one.
101 * Gets the translated ONC value from the result of getActiveValue() using
102 * loadTimeData. If no translation exists, returns the untranslated value.
103 * @param {string} key The property key.
104 * @return {?} The translation if available or the value if not.
106 getTranslatedValue: function(key) {
107 var value = this.getActiveValue(key);
108 if (typeof value != 'string')
110 var oncString = 'Onc' + key + value;
111 // Handle special cases
112 if (key == 'Name' && this.getActiveValue('Type') == 'Ethernet')
113 return loadTimeData.getString('ethernetName');
114 if (key == 'VPN.Type' && value == 'L2TP-IPsec') {
115 var auth = this.getActiveValue('VPN.IPsec.AuthenticationType');
116 if (auth != undefined)
119 oncString = oncString.replace(/\./g, '-');
120 if (loadTimeData.valueExists(oncString))
121 return loadTimeData.getString(oncString);
126 * Gets the recommended value of a property.
127 * @param {string} key The property key.
128 * @return {?} The property value or undefined.
130 getRecommendedValue: function(key) {
131 var property = this.getManagedProperty(key);
132 if (Array.isArray(property) || typeof property != 'object')
134 if (property['UserEditable'])
135 return property['UserPolicy'];
136 if (property['DeviceEditable'])
137 return property['DevicePolicy'];
138 // No value recommended by policy.
143 * Returns the Source of this configuration. If undefined returns 'None'.
144 * @return {string} The configuration source: 'None', 'User', 'Device',
145 * 'UserPolicy', or 'DevicePolicy'.
147 getSource: function() {
148 var source = this.getActiveValue('Source');
149 if (source == undefined)
151 assert(typeof source == 'string');
156 * Returns the WiFi security type (defaults to 'None').
157 * @return {string} The security type.
159 getWiFiSecurity: function() {
160 var security = this.getActiveValue('WiFi.Security');
161 if (security == undefined)
163 assert(typeof security == 'string');
168 * Get the effective value from a Managed property ONC dictionary.
169 * @param {Object} property The managed property ONC dictionary.
170 * @return {?} The effective value or undefined.
173 getEffectiveValueFromProperty_: function(property) {
174 if ('Effective' in property) {
175 var effective = property.Effective;
176 if (effective in property)
177 return property[effective];
183 * Returns the complete ONC dictionary.
185 getData: function() {