summaryrefslogtreecommitdiff
path: root/chromium/ui/webui/resources/cr_components/chromeos/network/network_property_list.js
blob: 00f9ae3f42dd19e8f53825c674b861210b1199ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Polymer element for displaying a list of network properties
 * in a list. This also supports editing fields inline for fields listed in
 * editFieldTypes.
 */
Polymer({
  is: 'network-property-list',

  behaviors: [I18nBehavior, CrPolicyNetworkBehavior],

  properties: {
    /**
     * If true, all fields are shown as editable. Fields without an edit type
     * or that are policy enforced will be shown as 'readonly'. Used for lists
     * with dynamic edit types (e.g. network-ip-config).
     */
    editable: {
      type: Boolean,
      value: false,
    },

    /**
     * The dictionary containing the properties to display.
     * @type {!Object|undefined}
     */
    propertyDict: {type: Object},

    /**
     * Fields to display.
     * @type {!Array<string>}
     */
    fields: {
      type: Array,
      value: function() {
        return [];
      },
    },

    /**
     * Edit type of editable fields. May contain a property for any field in
     * |fields|. Other properties will be ignored. Property values can be:
     *   'String' - A text input will be displayed.
     *   'StringArray' - A text input will be displayed that expects a comma
     *       separated list of strings.
     *   'Password' - A string with input type = password.
     *   TODO(stevenjb): Support types with custom validation, e.g. IPAddress.
     * When a field changes, the 'property-change' event will be fired with
     * the field name and the new value provided in the event detail.
     */
    editFieldTypes: {
      type: Object,
      value: function() {
        return {};
      },
    },

    /** Prefix used to look up property key translations. */
    prefix: {
      type: String,
      value: '',
    },
  },

  /**
   * Event triggered when an input field changes. Fires a 'property-change'
   * event with the field (property) name set to the target id, and the value
   * set to the target input value.
   * @param {!Event} event The input change event.
   * @private
   */
  onValueChange_: function(event) {
    if (!this.propertyDict) {
      return;
    }
    const key = event.target.id;
    let curValue = this.get(key, this.propertyDict);
    if (typeof curValue == 'object' && !Array.isArray(curValue)) {
      // Extract the property from an ONC managed dictionary.
      curValue = CrOnc.getActiveValue(
          /** @type {!CrOnc.ManagedProperty} */ (curValue));
    }
    const newValue = this.getValueFromEditField_(key, event.target.value);
    if (newValue == curValue) {
      return;
    }
    this.fire('property-change', {field: key, value: newValue});
  },

  /**
   * @param {string} key The property key.
   * @param {string} prefix
   * @return {string} The text to display for the property label.
   * @private
   */
  getPropertyLabel_: function(key, prefix) {
    let oncKey = 'Onc' + prefix + key;
    oncKey = oncKey.replace(/\./g, '-');
    if (this.i18nExists(oncKey)) {
      return this.i18n(oncKey);
    }
    // We do not provide translations for every possible network property key.
    // For keys specific to a type, strip the type prefix.
    let result = prefix + key;
    for (const entry in chrome.networkingPrivate.NetworkType) {
      const type = chrome.networkingPrivate.NetworkType[entry];
      if (result.startsWith(type + '.')) {
        result = result.substr(type.length + 1);
        break;
      }
    }
    return result;
  },

  /**
   * Generates a filter function dependent on propertyDict and editFieldTypes.
   * @param {string} prefix
   * @param {!Object} propertyDict
   * @param {!Object} editFieldTypes
   * @private
   */
  computeFilter_: function(prefix, propertyDict, editFieldTypes) {
    return key => {
      if (editFieldTypes.hasOwnProperty(key)) {
        return true;
      }
      const value = this.getPropertyValue_(key, prefix, propertyDict);
      return value !== undefined && value !== '';
    };
  },

  /**
   * @param {string} key The property key.
   * @param {!Object} propertyDict
   * @return {boolean}
   * @private
   */
  isPropertyEditable_: function(key, propertyDict) {
    const property = /** @type {!CrOnc.ManagedProperty|undefined} */ (
        this.get(key, propertyDict));
    if (property === undefined) {
      // Unspecified properties in policy configurations are not user
      // modifiable. https://crbug.com/819837.
      const source = propertyDict.Source;
      return source != 'UserPolicy' && source != 'DevicePolicy';
    }
    return !this.isNetworkPolicyEnforced(property);
  },

  /**
   * @param {string} key The property key.
   * @param {!Object} editFieldTypes
   * @return {boolean} True if the edit type for the key is a valid type.
   * @private
   */
  isEditType_: function(key, editFieldTypes) {
    const editType = editFieldTypes[key];
    return editType == 'String' || editType == 'StringArray' ||
        editType == 'Password';
  },

  /**
   * @param {string} key The property key.
   * @param {!Object} propertyDict
   * @param {!Object} editFieldTypes
   * @return {boolean}
   * @private
   */
  isEditable_: function(key, propertyDict, editFieldTypes) {
    return this.isEditType_(key, editFieldTypes) &&
        this.isPropertyEditable_(key, propertyDict);
  },

  /**
   * @param {string} key The property key.
   * @param {!Object} propertyDict
   * @param {!Object} editFieldTypes
   * @return {boolean}
   * @private
   */
  showEditable_: function(key, propertyDict, editFieldTypes) {
    return this.isEditable_(key, propertyDict, editFieldTypes) || this.editable;
  },

  /**
   * @param {string} key The property key.
   * @param {!Object} editFieldTypes
   * @return {string}
   * @private
   */
  getEditInputType_: function(key, editFieldTypes) {
    return editFieldTypes[key] == 'Password' ? 'password' : 'text';
  },

  /**
   * @param {string} key The property key.
   * @param {!Object} propertyDict
   * @return {*} The managed property dictionary associated with |key|.
   * @private
   */
  getProperty_: function(key, propertyDict) {
    const property = this.get(key, propertyDict);
    if (property === undefined && propertyDict.Source) {
      // Provide an empty property object with the network policy source.
      // See https://crbug.com/819837 for more info.
      return {Effective: propertyDict.Source};
    }
    return property;
  },

  /**
   * @param {string} key The property key.
   * @param {string} prefix
   * @param {!Object} propertyDict
   * @return {string} The text to display for the property value.
   * @private
   */
  getPropertyValue_: function(key, prefix, propertyDict) {
    let value = this.get(key, propertyDict);
    if (value === undefined) {
      return '';
    }
    if (typeof value == 'object' && !Array.isArray(value)) {
      // Extract the property from an ONC managed dictionary
      value =
          CrOnc.getActiveValue(/** @type {!CrOnc.ManagedProperty} */ (value));
    }
    if (Array.isArray(value)) {
      return value.join(', ');
    }

    const customValue = this.getCustomPropertyValue_(key, value);
    if (customValue) {
      return customValue;
    }
    if (typeof value == 'number' || typeof value == 'boolean') {
      return value.toString();
    }

    assert(typeof value == 'string');
    const valueStr = /** @type {string} */ (value);
    let oncKey = 'Onc' + prefix + key;
    oncKey = oncKey.replace(/\./g, '-');
    oncKey += '_' + valueStr;
    if (this.i18nExists(oncKey)) {
      return this.i18n(oncKey);
    }
    return valueStr;
  },

  /**
   * Converts edit field values to the correct edit type.
   * @param {string} key The property key.
   * @param {*} fieldValue The value from the field.
   * @return {*}
   * @private
   */
  getValueFromEditField_(key, fieldValue) {
    const editType = this.editFieldTypes[key];
    if (editType == 'StringArray') {
      return fieldValue.toString().split(/, */);
    }
    return fieldValue;
  },

  /**
   * @param {string} key The property key.
   * @param {*} value The property value.
   * @return {string} The text to display for the property value. If the key
   *     does not correspond to a custom property, an empty string is returned.
   */
  getCustomPropertyValue_: function(key, value) {
    if (key == 'Tether.BatteryPercentage') {
      assert(typeof value == 'number');
      return this.i18n('OncTether-BatteryPercentage_Value', value.toString());
    }

    if (key == 'Tether.SignalStrength') {
      assert(typeof value == 'number');
      // Possible |signalStrength| values should be 0, 25, 50, 75, and 100. Add
      // <= checks for robustness.
      if (value <= 24) {
        return this.i18n('OncTether-SignalStrength_Weak');
      }
      if (value <= 49) {
        return this.i18n('OncTether-SignalStrength_Okay');
      }
      if (value <= 74) {
        return this.i18n('OncTether-SignalStrength_Good');
      }
      if (value <= 99) {
        return this.i18n('OncTether-SignalStrength_Strong');
      }
      return this.i18n('OncTether-SignalStrength_VeryStrong');
    }

    if (key == 'Tether.Carrier') {
      assert(typeof value == 'string');
      return (!value || value == 'unknown-carrier') ?
          this.i18n('tetherUnknownCarrier') :
          value;
    }

    return '';
  },
});