preferences.js revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2010 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. 4 5cr.define('options', function() { 6 7 ///////////////////////////////////////////////////////////////////////////// 8 // Preferences class: 9 10 /** 11 * Preferences class manages access to Chrome profile preferences. 12 * @constructor 13 */ 14 function Preferences() { 15 } 16 17 cr.addSingletonGetter(Preferences); 18 19 /** 20 * Extracts preference value. 21 * @param {Object} dict Map of preference values passed to fetchPrefs 22 * callback. 23 * @param {string} name Preference name. 24 * @return preference value. 25 */ 26 Preferences.getPref = function (dict, name) { 27 var parts = name.split('.'); 28 var cur = dict; 29 for (var part; part = parts.shift(); ) { 30 if (cur[part]) { 31 cur = cur[part]; 32 } else { 33 return null; 34 } 35 } 36 return cur; 37 }; 38 39 /** 40 * Sets value of a boolean preference. 41 * and signals its changed value. 42 * @param {string} name Preference name. 43 * @param {boolean} value New preference value. 44 * @param {string} metric User metrics identifier. 45 */ 46 Preferences.setBooleanPref = function (name, value, metric) { 47 var arguments = [name, value ? 'true' : 'false']; 48 if (metric != undefined) arguments.push(metric); 49 chrome.send('setBooleanPref', arguments); 50 }; 51 52 /** 53 * Sets value of an integer preference. 54 * and signals its changed value. 55 * @param {string} name Preference name. 56 * @param {number} value New preference value. 57 * @param {string} metric User metrics identifier. 58 */ 59 Preferences.setIntegerPref = function(name, value, metric) { 60 var arguments = [name, String(value)]; 61 if (metric != undefined) arguments.push(metric); 62 chrome.send('setIntegerPref', arguments); 63 }; 64 65 /** 66 * Sets value of a string preference. 67 * and signals its changed value. 68 * @param {string} name Preference name. 69 * @param {string} value New preference value. 70 * @param {string} metric User metrics identifier. 71 */ 72 Preferences.setStringPref = function(name, value, metric) { 73 var arguments = [name, value]; 74 if (metric != undefined) arguments.push(metric); 75 chrome.send('setStringPref', arguments); 76 }; 77 78 /** 79 * Sets value of a JSON preference. 80 * and signals its changed value. 81 * @param {string} name Preference name. 82 * @param {string} value New preference value. 83 * @param {string} metric User metrics identifier. 84 */ 85 Preferences.setObjectPref = function(name, value, metric) { 86 var arguments = [name, JSON.stringify(value)]; 87 if (metric != undefined) arguments.push(metric); 88 chrome.send('setObjectPref', arguments); 89 }; 90 91 /** 92 * Clears value of a JSON preference. 93 * @param {string} name Preference name. 94 * @param {string} metric User metrics identifier. 95 */ 96 Preferences.clearPref = function(name, metric) { 97 var arguments = [name]; 98 if (metric != undefined) arguments.push(metric); 99 chrome.send('clearPref', arguments); 100 }; 101 102 Preferences.prototype = { 103 __proto__: cr.EventTarget.prototype, 104 105 // Map of registered preferences. 106 registeredPreferences_: {}, 107 108 /** 109 * Adds an event listener to the target. 110 * @param {string} type The name of the event. 111 * @param {!Function|{handleEvent:Function}} handler The handler for the 112 * event. This is called when the event is dispatched. 113 */ 114 addEventListener: function(type, handler) { 115 cr.EventTarget.prototype.addEventListener.call(this, type, handler); 116 this.registeredPreferences_[type] = true; 117 }, 118 119 /** 120 * Initializes preference reading and change notifications. 121 */ 122 initialize: function() { 123 var params1 = ['Preferences.prefsFetchedCallback']; 124 var params2 = ['Preferences.prefsChangedCallback']; 125 for (var prefName in this.registeredPreferences_) { 126 params1.push(prefName); 127 params2.push(prefName); 128 } 129 chrome.send('fetchPrefs', params1); 130 chrome.send('observePrefs', params2); 131 }, 132 133 /** 134 * Helper function for flattening of dictionary passed via fetchPrefs 135 * callback. 136 * @param {string} prefix Preference name prefix. 137 * @param {object} dict Map with preference values. 138 */ 139 flattenMapAndDispatchEvent_: function(prefix, dict) { 140 for (var prefName in dict) { 141 if (typeof dict[prefName] == 'object' && 142 !this.registeredPreferences_[prefix + prefName]) { 143 this.flattenMapAndDispatchEvent_(prefix + prefName + '.', 144 dict[prefName]); 145 } else { 146 var event = new cr.Event(prefix + prefName); 147 event.value = dict[prefName]; 148 this.dispatchEvent(event); 149 } 150 } 151 } 152 }; 153 154 /** 155 * Callback for fetchPrefs method. 156 * @param {object} dict Map of fetched property values. 157 */ 158 Preferences.prefsFetchedCallback = function(dict) { 159 Preferences.getInstance().flattenMapAndDispatchEvent_('', dict); 160 }; 161 162 /** 163 * Callback for observePrefs method. 164 * @param {array} notification An array defining changed preference values. 165 * notification[0] contains name of the change preference while its new value 166 * is stored in notification[1]. 167 */ 168 Preferences.prefsChangedCallback = function(notification) { 169 var event = new cr.Event(notification[0]); 170 event.value = notification[1]; 171 Preferences.getInstance().dispatchEvent(event); 172 }; 173 174 // Export 175 return { 176 Preferences: Preferences 177 }; 178 179}); 180 181