preference.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. const {Cc, Ci, Cu, CC} = require("chrome");
  5. const protocol = require("devtools/shared/protocol");
  6. const {Arg, method, RetVal} = protocol;
  7. const Services = require("Services");
  8. const {preferenceSpec} = require("devtools/shared/specs/preference");
  9. exports.register = function (handle) {
  10. handle.addGlobalActor(PreferenceActor, "preferenceActor");
  11. };
  12. exports.unregister = function (handle) {
  13. };
  14. var PreferenceActor = exports.PreferenceActor = protocol.ActorClassWithSpec(preferenceSpec, {
  15. typeName: "preference",
  16. getBoolPref: function (name) {
  17. return Services.prefs.getBoolPref(name);
  18. },
  19. getCharPref: function (name) {
  20. return Services.prefs.getCharPref(name);
  21. },
  22. getIntPref: function (name) {
  23. return Services.prefs.getIntPref(name);
  24. },
  25. getAllPrefs: function () {
  26. let prefs = {};
  27. Services.prefs.getChildList("").forEach(function (name, index) {
  28. // append all key/value pairs into a huge json object.
  29. try {
  30. let value;
  31. switch (Services.prefs.getPrefType(name)) {
  32. case Ci.nsIPrefBranch.PREF_STRING:
  33. value = Services.prefs.getCharPref(name);
  34. break;
  35. case Ci.nsIPrefBranch.PREF_INT:
  36. value = Services.prefs.getIntPref(name);
  37. break;
  38. case Ci.nsIPrefBranch.PREF_BOOL:
  39. value = Services.prefs.getBoolPref(name);
  40. break;
  41. default:
  42. }
  43. prefs[name] = {
  44. value: value,
  45. hasUserValue: Services.prefs.prefHasUserValue(name)
  46. };
  47. } catch (e) {
  48. // pref exists but has no user or default value
  49. }
  50. });
  51. return prefs;
  52. },
  53. setBoolPref: function (name, value) {
  54. Services.prefs.setBoolPref(name, value);
  55. Services.prefs.savePrefFile(null);
  56. },
  57. setCharPref: function (name, value) {
  58. Services.prefs.setCharPref(name, value);
  59. Services.prefs.savePrefFile(null);
  60. },
  61. setIntPref: function (name, value) {
  62. Services.prefs.setIntPref(name, value);
  63. Services.prefs.savePrefFile(null);
  64. },
  65. clearUserPref: function (name) {
  66. Services.prefs.clearUserPref(name);
  67. Services.prefs.savePrefFile(null);
  68. },
  69. });