| 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 |
const EXPORTED_SYMBOLS = [ |
|---|
| 38 |
'get', |
|---|
| 39 |
'getExt', |
|---|
| 40 |
'set', |
|---|
| 41 |
'setExt', |
|---|
| 42 |
'hasUserValue', |
|---|
| 43 |
'hasUserValueExt', |
|---|
| 44 |
'getChildren', |
|---|
| 45 |
'getChildrenExt', |
|---|
| 46 |
'reset', |
|---|
| 47 |
'resetExt', |
|---|
| 48 |
'resetBranch', |
|---|
| 49 |
'resetBranchExt', |
|---|
| 50 |
'resetAllExt', |
|---|
| 51 |
'addObserver', |
|---|
| 52 |
'removeObserver', |
|---|
| 53 |
'makeObserver' |
|---|
| 54 |
]; |
|---|
| 55 |
|
|---|
| 56 |
const EXT = 'extensions.dta.'; |
|---|
| 57 |
|
|---|
| 58 |
const Cc = Components.classes; |
|---|
| 59 |
const Ci = Components.interfaces; |
|---|
| 60 |
const nsIPrefBranch = Ci.nsIPrefBranch; |
|---|
| 61 |
const nsIPrefBranch2 = Ci.nsIPrefBranch2; |
|---|
| 62 |
|
|---|
| 63 |
const PREF_STRING = nsIPrefBranch.PREF_STRING; |
|---|
| 64 |
const PREF_INT = nsIPrefBranch.PREF_INT; |
|---|
| 65 |
const PREF_BOOL = nsIPrefBranch.PREF_BOOL; |
|---|
| 66 |
|
|---|
| 67 |
const SupportsString = Components.Constructor('@mozilla.org/supports-string;1', 'nsISupportsString'); |
|---|
| 68 |
|
|---|
| 69 |
const prefs = Cc['@mozilla.org/preferences-service;1'].getService(Ci.nsIPrefBranch); |
|---|
| 70 |
|
|---|
| 71 |
function get(key, defaultValue){ |
|---|
| 72 |
try { |
|---|
| 73 |
let rv; |
|---|
| 74 |
switch (prefs.getPrefType(key)) { |
|---|
| 75 |
case PREF_INT: |
|---|
| 76 |
rv = prefs.getIntPref(key); |
|---|
| 77 |
break; |
|---|
| 78 |
case PREF_BOOL: |
|---|
| 79 |
rv = prefs.getBoolPref(key); |
|---|
| 80 |
break; |
|---|
| 81 |
default: |
|---|
| 82 |
rv = getMultiByte(key); |
|---|
| 83 |
break; |
|---|
| 84 |
} |
|---|
| 85 |
if (rv != undefined) { |
|---|
| 86 |
return rv; |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
catch (ex) { |
|---|
| 90 |
|
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
return defaultValue; |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
function getExt(key, defaultValue) { |
|---|
| 97 |
return get(EXT + key, defaultValue); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
function set(key, value){ |
|---|
| 101 |
if (typeof value == 'number' || value instanceof Number) { |
|---|
| 102 |
return prefs.setIntPref(key, value); |
|---|
| 103 |
} |
|---|
| 104 |
if (typeof value == 'boolean' || value instanceof Boolean) { |
|---|
| 105 |
return prefs.setBoolPref(key, value); |
|---|
| 106 |
} |
|---|
| 107 |
return setMultiByte(key, value); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
function setExt(key, value){ |
|---|
| 111 |
return set(EXT + key, value); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
function getMultiByte(key, defaultValue){ |
|---|
| 115 |
try { |
|---|
| 116 |
return prefs.getComplexValue(key, Ci.nsISupportsString).data; |
|---|
| 117 |
} |
|---|
| 118 |
catch (ex) { |
|---|
| 119 |
|
|---|
| 120 |
} |
|---|
| 121 |
return defaultValue; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
function setMultiByte(key, value) { |
|---|
| 125 |
let str = new SupportsString(); |
|---|
| 126 |
str.data = value.toString(); |
|---|
| 127 |
prefs.setComplexValue(key, Ci.nsISupportsString, str); |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
function hasUserValue(key) { |
|---|
| 131 |
try { |
|---|
| 132 |
return prefs.prefHasUserValue(key); |
|---|
| 133 |
} |
|---|
| 134 |
catch (ex) { |
|---|
| 135 |
|
|---|
| 136 |
} |
|---|
| 137 |
return false; |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
function hasUserValueExt(key) { |
|---|
| 141 |
return hasUserValue(EXT + key); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
function getChildren(key) { |
|---|
| 145 |
return prefs.getChildList(key, {}); |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
function getChildrenExt(key) { |
|---|
| 149 |
return getChildren(EXT + key); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
function reset(key) { |
|---|
| 153 |
try { |
|---|
| 154 |
return prefs.clearUserPref(key); |
|---|
| 155 |
} |
|---|
| 156 |
catch (ex) { |
|---|
| 157 |
|
|---|
| 158 |
} |
|---|
| 159 |
return false; |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
function resetExt(key) { |
|---|
| 164 |
if (key.search(new RegExp('/^' + EXT + '/')) != 0) { |
|---|
| 165 |
key = EXT + key; |
|---|
| 166 |
} |
|---|
| 167 |
return reset(key); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
function resetBranch(branch) { |
|---|
| 171 |
try { |
|---|
| 172 |
prefs.resetBranch(branch); |
|---|
| 173 |
} |
|---|
| 174 |
catch (ex) { |
|---|
| 175 |
|
|---|
| 176 |
let children = prefs.getChildList(branch, {}); |
|---|
| 177 |
for each (let key in children) { |
|---|
| 178 |
reset(key); |
|---|
| 179 |
} |
|---|
| 180 |
} |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
function resetBranchExt(branch) { |
|---|
| 184 |
resetBranch('extension.dta.' + branch); |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
function resetAllExt() { |
|---|
| 188 |
resetBranchExt(''); |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
function addObserver(branch, obj) { |
|---|
| 192 |
makeObserver(obj); |
|---|
| 193 |
prefs.QueryInterface(nsIPrefBranch2).addObserver(branch, obj, true); |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
function removeObserver(branch, obj) { |
|---|
| 197 |
prefs.QueryInterface(nsIPrefBranch2).removeObserver(branch, obj); |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
function makeObserver(obj) { |
|---|
| 201 |
try { |
|---|
| 202 |
if ( |
|---|
| 203 |
obj.QueryInterface(Ci.nsISupportsWeakReference) |
|---|
| 204 |
&& obj.QueryInterface(Ci.nsIObserver) |
|---|
| 205 |
) { |
|---|
| 206 |
return; |
|---|
| 207 |
} |
|---|
| 208 |
} |
|---|
| 209 |
catch (ex) { |
|---|
| 210 |
|
|---|
| 211 |
} |
|---|
| 212 |
let __QueryInterface = obj.QueryInterface; |
|---|
| 213 |
obj.QueryInterface = function(iid) { |
|---|
| 214 |
try { |
|---|
| 215 |
if ( |
|---|
| 216 |
iid.equals(Components.interfaces.nsISupports) |
|---|
| 217 |
|| iid.equals(Components.interfaces.nsISupportsWeakReference) |
|---|
| 218 |
|| iid.equals(Components.interfaces.nsIWeakReference) |
|---|
| 219 |
|| iid.equals(Components.interfaces.nsIObserver) |
|---|
| 220 |
) { |
|---|
| 221 |
return obj; |
|---|
| 222 |
} |
|---|
| 223 |
if (__QueryInterface) { |
|---|
| 224 |
debug("calling original: " + iid); |
|---|
| 225 |
return __QueryInterface.call(this, iid); |
|---|
| 226 |
} |
|---|
| 227 |
throw Components.results.NS_ERROR_NO_INTERFACE; |
|---|
| 228 |
} |
|---|
| 229 |
catch (ex) { |
|---|
| 230 |
debug("requested interface not available: " + iid); |
|---|
| 231 |
throw ex; |
|---|
| 232 |
} |
|---|
| 233 |
}; |
|---|
| 234 |
|
|---|
| 235 |
obj.QueryReferent = function(iid) { |
|---|
| 236 |
return obj.QueryInterface(iid); |
|---|
| 237 |
}; |
|---|
| 238 |
|
|---|
| 239 |
obj.GetWeakReference = function() { |
|---|
| 240 |
return obj; |
|---|
| 241 |
}; |
|---|
| 242 |
} |
|---|