Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | module.metadata = { |
michael@0 | 8 | "stability": "unstable" |
michael@0 | 9 | }; |
michael@0 | 10 | |
michael@0 | 11 | // The minimum and maximum integers that can be set as preferences. |
michael@0 | 12 | // The range of valid values is narrower than the range of valid JS values |
michael@0 | 13 | // because the native preferences code treats integers as NSPR PRInt32s, |
michael@0 | 14 | // which are 32-bit signed integers on all platforms. |
michael@0 | 15 | const MAX_INT = 0x7FFFFFFF; |
michael@0 | 16 | const MIN_INT = -0x80000000; |
michael@0 | 17 | |
michael@0 | 18 | const {Cc,Ci,Cr} = require("chrome"); |
michael@0 | 19 | |
michael@0 | 20 | const prefService = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 21 | getService(Ci.nsIPrefService); |
michael@0 | 22 | const prefSvc = prefService.getBranch(null); |
michael@0 | 23 | const defaultBranch = prefService.getDefaultBranch(null); |
michael@0 | 24 | |
michael@0 | 25 | function Branch(branchName) { |
michael@0 | 26 | function getPrefKeys() { |
michael@0 | 27 | return keys(branchName).map(function(key) { |
michael@0 | 28 | return key.replace(branchName, ""); |
michael@0 | 29 | }); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | return Proxy.create({ |
michael@0 | 33 | get: function(receiver, pref) { |
michael@0 | 34 | return get(branchName + pref); |
michael@0 | 35 | }, |
michael@0 | 36 | set: function(receiver, pref, val) { |
michael@0 | 37 | set(branchName + pref, val); |
michael@0 | 38 | }, |
michael@0 | 39 | delete: function(pref) { |
michael@0 | 40 | reset(branchName + pref); |
michael@0 | 41 | return true; |
michael@0 | 42 | }, |
michael@0 | 43 | has: function hasPrefKey(pref) { |
michael@0 | 44 | return has(branchName + pref) |
michael@0 | 45 | }, |
michael@0 | 46 | getPropertyDescriptor: function(name) { |
michael@0 | 47 | return { |
michael@0 | 48 | value: get(branchName + name) |
michael@0 | 49 | }; |
michael@0 | 50 | }, |
michael@0 | 51 | enumerate: getPrefKeys, |
michael@0 | 52 | keys: getPrefKeys |
michael@0 | 53 | }, Branch.prototype); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | function get(name, defaultValue) { |
michael@0 | 57 | switch (prefSvc.getPrefType(name)) { |
michael@0 | 58 | case Ci.nsIPrefBranch.PREF_STRING: |
michael@0 | 59 | return prefSvc.getComplexValue(name, Ci.nsISupportsString).data; |
michael@0 | 60 | |
michael@0 | 61 | case Ci.nsIPrefBranch.PREF_INT: |
michael@0 | 62 | return prefSvc.getIntPref(name); |
michael@0 | 63 | |
michael@0 | 64 | case Ci.nsIPrefBranch.PREF_BOOL: |
michael@0 | 65 | return prefSvc.getBoolPref(name); |
michael@0 | 66 | |
michael@0 | 67 | case Ci.nsIPrefBranch.PREF_INVALID: |
michael@0 | 68 | return defaultValue; |
michael@0 | 69 | |
michael@0 | 70 | default: |
michael@0 | 71 | // This should never happen. |
michael@0 | 72 | throw new Error("Error getting pref " + name + |
michael@0 | 73 | "; its value's type is " + |
michael@0 | 74 | prefSvc.getPrefType(name) + |
michael@0 | 75 | ", which I don't know " + |
michael@0 | 76 | "how to handle."); |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | exports.get = get; |
michael@0 | 80 | |
michael@0 | 81 | function set(name, value) { |
michael@0 | 82 | var prefType; |
michael@0 | 83 | if (typeof value != "undefined" && value != null) |
michael@0 | 84 | prefType = value.constructor.name; |
michael@0 | 85 | |
michael@0 | 86 | switch (prefType) { |
michael@0 | 87 | case "String": |
michael@0 | 88 | { |
michael@0 | 89 | var string = Cc["@mozilla.org/supports-string;1"]. |
michael@0 | 90 | createInstance(Ci.nsISupportsString); |
michael@0 | 91 | string.data = value; |
michael@0 | 92 | prefSvc.setComplexValue(name, Ci.nsISupportsString, string); |
michael@0 | 93 | } |
michael@0 | 94 | break; |
michael@0 | 95 | |
michael@0 | 96 | case "Number": |
michael@0 | 97 | // We throw if the number is outside the range or not an integer, since |
michael@0 | 98 | // the result will not be what the consumer wanted to store. |
michael@0 | 99 | if (value > MAX_INT || value < MIN_INT) |
michael@0 | 100 | throw new Error("you cannot set the " + name + |
michael@0 | 101 | " pref to the number " + value + |
michael@0 | 102 | ", as number pref values must be in the signed " + |
michael@0 | 103 | "32-bit integer range -(2^31) to 2^31-1. " + |
michael@0 | 104 | "To store numbers outside that range, store " + |
michael@0 | 105 | "them as strings."); |
michael@0 | 106 | if (value % 1 != 0) |
michael@0 | 107 | throw new Error("cannot store non-integer number: " + value); |
michael@0 | 108 | prefSvc.setIntPref(name, value); |
michael@0 | 109 | break; |
michael@0 | 110 | |
michael@0 | 111 | case "Boolean": |
michael@0 | 112 | prefSvc.setBoolPref(name, value); |
michael@0 | 113 | break; |
michael@0 | 114 | |
michael@0 | 115 | default: |
michael@0 | 116 | throw new Error("can't set pref " + name + " to value '" + value + |
michael@0 | 117 | "'; it isn't a string, integer, or boolean"); |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | exports.set = set; |
michael@0 | 121 | |
michael@0 | 122 | function has(name) { |
michael@0 | 123 | return (prefSvc.getPrefType(name) != Ci.nsIPrefBranch.PREF_INVALID); |
michael@0 | 124 | } |
michael@0 | 125 | exports.has = has; |
michael@0 | 126 | |
michael@0 | 127 | function keys(root) { |
michael@0 | 128 | return prefSvc.getChildList(root); |
michael@0 | 129 | } |
michael@0 | 130 | exports.keys = keys; |
michael@0 | 131 | |
michael@0 | 132 | function isSet(name) { |
michael@0 | 133 | return (has(name) && prefSvc.prefHasUserValue(name)); |
michael@0 | 134 | } |
michael@0 | 135 | exports.isSet = isSet; |
michael@0 | 136 | |
michael@0 | 137 | function reset(name) { |
michael@0 | 138 | try { |
michael@0 | 139 | prefSvc.clearUserPref(name); |
michael@0 | 140 | } |
michael@0 | 141 | catch (e) { |
michael@0 | 142 | // The pref service throws NS_ERROR_UNEXPECTED when the caller tries |
michael@0 | 143 | // to reset a pref that doesn't exist or is already set to its default |
michael@0 | 144 | // value. This interface fails silently in those cases, so callers |
michael@0 | 145 | // can unconditionally reset a pref without having to check if it needs |
michael@0 | 146 | // resetting first or trap exceptions after the fact. It passes through |
michael@0 | 147 | // other exceptions, however, so callers know about them, since we don't |
michael@0 | 148 | // know what other exceptions might be thrown and what they might mean. |
michael@0 | 149 | if (e.result != Cr.NS_ERROR_UNEXPECTED) { |
michael@0 | 150 | throw e; |
michael@0 | 151 | } |
michael@0 | 152 | } |
michael@0 | 153 | } |
michael@0 | 154 | exports.reset = reset; |
michael@0 | 155 | |
michael@0 | 156 | function getLocalized(name, defaultValue) { |
michael@0 | 157 | let value = null; |
michael@0 | 158 | try { |
michael@0 | 159 | value = prefSvc.getComplexValue(name, Ci.nsIPrefLocalizedString).data; |
michael@0 | 160 | } |
michael@0 | 161 | finally { |
michael@0 | 162 | return value || defaultValue; |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | exports.getLocalized = getLocalized; |
michael@0 | 166 | |
michael@0 | 167 | function setLocalized(name, value) { |
michael@0 | 168 | // We can't use `prefs.set` here as we have to use `getDefaultBranch` |
michael@0 | 169 | // (instead of `getBranch`) in order to have `mIsDefault` set to true, here: |
michael@0 | 170 | // http://mxr.mozilla.org/mozilla-central/source/modules/libpref/src/nsPrefBranch.cpp#233 |
michael@0 | 171 | // Otherwise, we do not enter into this expected condition: |
michael@0 | 172 | // http://mxr.mozilla.org/mozilla-central/source/modules/libpref/src/nsPrefBranch.cpp#244 |
michael@0 | 173 | defaultBranch.setCharPref(name, value); |
michael@0 | 174 | } |
michael@0 | 175 | exports.setLocalized = setLocalized; |
michael@0 | 176 | |
michael@0 | 177 | exports.Branch = Branch; |
michael@0 | 178 |