toolkit/devtools/server/actors/preference.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const {Cc, Ci, Cu, CC} = require("chrome");
     6 const protocol = require("devtools/server/protocol");
     7 const {Arg, method, RetVal} = protocol;
     8 const {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
    10 Cu.import("resource://gre/modules/Services.jsm");
    12 exports.register = function(handle) {
    13   handle.addGlobalActor(PreferenceActor, "preferenceActor");
    14 };
    16 exports.unregister = function(handle) {
    17 };
    19 let PreferenceActor = protocol.ActorClass({
    20   typeName: "preference",
    22   getBoolPref: method(function(name) {
    23     return Services.prefs.getBoolPref(name);
    24   }, {
    25     request: { value: Arg(0) },
    26     response: { value: RetVal("boolean") }
    27   }),
    29   getCharPref: method(function(name) {
    30     return Services.prefs.getCharPref(name);
    31   }, {
    32     request: { value: Arg(0) },
    33     response: { value: RetVal("string") }
    34   }),
    36   getIntPref: method(function(name) {
    37     return Services.prefs.getIntPref(name);
    38   }, {
    39     request: { value: Arg(0) },
    40     response: { value: RetVal("number") }
    41   }),
    43   getAllPrefs: method(function() {
    44     let prefs = {};
    45     Services.prefs.getChildList("").forEach(function(name, index) {
    46       // append all key/value pairs into a huge json object.
    47       try {
    48         let value;
    49         switch (Services.prefs.getPrefType(name)) {
    50           case Ci.nsIPrefBranch.PREF_STRING:
    51             value = Services.prefs.getCharPref(name);
    52             break;
    53           case Ci.nsIPrefBranch.PREF_INT:
    54             value = Services.prefs.getIntPref(name);
    55             break;
    56           case Ci.nsIPrefBranch.PREF_BOOL:
    57             value = Services.prefs.getBoolPref(name);
    58             break;
    59           default:
    60         }
    61         prefs[name] = {
    62           value: value,
    63           hasUserValue: Services.prefs.prefHasUserValue(name)
    64         };
    65       } catch (e) {
    66         // pref exists but has no user or default value
    67       }
    68     });
    69     return prefs;
    70   }, {
    71     request: {},
    72     response: { value: RetVal("json") }
    73   }),
    75   setBoolPref: method(function(name, value) {
    76     Services.prefs.setBoolPref(name, value);
    77     Services.prefs.savePrefFile(null);
    78   }, {
    79     request: { name: Arg(0), value: Arg(1) },
    80     response: {}
    81   }),
    83   setCharPref: method(function(name, value) {
    84     Services.prefs.setCharPref(name, value);
    85     Services.prefs.savePrefFile(null);
    86   }, {
    87     request: { name: Arg(0), value: Arg(1) },
    88     response: {}
    89   }),
    91   setIntPref: method(function(name, value) {
    92     Services.prefs.setIntPref(name, value);
    93     Services.prefs.savePrefFile(null);
    94   }, {
    95     request: { name: Arg(0), value: Arg(1) },
    96     response: {}
    97   }),
    99   clearUserPref: method(function(name) {
   100     Services.prefs.clearUserPref(name);
   101     Services.prefs.savePrefFile(null);
   102   }, {
   103     request: { name: Arg(0) },
   104     response: {}
   105   }),
   106 });
   108 let PreferenceFront = protocol.FrontClass(PreferenceActor, {
   109   initialize: function(client, form) {
   110     protocol.Front.prototype.initialize.call(this, client);
   111     this.actorID = form.preferenceActor;
   112     client.addActorPool(this);
   113     this.manage(this);
   114   },
   115 });
   117 const _knownPreferenceFronts = new WeakMap();
   119 exports.getPreferenceFront = function(client, form) {
   120   if (_knownPreferenceFronts.has(client))
   121     return _knownPreferenceFronts.get(client);
   123   let front = new PreferenceFront(client, form);
   124   _knownPreferenceFronts.set(client, front);
   125   return front;
   126 };

mercurial