Bug 1348442: Part 2b - Add getWinUtils helper. r?aswan draft
authorKris Maglione <maglione.k@gmail.com>
Sat, 18 Mar 2017 15:16:16 -0700
changeset 501331 2791d9758ecf7906eea334be29f9b38f7daa67d1
parent 501330 e75396bb9ec071396dbef3c0e6b66f6a52f329fe
child 501332 8998d4d545be46f0d84a0d32e73b5699db6750d1
push id49931
push usermaglione.k@gmail.com
push dateSun, 19 Mar 2017 23:22:27 +0000
reviewersaswan
bugs1348442
milestone55.0a1
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
@@ -233,22 +233,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|.
@@ -292,16 +286,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
@@ -1296,16 +1300,17 @@ this.ExtensionUtils = {
   detectLanguage,
   extend,
   findPathInObject,
   flushJarCache,
   getConsole,
   getInnerWindowID,
   getMessageManager,
   getUniqueId,
+  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();
     }
 
     addEventListener("DOMWindowCreated", this, true);
     addEventListener("load", this, true);
     addEventListener("DOMContentLoaded", this, true);
     addEventListener("DOMWindowClose", this, true);
     addEventListener("MozScrolledAreaChanged", this, true);
   },
@@ -65,18 +64,17 @@ const BrowserListener = {
 
   receiveMessage({name, data}) {
     if (name === "Extension:InitBrowser") {
       this.init(data);
     }
   },
 
   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) {