+let JSMLoader = {
+ SANDBOX: Cu.nukeSandbox && false,
+
+ get addon() addon,
+
+ currentModule: null,
+
+ factories: [],
+
+ get name() name,
+
+ get module() moduleName,
+
+ globals: {},
+ modules: {},
+
+ times: {
+ all: 0,
+ add: function add(major, minor, delta) {
+ this.all += delta;
+
+ this[major] = (this[major] || 0) + delta;
+ if (minor) {
+ minor = ":" + minor;
+ this[minor] = (this[minor] || 0) + delta;
+ this[major + minor] = (this[major + minor] || 0) + delta;
+ }
+ },
+ clear: function clear() {
+ for (let key in this)
+ if (typeof this[key] !== "number")
+ delete this[key];
+ }
+ },
+
+ getTarget: function getTarget(url) {
+ let uri = Services.io.newURI(url, null, null);
+ if (uri.schemeIs("resource"))
+ return resourceProto.resolveURI(uri);
+
+ let chan = Services.io.newChannelFromURI(uri);
+ try { chan.cancel(Cr.NS_BINDING_ABORTED); } catch (e) {}
+ return chan.name;
+ },
+
+ _atexit: [],
+
+ atexit: function atexit(arg, self) {
+ if (typeof arg !== "string")
+ this._atexit.push(arguments);
+ else
+ for each (let [fn, self] in this._atexit)
+ try {
+ fn.call(self, arg);
+ }
+ catch (e) {
+ reportError(e);
+ }
+ },
+
+ _load: function _load(name, target) {
+ let urls = [name];
+ if (name.indexOf(":") === -1)
+ urls = this.config["module-paths"].map(path => path + name + ".jsm");
+
+ for each (let url in urls)
+ try {
+ var uri = this.getTarget(url);
+ if (uri in this.globals)
+ return this.modules[name] = this.globals[uri];
+
+ this.globals[uri] = this.modules[name];
+ bootstrap_jsm.loadSubScript(url, this.modules[name]);
+ return;
+ }
+ catch (e) {
+ debug("Loading " + name + ": " + e);
+ delete this.globals[uri];
+
+ if (typeof e != "string")
+ throw e;
+ }
+
+ throw Error("No such module: " + name);
+ },
+
+ load: function load(name, target) {
+ if (!this.modules.hasOwnProperty(name)) {
+ this.modules[name] = this.modules.base ? bootstrap.create(this.modules.base)
+ : bootstrap.import({ JSMLoader: this, module: global.module });
+
+ let currentModule = this.currentModule;
+ this.currentModule = this.modules[name];
+
+ try {
+ this._load(name, this.modules[name]);
+ }
+ catch (e) {
+ delete this.modules[name];
+ reportError(e);
+ throw e;
+ }
+ finally {
+ this.currentModule = currentModule;
+ }
+ }
+
+ let module = this.modules[name];
+ if (target)
+ for each (let symbol in module.EXPORTED_SYMBOLS)
+ target[symbol] = module[symbol];
+
+ return module;
+ },
+
+ // Cuts down on stupid, fscking url mangling.
+ get loadSubScript() bootstrap_jsm.loadSubScript,
+
+ cleanup: function unregister() {
+ for each (let factory in this.factories.splice(0))
+ manager.unregisterFactory(factory.classID, factory);
+ },
+
+ Factory: function Factory(class_) ({
+ __proto__: class_.prototype,
+
+ createInstance: function (outer, iid) {
+ try {
+ if (outer != null)
+ throw Cr.NS_ERROR_NO_AGGREGATION;
+ if (!class_.instance)
+ class_.instance = new class_();
+ return class_.instance.QueryInterface(iid);
+ }
+ catch (e) {
+ Cu.reportError(e);
+ throw e;
+ }
+ }
+ }),
+
+ registerFactory: function registerFactory(factory) {
+ manager.registerFactory(factory.classID,
+ String(factory.classID),
+ factory.contractID,
+ factory);
+ this.factories.push(factory);
+ }
+};
+
+function init() {
+ debug("bootstrap: init");
+
+ let manifestURI = getURI("chrome.manifest");
+ let manifest = httpGet(manifestURI)
+ .responseText
+ .replace(/#(resource)#/g, "$1")
+ .replace(/^\s*|\s*$|#.*/g, "")
+ .replace(/^\s*\n/gm, "");
+
+ for each (let line in manifest.split("\n")) {
+ let fields = line.split(/\s+/);
+ switch (fields[0]) {
+ case "category":
+ categoryManager.addCategoryEntry(fields[1], fields[2], fields[3], false, true);
+ categories.push([fields[1], fields[2]]);
+ break;
+ case "component":
+ components[fields[1]] = new FactoryProxy(getURI(fields[2]).spec, fields[1]);
+ break;
+ case "contract":
+ components[fields[2]].contractID = fields[1];
+ break;
+
+ case "resource":
+ moduleName = moduleName || fields[1];
+ resources.push(fields[1]);
+ resourceProto.setSubstitution(fields[1], getURI(fields[2]));
+ }
+ }
+
+ JSMLoader.config = JSON.parse(httpGet("resource://dactyl-local/config.json").responseText);
+
+ bootstrap_jsm = module(BOOTSTRAP);
+ if (!JSMLoader.SANDBOX)
+ bootstrap = bootstrap_jsm;
+ else {
+ bootstrap = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].createInstance(),
+ { sandboxName: BOOTSTRAP });
+ Services.scriptloader.loadSubScript(BOOTSTRAP, bootstrap);
+ }
+ bootstrap.require = JSMLoader.load("base").require;
+
+ // Flush the cache if necessary, just to be paranoid
+ let pref = "extensions.dactyl.cacheFlushCheck";
+ let val = addon.version;
+ if (!Services.prefs.prefHasUserValue(pref) || Services.prefs.getCharPref(pref) != val) {
+ var cacheFlush = true;
+ Services.obs.notifyObservers(null, "startupcache-invalidate", "");
+ Services.prefs.setCharPref(pref, val);
+ }
+