/usr/share/xul-ext/greasemonkey/modules/prefmanager.js is in xul-ext-greasemonkey 1.15-1~deb7u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | const EXPORTED_SYMBOLS = ['GM_PrefManager', 'GM_prefRoot'];
var gStringBundle = Components
.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService)
.createBundle("chrome://greasemonkey/locale/greasemonkey.properties");
/**
* Simple API on top of preferences for greasemonkey.
* Construct an instance by passing the startPoint of a preferences subtree.
* "greasemonkey." prefix is assumed.
*/
function GM_PrefManager(startPoint) {
startPoint = "extensions.greasemonkey." + (startPoint || "");
this.pref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch(startPoint);
this.observers = {};
};
GM_PrefManager.prototype.MIN_INT_32 = -0x80000000;
GM_PrefManager.prototype.MAX_INT_32 = 0x7FFFFFFF;
GM_PrefManager.prototype.nsISupportsString = Components.interfaces
.nsISupportsString;
/**
* whether a preference exists
*/
GM_PrefManager.prototype.exists = function(prefName) {
return this.pref.getPrefType(prefName) != 0;
};
/**
* enumerate preferences
*/
GM_PrefManager.prototype.listValues = function() {
return this.pref.getChildList("", {});
};
/**
* returns the named preference, or defaultValue if it does not exist
*/
GM_PrefManager.prototype.getValue = function(prefName, defaultValue) {
var prefType = this.pref.getPrefType(prefName);
// underlying preferences object throws an exception if pref doesn't exist
if (prefType == this.pref.PREF_INVALID) {
return defaultValue;
}
try {
switch (prefType) {
case this.pref.PREF_STRING:
return this.pref.getComplexValue(prefName, this.nsISupportsString).data;
case this.pref.PREF_BOOL:
return this.pref.getBoolPref(prefName);
case this.pref.PREF_INT:
return this.pref.getIntPref(prefName);
}
} catch(e) {
return defaultValue != undefined ? defaultValue : null;
}
return null;
};
/**
* sets the named preference to the specified value. values must be strings,
* booleans, or integers.
*/
GM_PrefManager.prototype.setValue = function(prefName, value) {
var prefType = typeof(value);
var goodType = false;
switch (prefType) {
case "string":
case "boolean":
goodType = true;
break;
case "number":
if (value % 1 == 0 &&
value >= this.MIN_INT_32 &&
value <= this.MAX_INT_32) {
goodType = true;
}
break;
}
if (!goodType) {
throw new Error(
gStringBundle.GetStringFromName('error.args.getValue'));
}
// underlying preferences object throws an exception if new pref has a
// different type than old one. i think we should not do this, so delete
// old pref first if this is the case.
if (this.exists(prefName) && prefType != typeof(this.getValue(prefName))) {
this.remove(prefName);
}
// set new value using correct method
switch (prefType) {
case "string":
var str = Components.classes["@mozilla.org/supports-string;1"]
.createInstance(this.nsISupportsString);
str.data = value;
this.pref.setComplexValue(prefName, this.nsISupportsString, str);
break;
case "boolean":
this.pref.setBoolPref(prefName, value);
break;
case "number":
this.pref.setIntPref(prefName, Math.floor(value));
break;
}
};
/**
* deletes the named preference or subtree
*/
GM_PrefManager.prototype.remove = function(prefName) {
this.pref.deleteBranch(prefName);
};
/**
* call a function whenever the named preference subtree changes
*/
GM_PrefManager.prototype.watch = function(prefName, watcher) {
// construct an observer
var observer = {
observe: function(subject, topic, prefName) { watcher(prefName); }
};
// store the observer in case we need to remove it later
this.observers[watcher] = observer;
this.pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal)
.addObserver(prefName, observer, false);
};
/**
* stop watching
*/
GM_PrefManager.prototype.unwatch = function(prefName, watcher) {
if (this.observers[watcher]) {
this.pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal)
.removeObserver(prefName, this.observers[watcher]);
}
};
var GM_prefRoot = new GM_PrefManager();
|