This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
/
bootstrap.js
210 lines (181 loc) · 6.83 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* 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/. */
"use strict";
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyGlobalGetters(this, ["fetch"]);
ChromeUtils.defineModuleGetter(this, "Services",
"resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "resProto",
"@mozilla.org/network/protocol;1?name=resource",
"nsISubstitutingProtocolHandler");
const RESOURCE_HOST = "activity-stream";
const BROWSER_READY_NOTIFICATION = "sessionstore-windows-restored";
const RESOURCE_BASE = "resource://activity-stream";
const ACTIVITY_STREAM_OPTIONS = {newTabURL: "about:newtab"};
let activityStream;
let modulesToUnload = new Set();
let startupData;
let startupReason;
let waitingForBrowserReady = true;
// Lazily load ActivityStream then find related modules to unload
XPCOMUtils.defineLazyModuleGetter(this, "ActivityStream",
"resource://activity-stream/lib/ActivityStream.jsm", null, null, () => {
// Helper to fetch a resource directory listing and call back with each item
const processListing = async (uri, cb) => {
try {
(await (await fetch(uri)).text())
.split("\n").slice(2).forEach(line => cb(line.split(" ").slice(1)));
} catch (e) {
// Silently ignore listings that fail to load
// probably because the resource: has been unloaded
}
};
// Look for modules one level deeper than the top resource URI
processListing(RESOURCE_BASE, ([directory, , , type]) => {
if (type === "DIRECTORY") {
// Look into this directory for .jsm files
const subDir = `${RESOURCE_BASE}/${directory}`;
processListing(subDir, ([name]) => {
if (name && name.search(/\.jsm$/) !== -1) {
modulesToUnload.add(`${subDir}/${name}`);
}
});
}
});
});
/**
* init - Initializes an instance of ActivityStream. This could be called by
* the startup() function exposed by bootstrap.js.
*
* @param {string} reason - Reason for initialization. Could be install, upgrade, or PREF_ON
*/
function init(reason) {
// Don't re-initialize
if (activityStream && activityStream.initialized) {
return;
}
const options = Object.assign({}, startupData || {}, ACTIVITY_STREAM_OPTIONS);
activityStream = new ActivityStream(options);
try {
activityStream.init(reason);
} catch (e) {
Cu.reportError(e);
}
}
/**
* uninit - Uninitializes the activityStream instance, if it exsits.This could be
* called by the shutdown() function exposed by bootstrap.js.
*
* @param {type} reason Reason for uninitialization. Could be uninstall, upgrade, or PREF_OFF
*/
function uninit(reason) {
// Make sure to only uninit once in case both pref change and shutdown happen
if (activityStream) {
activityStream.uninit(reason);
activityStream = null;
}
}
/**
* Check if an old pref has a custom value to migrate. Clears the pref so that
* it's the default after migrating (to avoid future need to migrate).
*
* @param oldPrefName {string} Pref to check and migrate
* @param cbIfNotDefault {function} Callback that gets the current pref value
*/
function migratePref(oldPrefName, cbIfNotDefault) {
// Nothing to do if the user doesn't have a custom value
if (!Services.prefs.prefHasUserValue(oldPrefName)) {
return;
}
// Figure out what kind of pref getter to use
let prefGetter;
switch (Services.prefs.getPrefType(oldPrefName)) {
case Services.prefs.PREF_BOOL:
prefGetter = "getBoolPref";
break;
case Services.prefs.PREF_INT:
prefGetter = "getIntPref";
break;
case Services.prefs.PREF_STRING:
prefGetter = "getStringPref";
break;
}
// Give the callback the current value then clear the pref
cbIfNotDefault(Services.prefs[prefGetter](oldPrefName));
Services.prefs.clearUserPref(oldPrefName);
}
/**
* onBrowserReady - Continues startup of the add-on after browser is ready.
*/
function onBrowserReady() {
waitingForBrowserReady = false;
init(startupReason);
// Do a one time migration of Tiles about:newtab prefs that have been modified
migratePref("browser.newtabpage.rows", rows => {
// Just disable top sites if rows are not desired
if (rows <= 0) {
Services.prefs.setBoolPref("browser.newtabpage.activity-stream.feeds.topsites", false);
} else {
Services.prefs.setIntPref("browser.newtabpage.activity-stream.topSitesRows", rows);
}
});
migratePref("browser.newtabpage.activity-stream.showTopSites", value => {
if (value === false) {
Services.prefs.setBoolPref("browser.newtabpage.activity-stream.feeds.topsites", false);
}
});
// Old activity stream topSitesCount pref showed 6 per row
migratePref("browser.newtabpage.activity-stream.topSitesCount", count => {
Services.prefs.setIntPref("browser.newtabpage.activity-stream.topSitesRows", Math.ceil(count / 6));
});
}
/**
* observe - nsIObserver callback to handle various browser notifications.
*/
function observe(subject, topic, data) {
switch (topic) {
case BROWSER_READY_NOTIFICATION:
Services.obs.removeObserver(observe, BROWSER_READY_NOTIFICATION);
// Avoid running synchronously during this event that's used for timing
Services.tm.dispatchToMainThread(() => onBrowserReady());
break;
}
}
// The functions below are required by bootstrap.js
this.install = function install(data, reason) {};
this.startup = function startup(data, reason) {
resProto.setSubstitutionWithFlags(RESOURCE_HOST,
Services.io.newURI("chrome/content/", null, data.resourceURI),
resProto.ALLOW_CONTENT_ACCESS);
// Cache startup data which contains stuff like the version number, etc.
// so we can use it when we init
startupData = data;
startupReason = reason;
// Only start Activity Stream up when the browser UI is ready
if (Services.startup.startingUp) {
Services.obs.addObserver(observe, BROWSER_READY_NOTIFICATION);
} else {
// Handle manual install or automatic install after manual uninstall
onBrowserReady();
}
};
this.shutdown = function shutdown(data, reason) {
resProto.setSubstitution(RESOURCE_HOST, null);
// Uninitialize Activity Stream
startupData = null;
startupReason = null;
uninit(reason);
// Stop waiting for browser to be ready
if (waitingForBrowserReady) {
Services.obs.removeObserver(observe, BROWSER_READY_NOTIFICATION);
}
// Unload any add-on modules that might might have been imported
modulesToUnload.forEach(Cu.unload);
};
this.uninstall = function uninstall(data, reason) {
if (activityStream) {
activityStream.uninstall(reason);
activityStream = null;
}
};