1 // Copyright (c) 2010-2011 by Kris Maglione <maglione.k@gmail.com>
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
6 // See https://wiki.mozilla.org/Extension_Manager:Bootstrapped_Extensions
9 const NAME = "bootstrap";
12 var { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
14 function module(uri) {
20 const { AddonManager } = module("resource://gre/modules/AddonManager.jsm");
21 const { XPCOMUtils } = module("resource://gre/modules/XPCOMUtils.jsm");
22 const { Services } = module("resource://gre/modules/Services.jsm");
24 const resourceProto = Services.io.getProtocolHandler("resource")
25 .QueryInterface(Ci.nsIResProtocolHandler);
26 const categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
27 const manager = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
29 const DISABLE_ACR = "resource://dactyl-content/disable-acr.jsm";
30 const BOOTSTRAP_JSM = "resource://dactyl/bootstrap.jsm";
31 const BOOTSTRAP_CONTRACT = "@dactyl.googlecode.com/base/bootstrap";
33 var JSMLoader = BOOTSTRAP_CONTRACT in Cc && Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
36 function reportError(e) {
37 dump("\n" + name + ": bootstrap: " + e + "\n" + (e.stack || Error().stack) + "\n");
41 dump(name + ": " + msg + "\n");
44 function httpGet(url) {
45 let xmlhttp = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
46 xmlhttp.overrideMimeType("text/plain");
47 xmlhttp.open("GET", url, false);
52 let initialized = false;
61 function updateLoader() {
63 JSMLoader.loader = Cc["@dactyl.googlecode.com/extra/utils"].getService(Ci.dactylIUtils);
69 * Performs necessary migrations after a version change.
71 function updateVersion() {
73 function isDev(ver) /^hg|pre$/.test(ver);
74 if (typeof require === "undefined" || addon === addonData)
77 require(global, "config");
78 require(global, "prefs");
79 config.lastVersion = localPrefs.get("lastVersion", null);
81 localPrefs.set("lastVersion", addon.version);
83 // We're switching from a nightly version to a stable or
84 // semi-stable version or vice versa.
86 // Disable automatic updates when switching to nightlies,
87 // restore the default action when switching to stable.
88 if (!config.lastVersion || isDev(config.lastVersion) != isDev(addon.version))
89 addon.applyBackgroundUpdates = AddonManager[isDev(addon.version) ? "AUTOUPDATE_DISABLE" : "AUTOUPDATE_DEFAULT"];
96 function startup(data, reason) {
97 debug("bootstrap: startup " + reasonToString(reason));
98 basePath = data.installPath;
103 debug("bootstrap: init" + " " + data.id);
107 name = data.id.replace(/@.*/, "");
108 AddonManager.getAddonByID(addon.id, function (a) {
113 if (typeof require !== "undefined")
114 require(global, "main");
117 if (basePath.isDirectory())
118 getURI = function getURI(path) {
119 let uri = Services.io.newFileURI(basePath);
121 return Services.io.newFileURI(uri.QueryInterface(Ci.nsIFileURL).file);
124 getURI = function getURI(path)
125 Services.io.newURI("jar:" + Services.io.newFileURI(basePath).spec.replace(/!/g, "%21") + "!" +
126 "/" + path, null, null);
138 * An XPCOM class factory proxy. Loads the JavaScript module at *url*
139 * when an instance is to be created and calls its NSGetFactory method
140 * to obtain the actual factory.
142 * @param {string} url The URL of the module housing the real factory.
143 * @param {string} classID The CID of the class this factory represents.
145 function FactoryProxy(url, classID) {
147 this.classID = Components.ID(classID);
149 FactoryProxy.prototype = {
150 QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory),
151 register: function () {
152 debug("bootstrap: register: " + this.classID + " " + this.contractID);
154 JSMLoader.registerFactory(this);
157 debug("bootstrap: create module: " + this.contractID);
159 Object.defineProperty(this, "module", { value: {}, enumerable: true });
160 JSMLoader.load(this.url, this.module);
163 createInstance: function (iids) {
164 return let (factory = this.module.NSGetFactory(this.classID))
165 factory.createInstance.apply(factory, arguments);
170 debug("bootstrap: init");
172 let manifestURI = getURI("chrome.manifest");
173 let manifest = httpGet(manifestURI.spec)
175 .replace(/^\s*|\s*$|#.*/g, "")
176 .replace(/^\s*\n/gm, "");
179 let chars = "0123456789abcdefghijklmnopqrstuv";
180 for (let n = Date.now(); n; n = Math.round(n / chars.length))
181 suffix += chars[n % chars.length];
183 for each (let line in manifest.split("\n")) {
184 let fields = line.split(/\s+/);
187 categoryManager.addCategoryEntry(fields[1], fields[2], fields[3], false, true);
188 categories.push([fields[1], fields[2]]);
191 components[fields[1]] = new FactoryProxy(getURI(fields[2]).spec, fields[1]);
194 components[fields[2]].contractID = fields[1];
198 resources.push(fields[1], fields[1] + suffix);
199 resourceProto.setSubstitution(fields[1], getURI(fields[2]));
200 resourceProto.setSubstitution(fields[1] + suffix, getURI(fields[2]));
204 // Flush the cache if necessary, just to be paranoid
205 let pref = "extensions.dactyl.cacheFlushCheck";
206 let val = addon.version;
207 if (!Services.prefs.prefHasUserValue(pref) || Services.prefs.getCharPref(pref) != val) {
208 var cacheFlush = true;
209 Services.obs.notifyObservers(null, "startupcache-invalidate", "");
210 Services.prefs.setCharPref(pref, val);
214 module(DISABLE_ACR).init(addon.id);
221 // Temporary hacks until platforms and dactyl releases that don't
222 // support Cu.unload are phased out.
224 // Upgrading from dactyl release without Cu.unload support.
225 Cu.unload(BOOTSTRAP_JSM);
226 for (let [name] in Iterator(JSMLoader.globals))
227 Cu.unload(~name.indexOf(":") ? name : "resource://dactyl" + JSMLoader.suffix + "/" + name);
229 else if (JSMLoader.bump != 6) {
230 // We're in a version without Cu.unload support and the
231 // JSMLoader interface has changed. Bump off the old one.
232 Services.scriptloader.loadSubScript("resource://dactyl" + suffix + "/bootstrap.jsm",
233 Cu.import(BOOTSTRAP_JSM, global));
237 if (!JSMLoader || JSMLoader.bump !== 6 || Cu.unload)
238 Cu.import(BOOTSTRAP_JSM, global);
240 JSMLoader.name = name;
241 JSMLoader.bootstrap = this;
243 JSMLoader.load(BOOTSTRAP_JSM, global);
245 JSMLoader.init(suffix);
246 JSMLoader.cacheFlush = cacheFlush;
247 JSMLoader.load("base.jsm", global);
249 if (!(BOOTSTRAP_CONTRACT in Cc)) {
250 // Use Sandbox to prevent closures over this scope
251 let sandbox = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].getService());
252 let factory = Cu.evalInSandbox("({ createInstance: function () this })", sandbox);
254 factory.classID = Components.ID("{f541c8b0-fe26-4621-a30b-e77d21721fb5}");
255 factory.contractID = BOOTSTRAP_CONTRACT;
256 factory.QueryInterface = XPCOMUtils.generateQI([Ci.nsIFactory]);
257 factory.wrappedJSObject = factory;
259 manager.registerFactory(factory.classID, String(factory.classID),
260 BOOTSTRAP_CONTRACT, factory);
263 Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader = !Cu.unload && JSMLoader;
265 for each (let component in components)
266 component.register();
268 Services.obs.notifyObservers(null, "dactyl-rehash", null);
272 if (addon !== addonData)
273 require(global, "main");
276 function shutdown(data, reason) {
277 debug("bootstrap: shutdown " + reasonToString(reason));
278 if (reason != APP_SHUTDOWN) {
280 module(DISABLE_ACR).cleanup();
282 Cu.unload(DISABLE_ACR);
288 if (~[ADDON_UPGRADE, ADDON_DOWNGRADE, ADDON_UNINSTALL].indexOf(reason))
289 Services.obs.notifyObservers(null, "dactyl-purge", null);
291 Services.obs.notifyObservers(null, "dactyl-cleanup", reasonToString(reason));
292 Services.obs.notifyObservers(null, "dactyl-cleanup-modules", reasonToString(reason));
295 for each (let [category, entry] in categories)
296 categoryManager.deleteCategoryEntry(category, entry, false);
297 for each (let resource in resources)
298 resourceProto.setSubstitution(resource, null);
302 function uninstall(data, reason) {
303 debug("bootstrap: uninstall " + reasonToString(reason));
304 if (reason == ADDON_UNINSTALL) {
305 Services.prefs.deleteBranch("extensions.dactyl.");
307 if (BOOTSTRAP_CONTRACT in Cc) {
308 let service = Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject;
309 manager.unregisterFactory(service.classID, service);
314 function reasonToString(reason) {
315 for each (let name in ["disable", "downgrade", "enable",
316 "install", "shutdown", "startup",
317 "uninstall", "upgrade"])
318 if (reason == global["ADDON_" + name.toUpperCase()] ||
319 reason == global["APP_" + name.toUpperCase()])
323 function install(data, reason) { debug("bootstrap: install " + reasonToString(reason)); }
325 // vim: set fdm=marker sw=4 ts=4 et: