-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootstrap.js
442 lines (377 loc) · 17.7 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* 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 { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/Home.jsm");
Cu.import("resource://gre/modules/HomeProvider.jsm");
Cu.import("resource://gre/modules/Prompt.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
// Pref used to persist feed panel data between app runs.
const FEEDS_PREF = "extensions.homepagefeeds.feeds";
// Old pref that may need to be migrated.
const FEEDS_PREF_OLD = "home.page.feeds";
// XXX: Using data URIs as a workaround until bug 993698 is fixed.
//const URLBAR_ICON_MDPI = "chrome://feeds/skin/icon_urlbar_mdpi.png";
//const URLBAR_ICON_HDPI = "chrome://feeds/skin/icon_urlbar_hdpi.png";
//const URLBAR_ICON_XHDPI = "chrome://feeds/skin/icon_urlbar_xhdpi.png";
//const URLBAR_ICON_XXHDPI = "chrome://feeds/skin/icon_urlbar_xxhdpi.png";
const URLBAR_ICON_MDPI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAABDUlEQVR42u3VsUpCYRiHcTUNqSEvoEtoCUdbgyCEhraIM0hg4Obi5OLQJegVBCHODuIVZEvRIgUei8ocpAvovD7DO7x80PAdTxDk8BvO/wwP53UwJSK/ah34o4GgUn3CDH00cYBMkgGBK0Qd20kEBvj6IfSKo7gBN7SLAH18m0iE5qoBN7aPO4jRiHuiFzzjGmfI676JnvMlp3ECEcT4xCXSGhk67wq+gQBt3DuhG2yggKnZWz4BN1bEyN5d97LZ5sj5BmxkB48QLLCl+62JHPqc6BgzvOFEtxJEnetWM9uVTyC0P6LZH3Tr6vMeRA2SCHR0C/U5C1Efvid6tyfS/QIRFmYbQzBZ/+H808ASnTUiP4oxmJUAAAAASUVORK5CYII=";
const URLBAR_ICON_HDPI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAABm0lEQVR42u3XPyjEYRjA8Ttx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brand.properties"],
["browser", "chrome://browser/locale/browser.properties"],
["feeds", "chrome://feeds/locale/feeds.properties"]
].forEach(function (aStringBundle) {
let [name, bundle] = aStringBundle;
XPCOMUtils.defineLazyGetter(Strings, name, function() {
return Services.strings.createBundle(bundle);
});
});
XPCOMUtils.defineLazyGetter(this, "FeedHelper", function() {
let win = Services.wm.getMostRecentWindow("navigator:browser");
if (win.FeedHelper) {
return win.FeedHelper;
}
Services.scriptloader.loadSubScript("chrome://feeds/content/FeedHelper.js", win);
return win["FeedHelper"];
});
function reportErrors(e) {
if (!e.errors) {
return;
}
if (e.message) {
Cu.reportError(e.message);
}
e.errors.forEach(error => Cu.reportError(error.message));
}
/**
* @return optionsCallback function for a basic list panel.
*/
function getOptionsCallback(panelId, title, datasetId, refreshDataset) {
return function() {
return {
title: title,
views: [{
type: Home.panels.View.LIST,
dataset: datasetId,
onrefresh: refreshDataset
}],
onuninstall: function() {
// Unregister the panel and delete its data if the user
// chooses to remove it in settings.
removeFeedPanel(panelId, datasetId);
unstoreFeed(panelId);
}
};
};
}
/**
* Monkey-patched version of FeedHandler.loadFeed.
*
* @param feed object created by DOMLinkAdded handler in browser.js
*/
function loadFeed(feed) {
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
let BrowserApp = chromeWin.BrowserApp;
let handlers = [];
// Make sure our custom handler is first in the list.
handlers.push({
name: Strings.brand.GetStringFromName("brandShortName"),
action: addFeedPanel
});
// Get the default feed handlers from FeedHandler.
let defaultHandlers = chromeWin.FeedHandler.getContentHandlers(this.TYPE_MAYBE_FEED);
defaultHandlers.forEach(function(handler) {
handlers.push({
name: handler.name,
action: function defaultHandlerAction(feed) {
// Merge the handler URL and the feed URL.
let readerURL = handler.uri;
readerURL = readerURL.replace(/%s/gi, encodeURIComponent(feed.href));
// Open the resultant URL in a new tab.
BrowserApp.addTab(readerURL, { parentId: BrowserApp.selectedTab.id });
}
});
});
// JSON for Prompt.
let p = new Prompt({
window: chromeWin,
// XXX: In Firefox 31+, we can get this string from browser.properties.
title: Strings.feeds.GetStringFromName("prompt.subscribeWith")
});
p.setSingleChoiceItems(handlers.map(function (handler) {
return { label: handler.name };
}));
p.show(function (data) {
if (data.button == -1) {
return;
}
// Call the action callback for the feed handler.
handlers[data.button].action(feed);
});
}
/**
* @param feed object created by DOMLinkAdded handler in browser.js
*/
function addFeedPanel(feed) {
// Check to see if a panel for the feed already exists.
if (feedPanelExists(feed.href)) {
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
chromeWin.NativeWindow.toast.show(Strings.feeds.GetStringFromName("toast.feedAlreadyExists"), "short");
return;
}
let uuidgen = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
// Prefix an add-on identifier for UI telemetry purposes.
let panelId = "home-feeds-" + uuidgen.generateUUID().toString();
let datasetId = uuidgen.generateUUID().toString();
function refreshDataset() {
FeedHelper.parseFeed(feed.href, function(parsedFeed) {
saveFeedItems(parsedFeed, datasetId);
});
}
// Immediately fetch and parse the feed to get title for panel.
FeedHelper.parseFeed(feed.href, function (parsedFeed) {
let title = parsedFeed.title.plainText();
Home.panels.register(panelId, getOptionsCallback(panelId, title, datasetId, refreshDataset));
Home.panels.install(panelId);
saveFeedItems(parsedFeed, datasetId);
storeFeed(feed.href, title, panelId, datasetId);
let toastMessage;
try {
toastMessage = Strings.feeds.formatStringFromName("toast.addedToHomepage", [Strings.brand.GetStringFromName("brandShortName")], 1)
} catch (e) {
// Fallback to old string if locale isn't updated.
toastMessage = Strings.feeds.GetStringFromName("toast.addedToFirefoxHomepage");
}
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
chromeWin.NativeWindow.toast.show(toastMessage, "short");
});
// Add periodic sync to update feed once per hour.
HomeProvider.addPeriodicSync(datasetId, 3600, refreshDataset);
}
/**
* Uninstalls and unregisters panel, and deletes all feed data.
*/
function removeFeedPanel(panelId, datasetId) {
Home.panels.uninstall(panelId);
Home.panels.unregister(panelId);
HomeProvider.removePeriodicSync(datasetId);
Task.spawn(function deleteAll() {
let storage = HomeProvider.getStorage(datasetId);
yield storage.deleteAll();
}).then(null, reportErrors);
}
/**
* @param parsedFeed nsIFeed
*/
function saveFeedItems(parsedFeed, datasetId) {
let items = FeedHelper.feedToItems(parsedFeed);
Task.spawn(function () {
let storage = HomeProvider.getStorage(datasetId);
yield storage.deleteAll();
yield storage.save(items);
}).then(null, reportErrors);
}
/**
* Checks to see if panel already exists for feed.
*/
function feedPanelExists(url) {
let feeds;
try {
feeds = JSON.parse(Services.prefs.getCharPref(FEEDS_PREF));
} catch (e) {
// If no feeds are stored, this feed definitely isn't stored!
return false;
}
for (let i = 0; i < feeds.length; i++) {
if (feeds[i].url == url) {
return true;
}
}
return false;
}
/**
* Stores feed panel data that we need to persist between app runs.
*/
function storeFeed(url, title, panelId, datasetId) {
let feeds;
try {
feeds = JSON.parse(Services.prefs.getCharPref(FEEDS_PREF));
} catch (e) {
feeds = [];
}
feeds.push({
url: url,
title: title,
panelId: panelId,
datasetId: datasetId
});
Services.prefs.setCharPref(FEEDS_PREF, JSON.stringify(feeds));
}
/**
* Removes feed from array that persists between app runs.
*/
function unstoreFeed(panelId) {
try {
let feeds = JSON.parse(Services.prefs.getCharPref(FEEDS_PREF));
for (let i = 0; i < feeds.length; i++) {
if (feeds[i].panelId === panelId) {
feeds.splice(i, 1);
break;
}
}
Services.prefs.setCharPref(FEEDS_PREF, JSON.stringify(feeds));
} catch (e) {
// We should never call unstoreFeed if the FEEDS_PREF doesn't exist.
Cu.reportError(e);
}
}
let gPageActionIcon;
let gPageActionId;
let gOriginalLoadFeed;
function onPageShow(event) {
let window = Services.wm.getMostRecentWindow("navigator:browser");
if (!window) {
return;
}
let selectedTab = window.BrowserApp.selectedTab;
// Ignore load events on frames and other documents.
// selectedTab may be null during startup.
if (!selectedTab || event.target != selectedTab.browser.contentDocument) {
return;
}
updatePageAction(window, selectedTab);
}
function onTabSelect(event) {
let window = event.currentTarget.ownerDocument.defaultView;
updatePageAction(window, window.BrowserApp.selectedTab);
}
function updatePageAction(window, tab) {
// Remove any current page action item.
if (gPageActionId) {
window.NativeWindow.pageactions.remove(gPageActionId);
gPageActionId = null;
}
let feeds = tab.browser.feeds;
// Bail if there are no feeds for this page.
if (!feeds || feeds.length == 0) {
return;
}
gPageActionId = window.NativeWindow.pageactions.add({
icon: gPageActionIcon,
title: Strings.feeds.GetStringFromName("pageAction.subscribeToPage"),
clickCallback: function onSubscribeClicked() {
if (feeds.length == 1) {
addFeedPanel(feeds[0]);
} else {
// Prompt the user to choose a feed. Logic copied from FeedHandler.js.
let p = new Prompt({
window: tab.browser.contentWindow,
title: Strings.browser.GetStringFromName("feedHandler.chooseFeed")
}).setSingleChoiceItems(feeds.map(function(feed) {
return { label: feed.title || feed.href }
})).show(function(data) {
let feedIndex = data.button;
if (feedIndex > -1) {
addFeedPanel(feeds[feedIndex]);
}
});
}
}
});
}
function loadIntoWindow(window) {
window.BrowserApp.deck.addEventListener("pageshow", onPageShow, false);
window.BrowserApp.deck.addEventListener("TabSelect", onTabSelect, false);
// Monkey-patch FeedHandler to add option to subscribe menu.
gOriginalLoadFeed = window.FeedHandler.loadFeed;
window.FeedHandler.loadFeed = loadFeed;
if (window.devicePixelRatio <= 1) {
gPageActionIcon = URLBAR_ICON_MDPI;
} else if (window.devicePixelRatio <= 1.5) {
gPageActionIcon = URLBAR_ICON_HDPI;
} else if (window.devicePixelRatio <= 2) {
gPageActionIcon = URLBAR_ICON_XHDPI;
} else {
gPageActionIcon = URLBAR_ICON_XXHDPI;
}
}
function unloadFromWindow(window) {
window.BrowserApp.deck.removeEventListener("pageshow", onPageShow, false);
window.BrowserApp.deck.removeEventListener("TabSelect", onTabSelect, false);
window.FeedHandler.loadFeed = gOriginalLoadFeed;
}
/**
* bootstrap.js API
*/
function install(aData, aReason) {}
function uninstall(aData, aReason) {}
let gWindowListener = {
onOpenWindow: function(aWindow) {
// Stop listening after the window has been opened.
Services.wm.removeListener(gWindowListener);
// Wait for startup to finish before interacting with the UI.
let win = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
win.addEventListener("UIReady", function onLoad() {
win.removeEventListener("UIReady", onLoad, false);
loadIntoWindow(win);
}, false);
},
onCloseWindow: function(aWindow) {},
onWindowTitleChange: function(aWindow, aTitle) {}
};
function startup(aData, aReason) {
if (aReason == ADDON_UPGRADE) {
// Migrate the feeds pref if it exists.
try {
let prefValue = Services.prefs.getCharPref(FEEDS_PREF_OLD);
Services.prefs.setCharPref(FEEDS_PREF, prefValue);
Services.prefs.clearUserPref(FEEDS_PREF_OLD);
} catch (e) {}
}
let win = Services.wm.getMostRecentWindow("navigator:browser");
if (win) {
// Load into the browser window if it already exists.
loadIntoWindow(win);
} else {
// Otherwise, listen for it to open.
Services.wm.addListener(gWindowListener);
}
try {
let feeds = JSON.parse(Services.prefs.getCharPref(FEEDS_PREF));
feeds.forEach(function(feed) {
function refreshDataset() {
FeedHelper.parseFeed(feed.url, function(parsedFeed) {
saveFeedItems(parsedFeed, feed.datasetId);
});
}
// Register any existing panels.
Home.panels.register(feed.panelId, getOptionsCallback(feed.panelId, feed.title, feed.datasetId, refreshDataset));
// Add periodic sync for existing feeds.
HomeProvider.addPeriodicSync(feed.datasetId, 3600, refreshDataset);
});
} catch (e) {}
}
function shutdown(aData, aReason) {
// When the application is shutting down we normally don't have to clean any changes made.
if (aReason == APP_SHUTDOWN) {
return;
}
unloadFromWindow(Services.wm.getMostRecentWindow("navigator:browser"));
// If the add-on is being uninstalled, also remove all panel data.
if (aReason == ADDON_UNINSTALL) {
try {
let feeds = JSON.parse(Services.prefs.getCharPref(FEEDS_PREF));
feeds.forEach(function (feed) {
removeFeedPanel(feed.panelId, feed.datasetId);
});
// Clear the stored feeds.
Services.prefs.clearUserPref(FEEDS_PREF);
} catch (e) {}
}
}