Bug 1348442: Part 2b - Add getWinUtils helper. r=aswan
authorKris Maglione <maglione.k@gmail.com>
Sat, 18 Mar 2017 15:16:16 -0700
changeset 349453 963bf8bab6e76ac7993af424088d8d7315475fa6
parent 349452 0b89dcab1adaaf2fdd7a4d1d3a83c0ca58620e5e
child 349454 02ffd84b3a0bc6f250eae27af8ec3e67b92113b5
push id31551
push usercbook@mozilla.com
push dateFri, 24 Mar 2017 13:24:56 +0000
treeherdermozilla-central@4c987b7ed54a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan
bugs1348442
milestone55.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 1348442: Part 2b - Add getWinUtils helper. r=aswan MozReview-Commit-ID: 9vvCbYovyoN
toolkit/components/extensions/ExtensionContent.jsm
toolkit/components/extensions/ExtensionTabs.jsm
toolkit/components/extensions/ExtensionUtils.jsm
toolkit/components/extensions/ext-browser-content.js
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -56,16 +56,17 @@ Cu.import("resource://gre/modules/Extens
 
 const {
   DefaultMap,
   EventEmitter,
   LocaleData,
   defineLazyGetter,
   flushJarCache,
   getInnerWindowID,
+  getWinUtils,
   promiseDocumentReady,
   runSafeSyncWithoutClone,
 } = ExtensionUtils;
 
 const {
   BaseContext,
   SchemaAPIManager,
 } = ExtensionCommon;
@@ -296,18 +297,17 @@ Script.prototype = {
       return false;
     }
 
     return true;
   },
 
   cleanup(window) {
     if (!this.remove_css) {
-      let winUtils = window.QueryInterface(Ci.nsIInterfaceRequestor)
-                           .getInterface(Ci.nsIDOMWindowUtils);
+      let winUtils = getWinUtils(window);
 
       let type = this.css_origin === "user" ? winUtils.USER_SHEET : winUtils.AUTHOR_SHEET;
       for (let url of this.cssURLs) {
         runSafeSyncWithoutClone(winUtils.removeSheetUsingURIString, url, type);
       }
     }
   },
 
