forked from adblockplus/buildtools
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap.js.tmpl
189 lines (171 loc) · 5.04 KB
/
bootstrap.js.tmpl
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
let {Services, atob, btoa, File, TextDecoder, TextEncoder} = Cu.import("resource://gre/modules/Services.jsm", null);
{%- if hasXMLHttpRequest %}
let XMLHttpRequest = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1", "nsIXMLHttpRequest");
{%- endif %}
let addonData = null;
function startup(params, reason)
{
addonData = params;
{%- if hasChromeRequires %}
Services.obs.addObserver(RequireObserver, "{{metadata.get('general', 'basename')}}-require", true);
onShutdown.add(function() Services.obs.removeObserver(RequireObserver, "{{metadata.get('general', 'basename')}}-require"));
{%- set hasShutdownHandlers = True %}
{%- endif %}
require("main");
}
function shutdown(params, reason)
{
{%- if chromeWindows %}
let windowNames = {{chromeWindows|json}};
for (let i = 0; i < windowNames.length; i++)
{
let enumerator = Services.wm.getEnumerator(windowNames[i]);
while (enumerator.hasMoreElements())
{
let window = enumerator.getNext().QueryInterface(Ci.nsIDOMWindow);
window.setTimeout("window.close()", 0); // Closing immediately might not work due to modal windows
try
{
window.close();
} catch(e) {}
}
}
{%- endif %}
{%- if hasShutdownHandlers %}
onShutdown.done = true;
for (let i = shutdownHandlers.length - 1; i >= 0; i --)
{
try
{
shutdownHandlers[i]();
}
catch (e)
{
Cu.reportError(e);
}
}
shutdownHandlers = null;
{%- endif %}
// Make sure to release our ties to the modules even if the sandbox cannot be
// released for some reason.
for (let key in require.scopes)
{
let scope = require.scopes[key];
let list = Object.keys(scope);
for (let i = 0; i < list.length; i++)
scope[list[i]] = null;
}
require.scopes = null;
addonData = null;
}
function install(params, reason) {}
function uninstall(params, reason)
{
{%- if hasVersionPref %}
const ADDON_UNINSTALL = 6; // https://developer.mozilla.org/en/Extensions/Bootstrapped_extensions#Reason_constants
if (reason == ADDON_UNINSTALL)
{
// Users often uninstall/reinstall extension to "fix" issues. Clear current
// version number on uninstall to rerun first-run actions in this scenario.
Services.prefs.clearUserPref("extensions.{{metadata.get('general', 'basename')}}.currentVersion");
}
{%- endif %}
}
{%- if hasShutdownHandlers %}
let shutdownHandlers = [];
let onShutdown =
{
done: false,
add: function(handler)
{
if (shutdownHandlers.indexOf(handler) < 0)
shutdownHandlers.push(handler);
},
remove: function(handler)
{
let index = shutdownHandlers.indexOf(handler);
if (index >= 0)
shutdownHandlers.splice(index, 1);
}
};
{%- endif %}
function require(module)
{
let scopes = require.scopes;
if (!(module in scopes))
{
{%- if 'info' in requires %}
if (module == "info")
{
let applications = {{applications|json}};
let appInfo = Services.appinfo;
scopes[module] = {};
scopes[module].exports =
{
addonID: addonData.id,
addonVersion: addonData.version,
addonRoot: addonData.resourceURI.spec,
addonName: "{{metadata.get('general', 'basename')}}",
application: (appInfo.ID in applications ? applications[appInfo.ID] : "other"),
applicationVersion: appInfo.version,
platform: "gecko",
platformVersion: appInfo.platformVersion
};
}
else
{
{%- endif %}
let url = addonData.resourceURI.spec + "lib/" + module + ".js";
scopes[module] = {
Cc: Cc,
Ci: Ci,
Cr: Cr,
Cu: Cu,
atob: atob,
btoa: btoa,
File: File,
require: require,
{% if hasShutdownHandlers %}
onShutdown: onShutdown,
{% endif %}
{%- if hasXMLHttpRequest %}
XMLHttpRequest: XMLHttpRequest,
{% endif %}
exports: {}};
{%- if multicompartment %}
let principal = Cc["@mozilla.org/systemprincipal;1"].getService(Ci.nsIPrincipal);
scopes[module] = new Cu.Sandbox(principal, {
sandboxName: url,
sandboxPrototype: scopes[module],
wantXrays: false
});
{%- endif %}
Services.scriptloader.loadSubScript(url, scopes[module]);
{%- if 'info' in requires %}
}
{%- endif %}
}
return scopes[module].exports;
}
require.scopes = {__proto__: null};
{%- if hasChromeRequires %}
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
let RequireObserver =
{
observe: function(subject, topic, data)
{
if (topic == "{{metadata.get('general', 'basename')}}-require")
{
subject.wrappedJSObject.exports = require(data);
}
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObserver])
};
{%- endif %}