cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / chrome / browser / resources / options / chromeos / onc_data.js
blob8114dbd2a33e27bcc47b7d29b003c09d78462ce6
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 */
11 cr.exportPath('cr.onc');
13 cr.define('cr.onc', function() {
14 'use strict';
16 /**
17 * @constructor
19 function OncData(data) {
20 this.data_ = data;
23 OncData.prototype = {
24 /** @return {string} The GUID of the network. */
25 guid: function() { return this.data_['GUID']; },
27 /**
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
31 * undefined.
33 getManagedProperty: function(key) {
34 var data = this.data_;
35 while (true) {
36 var index = key.indexOf('.');
37 if (index < 0)
38 break;
39 var keyComponent = key.substr(0, index);
40 if (!(keyComponent in data))
41 return undefined;
42 data = data[keyComponent];
43 key = key.substr(index + 1);
45 return data[key];
48 /**
49 * Sets the value of a property. Currently only supports unmanaged
50 * properties.
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_;
56 while (true) {
57 var index = key.indexOf('.');
58 if (index < 0)
59 break;
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);
66 if (!(key in data) ||
67 (typeof data[key] != 'object') ||
68 (!('Active' in data[key]) && !('Effective' in data[key]))) {
69 data[key] = value;
70 } else {
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;
81 /**
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')
89 return property;
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)
97 return effective;
98 // Otherwise this is an Object but not a Managed one.
99 return property;
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')
111 return value;
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)
119 oncString += auth;
121 oncString = oncString.replace(/\./g, '-');
122 if (loadTimeData.valueExists(oncString))
123 return loadTimeData.getString(oncString);
124 return value;
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')
135 return undefined;
136 if (property['UserEditable'])
137 return property['UserPolicy'];
138 if (property['DeviceEditable'])
139 return property['DevicePolicy'];
140 // No value recommended by policy.
141 return undefined;
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)
152 return 'None';
153 assert(typeof source == 'string');
154 return source;
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)
164 return 'None';
165 assert(typeof security == 'string');
166 return security;
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.
173 * @private
175 getEffectiveValueFromProperty_: function(property) {
176 if ('Effective' in property) {
177 var effective = property.Effective;
178 if (effective in property)
179 return property[effective];
181 return undefined;
185 * Returns the complete ONC dictionary.
187 getData: function() {
188 return this.data_;
192 return {
193 OncData: OncData