2012-02-09 09:58:59 -05:00
|
|
|
var EXPORTED_SYMBOLS = [ "VersionChange" ];
|
2012-01-18 16:13:32 -05:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/AddonManager.jsm");
|
2012-08-03 09:53:36 -04:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-09-03 10:46:01 -04:00
|
|
|
Cu.import("resource://firetray/logging.jsm");
|
2012-01-18 16:13:32 -05:00
|
|
|
|
2012-09-02 19:38:04 -04:00
|
|
|
let log = firetray.Logger.getLogger("VersionChange");
|
2012-01-18 16:13:32 -05:00
|
|
|
|
|
|
|
/**
|
2012-01-30 15:10:26 -05:00
|
|
|
* handles version changes.
|
|
|
|
* http://mike.kaply.com/2011/02/02/running-add-on-code-at-first-run-and-upgrade/
|
2012-01-18 16:13:32 -05:00
|
|
|
*/
|
2012-02-09 09:58:59 -05:00
|
|
|
var VersionChange = {
|
|
|
|
|
2012-09-03 10:46:01 -04:00
|
|
|
initialized: false,
|
|
|
|
addonId: null,
|
2012-08-03 09:53:36 -04:00
|
|
|
addonVersion: null,
|
2012-09-03 10:46:01 -04:00
|
|
|
addOnPrefs: null,
|
|
|
|
|
|
|
|
init: function(id, version, prefBranch) {
|
2012-09-03 18:25:29 -04:00
|
|
|
log.debug("VersionChange got: id="+id+" ver="+version+" prefBranch="+prefBranch);
|
2012-09-03 10:46:01 -04:00
|
|
|
this.addOnId = id;
|
|
|
|
this.addonVersion = version;
|
|
|
|
this.addOnPrefs = Services.prefs.getBranch(prefBranch);
|
2012-08-03 09:53:36 -04:00
|
|
|
|
2012-09-03 10:46:01 -04:00
|
|
|
this.initialized = true;
|
2012-08-03 09:53:36 -04:00
|
|
|
},
|
2012-01-18 16:13:32 -05:00
|
|
|
|
2012-01-26 01:37:08 -05:00
|
|
|
versionComparator: Cc["@mozilla.org/xpcom/version-comparator;1"]
|
|
|
|
.getService(Ci.nsIVersionComparator),
|
|
|
|
|
2012-09-03 10:46:01 -04:00
|
|
|
applyHooksAndWatchUninstall: function() {
|
|
|
|
if (!this.initialized) throw "VersionChange not initialized";
|
|
|
|
this.onVersionChange(this.addonVersion); // AddonManager.getAddonByID() async, whereas we need sync call
|
2012-01-18 16:13:32 -05:00
|
|
|
AddonManager.addAddonListener(this.uninstallListener);
|
2012-09-02 19:38:04 -04:00
|
|
|
log.debug("version change watching enabled");
|
2012-01-18 16:13:32 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// we need to remove pref 'installedVersion' on uninstalling to be able to
|
|
|
|
// detect reinstall later
|
|
|
|
uninstallListener: {
|
|
|
|
onUninstalling: function(addon) {
|
2012-08-03 09:53:36 -04:00
|
|
|
if (addon.id !== this.addonId) return;
|
2012-09-03 10:46:01 -04:00
|
|
|
this.addOnPrefs.clearUserPref("installedVersion");
|
2012-01-18 16:13:32 -05:00
|
|
|
},
|
|
|
|
onOperationCancelled: function(addon) {
|
2012-08-03 09:53:36 -04:00
|
|
|
if (addon.id !== this.addonId) return;
|
2012-01-18 16:13:32 -05:00
|
|
|
let beingUninstalled = (addon.pendingOperations & AddonManager.PENDING_UNINSTALL) != 0;
|
|
|
|
if (beingUninstalled)
|
2012-09-03 10:46:01 -04:00
|
|
|
this.addOnPrefs.clearUserPref("installedVersion");
|
2012-01-18 16:13:32 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-03 09:53:36 -04:00
|
|
|
onVersionChange: function() {
|
2012-09-03 18:25:29 -04:00
|
|
|
log.debug("VERSION: "+this.addonVersion);
|
2012-01-18 16:13:32 -05:00
|
|
|
|
2012-09-03 10:46:01 -04:00
|
|
|
var firstrun = this.addOnPrefs.getBoolPref("firstrun");
|
2012-01-18 16:13:32 -05:00
|
|
|
|
|
|
|
if (firstrun) {
|
2012-09-02 19:38:04 -04:00
|
|
|
log.debug("FIRST RUN");
|
2012-01-29 17:32:40 -05:00
|
|
|
this.initPrefs();
|
2012-08-03 07:53:40 -04:00
|
|
|
this._applyHooks("install");
|
2012-01-18 16:13:32 -05:00
|
|
|
|
|
|
|
} else {
|
|
|
|
try {
|
2012-09-03 10:46:01 -04:00
|
|
|
var installedVersion = this.addOnPrefs.getCharPref("installedVersion");
|
2012-08-03 09:53:36 -04:00
|
|
|
var versionDelta = this.versionComparator.compare(this.addonVersion, installedVersion);
|
2012-01-18 16:13:32 -05:00
|
|
|
if (versionDelta > 0) {
|
2012-09-03 10:46:01 -04:00
|
|
|
this.addOnPrefs.setCharPref("installedVersion", this.addonVersion);
|
2012-09-02 19:38:04 -04:00
|
|
|
log.debug("UPGRADE");
|
2012-08-03 07:53:40 -04:00
|
|
|
this._applyHooks("upgrade");
|
2012-01-18 16:13:32 -05:00
|
|
|
}
|
2012-01-18 18:13:40 -05:00
|
|
|
|
2012-01-18 16:13:32 -05:00
|
|
|
} catch (ex) {
|
2012-09-02 19:38:04 -04:00
|
|
|
log.debug("REINSTALL");
|
2012-01-29 17:32:40 -05:00
|
|
|
this.initPrefs();
|
2012-08-03 07:53:40 -04:00
|
|
|
this._applyHooks("reinstall");
|
2012-01-18 16:13:32 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 09:53:36 -04:00
|
|
|
|
2012-01-26 01:37:08 -05:00
|
|
|
},
|
|
|
|
|
2012-01-29 17:32:40 -05:00
|
|
|
initPrefs: function() {
|
2012-09-03 10:46:01 -04:00
|
|
|
this.addOnPrefs.setBoolPref("firstrun", false);
|
|
|
|
this.addOnPrefs.setCharPref("installedVersion", VersionChange.addonVersion);
|
2012-01-26 01:37:08 -05:00
|
|
|
},
|
|
|
|
|
2012-08-03 09:53:36 -04:00
|
|
|
_hooks: [], // collection of callbacks {id: 1, categories: [], fun: function}
|
2012-08-03 07:53:40 -04:00
|
|
|
|
|
|
|
addHook: function(categories, fun) {
|
2012-08-03 09:53:36 -04:00
|
|
|
if (Object.prototype.toString.call(categories) !== "[object Array]") throw new TypeError();
|
2012-08-03 07:53:40 -04:00
|
|
|
let id = this._hooks.push({})-1;
|
|
|
|
this._hooks[id] = {id: id, categories: categories, fun: fun};
|
|
|
|
return id;
|
2012-01-26 01:37:08 -05:00
|
|
|
},
|
|
|
|
|
2012-08-03 07:53:40 -04:00
|
|
|
removeHook: function(id) {return this._hooks[id].splice(id-1, 1);},
|
|
|
|
removeCategoryFromHook: function(category, id) {
|
|
|
|
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
},
|
|
|
|
|
|
|
|
_applyHooks: function(category) {
|
2012-08-03 09:53:36 -04:00
|
|
|
try {
|
2012-09-03 18:25:29 -04:00
|
|
|
log.debug("_hooks.len="+this._hooks.length+" category="+category);
|
2012-08-03 09:53:36 -04:00
|
|
|
for (let i=0,len=this._hooks.length; i<len; ++i) {
|
|
|
|
let cb = this._hooks[i];
|
|
|
|
if (cb.categories.indexOf(category) > -1) cb.fun();
|
2012-09-03 18:25:29 -04:00
|
|
|
else log.debug("cb id="+cb.id+" not in category: "+cb.categories+"\n"+cb.fun);
|
2012-08-03 09:53:36 -04:00
|
|
|
}
|
2012-09-03 18:25:29 -04:00
|
|
|
} catch(x){log.error(x);}
|
2012-08-03 07:53:40 -04:00
|
|
|
}
|
2012-01-26 01:37:08 -05:00
|
|
|
|
|
|
|
};
|