Bug 1368102: Part 7 - Remove ScriptMatcher and use WebExtensionConentScript directly. r?mixedpuppy draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 25 May 2017 21:37:51 -0700
changeset 585216 2acc1c9f2bc623d32a34044f15a1fc88abe9ef1c
parent 585215 123aafcd5e345ae1086494dcfb3ab99f8b744dbd
child 585217 56bcff99abdd98b20aa79f977737945c4ada1503
child 585256 eb34f6d0441f8b754dbfc92d5c2963bd70552b54
push id61052
push usermaglione.k@gmail.com
push dateFri, 26 May 2017 17:14:32 +0000
reviewersmixedpuppy
bugs1368102
milestone55.0a1
Bug 1368102: Part 7 - Remove ScriptMatcher and use WebExtensionConentScript directly. r?mixedpuppy MozReview-Commit-ID: 5upkXMiivBn
toolkit/components/extensions/ExtensionContent.jsm
toolkit/components/extensions/extension-process-script.js
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -222,32 +222,41 @@ class Script {
   compileScripts() {
     return this.js.map(url => this.scriptCache.get(url));
   }
 
   loadCSS() {
     return this.cssURLs.map(url => this.cssCache.get(url));
   }
 
+  preload() {
+    this.loadCSS();
+    this.compileScripts();
+  }
+
   cleanup(window) {
     if (!this.removeCss && this.cssURLs.length) {
       let winUtils = getWinUtils(window);
 
       let type = this.cssOrigin === "user" ? winUtils.USER_SHEET : winUtils.AUTHOR_SHEET;
       for (let url of this.cssURLs) {
         this.cssCache.deleteDocument(url, window.document);
         runSafeSyncWithoutClone(winUtils.removeSheetUsingURIString, url, type);
       }
 
       // Clear any sheets that were kept alive past their timeout as
       // a result of living in this document.
       this.cssCache.clear(CSS_EXPIRY_TIMEOUT_MS);
     }
   }
 
+  matchesWindow(window) {
+    return this.matcher.matchesWindow(window);
+  }
+
   async injectInto(window) {
     let context = this.extension.getContext(window);
 
     if (this.runAt === "document_end") {
       await promiseDocumentReady(window.document);
     } else if (this.runAt === "document_idle") {
       await promiseDocumentLoaded(window.document);
     }
--- a/toolkit/components/extensions/extension-process-script.js
+++ b/toolkit/components/extensions/extension-process-script.js
@@ -59,46 +59,20 @@ function parseScriptOptions(options) {
 }
 
 var extensions = new DefaultWeakMap(policy => {
   let extension = new ExtensionChild.BrowserExtensionContent(policy.initData);
   extension.policy = policy;
   return extension;
 });
 
-class ScriptMatcher {
-  constructor(matcher) {
-    this.matcher = matcher;
-
-    this._script = null;
-  }
-
-  get script() {
-    if (!this._script) {
-      this._script = new ExtensionContent.Script(extensions.get(this.matcher.extension),
-                                                 this.matcher);
-    }
-    return this._script;
-  }
-
-  preload() {
-    let {script} = this;
-
-    script.loadCSS();
-    script.compileScripts();
-  }
-
-  matchesWindow(window) {
-    return this.matcher.matchesWindow(window);
-  }
-
-  injectInto(window) {
-    return this.script.injectInto(window);
-  }
-}
+var contentScripts = new DefaultWeakMap(matcher => {
+  return new ExtensionContent.Script(extensions.get(matcher.extension),
+                                     matcher);
+});
 
 function getMessageManager(window) {
   let docShell = window.document.docShell.QueryInterface(Ci.nsIInterfaceRequestor);
   try {
     return docShell.getInterface(Ci.nsIContentFrameMessageManager);
   } catch (e) {
     // Some windows don't support this interface (hidden window).
     return null;
@@ -134,37 +108,35 @@ class ExtensionGlobal {
         return ExtensionContent.handleExtensionCapture(this.global, data.width, data.height, data.options);
       case "Extension:DetectLanguage":
         return ExtensionContent.handleDetectLanguage(this.global, target);
       case "Extension:Execute":
         let policy = WebExtensionPolicy.getByID(recipient.extensionId);
 
         let matcher = new WebExtensionContentScript(policy, parseScriptOptions(data.options));
 
-        let options = Object.assign(matcher, {
+        Object.assign(matcher, {
           wantReturnValue: data.options.wantReturnValue,
           removeCSS: data.options.remove_css,
           cssOrigin: data.options.cssOrigin,
           cssCode: data.options.cssCode,
           jsCode: data.options.jsCode,
         });
 
-        let script = new ScriptMatcher(options);
+        let script = contentScripts.get(matcher);
 
         return ExtensionContent.handleExtensionExecute(this.global, target, data.options, script);
       case "WebNavigation:GetFrame":
         return ExtensionContent.handleWebNavigationGetFrame(this.global, data.options);
       case "WebNavigation:GetAllFrames":
         return ExtensionContent.handleWebNavigationGetAllFrames(this.global);
     }
   }
 }
 
-let scriptMatchers = new DefaultWeakMap(matcher => new ScriptMatcher(matcher));
-
 // Responsible for creating ExtensionContexts and injecting content
 // scripts into them when new documents are created.
 DocumentManager = {
   globals: new Map(),
 
   // Initialize listeners that we need regardless of whether extensions are
   // enabled.
   earlyInit() {
@@ -256,17 +228,17 @@ DocumentManager = {
   },
 
   // Script loading
 
   injectExtensionScripts(extension) {
     for (let window of this.enumerateWindows()) {
       for (let script of extension.contentScripts) {
         if (script.matchesWindow(window)) {
-          scriptMatchers.get(script).injectInto(window);
+          contentScripts.get(script).injectInto(window);
         }
       }
     }
   },
 
   /**
    * Checks that all parent frames for the given withdow either have the
    * same add-on ID, or are special chrome-privileged documents such as
@@ -454,22 +426,22 @@ function ExtensionProcessScript() {
 
 ExtensionProcessScript.singleton = null;
 
 ExtensionProcessScript.prototype = {
   classID: Components.ID("{21f9819e-4cdf-49f9-85a0-850af91a5058}"),
   QueryInterface: XPCOMUtils.generateQI([Ci.mozIExtensionProcessScript]),
 
   preloadContentScript(contentScript) {
-    scriptMatchers.get(contentScript).preload();
+    contentScripts.get(contentScript).preload();
   },
 
   loadContentScript(contentScript, window) {
     if (DocumentManager.globals.has(getMessageManager(window))) {
-      scriptMatchers.get(contentScript).injectInto(window);
+      contentScripts.get(contentScript).injectInto(window);
     }
   },
 };
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ExtensionProcessScript]);
 
 DocumentManager.earlyInit();
 ExtensionManager.init();