2013-05-06 21:10:28 -07:00
|
|
|
/* 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 EXPORTED_SYMBOLS = [
|
|
|
|
"BackgroundPageThumbs",
|
|
|
|
];
|
|
|
|
|
|
|
|
const DEFAULT_CAPTURE_TIMEOUT = 30000; // ms
|
|
|
|
const DESTROY_BROWSER_TIMEOUT = 60000; // ms
|
|
|
|
const FRAME_SCRIPT_URL = "chrome://global/content/backgroundPageThumbsContent.js";
|
|
|
|
|
2013-07-12 21:03:18 -07:00
|
|
|
const TELEMETRY_HISTOGRAM_ID_PREFIX = "FX_THUMBNAILS_BG_";
|
|
|
|
|
2013-10-09 10:13:01 -07:00
|
|
|
// possible FX_THUMBNAILS_BG_CAPTURE_DONE_REASON_2 telemetry values
|
2013-07-12 21:03:18 -07:00
|
|
|
const TEL_CAPTURE_DONE_OK = 0;
|
|
|
|
const TEL_CAPTURE_DONE_TIMEOUT = 1;
|
2013-09-08 17:25:13 -07:00
|
|
|
// 2 and 3 were used when we had special handling for private-browsing.
|
2013-10-09 10:13:01 -07:00
|
|
|
const TEL_CAPTURE_DONE_CRASHED = 4;
|
2014-07-01 03:27:40 -07:00
|
|
|
const TEL_CAPTURE_DONE_BAD_URI = 5;
|
2013-07-12 21:03:18 -07:00
|
|
|
|
2013-05-06 21:10:28 -07:00
|
|
|
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
|
|
|
|
const HTML_NS = "http://www.w3.org/1999/xhtml";
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/PageThumbs.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2014-07-01 03:27:40 -07:00
|
|
|
const global = this;
|
|
|
|
|
2013-05-06 21:10:28 -07:00
|
|
|
const BackgroundPageThumbs = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously captures a thumbnail of the given URL.
|
|
|
|
*
|
|
|
|
* The page is loaded anonymously, and plug-ins are disabled.
|
|
|
|
*
|
|
|
|
* @param url The URL to capture.
|
|
|
|
* @param options An optional object that configures the capture. Its
|
|
|
|
* properties are the following, and all are optional:
|
|
|
|
* @opt onDone A function that will be asynchronously called when the
|
|
|
|
* capture is complete or times out. It's called as
|
|
|
|
* onDone(url),
|
|
|
|
* where `url` is the captured URL.
|
|
|
|
* @opt timeout The capture will time out after this many milliseconds have
|
2013-07-11 20:59:16 -07:00
|
|
|
* elapsed after the capture has progressed to the head of
|
|
|
|
* the queue and started. Defaults to 30000 (30 seconds).
|
2013-05-06 21:10:28 -07:00
|
|
|
*/
|
|
|
|
capture: function (url, options={}) {
|
2013-07-23 00:40:16 -07:00
|
|
|
if (!PageThumbs._prefEnabled()) {
|
|
|
|
if (options.onDone)
|
2013-09-12 09:25:57 -07:00
|
|
|
schedule(() => options.onDone(url));
|
2013-07-23 00:40:16 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-05-06 21:10:28 -07:00
|
|
|
this._captureQueue = this._captureQueue || [];
|
2013-07-11 00:50:55 -07:00
|
|
|
this._capturesByURL = this._capturesByURL || new Map();
|
2013-07-12 21:03:18 -07:00
|
|
|
|
|
|
|
tel("QUEUE_SIZE_ON_CAPTURE", this._captureQueue.length);
|
|
|
|
|
2013-07-11 00:50:55 -07:00
|
|
|
// We want to avoid duplicate captures for the same URL. If there is an
|
|
|
|
// existing one, we just add the callback to that one and we are done.
|
|
|
|
let existing = this._capturesByURL.get(url);
|
|
|
|
if (existing) {
|
|
|
|
if (options.onDone)
|
|
|
|
existing.doneCallbacks.push(options.onDone);
|
|
|
|
// The queue is already being processed, so nothing else to do...
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let cap = new Capture(url, this._onCaptureOrTimeout.bind(this), options);
|
2013-05-06 21:10:28 -07:00
|
|
|
this._captureQueue.push(cap);
|
2013-07-11 00:50:55 -07:00
|
|
|
this._capturesByURL.set(url, cap);
|
2013-05-06 21:10:28 -07:00
|
|
|
this._processCaptureQueue();
|
|
|
|
},
|
|
|
|
|
2013-09-09 19:58:57 -07:00
|
|
|
/**
|
2013-09-12 09:25:57 -07:00
|
|
|
* Asynchronously captures a thumbnail of the given URL if one does not
|
|
|
|
* already exist. Otherwise does nothing.
|
2013-09-09 19:58:57 -07:00
|
|
|
*
|
|
|
|
* @param url The URL to capture.
|
|
|
|
* @param options An optional object that configures the capture. See
|
|
|
|
* capture() for description.
|
|
|
|
*/
|
2013-09-12 09:25:57 -07:00
|
|
|
captureIfMissing: function (url, options={}) {
|
|
|
|
// The fileExistsForURL call is an optimization, potentially but unlikely
|
|
|
|
// incorrect, and no big deal when it is. After the capture is done, we
|
|
|
|
// atomically test whether the file exists before writing it.
|
|
|
|
PageThumbsStorage.fileExistsForURL(url).then(exists => {
|
2014-07-06 19:28:00 -07:00
|
|
|
if (exists) {
|
2013-09-09 19:58:57 -07:00
|
|
|
if (options.onDone)
|
|
|
|
options.onDone(url);
|
2013-09-12 09:25:57 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.capture(url, options);
|
|
|
|
}, err => {
|
|
|
|
if (options.onDone)
|
|
|
|
options.onDone(url);
|
|
|
|
});
|
2013-09-09 19:58:57 -07:00
|
|
|
},
|
|
|
|
|
2013-05-06 21:10:28 -07:00
|
|
|
/**
|
|
|
|
* Ensures that initialization of the thumbnail browser's parent window has
|
|
|
|
* begun.
|
|
|
|
*
|
|
|
|
* @return True if the parent window is completely initialized and can be
|
|
|
|
* used, and false if initialization has started but not completed.
|
|
|
|
*/
|
|
|
|
_ensureParentWindowReady: function () {
|
|
|
|
if (this._parentWin)
|
|
|
|
// Already fully initialized.
|
|
|
|
return true;
|
|
|
|
if (this._startedParentWinInit)
|
|
|
|
// Already started initializing.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this._startedParentWinInit = true;
|
|
|
|
|
2013-09-08 17:25:13 -07:00
|
|
|
// Create an html:iframe, stick it in the parent document, and
|
|
|
|
// use it to host the browser. about:blank will not have the system
|
|
|
|
// principal, so it can't host, but a document with a chrome URI will.
|
|
|
|
let hostWindow = Services.appShell.hiddenDOMWindow;
|
|
|
|
let iframe = hostWindow.document.createElementNS(HTML_NS, "iframe");
|
|
|
|
iframe.setAttribute("src", "chrome://global/content/mozilla.xhtml");
|
|
|
|
let onLoad = function onLoadFn() {
|
|
|
|
iframe.removeEventListener("load", onLoad, true);
|
|
|
|
this._parentWin = iframe.contentWindow;
|
|
|
|
this._processCaptureQueue();
|
|
|
|
}.bind(this);
|
|
|
|
iframe.addEventListener("load", onLoad, true);
|
|
|
|
hostWindow.document.documentElement.appendChild(iframe);
|
|
|
|
this._hostIframe = iframe;
|
2013-05-06 21:10:28 -07:00
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys the service. Queued and pending captures will never complete, and
|
|
|
|
* their consumer callbacks will never be called.
|
|
|
|
*/
|
|
|
|
_destroy: function () {
|
|
|
|
if (this._captureQueue)
|
|
|
|
this._captureQueue.forEach(cap => cap.destroy());
|
|
|
|
this._destroyBrowser();
|
|
|
|
if (this._hostIframe)
|
|
|
|
this._hostIframe.remove();
|
|
|
|
delete this._captureQueue;
|
|
|
|
delete this._hostIframe;
|
|
|
|
delete this._startedParentWinInit;
|
|
|
|
delete this._parentWin;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the thumbnail browser if it doesn't already exist.
|
|
|
|
*/
|
|
|
|
_ensureBrowser: function () {
|
|
|
|
if (this._thumbBrowser)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let browser = this._parentWin.document.createElementNS(XUL_NS, "browser");
|
|
|
|
browser.setAttribute("type", "content");
|
|
|
|
browser.setAttribute("remote", "true");
|
2013-06-04 14:37:02 -07:00
|
|
|
|
2013-08-23 17:38:21 -07:00
|
|
|
// Size the browser. Make its aspect ratio the same as the canvases' that
|
|
|
|
// the thumbnails are drawn into; the canvases' aspect ratio is the same as
|
|
|
|
// the screen's, so use that. Aim for a size in the ballpark of 1024x768.
|
|
|
|
let [swidth, sheight] = [{}, {}];
|
2013-06-04 17:25:50 -07:00
|
|
|
Cc["@mozilla.org/gfx/screenmanager;1"].
|
|
|
|
getService(Ci.nsIScreenManager).
|
|
|
|
primaryScreen.
|
2013-08-23 17:38:21 -07:00
|
|
|
GetRectDisplayPix({}, {}, swidth, sheight);
|
|
|
|
let bwidth = Math.min(1024, swidth.value);
|
|
|
|
// Setting the width and height attributes doesn't work -- the resulting
|
|
|
|
// thumbnails are blank and transparent -- but setting the style does.
|
|
|
|
browser.style.width = bwidth + "px";
|
|
|
|
browser.style.height = (bwidth * sheight.value / swidth.value) + "px";
|
2013-06-04 14:37:02 -07:00
|
|
|
|
2013-05-06 21:10:28 -07:00
|
|
|
this._parentWin.document.documentElement.appendChild(browser);
|
|
|
|
|
2013-09-10 20:22:57 -07:00
|
|
|
// an event that is sent if the remote process crashes - no need to remove
|
|
|
|
// it as we want it to be there as long as the browser itself lives.
|
|
|
|
browser.addEventListener("oop-browser-crashed", () => {
|
|
|
|
Cu.reportError("BackgroundThumbnails remote process crashed - recovering");
|
|
|
|
this._destroyBrowser();
|
|
|
|
let curCapture = this._captureQueue.length ? this._captureQueue[0] : null;
|
|
|
|
// we could retry the pending capture, but it's possible the crash
|
|
|
|
// was due directly to it, so trying again might just crash again.
|
|
|
|
// We could keep a flag to indicate if it previously crashed, but
|
|
|
|
// "resetting" the capture requires more work - so for now, we just
|
|
|
|
// discard it.
|
|
|
|
if (curCapture && curCapture.pending) {
|
2013-10-09 10:13:01 -07:00
|
|
|
curCapture._done(null, TEL_CAPTURE_DONE_CRASHED);
|
2013-09-10 20:22:57 -07:00
|
|
|
// _done automatically continues queue processing.
|
|
|
|
}
|
|
|
|
// else: we must have been idle and not currently doing a capture (eg,
|
|
|
|
// maybe a GC or similar crashed) - so there's no need to attempt a
|
|
|
|
// queue restart - the next capture request will set everything up.
|
|
|
|
});
|
|
|
|
|
2013-05-06 21:10:28 -07:00
|
|
|
browser.messageManager.loadFrameScript(FRAME_SCRIPT_URL, false);
|
|
|
|
this._thumbBrowser = browser;
|
|
|
|
},
|
|
|
|
|
|
|
|
_destroyBrowser: function () {
|
|
|
|
if (!this._thumbBrowser)
|
|
|
|
return;
|
|
|
|
this._thumbBrowser.remove();
|
|
|
|
delete this._thumbBrowser;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the next capture if the queue is not empty and the service is fully
|
|
|
|
* initialized.
|
|
|
|
*/
|
|
|
|
_processCaptureQueue: function () {
|
|
|
|
if (!this._captureQueue.length ||
|
|
|
|
this._captureQueue[0].pending ||
|
|
|
|
!this._ensureParentWindowReady())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ready to start the first capture in the queue.
|
|
|
|
this._ensureBrowser();
|
|
|
|
this._captureQueue[0].start(this._thumbBrowser.messageManager);
|
|
|
|
if (this._destroyBrowserTimer) {
|
|
|
|
this._destroyBrowserTimer.cancel();
|
|
|
|
delete this._destroyBrowserTimer;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-09-10 20:22:57 -07:00
|
|
|
* Called when the current capture completes or fails (eg, times out, remote
|
|
|
|
* process crashes.)
|
2013-05-06 21:10:28 -07:00
|
|
|
*/
|
|
|
|
_onCaptureOrTimeout: function (capture) {
|
2013-07-11 20:59:16 -07:00
|
|
|
// Since timeouts start as an item is being processed, only the first
|
|
|
|
// item in the queue can be passed to this method.
|
|
|
|
if (capture !== this._captureQueue[0])
|
|
|
|
throw new Error("The capture should be at the head of the queue.");
|
|
|
|
this._captureQueue.shift();
|
2013-07-11 00:50:55 -07:00
|
|
|
this._capturesByURL.delete(capture.url);
|
2013-05-06 21:10:28 -07:00
|
|
|
|
|
|
|
// Start the destroy-browser timer *before* processing the capture queue.
|
|
|
|
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
timer.initWithCallback(this._destroyBrowser.bind(this),
|
|
|
|
this._destroyBrowserTimeout,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
this._destroyBrowserTimer = timer;
|
|
|
|
|
|
|
|
this._processCaptureQueue();
|
|
|
|
},
|
|
|
|
|
|
|
|
_destroyBrowserTimeout: DESTROY_BROWSER_TIMEOUT,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a single capture request in the capture queue.
|
|
|
|
*
|
|
|
|
* @param url The URL to capture.
|
|
|
|
* @param captureCallback A function you want called when the capture
|
|
|
|
* completes.
|
|
|
|
* @param options The capture options.
|
|
|
|
*/
|
|
|
|
function Capture(url, captureCallback, options) {
|
|
|
|
this.url = url;
|
|
|
|
this.captureCallback = captureCallback;
|
|
|
|
this.options = options;
|
|
|
|
this.id = Capture.nextID++;
|
2013-07-12 21:03:18 -07:00
|
|
|
this.creationDate = new Date();
|
2013-07-11 00:50:55 -07:00
|
|
|
this.doneCallbacks = [];
|
|
|
|
if (options.onDone)
|
|
|
|
this.doneCallbacks.push(options.onDone);
|
2013-05-06 21:10:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Capture.prototype = {
|
|
|
|
|
|
|
|
get pending() {
|
|
|
|
return !!this._msgMan;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a message to the content script to start the capture.
|
|
|
|
*
|
|
|
|
* @param messageManager The nsIMessageSender of the thumbnail browser.
|
|
|
|
*/
|
|
|
|
start: function (messageManager) {
|
2013-07-12 21:03:18 -07:00
|
|
|
this.startDate = new Date();
|
|
|
|
tel("CAPTURE_QUEUE_TIME_MS", this.startDate - this.creationDate);
|
|
|
|
|
2013-07-12 21:03:15 -07:00
|
|
|
// timeout timer
|
|
|
|
let timeout = typeof(this.options.timeout) == "number" ?
|
|
|
|
this.options.timeout :
|
2013-07-11 20:59:16 -07:00
|
|
|
DEFAULT_CAPTURE_TIMEOUT;
|
|
|
|
this._timeoutTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
2013-07-12 21:03:15 -07:00
|
|
|
this._timeoutTimer.initWithCallback(this, timeout,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
2013-07-11 20:59:16 -07:00
|
|
|
|
2013-07-12 21:03:15 -07:00
|
|
|
// didCapture registration
|
2013-05-06 21:10:28 -07:00
|
|
|
this._msgMan = messageManager;
|
|
|
|
this._msgMan.sendAsyncMessage("BackgroundPageThumbs:capture",
|
|
|
|
{ id: this.id, url: this.url });
|
|
|
|
this._msgMan.addMessageListener("BackgroundPageThumbs:didCapture", this);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The only intended external use of this method is by the service when it's
|
|
|
|
* uninitializing and doing things like destroying the thumbnail browser. In
|
|
|
|
* that case the consumer's completion callback will never be called.
|
|
|
|
*/
|
|
|
|
destroy: function () {
|
2013-07-11 20:59:16 -07:00
|
|
|
// This method may be called for captures that haven't started yet, so
|
|
|
|
// guard against not yet having _timeoutTimer, _msgMan etc properties...
|
|
|
|
if (this._timeoutTimer) {
|
|
|
|
this._timeoutTimer.cancel();
|
|
|
|
delete this._timeoutTimer;
|
|
|
|
}
|
2013-05-06 21:10:28 -07:00
|
|
|
if (this._msgMan) {
|
|
|
|
this._msgMan.removeMessageListener("BackgroundPageThumbs:didCapture",
|
|
|
|
this);
|
|
|
|
delete this._msgMan;
|
|
|
|
}
|
|
|
|
delete this.captureCallback;
|
2013-10-09 10:13:03 -07:00
|
|
|
delete this.doneCallbacks;
|
|
|
|
delete this.options;
|
2013-05-06 21:10:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// Called when the didCapture message is received.
|
|
|
|
receiveMessage: function (msg) {
|
2014-07-01 03:27:40 -07:00
|
|
|
if (msg.data.imageData)
|
|
|
|
tel("CAPTURE_SERVICE_TIME_MS", new Date() - this.startDate);
|
2013-07-12 21:03:18 -07:00
|
|
|
|
2013-05-06 21:10:28 -07:00
|
|
|
// A different timed-out capture may have finally successfully completed, so
|
|
|
|
// discard messages that aren't meant for this capture.
|
2014-07-01 03:27:40 -07:00
|
|
|
if (msg.data.id != this.id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (msg.data.failReason) {
|
|
|
|
let reason = global["TEL_CAPTURE_DONE_" + msg.data.failReason];
|
|
|
|
this._done(null, reason);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._done(msg.data, TEL_CAPTURE_DONE_OK);
|
2013-05-06 21:10:28 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// Called when the timeout timer fires.
|
|
|
|
notify: function () {
|
2013-10-09 10:13:01 -07:00
|
|
|
this._done(null, TEL_CAPTURE_DONE_TIMEOUT);
|
2013-05-06 21:10:28 -07:00
|
|
|
},
|
|
|
|
|
2013-10-09 10:13:01 -07:00
|
|
|
_done: function (data, reason) {
|
2013-05-06 21:10:28 -07:00
|
|
|
// Note that _done will be called only once, by either receiveMessage or
|
2013-10-09 10:13:03 -07:00
|
|
|
// notify, since it calls destroy here, which cancels the timeout timer and
|
2013-05-06 21:10:28 -07:00
|
|
|
// removes the didCapture message listener.
|
2013-10-09 10:13:03 -07:00
|
|
|
let { captureCallback, doneCallbacks, options } = this;
|
|
|
|
this.destroy();
|
2013-05-06 21:10:28 -07:00
|
|
|
|
2013-10-09 10:13:01 -07:00
|
|
|
if (typeof(reason) != "number")
|
|
|
|
throw new Error("A done reason must be given.");
|
|
|
|
tel("CAPTURE_DONE_REASON_2", reason);
|
2013-07-12 21:03:18 -07:00
|
|
|
if (data && data.telemetry) {
|
|
|
|
// Telemetry is currently disabled in the content process (bug 680508).
|
|
|
|
for (let id in data.telemetry) {
|
|
|
|
tel(id, data.telemetry[id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 15:12:22 -07:00
|
|
|
let done = () => {
|
2013-10-09 10:13:03 -07:00
|
|
|
captureCallback(this);
|
|
|
|
for (let callback of doneCallbacks) {
|
2013-07-11 00:50:55 -07:00
|
|
|
try {
|
2013-10-09 10:13:03 -07:00
|
|
|
callback.call(options, this.url);
|
2013-07-11 00:50:55 -07:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
Cu.reportError(err);
|
|
|
|
}
|
2013-05-06 21:10:28 -07:00
|
|
|
}
|
2013-09-11 15:12:22 -07:00
|
|
|
};
|
2013-05-06 21:10:28 -07:00
|
|
|
|
2013-09-08 17:25:13 -07:00
|
|
|
if (!data) {
|
2013-09-11 15:12:22 -07:00
|
|
|
done();
|
2013-05-06 21:10:28 -07:00
|
|
|
return;
|
|
|
|
}
|
2013-09-08 17:25:13 -07:00
|
|
|
|
2013-09-12 09:25:57 -07:00
|
|
|
PageThumbs._store(this.url, data.finalURL, data.imageData, true)
|
2013-09-11 15:12:22 -07:00
|
|
|
.then(done, done);
|
2013-05-06 21:10:28 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Capture.nextID = 0;
|
|
|
|
|
2013-07-12 21:03:18 -07:00
|
|
|
/**
|
|
|
|
* Adds a value to one of this module's telemetry histograms.
|
|
|
|
*
|
|
|
|
* @param histogramID This is prefixed with this module's ID.
|
|
|
|
* @param value The value to add.
|
|
|
|
*/
|
|
|
|
function tel(histogramID, value) {
|
|
|
|
let id = TELEMETRY_HISTOGRAM_ID_PREFIX + histogramID;
|
|
|
|
Services.telemetry.getHistogramById(id).add(value);
|
|
|
|
}
|
|
|
|
|
2013-07-12 21:03:15 -07:00
|
|
|
function schedule(callback) {
|
|
|
|
Services.tm.mainThread.dispatch(callback, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
}
|