forked from mozilla/adbhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.js
103 lines (85 loc) · 2.69 KB
/
bootstrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
const REASON = [ 'unknown', 'startup', 'shutdown', 'enable', 'disable',
'install', 'uninstall', 'upgrade', 'downgrade' ];
// Usefull piece of code from :bent
// http://mxr.mozilla.org/mozilla-central/source/dom/workers/test/extensions/bootstrap/bootstrap.js
function registerAddonResourceHandler(data) {
let file = data.installPath;
let fileuri = file.isDirectory() ?
Services.io.newFileURI(file) :
Services.io.newURI("jar:" + file.path + "!/", null, null);
let resourceName = encodeURIComponent(data.id.replace("@", "at"));
Services.io.getProtocolHandler("resource").
QueryInterface(Ci.nsIResProtocolHandler).
setSubstitution(resourceName, fileuri);
return "resource://" + resourceName + "/";
}
let mainModule;
let loader;
let unload;
function install(data, reason) {}
function startup(data, reason) {
let uri = registerAddonResourceHandler(data);
let loaderModule =
Cu.import('resource://gre/modules/commonjs/toolkit/loader.js').Loader;
let { Loader, Require, Main } = loaderModule;
unload = loaderModule.unload;
let loaderOptions = {
paths: {
"./": uri,
"": "resource://gre/modules/commonjs/"
},
modules: {
"toolkit/loader": loaderModule
}
};
/**
* setup a console object that only dumps messages if
* LOGPREF is true
*/
const LOGPREF = "[email protected]";
const LOGPREFIX = "ADB Addon Helper:";
try {
Services.prefs.getBoolPref(LOGPREF);
} catch(e) {
// Doesn't exist yet
Services.prefs.setBoolPref(LOGPREF, false);
}
function canLog() {
return Services.prefs.getBoolPref(LOGPREF);
}
const { ConsoleAPI } = Cu.import("resource://gre/modules/devtools/Console.jsm");
let _console = new ConsoleAPI();
loaderOptions.globals = {
console: {
log: function(...args) {
canLog() && _console.log(LOGPREFIX, ...args);
},
warn: function(...args) {
canLog() && _console.warn(LOGPREFIX, ...args);
},
error: function(...args) {
canLog() && _console.error(LOGPREFIX, ...args);
},
debug: function(...args) {
canLog() && _console.debug(LOGPREFIX, ...args);
}
}
}
loader = Loader(loaderOptions);
let require_ = Require(loader, { id: "./addon" });
mainModule = require_("./main");
}
function shutdown(data, reasonCode) {
let reason = REASON[reasonCode];
if (loader) {
unload(loader, reason);
unload = null;
}
if (mainModule && mainModule.shutdown) {
mainModule.shutdown();
}
}
function uninstall(data, reason) {}