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