Bug 1368102: Part 7 - Remove ScriptMatcher and use WebExtensionConentScript directly. r?mixedpuppy
MozReview-Commit-ID: 5upkXMiivBn
--- 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();