Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / resources / options / chromeos / onc_data.js
blob5a45e6ab15c6a4643990419341ccccdd83a881e6
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.
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';
13 /**
14 * @constructor
16 function OncData(data) {
17 this.data_ = data;
20 OncData.prototype = {
22 /**
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
26 * undefined.
28 getManagedProperty: function(key) {
29 var data = this.data_;
30 while (true) {
31 var index = key.indexOf('.');
32 if (index < 0)
33 break;
34 var keyComponent = key.substr(0, index);
35 if (!(keyComponent in data))
36 return undefined;
37 data = data[keyComponent];
38 key = key.substr(index + 1);
40 return data[key];
43 /**
44 * Sets the value of a property. Currently only supports unmanaged
45 * properties.
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_;
51 while (true) {
52 var index = key.indexOf('.');
53 if (index < 0)
54 break;
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);
61 if (!(key in data) ||
62 (typeof data[key] != 'object') ||
63 (!('Active' in data[key]) && !('Effective' in data[key]))) {
64 data[key] = value;
65 } else {
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;
76 /**
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')
84 return property;
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)
91 return effective;
92 // Otherwise this is an Object but not a Managed one.
93 return property;
96 /**
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')
105 return value;
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)
113 oncString += auth;
115 oncString = oncString.replace(/\./g, '-');
116 if (loadTimeData.valueExists(oncString))
117 return loadTimeData.getString(oncString);
118 return value;
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')
129 return undefined;
130 if (property['UserEditable'])
131 return property['UserPolicy'];
132 if (property['DeviceEditable'])
133 return property['DevicePolicy'];
134 // No value recommended by policy.
135 return undefined;
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)
146 return 'None';
147 return source;
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)
157 return 'None';
158 return security;
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.
165 * @private
167 getEffectiveValueFromProperty_: function(property) {
168 if ('Effective' in property) {
169 var effective = property.Effective;
170 if (effective in property)
171 return property[effective];
173 return undefined;
177 return {
178 OncData: OncData