Bug 698371 - Move common PageThumbs code to PageThumbUtils. r=dao
☠☠ backed out by 5e9facc28af9 ☠ ☠
authorJim Mathies <jmathies@mozilla.com>
Fri, 17 Oct 2014 18:13:57 -0500
changeset 211215 17bbdeffa8a63c894023012e61cd85508059c398
parent 211214 e91747e0ee2fb768201ee4d2964c45225fdfb3dc
child 211216 d004bfd7f706abd404ec50881dfc388b312258e8
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersdao
bugs698371
milestone36.0a1
Bug 698371 - Move common PageThumbs code to PageThumbUtils. r=dao
toolkit/components/thumbnails/PageThumbUtils.jsm
toolkit/components/thumbnails/PageThumbs.jsm
toolkit/components/thumbnails/moz.build
new file mode 100644
--- /dev/null
+++ b/toolkit/components/thumbnails/PageThumbUtils.jsm
@@ -0,0 +1,102 @@
+/* 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/. */
+
+/*
+ * Common thumbnailing routines used by various consumers, including
+ * PageThumbs and backgroundPageThumbsContent.
+ */
+
+this.EXPORTED_SYMBOLS = ["PageThumbUtils"];
+
+const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
+Cu.import("resource://gre/modules/Services.jsm");
+
+this.PageThumbUtils = {
+  // The default background color for page thumbnails.
+  THUMBNAIL_BG_COLOR: "#fff",
+  // The namespace for thumbnail canvas elements.
+  HTML_NAMESPACE: "http://www.w3.org/1999/xhtml",
+
+  /**
+   * Creates a new canvas element in the context of aWindow, or if aWindow
+   * is undefined, in the context of hiddenDOMWindow.
+   *
+   * @param aWindow (optional) The document of this window will be used to
+   *  create the canvas.  If not given, the hidden window will be used.
+   * @return The newly created canvas.
+   */
+  createCanvas: function (aWindow) {
+    let doc = (aWindow || Services.appShell.hiddenDOMWindow).document;
+    let canvas = doc.createElementNS(this.HTML_NAMESPACE, "canvas");
+    canvas.mozOpaque = true;
+    canvas.mozImageSmoothingEnabled = true;
+    let [thumbnailWidth, thumbnailHeight] = this.getThumbnailSize();
+    canvas.width = thumbnailWidth;
+    canvas.height = thumbnailHeight;
+    return canvas;
+  },
+
+  /**
+   * Calculates a preferred initial thumbnail size based on current desktop
+   * dimensions. The resulting dims will generally be about 1/3 the
+   * size of the desktop. (jimm: why??)
+   *
+   * @return The calculated thumbnail size or a default if unable to calculate.
+   */
+  getThumbnailSize: function () {
+    if (!this._thumbnailWidth || !this._thumbnailHeight) {
+      let screenManager = Cc["@mozilla.org/gfx/screenmanager;1"]
+                            .getService(Ci.nsIScreenManager);
+      let left = {}, top = {}, width = {}, height = {};
+      screenManager.primaryScreen.GetRectDisplayPix(left, top, width, height);
+      this._thumbnailWidth = Math.round(width.value / 3);
+      this._thumbnailHeight = Math.round(height.value / 3);
+    }
+    return [this._thumbnailWidth, this._thumbnailHeight];
+  },
+
+  /**
+   * Determine a good thumbnail crop size and scale for a given content
+   * window.
+   *
+   * @param aWindow The content window.
+   * @param aCanvas The target canvas.
+   * @return An array containing width, height and scale.
+   */
+  determineCropSize: function (aWindow, aCanvas) {
+    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"}};
+
+    try {
+      utils.getScrollbarSize(false, sbWidth, sbHeight);
+    } catch (e) {
+      // This might fail if the window does not have a presShell.
+      Cu.reportError("Unable to get scrollbar size in determineCropSize.");
+      sbWidth.value = sbHeight.value = 0;
+    }
+
+    // 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: thumbnailWidth, height: thumbnailHeight} = aCanvas;
+    let scale = Math.min(Math.max(thumbnailWidth / sw, thumbnailHeight / sh), 1);
+    let scaledWidth = sw * scale;
+    let scaledHeight = sh * scale;
+
+    if (scaledHeight > thumbnailHeight)
+      sh -= Math.floor(Math.abs(scaledHeight - thumbnailHeight) * scale);
+
+    if (scaledWidth > thumbnailWidth)
+      sw -= Math.floor(Math.abs(scaledWidth - thumbnailWidth) * scale);
+
+    return [sw, sh, scale];
+  }
+};
--- a/toolkit/components/thumbnails/PageThumbs.jsm
+++ b/toolkit/components/thumbnails/PageThumbs.jsm
@@ -370,84 +370,23 @@ this.PageThumbs = {
    * Unregister an expiration filter.
    * @param aFilter A filter that was previously passed to addExpirationFilter.
    */
   removeExpirationFilter: function PageThumbs_removeExpirationFilter(aFilter) {
     PageThumbsExpiration.removeFilter(aFilter);
   },
 
   /**
-   * Determines the crop size for a given content window.
-   * @param aWindow The content window.
-   * @param aCanvas The target canvas.
-   * @return An array containing width, height and scale.
-   */
-  _determineCropSize: function PageThumbs_determineCropSize(aWindow, aCanvas) {
-    let utils = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
-                       .getInterface(Ci.nsIDOMWindowUtils);
-    let sbWidth = {}, sbHeight = {};
-
-    try {
-      utils.getScrollbarSize(false, sbWidth, sbHeight);
-    } catch (e) {
-      // This might fail if the window does not have a presShell.
-      Cu.reportError("Unable to get scrollbar size in _determineCropSize.");
-      sbWidth.value = sbHeight.value = 0;
-    }
-
-    // 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: thumbnailWidth, height: thumbnailHeight} = aCanvas;
-    let scale = Math.min(Math.max(thumbnailWidth / sw, thumbnailHeight / sh), 1);
-    let scaledWidth = sw * scale;
-    let scaledHeight = sh * scale;
-
-    if (scaledHeight > thumbnailHeight)
-      sh -= Math.floor(Math.abs(scaledHeight - thumbnailHeight) * scale);
-
-    if (scaledWidth > thumbnailWidth)
-      sw -= Math.floor(Math.abs(scaledWidth - thumbnailWidth) * scale);
-
-    return [sw, sh, scale];
-  },
-
-  /**
    * Creates a new hidden canvas element.
    * @param aWindow The document of this window will be used to create the
    *                canvas.  If not given, the hidden window will be used.
    * @return The newly created canvas.
    */
   createCanvas: function PageThumbs_createCanvas(aWindow) {
-    let doc = (aWindow || Services.appShell.hiddenDOMWindow).document;
-    let canvas = doc.createElementNS(HTML_NAMESPACE, "canvas");
-    canvas.mozOpaque = true;
-    canvas.mozImageSmoothingEnabled = true;
-    let [thumbnailWidth, thumbnailHeight] = this._getThumbnailSize();
-    canvas.width = thumbnailWidth;
-    canvas.height = thumbnailHeight;
-    return canvas;
-  },
-
-  /**
-   * Calculates the thumbnail size based on current desktop's dimensions.
-   * @return The calculated thumbnail size or a default if unable to calculate.
-   */
-  _getThumbnailSize: function PageThumbs_getThumbnailSize() {
-    if (!this._thumbnailWidth || !this._thumbnailHeight) {
-      let screenManager = Cc["@mozilla.org/gfx/screenmanager;1"]
-                            .getService(Ci.nsIScreenManager);
-      let left = {}, top = {}, width = {}, height = {};
-      screenManager.primaryScreen.GetRectDisplayPix(left, top, width, height);
-      this._thumbnailWidth = Math.round(width.value / 3);
-      this._thumbnailHeight = Math.round(height.value / 3);
-    }
-    return [this._thumbnailWidth, this._thumbnailHeight];
+    return PageThumbUtils.createCanvas(aWindow);
   },
 
   /**
    * Given a channel, returns true if it should be considered an "error
    * response", false otherwise.
    */
   _isChannelErrorResponse: function(channel) {
     // No valid document channel sounds like an error to me!
--- a/toolkit/components/thumbnails/moz.build
+++ b/toolkit/components/thumbnails/moz.build
@@ -10,11 +10,12 @@ EXTRA_COMPONENTS += [
     'BrowserPageThumbs.manifest',
     'PageThumbsProtocol.js',
 ]
 
 EXTRA_JS_MODULES += [
     'BackgroundPageThumbs.jsm',
     'PageThumbs.jsm',
     'PageThumbsWorker.js',
+    'PageThumbUtils.jsm',
 ]
 
 JAR_MANIFESTS += ['jar.mn']