1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-02-15 10:45:46 +01:00

Save option restore data until add-on is disabled. Cleanup preferences on uninstall.

This commit is contained in:
Kris Maglione
2011-07-29 05:29:06 -04:00
parent c165fd95be
commit a0de5d9e24
9 changed files with 180 additions and 130 deletions

View File

@@ -958,8 +958,9 @@ Class.prototype = {
func.superapply(self, Array.slice(arguments, 1));
}
}
try {
if ("value" in desc && i in this.localizedProperties)
if ("value" in desc && (k in this.localizedProperties || k in this.magicalProperties))
this[k] = desc.value;
else
Object.defineProperty(this, k, desc);
@@ -967,7 +968,9 @@ Class.prototype = {
catch (e) {}
}, this);
}
}
},
magicalProperties: {}
};
Class.makeClosure = function makeClosure() {
const self = this;

View File

@@ -700,7 +700,8 @@ var JavaScript = Module("javascript", {
modes.addMode("REPL", {
description: "JavaScript Read Eval Print Loop",
bases: [modes.COMMAND_LINE]
bases: [modes.COMMAND_LINE],
displayName: "REPL"
});
},
commandline: function initCommandLine(dactyl, modules, window) {

View File

@@ -78,6 +78,8 @@ var Option = Class("Option", {
this.globalValue = this.defaultValue;
},
magicalProperties: Set(["cleanupValue"]),
/**
* @property {string} This option's description, as shown in :listoptions.
*/
@@ -91,6 +93,13 @@ var Option = Class("Option", {
get isDefault() this.stringValue === this.stringDefaultValue,
/** @property {value} The value to reset this option to at cleanup time. */
get cleanupValue() options.cleanupPrefs.get(this.name),
set cleanupValue(value) {
if (options.cleanupPrefs.get(this.name) == null)
options.cleanupPrefs.set(this.name, value);
},
/** @property {value} The option's global value. @see #scope */
get globalValue() { try { return options.store.get(this.name, {}).value; } catch (e) { util.reportError(e); throw e; } },
set globalValue(val) { options.store.set(this.name, { value: val, time: Date.now() }); },
@@ -282,8 +291,6 @@ var Option = Class("Option", {
*/
scope: 1, // Option.SCOPE_GLOBAL // XXX set to BOTH by default someday? - kstep
cleanupValue: null,
/**
* @property {function(CompletionContext, Args)} This option's completer.
* @see CompletionContext
@@ -817,7 +824,7 @@ var Options = Module("options", {
cleanup: function cleanup() {
for (let opt in this)
if (opt.cleanupValue != null)
opt.value = opt.parse(opt.cleanupValue);
opt.stringValue = opt.cleanupValue;
},
/**
@@ -883,6 +890,13 @@ var Options = Module("options", {
setPref: deprecated("prefs.set", function setPref() prefs.set.apply(prefs, arguments)),
withContext: deprecated("prefs.withContext", function withContext() prefs.withContext.apply(prefs, arguments)),
cleanupPrefs: Class.memoize(function () localPrefs.Branch("cleanup.option.")),
cleanup: function cleanup(reason) {
if (~["disable", "uninstall"].indexOf(reason))
this.cleanupPrefs.resetBranch();
},
/**
* Returns the option with *name* in the specified *scope*.
*

View File

@@ -20,7 +20,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
RESTORE: "extensions.dactyl.restore.",
INIT: {},
init: function (branch, defaults) {
init: function init(branch, defaults) {
this._prefContexts = [];
this.branch = services.pref[defaults ? "getDefaultBranch" : "getBranch"](branch || "");
@@ -34,7 +34,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
this._observers = {};
},
cleanup: function cleanup() {
cleanup: function cleanup(reason) {
if (this.defaults != this)
this.defaults.cleanup();
this._observers = {};
@@ -43,8 +43,25 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
this.observe.unregister();
delete this.observe;
}
if (reason == "uninstall")
localPrefs.resetBranch();
},
/**
* Returns the full name of this object's preference branch.
*/
get root() this.branch.root,
/**
* Returns a new Prefs instance for the sub-branch *branch* of this
* branch.
*
* @param {string} branch The branch to branch to.
* @returns {Prefs}
*/
Branch: function Branch(branch) Prefs(this.root + branch),
observe: null,
observers: {
"nsPref:changed": function (subject, data) {
@@ -68,7 +85,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
* @param {function(object)} callback The callback, called with the
* new value of the preference whenever it changes.
*/
watch: function (pref, callback, strong) {
watch: function watch(pref, callback, strong) {
if (!this.observe) {
util.addObserver(this);
this.branch.addObserver("", this, false);
@@ -160,9 +177,9 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
* @param {string} branch The branch in which to search preferences.
* @default ""
*/
getNames: function (branch) this.branch.getChildList(branch || "", { value: 0 }),
getNames: function getNames(branch) this.branch.getChildList(branch || "", { value: 0 }),
_checkSafe: function (name, message, value) {
_checkSafe: function _checkSafe(name, message, value) {
let curval = this.get(name, null);
if (arguments.length > 2 && curval === value)
return;
@@ -184,7 +201,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
* @param {string} name The preference name.
* @param {value} value The new preference value.
*/
safeReset: function (name, message) {
safeReset: function safeReset(name, message) {
this._checkSafe(name, message);
this.reset(name);
this.reset(this.SAVED + name);
@@ -197,7 +214,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
* @param {string} name The preference name.
* @param {value} value The new preference value.
*/
safeSet: function (name, value, message, skipSave) {
safeSet: function safeSet(name, value, message, skipSave) {
this._checkSafe(name, message, value);
this.set(name, value);
this[skipSave ? "reset" : "set"](this.SAVED + name, value);
@@ -209,7 +226,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
* @param {string} name The preference name.
* @param {value} value The new preference value.
*/
set: function (name, value) {
set: function set(name, value) {
if (this._prefContexts.length)
this._prefContexts[this._prefContexts.length - 1][name] = this.get(name, null);
@@ -242,6 +259,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
else
throw FailedAssertion("Unknown preference type: " + typeof value + " (" + name + "=" + value + ")");
}
return value;
},
/**
@@ -250,7 +268,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
*
* @param {string} name The preference to save.
*/
save: function (name) {
save: function save(name) {
let val = this.get(name);
this.set(this.RESTORE + name, val);
this.safeSet(name, val);
@@ -262,7 +280,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
* @param {string} branch The branch from which to restore
* preferences. @optional
*/
restore: function (branch) {
restore: function restore(branch) {
this.getNames(this.RESTORE + (branch || "")).forEach(function (pref) {
this.safeSet(pref.substr(this.RESTORE.length), this.get(pref), null, true);
this.reset(pref);
@@ -274,19 +292,28 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
*
* @param {string} name The preference name.
*/
reset: function (name) {
reset: function reset(name) {
try {
this.branch.clearUserPref(name);
}
catch (e) {} // ignore - thrown if not a user set value
},
/**
* Resets the preference branch *branch* to its default value.
*
* @param {string} branch The preference name. @optional
*/
resetBranch: function resetBranch(branch) {
this.getNames(branch).forEach(this.closure.reset);
},
/**
* Toggles the value of the boolean preference *name*.
*
* @param {string} name The preference name.
*/
toggle: function (name) {
toggle: function toggle(name) {
util.assert(this.branch.getPrefType(name) === Ci.nsIPrefBranch.PREF_BOOL,
_("error.trailingCharacters", name + "!"));
this.set(name, !this.get(name));

View File

@@ -115,7 +115,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
*
* @param {object} obj
*/
addObserver: function (obj) {
addObserver: update(function addObserver(obj) {
if (!obj.observers)
obj.observers = obj.observe;
@@ -137,7 +137,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
}
catch (e) {
if (typeof util === "undefined")
dump("dactyl: error: " + e + "\n" + (e.stack || Error().stack).replace(/^/gm, "dactyl: "));
addObserver.dump("dactyl: error: " + e + "\n" + (e.stack || addObserver.Error().stack).replace(/^/gm, "dactyl: "));
else
util.reportError(e);
}
@@ -145,7 +145,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
obj.observe.unregister = function () register("removeObserver");
register("addObserver");
},
}, { dump: dump, Error: Error }),
/*
* Tests a condition and throws a FailedAssertion error on
@@ -1226,13 +1226,13 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
},
observers: {
"dactyl-cleanup-modules": function () {
defineModule.loadLog.push("dactyl: util: observe: dactyl-cleanup-modules");
"dactyl-cleanup-modules": function (subject, reason) {
defineModule.loadLog.push("dactyl: util: observe: dactyl-cleanup-modules " + reason);
for (let module in values(defineModule.modules))
if (module.cleanup) {
util.dump("cleanup: " + module.constructor.className);
util.trapErrors(module.cleanup, module);
util.trapErrors(module.cleanup, module, reason);
}
JSMLoader.cleanup();
@@ -1256,6 +1256,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
"dactyl-purge": function () {
this.rehashing = 1;
},
"toplevel-window-ready": function (window, data) {
window.addEventListener("DOMContentLoaded", wrapCallback(function listener(event) {
if (event.originalTarget === window.document) {