forked from greasemonkey/greasemonkey
/
prefmanager.js
150 lines (130 loc) · 4.01 KB
/
prefmanager.js
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
var GM_prefRoot = new GM_PrefManager();
GM_PrefManager.MIN_INT_32 = -0x80000000;
GM_PrefManager.MAX_INT_32 = 0x7FFFFFFF;
/**
* 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) {
if (!startPoint) {
startPoint = "";
}
startPoint = "greasemonkey." + startPoint;
var pref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch(startPoint);
var observers = {};
const nsISupportsString = Components.interfaces.nsISupportsString;
/**
* whether a preference exists
*/
this.exists = function(prefName) {
return pref.getPrefType(prefName) != 0;
};
/**
* enumerate preferences
*/
this.listValues = function() {
return pref.getChildList("", {});
}
/**
* returns the named preference, or defaultValue if it does not exist
*/
this.getValue = function(prefName, defaultValue) {
var prefType = pref.getPrefType(prefName);
// underlying preferences object throws an exception if pref doesn't exist
if (prefType == pref.PREF_INVALID) {
return defaultValue;
}
try {
switch (prefType) {
case pref.PREF_STRING:
return pref.getComplexValue(prefName, nsISupportsString).data;
case pref.PREF_BOOL:
return pref.getBoolPref(prefName);
case pref.PREF_INT:
return pref.getIntPref(prefName);
}
} catch(ex) {
return defaultValue != undefined ? defaultValue : null;
}
return null;
};
/**
* sets the named preference to the specified value. values must be strings,
* booleans, or integers.
*/
this.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 >= GM_PrefManager.MIN_INT_32 &&
value <= GM_PrefManager.MAX_INT_32) {
goodType = true;
}
break;
}
if (!goodType) {
throw new Error("Unsupported type for GM_setValue. Supported types " +
"are: string, bool, and 32 bit integers.");
}
// 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(nsISupportsString);
str.data = value;
pref.setComplexValue(prefName, nsISupportsString, str);
break;
case "boolean":
pref.setBoolPref(prefName, value);
break;
case "number":
pref.setIntPref(prefName, Math.floor(value));
break;
}
};
/**
* deletes the named preference or subtree
*/
this.remove = function(prefName) {
pref.deleteBranch(prefName);
};
/**
* call a function whenever the named preference subtree changes
*/
this.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
observers[watcher] = observer;
pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal).
addObserver(prefName, observer, false);
};
/**
* stop watching
*/
this.unwatch = function(prefName, watcher) {
if (observers[watcher]) {
pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal).
removeObserver(prefName, observers[watcher]);
}
};
}