service.js 5.2 KB

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