]> git.donarmstrong.com Git - dactyl.git/blob - common/bootstrap.js
b17cba4c3be23af9c6d3e9a4a4d504dbeee81a78
[dactyl.git] / common / bootstrap.js
1 // Copyright (c) 2010-2011 by Kris Maglione <maglione.k@gmail.com>
2 //
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
5 //
6 // See https://wiki.mozilla.org/Extension_Manager:Bootstrapped_Extensions
7 // for details.
8
9 const NAME = "bootstrap";
10 const global = this;
11
12 const Cc = Components.classes;
13 const Ci = Components.interfaces;
14 const Cu = Components.utils;
15 const Cr = Components.results;
16
17 function module(uri) {
18     let obj = {};
19     Cu.import(uri, obj);
20     return obj;
21 }
22
23 const { AddonManager } = module("resource://gre/modules/AddonManager.jsm");
24 const { XPCOMUtils }   = module("resource://gre/modules/XPCOMUtils.jsm");
25 const { Services }     = module("resource://gre/modules/Services.jsm");
26
27 const resourceProto = Services.io.getProtocolHandler("resource")
28                               .QueryInterface(Ci.nsIResProtocolHandler);
29 const categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
30 const manager = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
31
32 const BOOTSTRAP_CONTRACT = "@dactyl.googlecode.com/base/bootstrap";
33 JSMLoader = JSMLoader || BOOTSTRAP_CONTRACT in Cc && Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
34
35 var JSMLoader = BOOTSTRAP_CONTRACT in Components.classes &&
36     Components.classes[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
37
38 // Temporary migration code.
39 if (!JSMLoader && "@mozilla.org/fuel/application;1" in Components.classes)
40     JSMLoader = Components.classes["@mozilla.org/fuel/application;1"]
41                           .getService(Components.interfaces.extIApplication)
42                           .storage.get("dactyl.JSMLoader", null);
43
44
45 function reportError(e) {
46     dump("\ndactyl: bootstrap: " + e + "\n" + (e.stack || Error().stack) + "\n");
47     Cu.reportError(e);
48 }
49
50 function httpGet(url) {
51     let xmlhttp = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
52     xmlhttp.overrideMimeType("text/plain");
53     xmlhttp.open("GET", url, false);
54     xmlhttp.send(null);
55     return xmlhttp;
56 }
57
58 let initialized = false;
59 let addon = null;
60 let addonData = null;
61 let basePath = null;
62 let categories = [];
63 let components = {};
64 let resources = [];
65 let getURI = null;
66
67 function updateVersion() {
68     try {
69         function isDev(ver) /^hg|pre$/.test(ver);
70         if (typeof require === "undefined" || addon === addonData)
71             return;
72
73         require(global, "config");
74         require(global, "prefs");
75         config.lastVersion = localPrefs.get("lastVersion", null);
76
77         localPrefs.set("lastVersion", addon.version);
78
79         if (!config.lastVersion || isDev(config.lastVersion) != isDev(addon.version))
80             addon.applyBackgroundUpdates = AddonManager[isDev(addon.version) ? "AUTOUPDATE_DISABLE" : "AUTOUPDATE_DEFAULT"];
81     }
82     catch (e) {
83         reportError(e);
84     }
85 }
86
87 function startup(data, reason) {
88     dump("dactyl: bootstrap: startup " + reasonToString(reason) + "\n");
89     basePath = data.installPath;
90
91     if (!initialized) {
92         initialized = true;
93
94         dump("dactyl: bootstrap: init" + " " + data.id + "\n");
95
96         addonData = data;
97         addon = data;
98         AddonManager.getAddonByID(addon.id, function (a) {
99             addon = a;
100             updateVersion();
101         });
102
103         if (basePath.isDirectory())
104             getURI = function getURI(path) {
105                 let uri = Services.io.newFileURI(basePath);
106                 uri.path += path;
107                 return Services.io.newFileURI(uri.QueryInterface(Ci.nsIFileURL).file);
108             };
109         else
110             getURI = function getURI(path)
111                 Services.io.newURI("jar:" + Services.io.newFileURI(basePath).spec + "!/" + path, null, null);
112
113         try {
114             init();
115         }
116         catch (e) {
117             reportError(e);
118         }
119     }
120 }
121
122 function FactoryProxy(url, classID) {
123     this.url = url;
124     this.classID = Components.ID(classID);
125 }
126 FactoryProxy.prototype = {
127     QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory),
128     register: function () {
129         dump("dactyl: bootstrap: register: " + this.classID + " " + this.contractID + "\n");
130
131         JSMLoader.registerFactory(this);
132     },
133     get module() {
134         dump("dactyl: bootstrap: create module: " + this.contractID + "\n");
135
136         Object.defineProperty(this, "module", { value: {}, enumerable: true });
137         JSMLoader.load(this.url, this.module);
138         return this.module;
139     },
140     createInstance: function (iids) {
141         return let (factory = this.module.NSGetFactory(this.classID))
142             factory.createInstance.apply(factory, arguments);
143     }
144 }
145
146 function init() {
147     dump("dactyl: bootstrap: init\n");
148
149     let manifestURI = getURI("chrome.manifest");
150     let manifest = httpGet(manifestURI.spec)
151             .responseText
152             .replace(/^\s*|\s*$|#.*/g, "")
153             .replace(/^\s*\n/gm, "");
154
155     let suffix = "-";
156     let chars = "0123456789abcdefghijklmnopqrstuv";
157     for (let n = Date.now(); n; n = Math.round(n / chars.length))
158         suffix += chars[n % chars.length];
159     suffix = "";
160
161     for each (let line in manifest.split("\n")) {
162         let fields = line.split(/\s+/);
163         switch(fields[0]) {
164         case "category":
165             categoryManager.addCategoryEntry(fields[1], fields[2], fields[3], false, true);
166             categories.push([fields[1], fields[2]]);
167             break;
168         case "component":
169             components[fields[1]] = new FactoryProxy(getURI(fields[2]).spec, fields[1]);
170             break;
171         case "contract":
172             components[fields[2]].contractID = fields[1];
173             break;
174
175         case "resource":
176             resources.push(fields[1], fields[1] + suffix);
177             resourceProto.setSubstitution(fields[1], getURI(fields[2]));
178             resourceProto.setSubstitution(fields[1] + suffix, getURI(fields[2]));
179         }
180     }
181
182     try {
183         module("resource://dactyl-content/disable-acr.jsm").init(addon.id);
184     }
185     catch (e) {
186         reportError(e);
187     }
188
189     if (JSMLoader && JSMLoader.bump !== 4) // Temporary hack
190         Services.scriptloader.loadSubScript("resource://dactyl" + suffix + "/bootstrap.jsm",
191             Cu.import("resource://dactyl/bootstrap.jsm", global));
192
193     if (!JSMLoader || JSMLoader.bump !== 4)
194         Cu.import("resource://dactyl/bootstrap.jsm", global);
195
196     JSMLoader.bootstrap = this;
197
198     JSMLoader.load("resource://dactyl/bootstrap.jsm", global);
199
200     JSMLoader.init(suffix);
201     JSMLoader.load("base.jsm", global);
202
203     if (!(BOOTSTRAP_CONTRACT in Cc))
204         manager.registerFactory(Components.ID("{f541c8b0-fe26-4621-a30b-e77d21721fb5}"),
205                                 String("{f541c8b0-fe26-4621-a30b-e77d21721fb5}"),
206                                 BOOTSTRAP_CONTRACT, {
207             QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]),
208             instance: {
209                 QueryInterface: XPCOMUtils.generateQI([]),
210                 contractID: BOOTSTRAP_CONTRACT,
211                 wrappedJSObject: {}
212             },
213             createInstance: function () this.instance
214         })
215
216     Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader = JSMLoader;
217
218     for each (let component in components)
219         component.register();
220
221     Services.obs.notifyObservers(null, "dactyl-rehash", null);
222     updateVersion();
223     require(global, "overlay");
224 }
225
226 function shutdown(data, reason) {
227     dump("dactyl: bootstrap: shutdown " + reasonToString(reason) + "\n");
228     if (reason != APP_SHUTDOWN) {
229         try {
230             module("resource://dactyl-content/disable-acr.jsm").cleanup();
231         }
232         catch (e) {
233             reportError(e);
234         }
235
236         if ([ADDON_UPGRADE, ADDON_DOWNGRADE, ADDON_UNINSTALL].indexOf(reason) >= 0)
237             Services.obs.notifyObservers(null, "dactyl-purge", null);
238
239         Services.obs.notifyObservers(null, "dactyl-cleanup", null);
240         Services.obs.notifyObservers(null, "dactyl-cleanup-modules", null);
241
242         JSMLoader.purge();
243         for each (let [category, entry] in categories)
244             categoryManager.deleteCategoryEntry(category, entry, false);
245         for each (let resource in resources)
246             resourceProto.setSubstitution(resource, null);
247     }
248 }
249
250 function reasonToString(reason) {
251     for each (let name in ["disable", "downgrade", "enable",
252                            "install", "shutdown", "startup",
253                            "uninstall", "upgrade"])
254         if (reason == global["ADDON_" + name.toUpperCase()] ||
255             reason == global["APP_" + name.toUpperCase()])
256             return name;
257 }
258
259 function install(data, reason) { dump("dactyl: bootstrap: install " + reasonToString(reason) + "\n"); }
260 function uninstall(data, reason) { dump("dactyl: bootstrap: uninstall " + reasonToString(reason) + "\n"); }
261
262 // vim: set fdm=marker sw=4 ts=4 et: