summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/settings/prefs/prefs.js
blob: febc22d41b1e70e46394148759d257e9ffb05762 (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
/* 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
 * 'cr-settings-prefs' is an element which serves as a model for
 * interaction with settings which are stored in Chrome's
 * Preferences.
 *
 * Example:
 *
 *    <cr-settings-prefs id="prefs"></cr-settings-prefs>
 *    <cr-settings-a11y-page prefs="{{this.$.prefs}}"></cr-settings-a11y-page>
 *
 * @group Chrome Settings Elements
 * @element cr-settings-a11y-page
 */
(function() {
  'use strict';

  Polymer({
    is: 'cr-settings-prefs',

    properties: {
      /**
       * Object containing all preferences.
       */
      prefStore: {
        type: Object,
        value: function() { return {}; },
        notify: true,
      },
    },

    /** @override */
    created: function() {
      CrSettingsPrefs.isInitialized = false;

      chrome.settingsPrivate.onPrefsChanged.addListener(
          this.onPrefsChanged_.bind(this));
      chrome.settingsPrivate.getAllPrefs(this.onPrefsFetched_.bind(this));
    },

    /**
     * Called when prefs in the underlying Chrome pref store are changed.
     * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs The prefs that
     *     changed.
     * @private
     */
    onPrefsChanged_: function(prefs) {
      this.updatePrefs_(prefs, false);
    },

    /**
     * Called when prefs are fetched from settingsPrivate.
     * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs
     * @private
     */
    onPrefsFetched_: function(prefs) {
      this.updatePrefs_(prefs, true);

      CrSettingsPrefs.isInitialized = true;
      document.dispatchEvent(new Event(CrSettingsPrefs.INITIALIZED));
    },


    /**
     * Updates the settings model with the given prefs.
     * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs
     * @param {boolean} shouldObserve Whether to add an ObjectObserver for each
     *     of the prefs.
     * @private
     */
    updatePrefs_: function(prefs, shouldObserve) {
      prefs.forEach(function(prefObj) {
        let root = this.prefStore;
        let tokens = prefObj.key.split('.');

        assert(tokens.length > 0);

        for (let i = 0; i < tokens.length; i++) {
          let token = tokens[i];

          if (!root.hasOwnProperty(token)) {
            let path = 'prefStore.' + tokens.slice(0, i + 1).join('.');
            this.setPathValue(path, {});
          }
          root = root[token];
        }

        // NOTE: Do this copy rather than just a re-assignment, so that the
        // ObjectObserver fires.
        for (let objKey in prefObj) {
          let path = 'prefStore.' + prefObj.key + '.' + objKey;
          this.setPathValue(path, prefObj[objKey]);
        }

        if (shouldObserve) {
          let keyObserver = new ObjectObserver(root);
          keyObserver.open(
              this.propertyChangeCallback_.bind(this, prefObj.key));
        }
      }, this);
    },

    /**
     * Called when a property of a pref changes.
     * @param {string} propertyPath The path before the property names.
     * @param {!Array<string>} added An array of keys which were added.
     * @param {!Array<string>} removed An array of keys which were removed.
     * @param {!Array<string>} changed An array of keys of properties whose
     *     values changed.
     * @param {function(string) : *} getOldValueFn A function which takes a
     *     property name and returns the old value for that property.
     * @private
     */
    propertyChangeCallback_: function(
        propertyPath, added, removed, changed, getOldValueFn) {
      for (let property in changed) {
        // UI should only be able to change the value of a setting for now, not
        // disabled, etc.
        assert(property == 'value');

        let newValue = changed[property];
        assert(newValue !== undefined);

        chrome.settingsPrivate.setPref(
            propertyPath,
            newValue,
            /* pageId */ '',
            /* callback */ function() {});
      }
    },
  });
})();