Bug 698371 - Move determineCropSize calls to the child for remote browsers. r=dao

This commit is contained in:
Jim Mathies 2014-10-18 09:09:32 -05:00
parent 2ed26ca9e3
commit b7cd1be4de
3 changed files with 36 additions and 44 deletions

View File

@ -13,6 +13,7 @@ const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Promise.jsm", this);
this.PageThumbUtils = {
// The default background color for page thumbnails.
@ -67,11 +68,13 @@ this.PageThumbUtils = {
* @return An array containing width, height and scale.
*/
determineCropSize: function (aWindow, aCanvas) {
if (Cu.isCrossProcessWrapper(aWindow)) {
throw new Error('Do not pass cpows here.');
}
let utils = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
// aWindow may be a cpow, add exposed props security values.
let sbWidth = {__exposedProps__: {"value": "rw"}},
sbHeight = {__exposedProps__: {"value": "rw"}};
let sbWidth = {}, sbHeight = {};
try {
utils.getScrollbarSize(false, sbWidth, sbHeight);
@ -83,20 +86,20 @@ this.PageThumbUtils = {
// Even in RTL mode, scrollbars are always on the right.
// So there's no need to determine a left offset.
let sw = aWindow.innerWidth - sbWidth.value;
let sh = aWindow.innerHeight - sbHeight.value;
let width = aWindow.innerWidth - sbWidth.value;
let height = aWindow.innerHeight - sbHeight.value;
let {width: thumbnailWidth, height: thumbnailHeight} = aCanvas;
let scale = Math.min(Math.max(thumbnailWidth / sw, thumbnailHeight / sh), 1);
let scaledWidth = sw * scale;
let scaledHeight = sh * scale;
let scale = Math.min(Math.max(thumbnailWidth / width, thumbnailHeight / height), 1);
let scaledWidth = width * scale;
let scaledHeight = height * scale;
if (scaledHeight > thumbnailHeight)
sh -= Math.floor(Math.abs(scaledHeight - thumbnailHeight) * scale);
height -= Math.floor(Math.abs(scaledHeight - thumbnailHeight) * scale);
if (scaledWidth > thumbnailWidth)
sw -= Math.floor(Math.abs(scaledWidth - thumbnailWidth) * scale);
width -= Math.floor(Math.abs(scaledWidth - thumbnailWidth) * scale);
return [sw, sh, scale];
return [width, height, scale];
}
};

View File

@ -216,12 +216,9 @@ this.PageThumbs = {
// participate in this service's telemetry, which is why this method exists.
_captureToCanvas: function (aBrowser, aCanvas, aCallback) {
if (aBrowser.isRemoteBrowser) {
let [sw, sh, scale] =
PageThumbUtils.determineCropSize(aBrowser.contentWindowAsCPOW, aCanvas);
Task.spawn(function () {
let data =
yield this._captureRemoteThumbnail(aBrowser, sw, sh, scale,
PageThumbUtils.THUMBNAIL_BG_COLOR);
yield this._captureRemoteThumbnail(aBrowser, aCanvas);
let canvas = data.thumbnail;
let ctx = canvas.getContext("2d");
let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
@ -234,7 +231,7 @@ this.PageThumbs = {
}
// Generate in-process content thumbnail
let [sw, sh, scale] =
let [width, height, scale] =
PageThumbUtils.determineCropSize(aBrowser.contentWindow, aCanvas);
let ctx = aCanvas.getContext("2d");
@ -244,7 +241,7 @@ this.PageThumbs = {
try {
// Draw the window contents to the canvas.
ctx.drawWindow(aBrowser.contentWindow, 0, 0, sw, sh,
ctx.drawWindow(aBrowser.contentWindow, 0, 0, width, height,
PageThumbUtils.THUMBNAIL_BG_COLOR,
ctx.DRAWWINDOW_DO_NOT_FLUSH);
} catch (e) {
@ -258,18 +255,13 @@ this.PageThumbs = {
},
/**
* Request a thumbnail using requested bounds and scale factor.
* @param aWidth - (optional) a width value less than or equal to the
* innerWidth of the dom window. Defaults to the visible frame.
* @param aHeight - (optional) a height value less than or equal to the
* innerHeight of the dom window. Defaults to the visible frame.
* @param aScaleFactor - (optional) 0.0 - 1.0 scale factor applied to the
* returned thumbnail. Defaults to 1.0.
* @param aCssBackground - (optional) a css '#fff' color value to use as
* the background color of the thumbnail.
* Asynchrnously render an appropriately scaled thumbnail to canvas.
*
* @param aBrowser The browser to capture a thumbnail from.
* @param aCanvas The canvas to draw to.
* @return a promise
*/
_captureRemoteThumbnail: function (aBrowser, aWidth, aHeight,
aScaleFactor, aCssBackground) {
_captureRemoteThumbnail: function (aBrowser, aCanvas) {
let deferred = Promise.defer();
// The index we send with the request so we can identify the
@ -308,18 +300,13 @@ this.PageThumbs = {
// xxx wish there was a way to skip this encoding step
reader.readAsDataURL(imageBlob);
}
mm.addMessageListener("Browser:Thumbnail:Response", thumbFunc);
// Send a thumbnail request
let width = aWidth || 0;
let height = aHeight || 0;
let scale = aScaleFactor || 1.0;
let background = aCssBackground || "#fff";
mm.addMessageListener("Browser:Thumbnail:Response", thumbFunc);
mm.sendAsyncMessage("Browser:Thumbnail:Request", {
width: width,
height: height,
scale: scale,
background: background,
canvasWidth: aCanvas.width,
canvasHeight: aCanvas.height,
background: PageThumbUtils.THUMBNAIL_BG_COLOR,
id: index
});

View File

@ -11,7 +11,8 @@ Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import("resource://gre/modules/RemoteAddonsChild.jsm");
Cu.import("resource://gre/modules/Timer.jsm");
const HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
XPCOMUtils.defineLazyModuleGetter(this, "PageThumbUtils",
"resource://gre/modules/PageThumbUtils.jsm");
#ifdef MOZ_CRASHREPORTER
XPCOMUtils.defineLazyServiceGetter(this, "CrashReporter",
@ -374,19 +375,20 @@ addMessageListener("UpdateCharacterSet", function (aMessage) {
* Remote thumbnail request handler for PageThumbs thumbnails.
*/
addMessageListener("Browser:Thumbnail:Request", function (aMessage) {
let thumbnail = content.document.createElementNS(HTML_NAMESPACE, "canvas");
let thumbnail = content.document.createElementNS(PageThumbUtils.HTML_NAMESPACE,
"canvas");
thumbnail.mozOpaque = true;
thumbnail.mozImageSmoothingEnabled = true;
// width and height are crop dims
let width = aMessage.data.width || content.innerWidth;
let height = aMessage.data.height || content.innerHeight;
thumbnail.width = Math.round(width * aMessage.data.scale);
thumbnail.height = Math.round(height * aMessage.data.scale);
thumbnail.width = aMessage.data.canvasWidth;
thumbnail.height = aMessage.data.canvasHeight;
let [width, height, scale] =
PageThumbUtils.determineCropSize(content, thumbnail);
let ctx = thumbnail.getContext("2d");
ctx.save();
ctx.scale(aMessage.data.scale, aMessage.data.scale);
ctx.scale(scale, scale);
ctx.drawWindow(content, 0, 0, width, height,
aMessage.data.background,
ctx.DRAWWINDOW_DO_NOT_FLUSH);