Bug 1587846 - [remote] Fix payload of return value for Page.captureScreenshot. r=remote-protocol-reviewers,ato
authorHenrik Skupin <mail@hskupin.info>
Thu, 07 Nov 2019 19:01:25 +0000
changeset 501146 e8155e813e54b0c8d00a5f11fc96560d212ef9aa
parent 501145 e84eb54d56518c979589913a5196293a231d4425
child 501147 cfee639c527a2329efe78accce8e296a8f9c7d83
push id100021
push userhskupin@mozilla.com
push dateThu, 07 Nov 2019 19:03:59 +0000
treeherderautoland@3ef1ca5d2649 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersremote-protocol-reviewers, ato
bugs1587846
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1587846 - [remote] Fix payload of return value for Page.captureScreenshot. r=remote-protocol-reviewers,ato The actual screenshot data should be returned via the "data" property of an object. Also the data URL prefix has to be stripped off, so that only the base64 encoded data will be transmitted. Differential Revision: https://phabricator.services.mozilla.com/D52144
remote/domains/parent/Page.jsm
remote/test/browser/page/browser_captureScreenshot.js
--- a/remote/domains/parent/Page.jsm
+++ b/remote/domains/parent/Page.jsm
@@ -115,17 +115,22 @@ class Page extends Domain {
     const ctx = canvas.getContext("2d");
     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();
 
-    return canvas.toDataURL();
+    const url = canvas.toDataURL();
+
+    // only return the base64 encoded data without the data URL prefix
+    const data = url.substring(url.indexOf(",") + 1);
+
+    return { data };
   }
 
   async enable() {
     if (this.enabled) {
       return;
     }
 
     this.enabled = true;
--- a/remote/test/browser/page/browser_captureScreenshot.js
+++ b/remote/test/browser/page/browser_captureScreenshot.js
@@ -4,72 +4,71 @@
 "use strict";
 
 async function getDevicePixelRatio() {
   return ContentTask.spawn(gBrowser.selectedBrowser, null, function() {
     return content.devicePixelRatio;
   });
 }
 
-async function getImageDetails(client, image) {
-  return ContentTask.spawn(gBrowser.selectedBrowser, image, async function(
-    image
-  ) {
-    let infoPromise = new Promise(resolve => {
-      const img = new content.Image();
-      img.addEventListener(
-        "load",
-        () => {
-          resolve({
-            width: img.width,
-            height: img.height,
-          });
-        },
-        { once: true }
-      );
-      img.src = image;
-    });
-    return infoPromise;
-  });
+async function getImageDetails(format, data) {
+  return ContentTask.spawn(
+    gBrowser.selectedBrowser,
+    { format, data },
+    async function({ format, data }) {
+      return new Promise(resolve => {
+        const img = new content.Image();
+        img.addEventListener(
+          "load",
+          () => {
+            resolve({
+              width: img.width,
+              height: img.height,
+            });
+          },
+          { once: true }
+        );
+
+        img.src = `data:image/${format};base64,${data}`;
+      });
+    }
+  );
 }
 
 async function getViewportRect() {
   return ContentTask.spawn(gBrowser.selectedBrowser, null, function() {
     return {
       left: content.pageXOffset,
       top: content.pageYOffset,
       width: content.innerWidth,
       height: content.innerHeight,
     };
   });
 }
 
-add_task(async function documentSmallerThanViewport(client) {
+add_task(async function documentSmallerThanViewport({ Page }) {
   loadURL(toDataURL("<div>Hello world"));
 
-  const { Page } = client;
-
   info("Check that captureScreenshot() captures the viewport by default");
-  const screenshot = await Page.captureScreenshot();
+  const { data } = await Page.captureScreenshot();
+  ok(!!data, "Screenshot data is not empty");
 
   const scale = await getDevicePixelRatio();
   const viewportRect = await getViewportRect();
-  const { width, height } = await getImageDetails(client, screenshot);
-
+  const { width, height } = await getImageDetails("png", data);
   is(width, (viewportRect.width - viewportRect.left) * scale);
   is(height, (viewportRect.height - viewportRect.top) * scale);
 });
 
-add_task(async function documentLargerThanViewport(client) {
+add_task(async function documentLargerThanViewport({ Page }) {
   loadURL(toDataURL("<div style='margin: 100vh 100vw'>Hello world"));
 
-  const { Page } = client;
-
   info("Check that captureScreenshot() captures the viewport by default");
-  const screenshot = await Page.captureScreenshot();
+  const { data } = await Page.captureScreenshot();
+  ok(!!data, "Screenshot data is not empty");
 
   const scale = await getDevicePixelRatio();
   const viewportRect = await getViewportRect();
-  const { width, height } = await getImageDetails(client, screenshot);
+  const { width, height } = await getImageDetails("png", data);
 
   is(width, (viewportRect.width - viewportRect.left) * scale);
   is(height, (viewportRect.height - viewportRect.top) * scale);
 });