mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-27 00:39:16 +00:00
6bedaf2149
- Bug 1193050 - Update the copy of GentiumPlus used in font-inspector test. r=jdaggett (253fce5764)
- align tests (5b817116a0)
- Bug 1246106 - pass aStyleBorder as reference to ComputeBorderOverflow. r=mats (53ccaa1282)
- align tests (81b3943058)
- Bug 1269080 - Convert NS_ERROR to NS_WARNING when setting an invalid scheme in nsSimpleURI. r=mcmanus (cad86c963d)
- Bug 1248558 - "[Static Analysis][Unintentional integer overflow][CacheFile.cpp::PreloadChunk, CacheFile.cpp::GetChunkLocked]". r=michal (6a9e34dfbe)
- Bug 1121672 - Make CacheFile::IsDoomed() protect mHandle with lock, r=michal (9e925b903a)
- Bug 1253040 - Do not write metadata if CacheFile is killed, r=honzab (c0378088b2)
- grammar as 1268922 (c801618b1d)
- Bug 1247548 - Changed nsCookieService::EnsureReadComplete and nsCookieService::PurgeCookeis to allocate nsTArray instead of AutoTArray. r=jdm (0e2bb0465e)
- var-let (1702e664dc)
- Bug 1233813 - Fix mDNS bug in resolving services with no TXT records. r=schien (4ce7e5fca0)
- Bug 1266632 - Add a null check in nsHostResolver::SizeOfIncludingThis. r=sworkman. (035d4a7130)
- Bug 1267000 - null deref with spdy proxy r=hurley (099db40d0f)
- Bug 1240932 - figure out 'network id' on Linux. r=mcmanus (47ceb12a70)
- Bug 1240932: add Telemetry to record network id successes, r=mcmanus (6985d1a4ce)
- Bug 1205158 - Use channel->Open2() netwerk/test/ (r=sicking) (64de01cda3)
- Bug 1240932 - figure out 'network id' on OS X, r=mcmanus (7ee4875835)
- Bug 1240932 - figure out 'network id' on Windows, r=mcmanus (3227a81c14)
- Bug 1188644 - Use channel->ascynOpen2 in netwerk/test (r=mcmanus) (7097897cdc)
- Bug 1264887: |nsWifiScannerDBus|: Hold DBusMessage references in |RefPtr|, r=josh (7e68e8663f)
- Bug 1196638 - Infer the proper button for synthesized mouse clicks on all callsites. r=jmaher (838230baaa)
- Bug 1258532 - Port test_bug574596.html to mochitest-plain in order to enable it on e10s; r=mrbkap (ae5693ab54)
- Bug 1252855 - allow setting a specific list of prefs from the content process, r=mrbkap,margaret,haik (b13645826a)
- Bug 1215965 - Remove use of non-standard features from toolkit/components/social/SocialService.jsm. r=mixedpuppy (a1d79f295c)
- ship cmpiler for XP (ac60884173)
- Bug 1262814 - Warn on Wlanapi.dll load or init fail, don't fail assertion, r=mcmanus (aa1335320a)
- Bug 1137151: Marked destructors of ref-counted time-zone classes as protected, r=dhylands (5668226442)
- Bug 1225549 - Modify the data type of mThreshold of NetworkParams to long long from long. r=ettseng (7164a517ac)
- Bug 1139805 - B2G NetworkManager: move NetworkManager.js out of EXTRA_PP_COMPONENTS in moz.build. r=echen (e857f53cfc)
- Bug 1087608 - ensuring multitap gestures do not resolve to explore. r=eeejay (6c4b603738)
- bug 1259023 - make nsIAccessible.{Next,Prev}Sibling work with proxied accessibles r=yzen (84b47a82e0)
- bug 1259023 - make nsIAccessible.indexInParent work on proxied accessibles r=yzen (3531b45e35)
- bug 1250882 - implement xpcAccessible::GetState() for proxied accessibles r=davidb (5e58214441)
- bug 1250882 - implement xpcAccessible::Name() for proxied accessibles r=davidb (4b11af6ecc)
- bug 1250882 - implement xpcAccessible::GetDescription() for proxied accessibles r=davidb (d0d0e133fb)
- bug 1250882 - implement xpcAccessible::GetLanguage() for proxied accessibles r=davidb (88e98a5b24)
- bug 1250882 - implement xpcAccessible::GetValue() for proxied accessibles r=davidb (9ad4347d45)
- bug 1250882 - implement xpcAccessible::GetBounds() for proxied accessibles r=davidb (e67f7c551b)
- Bug 1249930 - menupoup shouldn't look for children in XBL anonymous content, r=davidb (14b3e39954)
- Bug 1243233 - Test ALLOW_COMPILER_WARNINGS instead of WARNINGS_AS_ERRORS, and move it to after it is set, r=glandium (216c407d20)
- Bug 1245992 - Update the Safe Browsing phishing interstitial page. r=flod,past (80afbfc6c9)
- Bug 1245260 - Ignore redundant calls to RestyleManager::IncrementAnimationGeneration; r=dbaron (e35571b613)
- Bug 1182778, r=margaret,f=bz (b379c97f8e)
- Bug 1258212 - Hook the parent up to the cild for registerContentHandler. r=gwright (039c75d4d5)
360 lines
11 KiB
JavaScript
360 lines
11 KiB
JavaScript
// -*- 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";
|
|
|
|
this.EXPORTED_SYMBOLS = ["ReaderMode"];
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.importGlobalProperties(["XMLHttpRequest"]);
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "CommonUtils", "resource://services-common/utils.js");
|
|
XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ReaderWorker", "resource://gre/modules/reader/ReaderWorker.jsm");
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Task", "resource://gre/modules/Task.jsm");
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "Readability", function() {
|
|
let scope = {};
|
|
Services.scriptloader.loadSubScript("resource://gre/modules/reader/Readability.js", scope);
|
|
return scope["Readability"];
|
|
});
|
|
|
|
this.ReaderMode = {
|
|
// Version of the cache schema.
|
|
CACHE_VERSION: 1,
|
|
|
|
DEBUG: 0,
|
|
|
|
// Don't try to parse the page if it has too many elements (for memory and
|
|
// performance reasons)
|
|
get maxElemsToParse() {
|
|
delete this.parseNodeLimit;
|
|
|
|
Services.prefs.addObserver("reader.parse-node-limit", this, false);
|
|
return this.parseNodeLimit = Services.prefs.getIntPref("reader.parse-node-limit");
|
|
},
|
|
|
|
get isEnabledForParseOnLoad() {
|
|
delete this.isEnabledForParseOnLoad;
|
|
|
|
// Listen for future pref changes.
|
|
Services.prefs.addObserver("reader.parse-on-load.", this, false);
|
|
|
|
return this.isEnabledForParseOnLoad = this._getStateForParseOnLoad();
|
|
},
|
|
|
|
get isOnLowMemoryPlatform() {
|
|
let memory = Cc["@mozilla.org/xpcom/memory-service;1"].getService(Ci.nsIMemory);
|
|
delete this.isOnLowMemoryPlatform;
|
|
return this.isOnLowMemoryPlatform = memory.isLowMemoryPlatform();
|
|
},
|
|
|
|
_getStateForParseOnLoad: function () {
|
|
let isEnabled = Services.prefs.getBoolPref("reader.parse-on-load.enabled");
|
|
let isForceEnabled = Services.prefs.getBoolPref("reader.parse-on-load.force-enabled");
|
|
// For low-memory devices, don't allow reader mode since it takes up a lot of memory.
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=792603 for details.
|
|
return isForceEnabled || (isEnabled && !this.isOnLowMemoryPlatform);
|
|
},
|
|
|
|
observe: function(aMessage, aTopic, aData) {
|
|
switch(aTopic) {
|
|
case "nsPref:changed":
|
|
if (aData.startsWith("reader.parse-on-load.")) {
|
|
this.isEnabledForParseOnLoad = this._getStateForParseOnLoad();
|
|
} else if (aData === "reader.parse-node-limit") {
|
|
this.parseNodeLimit = Services.prefs.getIntPref(aData);
|
|
}
|
|
break;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Returns original URL from an about:reader URL.
|
|
*
|
|
* @param url An about:reader URL.
|
|
* @return The original URL for the article, or null if we did not find
|
|
* a properly formatted about:reader URL.
|
|
*/
|
|
getOriginalUrl: function(url) {
|
|
if (!url.startsWith("about:reader?")) {
|
|
return null;
|
|
}
|
|
|
|
let searchParams = new URLSearchParams(url.substring("about:reader?".length));
|
|
if (!searchParams.has("url")) {
|
|
return null;
|
|
}
|
|
let encodedURL = searchParams.get("url");
|
|
try {
|
|
return decodeURIComponent(encodedURL);
|
|
} catch (e) {
|
|
Cu.reportError("Error decoding original URL: " + e);
|
|
return encodedURL;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Decides whether or not a document is reader-able without parsing the whole thing.
|
|
*
|
|
* @param doc A document to parse.
|
|
* @return boolean Whether or not we should show the reader mode button.
|
|
*/
|
|
isProbablyReaderable: function(doc) {
|
|
let uri = Services.io.newURI(doc.location.href, null, null);
|
|
|
|
if (!this._shouldCheckUri(uri)) {
|
|
return false;
|
|
}
|
|
|
|
return new Readability(uri, doc).isProbablyReaderable();
|
|
},
|
|
|
|
/**
|
|
* Gets an article from a loaded browser's document. This method will not attempt
|
|
* to parse certain URIs (e.g. about: URIs).
|
|
*
|
|
* @param doc A document to parse.
|
|
* @return {Promise}
|
|
* @resolves JS object representing the article, or null if no article is found.
|
|
*/
|
|
parseDocument: Task.async(function* (doc) {
|
|
let uri = Services.io.newURI(doc.documentURI, null, null);
|
|
if (!this._shouldCheckUri(uri)) {
|
|
this.log("Reader mode disabled for URI");
|
|
return null;
|
|
}
|
|
|
|
return yield this._readerParse(uri, doc);
|
|
}),
|
|
|
|
/**
|
|
* Downloads and parses a document from a URL.
|
|
*
|
|
* @param url URL to download and parse.
|
|
* @return {Promise}
|
|
* @resolves JS object representing the article, or null if no article is found.
|
|
*/
|
|
downloadAndParseDocument: Task.async(function* (url) {
|
|
let uri = Services.io.newURI(url, null, null);
|
|
let doc = yield this._downloadDocument(url);
|
|
return yield this._readerParse(uri, doc);
|
|
}),
|
|
|
|
_downloadDocument: function (url) {
|
|
return new Promise((resolve, reject) => {
|
|
let xhr = new XMLHttpRequest();
|
|
xhr.open("GET", url, true);
|
|
xhr.onerror = evt => reject(evt.error);
|
|
xhr.responseType = "document";
|
|
xhr.onload = evt => {
|
|
if (xhr.status !== 200) {
|
|
reject("Reader mode XHR failed with status: " + xhr.status);
|
|
return;
|
|
}
|
|
|
|
let doc = xhr.responseXML;
|
|
|
|
// Manually follow a meta refresh tag if one exists.
|
|
let meta = doc.querySelector("meta[http-equiv=refresh]");
|
|
if (meta) {
|
|
let content = meta.getAttribute("content");
|
|
if (content) {
|
|
let urlIndex = content.indexOf("URL=");
|
|
if (urlIndex > -1) {
|
|
let url = content.substring(urlIndex + 4);
|
|
let ssm = Services.scriptSecurityManager;
|
|
let flags = ssm.LOAD_IS_AUTOMATIC_DOCUMENT_REPLACEMENT |
|
|
ssm.DISALLOW_INHERIT_PRINCIPAL;
|
|
try {
|
|
ssm.checkLoadURIStrWithPrincipal(doc.nodePrincipal, url, flags);
|
|
} catch (ex) {
|
|
let errorMsg = "Reader mode disallowed meta refresh (reason: " + ex + ").";
|
|
if (Services.prefs.getBoolPref("reader.errors.includeURLs"))
|
|
errorMsg += " Refresh target URI: '" + url + "'.";
|
|
reject(errorMsg);
|
|
return;
|
|
}
|
|
// Otherwise, pass an object indicating our new URL:
|
|
reject({newURL: url});
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
resolve(doc);
|
|
}
|
|
xhr.send();
|
|
});
|
|
},
|
|
|
|
|
|
/**
|
|
* Retrieves an article from the cache given an article URI.
|
|
*
|
|
* @param url The article URL.
|
|
* @return {Promise}
|
|
* @resolves JS object representing the article, or null if no article is found.
|
|
* @rejects OS.File.Error
|
|
*/
|
|
getArticleFromCache: Task.async(function* (url) {
|
|
let path = this._toHashedPath(url);
|
|
try {
|
|
let array = yield OS.File.read(path);
|
|
return JSON.parse(new TextDecoder().decode(array));
|
|
} catch (e) {
|
|
if (!(e instanceof OS.File.Error) || !e.becauseNoSuchFile)
|
|
throw e;
|
|
return null;
|
|
}
|
|
}),
|
|
|
|
/**
|
|
* Stores an article in the cache.
|
|
*
|
|
* @param article JS object representing article.
|
|
* @return {Promise}
|
|
* @resolves When the article is stored.
|
|
* @rejects OS.File.Error
|
|
*/
|
|
storeArticleInCache: Task.async(function* (article) {
|
|
let array = new TextEncoder().encode(JSON.stringify(article));
|
|
let path = this._toHashedPath(article.url);
|
|
yield this._ensureCacheDir();
|
|
yield OS.File.writeAtomic(path, array, { tmpPath: path + ".tmp" });
|
|
}),
|
|
|
|
/**
|
|
* Removes an article from the cache given an article URI.
|
|
*
|
|
* @param url The article URL.
|
|
* @return {Promise}
|
|
* @resolves When the article is removed.
|
|
* @rejects OS.File.Error
|
|
*/
|
|
removeArticleFromCache: Task.async(function* (url) {
|
|
let path = this._toHashedPath(url);
|
|
yield OS.File.remove(path);
|
|
}),
|
|
|
|
log: function(msg) {
|
|
if (this.DEBUG)
|
|
dump("Reader: " + msg);
|
|
},
|
|
|
|
_shouldCheckUri: function (uri) {
|
|
if ((uri.prePath + "/") === uri.spec) {
|
|
this.log("Not parsing home page: " + uri.spec);
|
|
return false;
|
|
}
|
|
|
|
if (!(uri.schemeIs("http") || uri.schemeIs("https") || uri.schemeIs("file"))) {
|
|
this.log("Not parsing URI scheme: " + uri.scheme);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
},
|
|
|
|
/**
|
|
* Attempts to parse a document into an article. Heavy lifting happens
|
|
* in readerWorker.js.
|
|
*
|
|
* @param uri The article URI.
|
|
* @param doc The document to parse.
|
|
* @return {Promise}
|
|
* @resolves JS object representing the article, or null if no article is found.
|
|
*/
|
|
_readerParse: Task.async(function* (uri, doc) {
|
|
if (this.parseNodeLimit) {
|
|
let numTags = doc.getElementsByTagName("*").length;
|
|
if (numTags > this.parseNodeLimit) {
|
|
this.log("Aborting parse for " + uri.spec + "; " + numTags + " elements found");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
let uriParam = {
|
|
spec: uri.spec,
|
|
host: uri.host,
|
|
prePath: uri.prePath,
|
|
scheme: uri.scheme,
|
|
pathBase: Services.io.newURI(".", null, uri).spec
|
|
};
|
|
|
|
let serializer = Cc["@mozilla.org/xmlextras/xmlserializer;1"].
|
|
createInstance(Ci.nsIDOMSerializer);
|
|
let serializedDoc = yield Promise.resolve(serializer.serializeToString(doc));
|
|
|
|
let article = null;
|
|
try {
|
|
article = yield ReaderWorker.post("parseDocument", [uriParam, serializedDoc]);
|
|
} catch (e) {
|
|
Cu.reportError("Error in ReaderWorker: " + e);
|
|
}
|
|
|
|
if (!article) {
|
|
this.log("Worker did not return an article");
|
|
return null;
|
|
}
|
|
|
|
// Readability returns a URI object, but we only care about the URL.
|
|
article.url = article.uri.spec;
|
|
delete article.uri;
|
|
|
|
let flags = Ci.nsIDocumentEncoder.OutputSelectionOnly | Ci.nsIDocumentEncoder.OutputAbsoluteLinks;
|
|
article.title = Cc["@mozilla.org/parserutils;1"].getService(Ci.nsIParserUtils)
|
|
.convertToPlainText(article.title, flags, 0);
|
|
return article;
|
|
}),
|
|
|
|
get _cryptoHash() {
|
|
delete this._cryptoHash;
|
|
return this._cryptoHash = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
|
|
},
|
|
|
|
get _unicodeConverter() {
|
|
delete this._unicodeConverter;
|
|
this._unicodeConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
this._unicodeConverter.charset = "utf8";
|
|
return this._unicodeConverter;
|
|
},
|
|
|
|
/**
|
|
* Calculate the hashed path for a stripped article URL.
|
|
*
|
|
* @param url The article URL. This should have referrers removed.
|
|
* @return The file path to the cached article.
|
|
*/
|
|
_toHashedPath: function (url) {
|
|
let value = this._unicodeConverter.convertToByteArray(url);
|
|
this._cryptoHash.init(this._cryptoHash.MD5);
|
|
this._cryptoHash.update(value, value.length);
|
|
|
|
let hash = CommonUtils.encodeBase32(this._cryptoHash.finish(false));
|
|
let fileName = hash.substring(0, hash.indexOf("=")) + ".json";
|
|
return OS.Path.join(OS.Constants.Path.profileDir, "readercache", fileName);
|
|
},
|
|
|
|
/**
|
|
* Ensures the cache directory exists.
|
|
*
|
|
* @return Promise
|
|
* @resolves When the cache directory exists.
|
|
* @rejects OS.File.Error
|
|
*/
|
|
_ensureCacheDir: function () {
|
|
let dir = OS.Path.join(OS.Constants.Path.profileDir, "readercache");
|
|
return OS.File.exists(dir).then(exists => {
|
|
if (!exists) {
|
|
return OS.File.makeDir(dir);
|
|
}
|
|
});
|
|
}
|
|
};
|