summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/settings/prefs/prefs_behavior.js
blob: 8c9446000eff99dfe1d3c4ef2970328a52305880 (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
// 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 Common prefs behavior.
 */

/** @polymerBehavior */
var PrefsBehavior = {
  /**
   * Gets the pref at the given prefPath. Throws if the pref is not found.
   * @param {string} prefPath
   * @return {!chrome.settingsPrivate.PrefObject}
   * @protected
   */
  getPref: function(prefPath) {
    var pref = /** @type {!chrome.settingsPrivate.PrefObject} */(
        this.get(prefPath, this.prefs));
    assert(typeof pref != 'undefined', 'Pref is missing: ' + prefPath);
    return pref;
  },

  /**
   * Sets the value of the pref at the given prefPath. Throws if the pref is not
   * found.
   * @param {string} prefPath
   * @param {*} value
   * @protected
   */
  setPrefValue: function(prefPath, value) {
    this.getPref(prefPath);  // Ensures we throw if the pref is not found.
    this.set('prefs.' + prefPath + '.value', value);
  },

  /**
   * Appends the item to the pref list at the given key if the item is not
   * already in the list. Asserts if the pref itself is not found or is not an
   * Array type.
   * @param {string} key
   * @param {*} item
   * @protected
   */
  appendPrefListItem: function(key, item) {
    var pref = this.getPref(key);
    assert(pref && pref.type == chrome.settingsPrivate.PrefType.LIST);
    if (pref.value.indexOf(item) == -1)
      this.push('prefs.' + key + '.value', item);
  },

  /**
   * Deletes the given item from the pref at the given key if the item is found.
   * Asserts if the pref itself is not found or is not an Array type.
   * @param {string} key
   * @param {*} item
   * @protected
   */
  deletePrefListItem: function(key, item) {
    assert(this.getPref(key).type == chrome.settingsPrivate.PrefType.LIST);
    this.arrayDelete('prefs.' + key + '.value', item);
  },
};