preference.js (3144B)
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/. */ 4 5 "use strict"; 6 7 const { Actor } = require("resource://devtools/shared/protocol.js"); 8 const { 9 preferenceSpec, 10 } = require("resource://devtools/shared/specs/preference.js"); 11 12 const { PREF_STRING, PREF_INT, PREF_BOOL } = Services.prefs; 13 14 function ensurePrefType(name, expectedType) { 15 const type = Services.prefs.getPrefType(name); 16 if (type !== expectedType) { 17 throw new Error(`preference is not of the right type: ${name}`); 18 } 19 } 20 21 /** 22 * Normally the preferences are set using Services.prefs, but this actor allows 23 * a devtools client to set preferences on the debuggee. This is particularly useful 24 * when remote debugging, and the preferences should persist to the remote target 25 * and not to the client. If used for a local target, it effectively behaves the same 26 * as using Services.prefs. 27 * 28 * This actor is used as a global-scoped actor, targeting the entire browser, not an 29 * individual tab. 30 */ 31 class PreferenceActor extends Actor { 32 constructor(conn) { 33 super(conn, preferenceSpec); 34 } 35 getTraits() { 36 // The *Pref traits are used to know if remote-debugging bugs related to 37 // specific preferences are fixed on the server or if the client should set 38 // default values for them. See the about:debugging module 39 // runtime-default-preferences.js 40 return {}; 41 } 42 43 getBoolPref(name) { 44 ensurePrefType(name, PREF_BOOL); 45 return Services.prefs.getBoolPref(name); 46 } 47 48 getCharPref(name) { 49 ensurePrefType(name, PREF_STRING); 50 return Services.prefs.getCharPref(name); 51 } 52 53 getIntPref(name) { 54 ensurePrefType(name, PREF_INT); 55 return Services.prefs.getIntPref(name); 56 } 57 58 getAllPrefs() { 59 const prefs = {}; 60 Services.prefs.getChildList("").forEach(function (name) { 61 // append all key/value pairs into a huge json object. 62 try { 63 let value; 64 switch (Services.prefs.getPrefType(name)) { 65 case PREF_STRING: 66 value = Services.prefs.getCharPref(name); 67 break; 68 case PREF_INT: 69 value = Services.prefs.getIntPref(name); 70 break; 71 case PREF_BOOL: 72 value = Services.prefs.getBoolPref(name); 73 break; 74 default: 75 } 76 prefs[name] = { 77 value, 78 hasUserValue: Services.prefs.prefHasUserValue(name), 79 }; 80 } catch (e) { 81 // pref exists but has no user or default value 82 } 83 }); 84 return prefs; 85 } 86 87 setBoolPref(name, value) { 88 Services.prefs.setBoolPref(name, value); 89 Services.prefs.savePrefFile(null); 90 } 91 92 setCharPref(name, value) { 93 Services.prefs.setCharPref(name, value); 94 Services.prefs.savePrefFile(null); 95 } 96 97 setIntPref(name, value) { 98 Services.prefs.setIntPref(name, value); 99 Services.prefs.savePrefFile(null); 100 } 101 102 clearUserPref(name) { 103 Services.prefs.clearUserPref(name); 104 Services.prefs.savePrefFile(null); 105 } 106 } 107 108 exports.PreferenceActor = PreferenceActor;