2014-12-30 18:44:34 +03:00
|
|
|
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
|
|
|
|
/* 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";
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = [ "ReaderParent" ];
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ReaderMode", "resource://gre/modules/ReaderMode.jsm");
|
|
|
|
|
2015-02-11 18:29:34 +03:00
|
|
|
const gStringBundle = Services.strings.createBundle("chrome://browser/locale/readerMode.properties");
|
|
|
|
|
2014-12-30 18:44:34 +03:00
|
|
|
let ReaderParent = {
|
|
|
|
|
|
|
|
MESSAGES: [
|
|
|
|
"Reader:AddToList",
|
|
|
|
"Reader:ArticleGet",
|
|
|
|
"Reader:FaviconRequest",
|
|
|
|
"Reader:ListStatusRequest",
|
|
|
|
"Reader:RemoveFromList",
|
|
|
|
"Reader:Share",
|
|
|
|
"Reader:SystemUIVisibility",
|
2015-01-03 04:20:12 +03:00
|
|
|
"Reader:UpdateReaderButton",
|
2015-02-20 23:56:00 +03:00
|
|
|
"Reader:SetIntPref",
|
|
|
|
"Reader:SetCharPref",
|
2014-12-30 18:44:34 +03:00
|
|
|
],
|
|
|
|
|
|
|
|
init: function() {
|
|
|
|
let mm = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
|
|
|
|
for (let msg of this.MESSAGES) {
|
|
|
|
mm.addMessageListener(msg, this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(message) {
|
|
|
|
switch (message.name) {
|
|
|
|
case "Reader:AddToList":
|
|
|
|
// XXX: To implement.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Reader:ArticleGet":
|
|
|
|
this._getArticle(message.data.url, message.target).then((article) => {
|
2015-01-24 02:23:21 +03:00
|
|
|
// Make sure the target browser is still alive before trying to send data back.
|
|
|
|
if (message.target.messageManager) {
|
|
|
|
message.target.messageManager.sendAsyncMessage("Reader:ArticleData", { article: article });
|
|
|
|
}
|
2014-12-30 18:44:34 +03:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Reader:FaviconRequest": {
|
|
|
|
// XXX: To implement.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Reader:ListStatusRequest":
|
|
|
|
// XXX: To implement.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Reader:RemoveFromList":
|
|
|
|
// XXX: To implement.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Reader:Share":
|
|
|
|
// XXX: To implement.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Reader:SystemUIVisibility":
|
|
|
|
// XXX: To implement.
|
|
|
|
break;
|
|
|
|
|
2015-01-03 04:20:12 +03:00
|
|
|
case "Reader:UpdateReaderButton": {
|
|
|
|
let browser = message.target;
|
|
|
|
if (message.data && message.data.isArticle !== undefined) {
|
|
|
|
browser.isArticle = message.data.isArticle;
|
|
|
|
}
|
|
|
|
this.updateReaderButton(browser);
|
2014-12-30 18:44:34 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-02-20 23:56:00 +03:00
|
|
|
case "Reader:SetIntPref": {
|
|
|
|
if (message.data && message.data.name !== undefined) {
|
|
|
|
Services.prefs.setIntPref(message.data.name, message.data.value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Reader:SetCharPref": {
|
|
|
|
if (message.data && message.data.name !== undefined) {
|
|
|
|
Services.prefs.setCharPref(message.data.name, message.data.value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-12-30 18:44:34 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-01-03 04:20:12 +03:00
|
|
|
updateReaderButton: function(browser) {
|
|
|
|
let win = browser.ownerDocument.defaultView;
|
|
|
|
if (browser != win.gBrowser.selectedBrowser) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let button = win.document.getElementById("reader-mode-button");
|
|
|
|
if (browser.currentURI.spec.startsWith("about:reader")) {
|
|
|
|
button.setAttribute("readeractive", true);
|
|
|
|
button.hidden = false;
|
2015-02-12 20:15:45 +03:00
|
|
|
button.setAttribute("tooltiptext", gStringBundle.GetStringFromName("readerView.exit"));
|
2015-01-03 04:20:12 +03:00
|
|
|
} else {
|
|
|
|
button.removeAttribute("readeractive");
|
2015-02-12 20:15:45 +03:00
|
|
|
button.setAttribute("tooltiptext", gStringBundle.GetStringFromName("readerView.enter"));
|
2015-01-03 04:20:12 +03:00
|
|
|
button.hidden = !browser.isArticle;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-11 18:29:34 +03:00
|
|
|
handleReaderButtonEvent: function(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
if ((event.type == "click" && event.button != 0) ||
|
|
|
|
(event.type == "keypress" && event.charCode != Ci.nsIDOMKeyEvent.DOM_VK_SPACE &&
|
|
|
|
event.keyCode != Ci.nsIDOMKeyEvent.DOM_VK_RETURN)) {
|
|
|
|
return; // Left click, space or enter only
|
|
|
|
}
|
|
|
|
|
2015-01-03 04:20:12 +03:00
|
|
|
let win = event.target.ownerDocument.defaultView;
|
|
|
|
let url = win.gBrowser.selectedBrowser.currentURI.spec;
|
2015-02-05 05:27:39 +03:00
|
|
|
|
2015-01-03 04:20:12 +03:00
|
|
|
if (url.startsWith("about:reader")) {
|
2015-02-05 05:27:39 +03:00
|
|
|
let originalURL = this._getOriginalUrl(url);
|
|
|
|
if (!originalURL) {
|
|
|
|
Cu.reportError("Error finding original URL for about:reader URL: " + url);
|
|
|
|
} else {
|
2015-02-24 07:44:27 +03:00
|
|
|
win.openUILinkIn(originalURL, "current", {"allowPinnedTabHostChange": true});
|
2015-02-05 05:27:39 +03:00
|
|
|
}
|
2015-01-03 04:20:12 +03:00
|
|
|
} else {
|
2015-02-24 07:44:27 +03:00
|
|
|
win.openUILinkIn("about:reader?url=" + encodeURIComponent(url), "current", {"allowPinnedTabHostChange": true});
|
2015-01-03 04:20:12 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-02-05 05:27:39 +03:00
|
|
|
parseReaderUrl: function(url) {
|
|
|
|
if (!url.startsWith("about:reader?")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this._getOriginalUrl(url);
|
|
|
|
},
|
|
|
|
|
2015-01-03 04:20:12 +03:00
|
|
|
/**
|
|
|
|
* Returns original URL from an about:reader URL.
|
|
|
|
*
|
|
|
|
* @param url An about:reader URL.
|
2015-02-05 05:27:39 +03:00
|
|
|
* @return The original URL for the article, or null if we did not find
|
|
|
|
* a properly formatted about:reader URL.
|
2015-01-03 04:20:12 +03:00
|
|
|
*/
|
|
|
|
_getOriginalUrl: function(url) {
|
2015-02-05 05:27:39 +03:00
|
|
|
let searchParams = new URLSearchParams(url.substring("about:reader?".length));
|
2015-01-03 04:20:12 +03:00
|
|
|
if (!searchParams.has("url")) {
|
2015-02-05 05:27:39 +03:00
|
|
|
return null;
|
2015-01-03 04:20:12 +03:00
|
|
|
}
|
|
|
|
return decodeURIComponent(searchParams.get("url"));
|
|
|
|
},
|
|
|
|
|
2014-12-30 18:44:34 +03:00
|
|
|
/**
|
|
|
|
* Gets an article for a given URL. This method will download and parse a document
|
|
|
|
* if it does not find the article in the tab data or the cache.
|
|
|
|
*
|
|
|
|
* @param url The article URL.
|
|
|
|
* @param browser The browser where the article is currently loaded.
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves JS object representing the article, or null if no article is found.
|
|
|
|
*/
|
|
|
|
_getArticle: Task.async(function* (url, browser) {
|
|
|
|
// First, look for a saved article.
|
|
|
|
let article = yield this._getSavedArticle(browser);
|
|
|
|
if (article && article.url == url) {
|
|
|
|
return article;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, try to find a parsed article in the cache.
|
|
|
|
let uri = Services.io.newURI(url, null, null);
|
|
|
|
article = yield ReaderMode.getArticleFromCache(uri);
|
|
|
|
if (article) {
|
|
|
|
return article;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Article hasn't been found in the cache, we need to
|
|
|
|
// download the page and parse the article out of it.
|
|
|
|
return yield ReaderMode.downloadAndParseDocument(url);
|
|
|
|
}),
|
|
|
|
|
|
|
|
_getSavedArticle: function(browser) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let mm = browser.messageManager;
|
|
|
|
let listener = (message) => {
|
|
|
|
mm.removeMessageListener("Reader:SavedArticleData", listener);
|
|
|
|
resolve(message.data.article);
|
|
|
|
};
|
|
|
|
mm.addMessageListener("Reader:SavedArticleData", listener);
|
|
|
|
mm.sendAsyncMessage("Reader:SavedArticleGet");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|