gecko-dev/toolkit/components/extensions/parent/ext-contentScripts.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

197 lines
5.4 KiB
JavaScript

/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
/* exported registerContentScript, unregisterContentScript */
/* global registerContentScript, unregisterContentScript */
var {ExtensionUtils} = ChromeUtils.import("resource://gre/modules/ExtensionUtils.jsm");
var {
ExtensionError,
getUniqueId,
} = ExtensionUtils;
/**
* Represents (in the main browser process) a content script registered
* programmatically (instead of being included in the addon manifest).
*
* @param {ProxyContextParent} context
* The parent proxy context related to the extension context which
* has registered the content script.
* @param {RegisteredContentScriptOptions} details
* The options object related to the registered content script
* (which has the properties described in the content_scripts.json
* JSON API schema file).
*/
class ContentScriptParent {
constructor({context, details}) {
this.context = context;
this.scriptId = getUniqueId();
this.blobURLs = new Set();
this.options = this._convertOptions(details);
context.callOnClose(this);
}
close() {
this.destroy();
}
destroy() {
if (this.destroyed) {
throw new Error("Unable to destroy ContentScriptParent twice");
}
this.destroyed = true;
this.context.forgetOnClose(this);
for (const blobURL of this.blobURLs) {
this.context.cloneScope.URL.revokeObjectURL(blobURL);
}
this.blobURLs.clear();
this.context = null;
this.options = null;
}
_convertOptions(details) {
const {context} = this;
const options = {
matches: details.matches,
excludeMatches: details.excludeMatches,
includeGlobs: details.includeGlobs,
excludeGlobs: details.excludeGlobs,
allFrames: details.allFrames,
matchAboutBlank: details.matchAboutBlank,
runAt: details.runAt || "document_idle",
jsPaths: [],
cssPaths: [],
};
const convertCodeToURL = (data, mime) => {
const blob = new context.cloneScope.Blob(data, {type: mime});
const blobURL = context.cloneScope.URL.createObjectURL(blob);
this.blobURLs.add(blobURL);
return blobURL;
};
if (details.js && details.js.length > 0) {
options.jsPaths = details.js.map(data => {
if (data.file) {
return data.file;
}
return convertCodeToURL([data.code], "text/javascript");
});
}
if (details.css && details.css.length > 0) {
options.cssPaths = details.css.map(data => {
if (data.file) {
return data.file;
}
return convertCodeToURL([data.code], "text/css");
});
}
return options;
}
serialize() {
return this.options;
}
}
this.contentScripts = class extends ExtensionAPI {
getAPI(context) {
const {extension} = context;
// Map of the content script registered from the extension context.
//
// Map<scriptId -> ContentScriptParent>
const parentScriptsMap = new Map();
// Unregister all the scriptId related to a context when it is closed.
context.callOnClose({
close() {
if (parentScriptsMap.size === 0) {
return;
}
const scriptIds = Array.from(parentScriptsMap.keys());
for (let scriptId of scriptIds) {
extension.registeredContentScripts.delete(scriptId);
}
extension.updateContentScripts();
extension.broadcast("Extension:UnregisterContentScripts", {
id: extension.id,
scriptIds,
});
},
});
return {
contentScripts: {
async register(details) {
for (let origin of details.matches) {
if (!extension.whiteListedHosts.subsumes(new MatchPattern(origin))) {
throw new ExtensionError(`Permission denied to register a content script for ${origin}`);
}
}
const contentScript = new ContentScriptParent({context, details});
const {scriptId} = contentScript;
parentScriptsMap.set(scriptId, contentScript);
const scriptOptions = contentScript.serialize();
await extension.broadcast("Extension:RegisterContentScript", {
id: extension.id,
options: scriptOptions,
scriptId,
});
extension.registeredContentScripts.set(scriptId, scriptOptions);
extension.updateContentScripts();
return scriptId;
},
// This method is not available to the extension code, the extension code
// doesn't have access to the internally used scriptId, on the contrary
// the extension code will call script.unregister on the script API object
// that is resolved from the register API method returned promise.
async unregister(scriptId) {
const contentScript = parentScriptsMap.get(scriptId);
if (!contentScript) {
Cu.reportError(new Error(`No such content script ID: ${scriptId}`));
return;
}
parentScriptsMap.delete(scriptId);
extension.registeredContentScripts.delete(scriptId);
extension.updateContentScripts();
contentScript.destroy();
await extension.broadcast("Extension:UnregisterContentScripts", {
id: extension.id,
scriptIds: [scriptId],
});
},
},
};
}
};