2015-10-13 18:52:26 +03: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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2018-06-08 15:16:29 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { InvalidArgumentError } = ChromeUtils.import(
|
|
|
|
"chrome://marionette/content/error.js"
|
|
|
|
);
|
|
|
|
const { Log } = ChromeUtils.import("chrome://marionette/content/log.js");
|
2018-02-26 20:42:56 +03:00
|
|
|
|
2018-08-28 14:40:11 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "logger", Log.get);
|
2018-06-08 15:16:29 +03:00
|
|
|
XPCOMUtils.defineLazyGlobalGetters(this, ["crypto"]);
|
2016-04-18 04:37:14 +03:00
|
|
|
|
2015-10-13 18:52:26 +03:00
|
|
|
this.EXPORTED_SYMBOLS = ["capture"];
|
|
|
|
|
|
|
|
const CONTEXT_2D = "2d";
|
|
|
|
const BG_COLOUR = "rgb(255,255,255)";
|
2019-08-27 16:01:01 +03:00
|
|
|
const MAX_CANVAS_DIMENSION = 32767;
|
2015-10-13 18:52:26 +03:00
|
|
|
const PNG_MIME = "image/png";
|
|
|
|
const XHTML_NS = "http://www.w3.org/1999/xhtml";
|
|
|
|
|
2017-07-26 15:11:53 +03:00
|
|
|
/**
|
|
|
|
* Provides primitives to capture screenshots.
|
|
|
|
*
|
|
|
|
* @namespace
|
|
|
|
*/
|
2015-10-13 18:52:26 +03:00
|
|
|
this.capture = {};
|
|
|
|
|
2016-12-20 17:30:48 +03:00
|
|
|
capture.Format = {
|
|
|
|
Base64: 0,
|
|
|
|
Hash: 1,
|
|
|
|
};
|
|
|
|
|
2015-10-13 18:52:26 +03:00
|
|
|
/**
|
2019-08-27 16:08:24 +03:00
|
|
|
* Draw a rectangle off the framebuffer.
|
2015-10-13 18:52:26 +03:00
|
|
|
*
|
2016-12-05 20:27:15 +03:00
|
|
|
* @param {DOMWindow} win
|
|
|
|
* The DOM window used for the framebuffer, and providing the interfaces
|
|
|
|
* for creating an HTMLCanvasElement.
|
2015-10-13 18:52:26 +03:00
|
|
|
* @param {number} left
|
|
|
|
* The left, X axis offset of the rectangle.
|
|
|
|
* @param {number} top
|
|
|
|
* The top, Y axis offset of the rectangle.
|
|
|
|
* @param {number} width
|
|
|
|
* The width dimension of the rectangle to paint.
|
|
|
|
* @param {number} height
|
|
|
|
* The height dimension of the rectangle to paint.
|
2017-05-09 19:23:47 +03:00
|
|
|
* @param {HTMLCanvasElement=} canvas
|
|
|
|
* Optional canvas to reuse for the screenshot.
|
2017-05-09 19:26:08 +03:00
|
|
|
* @param {number=} flags
|
|
|
|
* Optional integer representing flags to pass to drawWindow; these
|
|
|
|
* are defined on CanvasRenderingContext2D.
|
2019-08-27 16:01:01 +03:00
|
|
|
* @param {number=} dX
|
2019-08-27 16:08:24 +03:00
|
|
|
* Horizontal offset between the browser window and content area. Defaults to 0.
|
2019-08-27 16:01:01 +03:00
|
|
|
* @param {number=} dY
|
|
|
|
* Vertical offset between the browser window and content area. Defaults to 0.
|
|
|
|
* @param {boolean=} readback
|
|
|
|
* If true, read back a snapshot of the pixel data currently in the
|
|
|
|
* compositor/window. Defaults to false.
|
2015-10-13 18:52:26 +03:00
|
|
|
*
|
|
|
|
* @return {HTMLCanvasElement}
|
|
|
|
* The canvas on which the selection from the window's framebuffer
|
|
|
|
* has been painted on.
|
|
|
|
*/
|
2019-08-27 16:01:01 +03:00
|
|
|
capture.canvas = async function(
|
2017-06-30 02:40:24 +03:00
|
|
|
win,
|
2019-08-27 16:01:01 +03:00
|
|
|
browsingContext,
|
2017-06-30 02:40:24 +03:00
|
|
|
left,
|
|
|
|
top,
|
|
|
|
width,
|
|
|
|
height,
|
2019-08-27 16:01:01 +03:00
|
|
|
{ canvas = null, flags = null, dX = 0, dY = 0, readback = false } = {}
|
2017-05-09 19:26:08 +03:00
|
|
|
) {
|
2017-06-30 02:40:24 +03:00
|
|
|
const scale = win.devicePixelRatio;
|
2017-06-24 00:13:27 +03:00
|
|
|
|
2019-08-27 16:01:01 +03:00
|
|
|
let canvasHeight = height * scale;
|
|
|
|
let canvasWidth = width * scale;
|
|
|
|
|
|
|
|
// Cap the screenshot size for width and height at 2^16 pixels,
|
|
|
|
// which is the maximum allowed canvas size. Higher dimensions will
|
|
|
|
// trigger exceptions in Gecko.
|
|
|
|
if (canvasWidth > MAX_CANVAS_DIMENSION) {
|
|
|
|
logger.warn(
|
|
|
|
"Limiting screen capture width to maximum allowed " +
|
|
|
|
MAX_CANVAS_DIMENSION +
|
|
|
|
" pixels"
|
|
|
|
);
|
|
|
|
width = Math.floor(MAX_CANVAS_DIMENSION / scale);
|
|
|
|
canvasWidth = width * scale;
|
|
|
|
}
|
2018-08-28 14:40:11 +03:00
|
|
|
|
2019-08-27 16:01:01 +03:00
|
|
|
if (canvasHeight > MAX_CANVAS_DIMENSION) {
|
|
|
|
logger.warn(
|
|
|
|
"Limiting screen capture height to maximum allowed " +
|
|
|
|
MAX_CANVAS_DIMENSION +
|
|
|
|
" pixels"
|
|
|
|
);
|
|
|
|
height = Math.floor(MAX_CANVAS_DIMENSION / scale);
|
|
|
|
canvasHeight = height * scale;
|
|
|
|
}
|
2018-08-28 14:40:11 +03:00
|
|
|
|
2019-08-27 16:01:01 +03:00
|
|
|
if (canvas === null) {
|
2017-05-09 19:23:47 +03:00
|
|
|
canvas = win.document.createElementNS(XHTML_NS, "canvas");
|
2018-08-28 14:40:11 +03:00
|
|
|
canvas.width = canvasWidth;
|
|
|
|
canvas.height = canvasHeight;
|
2017-05-09 19:23:47 +03:00
|
|
|
}
|
2015-10-13 18:52:26 +03:00
|
|
|
|
2019-08-27 16:01:01 +03:00
|
|
|
const ctx = canvas.getContext(CONTEXT_2D);
|
2019-08-08 00:00:58 +03:00
|
|
|
|
2019-08-27 16:01:01 +03:00
|
|
|
if (readback) {
|
|
|
|
if (flags === null) {
|
|
|
|
flags =
|
|
|
|
ctx.DRAWWINDOW_DRAW_CARET |
|
|
|
|
ctx.DRAWWINDOW_DRAW_VIEW |
|
|
|
|
ctx.DRAWWINDOW_USE_WIDGET_LAYERS;
|
|
|
|
}
|
2016-12-28 16:13:31 +03:00
|
|
|
|
2019-08-27 16:01:01 +03:00
|
|
|
// drawWindow doesn't take scaling into account.
|
|
|
|
ctx.scale(scale, scale);
|
|
|
|
ctx.drawWindow(win, left + dX, top + dY, width, height, BG_COLOUR, flags);
|
|
|
|
} else {
|
|
|
|
let rect = new DOMRect(left, top, width, height);
|
|
|
|
let snapshot = await browsingContext.currentWindowGlobal.drawSnapshot(
|
|
|
|
rect,
|
|
|
|
scale,
|
|
|
|
BG_COLOUR
|
|
|
|
);
|
|
|
|
|
|
|
|
ctx.drawImage(snapshot, 0, 0);
|
|
|
|
|
|
|
|
// Bug 1574935 - Huge dimensions can trigger an OOM because multiple copies
|
|
|
|
// of the bitmap will exist in memory. Force the removal of the snapshot
|
|
|
|
// because it is no longer needed.
|
|
|
|
snapshot.close();
|
|
|
|
}
|
2015-10-13 18:52:26 +03:00
|
|
|
|
|
|
|
return canvas;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode the contents of an HTMLCanvasElement to a Base64 encoded string.
|
|
|
|
*
|
|
|
|
* @param {HTMLCanvasElement} canvas
|
|
|
|
* The canvas to encode.
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
* A Base64 encoded string.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
capture.toBase64 = function(canvas) {
|
2015-10-13 18:52:26 +03:00
|
|
|
let u = canvas.toDataURL(PNG_MIME);
|
|
|
|
return u.substring(u.indexOf(",") + 1);
|
|
|
|
};
|
2016-04-18 04:37:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hash the contents of an HTMLCanvasElement to a SHA-256 hex digest.
|
|
|
|
*
|
|
|
|
* @param {HTMLCanvasElement} canvas
|
|
|
|
* The canvas to encode.
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
* A hex digest of the SHA-256 hash of the base64 encoded string.
|
|
|
|
*/
|
2017-06-30 02:40:24 +03:00
|
|
|
capture.toHash = function(canvas) {
|
2016-04-18 04:37:14 +03:00
|
|
|
let u = capture.toBase64(canvas);
|
|
|
|
let buffer = new TextEncoder("utf-8").encode(u);
|
|
|
|
return crypto.subtle.digest("SHA-256", buffer).then(hash => hex(hash));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert buffer into to hex.
|
|
|
|
*
|
|
|
|
* @param {ArrayBuffer} buffer
|
|
|
|
* The buffer containing the data to convert to hex.
|
|
|
|
*
|
|
|
|
* @return {string}
|
|
|
|
* A hex digest of the input buffer.
|
|
|
|
*/
|
|
|
|
function hex(buffer) {
|
|
|
|
let hexCodes = [];
|
|
|
|
let view = new DataView(buffer);
|
|
|
|
for (let i = 0; i < view.byteLength; i += 4) {
|
|
|
|
let value = view.getUint32(i);
|
|
|
|
let stringValue = value.toString(16);
|
2017-06-30 02:40:24 +03:00
|
|
|
let padding = "00000000";
|
2016-04-18 04:37:14 +03:00
|
|
|
let paddedValue = (padding + stringValue).slice(-padding.length);
|
|
|
|
hexCodes.push(paddedValue);
|
|
|
|
}
|
|
|
|
return hexCodes.join("");
|
2017-06-30 02:40:24 +03:00
|
|
|
}
|