diff options
Diffstat (limited to 'chromium/chrome/browser/resources/settings/prefs')
3 files changed, 167 insertions, 0 deletions
diff --git a/chromium/chrome/browser/resources/settings/prefs/prefs.html b/chromium/chrome/browser/resources/settings/prefs/prefs.html new file mode 100644 index 00000000000..2b5323003d0 --- /dev/null +++ b/chromium/chrome/browser/resources/settings/prefs/prefs.html @@ -0,0 +1,8 @@ +<link rel="import" href="chrome://resources/polymer/v0_8/polymer/polymer.html"> +<link rel="import" href="chrome://resources/html/assert.html"> +<link rel="import" href="chrome://resources/html/cr.html"> + +<dom-module id="cr-settings-prefs"> + <script src="prefs_types.js"></script> + <script src="prefs.js"></script> +</dom-module> diff --git a/chromium/chrome/browser/resources/settings/prefs/prefs.js b/chromium/chrome/browser/resources/settings/prefs/prefs.js new file mode 100644 index 00000000000..febc22d41b1 --- /dev/null +++ b/chromium/chrome/browser/resources/settings/prefs/prefs.js @@ -0,0 +1,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() {}); + } + }, + }); +})(); diff --git a/chromium/chrome/browser/resources/settings/prefs/prefs_types.js b/chromium/chrome/browser/resources/settings/prefs/prefs_types.js new file mode 100644 index 00000000000..d757fc03dcd --- /dev/null +++ b/chromium/chrome/browser/resources/settings/prefs/prefs_types.js @@ -0,0 +1,23 @@ +// 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 Types for CrSettingsPrefsElement. + */ + +if (CrSettingsPrefs === undefined) { + var CrSettingsPrefs = {}; + + /** + * The type of the event fired when prefs have been fetched and initialized. + * @const {string} + */ + CrSettingsPrefs.INITIALIZED = 'cr-settings-prefs-initialized'; + + /** + * Global boolean set to true when all settings have been initialized. + * @type {boolean} + */ + CrSettingsPrefs.isInitialized = false; +} |