From 2f16ad43cbd04abcb905f30115f6d00ae883d17f Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sun, 4 Jun 2017 13:29:03 -0700 Subject: [PATCH] Bug 1368102: Part 7 - Remove ScriptMatcher and use WebExtensionConentScript directly. r=mixedpuppy MozReview-Commit-ID: 5upkXMiivBn --HG-- extra : rebase_source : 9d16872a69576bbc364866dfd24d1ad6dc755d3e --- toolkit/components/extensions/Extension.jsm | 2 +- .../extensions/ExtensionContent.jsm | 9 ++++ .../extensions/extension-process-script.js | 47 ++++--------------- 3 files changed, 20 insertions(+), 38 deletions(-) diff --git a/toolkit/components/extensions/Extension.jsm b/toolkit/components/extensions/Extension.jsm index 51e2355b85fb..21f018a5d273 100644 --- a/toolkit/components/extensions/Extension.jsm +++ b/toolkit/components/extensions/Extension.jsm @@ -1112,7 +1112,7 @@ this.Extension = class extends ExtensionData { Management.emit("shutdown", this); this.emit("shutdown"); - Services.ppmm.broadcastAsyncMessage("Extension:Shutdown", {id: this.id}); + await this.broadcast("Extension:Shutdown", {id: this.id}); MessageChannel.abortResponses({extensionId: this.id}); diff --git a/toolkit/components/extensions/ExtensionContent.jsm b/toolkit/components/extensions/ExtensionContent.jsm index ee8af787d821..1306eee893ef 100644 --- a/toolkit/components/extensions/ExtensionContent.jsm +++ b/toolkit/components/extensions/ExtensionContent.jsm @@ -227,6 +227,11 @@ class Script { 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); @@ -243,6 +248,10 @@ class Script { } } + matchesWindow(window) { + return this.matcher.matchesWindow(window); + } + async injectInto(window) { let context = this.extension.getContext(window); diff --git a/toolkit/components/extensions/extension-process-script.js b/toolkit/components/extensions/extension-process-script.js index 1baf225a80d7..e70b6b1b26ad 100644 --- a/toolkit/components/extensions/extension-process-script.js +++ b/toolkit/components/extensions/extension-process-script.js @@ -64,36 +64,10 @@ var extensions = new DefaultWeakMap(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); @@ -139,7 +113,7 @@ class ExtensionGlobal { 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.css_origin, @@ -147,7 +121,7 @@ class ExtensionGlobal { 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": @@ -158,8 +132,6 @@ class ExtensionGlobal { } } -let scriptMatchers = new DefaultWeakMap(matcher => new ScriptMatcher(matcher)); - // Responsible for creating ExtensionContexts and injecting content // scripts into them when new documents are created. DocumentManager = { @@ -261,7 +233,7 @@ DocumentManager = { 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); } } } @@ -428,6 +400,7 @@ ExtensionManager = { if (isContentProcess) { policy.active = false; } + Services.cpmm.sendAsyncMessage("Extension:ShutdownComplete"); break; } @@ -459,12 +432,12 @@ ExtensionProcessScript.prototype = { 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); } }, };