@@ -333,18 +333,17 @@ Script.prototype = {
    *        change.
    * @param {string} when
    *        The document's current load state, or if triggered by a
    *        document state change, the new document state that triggered
    *        the injection.
    */
   tryInject(window, sandbox, shouldRun, when) {
     if (this.cssURLs.length && shouldRun("document_start")) {
-      let winUtils = window.QueryInterface(Ci.nsIInterfaceRequestor)
-                           .getInterface(Ci.nsIDOMWindowUtils);
+      let winUtils = getWinUtils(window);
 
       let innerWindowID = winUtils.currentInnerWindowID;
 
       let type = this.css_origin === "user" ? winUtils.USER_SHEET : winUtils.AUTHOR_SHEET;
 
       if (this.remove_css) {
         for (let url of this.cssURLs) {
           runSafeSyncWithoutClone(winUtils.removeSheetUsingURIString, url, type);
@@ -1093,20 +1092,17 @@ class ExtensionGlobal {
     this.global = global;
 
     MessageChannel.addListener(global, "Extension:Capture", this);
     MessageChannel.addListener(global, "Extension:DetectLanguage", this);
     MessageChannel.addListener(global, "Extension:Execute", this);
     MessageChannel.addListener(global, "WebNavigation:GetFrame", this);
     MessageChannel.addListener(global, "WebNavigation:GetAllFrames", this);
 
-    this.windowId = global.content
-                          .QueryInterface(Ci.nsIInterfaceRequestor)
-                          .getInterface(Ci.nsIDOMWindowUtils)
-                          .outerWindowID;
+    this.windowId = getWinUtils(global.content).outerWindowID;
 
     global.sendAsyncMessage("Extension:TopWindowID", {windowId: this.windowId});
   }
 
   uninit() {
     this.global.sendAsyncMessage("Extension:RemoveTopWindowID", {windowId: this.windowId});
   }
 
--- a/toolkit/components/extensions/ExtensionTabs.jsm
+++ b/toolkit/components/extensions/ExtensionTabs.jsm
@@ -20,16 +20,17 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 
 Cu.import("resource://gre/modules/ExtensionUtils.jsm");
 
 const {
   DefaultMap,
   DefaultWeakMap,
   EventEmitter,
   ExtensionError,
+  getWinUtils,
 } = ExtensionUtils;
 
 /**
  * The platform-specific type of native tab objects, which are wrapped by
  * TabBase instances.
  *
  * @typedef {Object|XULElement} NativeTab
  */
@@ -1142,17 +1143,17 @@ class WindowTrackerBase extends EventEmi
 
     this._statusListeners = new DefaultWeakMap(listener => {
       return new StatusListener(listener);
     });
 
     this._windowIds = new DefaultWeakMap(window => {
       window.QueryInterface(Ci.nsIInterfaceRequestor);
 
-      return window.getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
+      return getWinUtils(window).outerWindowID;
     });
   }
 
   isBrowserWindow(window) {
     let {documentElement} = window.document;
 
     return documentElement.getAttribute("windowtype") === "navigator:browser";
   }
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -266,22 +266,16 @@ function runSafe(context, f, ...args) {
   }
   if (context.unloaded) {
     dump(`runSafe failure: context is already unloaded ${filterStack(new Error())}\n`);
     return undefined;
   }
   return runSafeWithoutClone(f, ...args);
 }
 
-function getInnerWindowID(window) {
-  return window.QueryInterface(Ci.nsIInterfaceRequestor)
-    .getInterface(Ci.nsIDOMWindowUtils)
-    .currentInnerWindowID;
-}
-
 // Return true if the given value is an instance of the given
 // native type.
 function instanceOf(value, type) {
   return {}.toString.call(value) == `[object ${type}]`;
 }
 
 // Extend the object |obj| with the property descriptors of each object in
 // |args|.
@@ -325,16 +319,26 @@ class DefaultMap extends Map {
   get(key) {
     if (!this.has(key)) {
       this.set(key, this.defaultConstructor(key));
     }
     return super.get(key);
   }
 }
 
+const _winUtils = new DefaultWeakMap(win => {
+  return win.QueryInterface(Ci.nsIInterfaceRequestor)
+            .getInterface(Ci.nsIDOMWindowUtils);
+});
+const getWinUtils = win => _winUtils.get(win);
+
+function getInnerWindowID(window) {
+  return getWinUtils(window).currentInnerWindowID;
+}
+
 class SpreadArgs extends Array {
   constructor(args) {
     super();
     this.push(...args);
   }
 }
 
 // Manages icon details for toolbar buttons in the |pageAction| and
@@ -1330,16 +1334,17 @@ this.ExtensionUtils = {
   extend,
   findPathInObject,
   flushJarCache,
   getConsole,
   getInnerWindowID,
   getMessageManager,
   getUniqueId,
   filterStack,
+  getWinUtils,
   ignoreEvent,
   injectAPI,
   instanceOf,
   normalizeTime,
   promiseDocumentLoaded,
   promiseDocumentReady,
   promiseEvent,
   promiseObserved,
--- a/toolkit/components/extensions/ext-browser-content.js
+++ b/toolkit/components/extensions/ext-browser-content.js
@@ -18,16 +18,17 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 XPCOMUtils.defineLazyModuleGetter(this, "setTimeout",
                                   "resource://gre/modules/Timer.jsm");
 
 XPCOMUtils.defineLazyGetter(this, "colorUtils", () => {
   return require("devtools/shared/css/color").colorUtils;
 });
 
 const {
+  getWinUtils,
   stylesheetMap,
 } = ExtensionUtils;
 
 /* globals addMessageListener, addEventListener, content, docShell, removeEventListener, sendAsyncMessage */
 
 // Minimum time between two resizes.
 const RESIZE_TIMEOUT = 100;
 
@@ -38,19 +39,17 @@ const BrowserListener = {
 
     this.isInline = isInline;
     this.maxWidth = maxWidth;
     this.maxHeight = maxHeight;
 
     this.oldBackground = null;
 
     if (allowScriptsToClose) {
-      content.QueryInterface(Ci.nsIInterfaceRequestor)
-             .getInterface(Ci.nsIDOMWindowUtils)
-             .allowScriptsToClose();
+      getWinUtils(content).allowScriptsToClose();
     }
 
     // Force external links to open in tabs.
     docShell.isAppTab = true;
 
     if (blockParser) {
       this.blockingPromise = new Promise(resolve => {
         this.unblockParser = resolve;
@@ -81,18 +80,17 @@ const BrowserListener = {
       if (this.unblockParser) {
         this.unblockParser();
         this.blockingPromise = null;
       }
     }
   },
 
   loadStylesheets() {
-    let winUtils = content.QueryInterface(Ci.nsIInterfaceRequestor)
-                          .getInterface(Ci.nsIDOMWindowUtils);
+    let winUtils = getWinUtils(content);
 
     for (let url of this.stylesheets) {
       winUtils.addSheet(stylesheetMap.get(url), winUtils.AGENT_SHEET);
     }
   },
 
   handleEvent(event) {
     switch (event.type) {