summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/extensions/child
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /comm/mail/components/extensions/child
parentInitial commit. (diff)
downloadthunderbird-upstream.tar.xz
thunderbird-upstream.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--comm/mail/components/extensions/child/.eslintrc.js15
-rw-r--r--comm/mail/components/extensions/child/ext-extensionScripts.js83
-rw-r--r--comm/mail/components/extensions/child/ext-mail.js28
-rw-r--r--comm/mail/components/extensions/child/ext-menus.js290
-rw-r--r--comm/mail/components/extensions/child/ext-tabs.js23
5 files changed, 439 insertions, 0 deletions
diff --git a/comm/mail/components/extensions/child/.eslintrc.js b/comm/mail/components/extensions/child/.eslintrc.js
new file mode 100644
index 0000000000..970cd0874e
--- /dev/null
+++ b/comm/mail/components/extensions/child/.eslintrc.js
@@ -0,0 +1,15 @@
+"use strict";
+
+module.exports = {
+ globals: {
+ // These are defined in the WebExtension script scopes by ExtensionCommon.jsm.
+ // From toolkit/components/extensions/.eslintrc.js.
+ ExtensionAPI: true,
+ ExtensionCommon: true,
+ extensions: true,
+ ExtensionUtils: true,
+
+ // From toolkit/components/extensions/child/.eslintrc.js.
+ EventManager: true,
+ },
+};
diff --git a/comm/mail/components/extensions/child/ext-extensionScripts.js b/comm/mail/components/extensions/child/ext-extensionScripts.js
new file mode 100644
index 0000000000..5d5f364c3e
--- /dev/null
+++ b/comm/mail/components/extensions/child/ext-extensionScripts.js
@@ -0,0 +1,83 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var { ExtensionError } = ExtensionUtils;
+
+/**
+ * Represents (in the child extension process) a script registered
+ * programmatically (instead of being included in the addon manifest).
+ *
+ * @param {ExtensionPageContextChild} context
+ * The extension context which has registered the script.
+ * @param {string} scriptId
+ * An unique id that represents the registered script
+ * (generated and used internally to identify it across the different processes).
+ */
+class ExtensionScriptChild {
+ constructor(type, context, scriptId) {
+ this.type = type;
+ this.context = context;
+ this.scriptId = scriptId;
+ this.unregistered = false;
+ }
+
+ async unregister() {
+ if (this.unregistered) {
+ throw new ExtensionError("script already unregistered");
+ }
+
+ this.unregistered = true;
+
+ await this.context.childManager.callParentAsyncFunction(
+ "extensionScripts.unregister",
+ [this.scriptId]
+ );
+
+ this.context = null;
+ }
+
+ api() {
+ const { context } = this;
+
+ return {
+ unregister: () => {
+ return context.wrapPromise(this.unregister());
+ },
+ };
+ }
+}
+
+this.extensionScripts = class extends ExtensionAPI {
+ getAPI(context) {
+ let api = {
+ register(options) {
+ return context.cloneScope.Promise.resolve().then(async () => {
+ const scriptId = await context.childManager.callParentAsyncFunction(
+ "extensionScripts.register",
+ [this.type, options]
+ );
+
+ const registeredScript = new ExtensionScriptChild(
+ this.type,
+ context,
+ scriptId
+ );
+
+ return Cu.cloneInto(registeredScript.api(), context.cloneScope, {
+ cloneFunctions: true,
+ });
+ });
+ },
+ };
+
+ return {
+ composeScripts: { type: "compose", ...api },
+ messageDisplayScripts: { type: "messageDisplay", ...api },
+ };
+ }
+};
diff --git a/comm/mail/components/extensions/child/ext-mail.js b/comm/mail/components/extensions/child/ext-mail.js
new file mode 100644
index 0000000000..4c85692f91
--- /dev/null
+++ b/comm/mail/components/extensions/child/ext-mail.js
@@ -0,0 +1,28 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+extensions.registerModules({
+ extensionScripts: {
+ url: "chrome://messenger/content/child/ext-extensionScripts.js",
+ scopes: ["addon_child"],
+ paths: [["composeScripts"], ["messageDisplayScripts"]],
+ },
+ identity: {
+ url: "chrome://extensions/content/child/ext-identity.js",
+ scopes: ["addon_child"],
+ paths: [["identity"]],
+ },
+ menus: {
+ url: "chrome://messenger/content/child/ext-menus.js",
+ scopes: ["addon_child"],
+ paths: [["menus"]],
+ },
+ tabs: {
+ url: "chrome://messenger/content/child/ext-tabs.js",
+ scopes: ["addon_child"],
+ paths: [["tabs"]],
+ },
+});
diff --git a/comm/mail/components/extensions/child/ext-menus.js b/comm/mail/components/extensions/child/ext-menus.js
new file mode 100644
index 0000000000..a8dab40b15
--- /dev/null
+++ b/comm/mail/components/extensions/child/ext-menus.js
@@ -0,0 +1,290 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var { withHandlingUserInput } = ExtensionCommon;
+
+var { ExtensionError } = ExtensionUtils;
+
+// If id is not specified for an item we use an integer.
+// This ID need only be unique within a single addon. Since all addon code that
+// can use this API runs in the same process, this local variable suffices.
+var gNextMenuItemID = 0;
+
+// Map[Extension -> Map[string or id, ContextMenusClickPropHandler]]
+var gPropHandlers = new Map();
+
+// The menus API supports an "onclick" attribute in the create/update
+// methods to register a callback. This class manages these onclick properties.
+class ContextMenusClickPropHandler {
+ constructor(context) {
+ this.context = context;
+ // Map[string or integer -> callback]
+ this.onclickMap = new Map();
+ this.dispatchEvent = this.dispatchEvent.bind(this);
+ }
+
+ // A listener on menus.onClicked that forwards the event to the only
+ // listener, if any.
+ dispatchEvent(info, tab) {
+ let onclick = this.onclickMap.get(info.menuItemId);
+ if (onclick) {
+ // No need for runSafe or anything because we are already being run inside
+ // an event handler -- the event is just being forwarded to the actual
+ // handler.
+ withHandlingUserInput(this.context.contentWindow, () =>
+ onclick(info, tab)
+ );
+ }
+ }
+
+ // Sets the `onclick` handler for the given menu item.
+ // The `onclick` function MUST be owned by `this.context`.
+ setListener(id, onclick) {
+ if (this.onclickMap.size === 0) {
+ this.context.childManager
+ .getParentEvent("menus.onClicked")
+ .addListener(this.dispatchEvent);
+ this.context.callOnClose(this);
+ }
+ this.onclickMap.set(id, onclick);
+
+ let propHandlerMap = gPropHandlers.get(this.context.extension);
+ if (!propHandlerMap) {
+ propHandlerMap = new Map();
+ } else {
+ // If the current callback was created in a different context, remove it
+ // from the other context.
+ let propHandler = propHandlerMap.get(id);
+ if (propHandler && propHandler !== this) {
+ propHandler.unsetListener(id);
+ }
+ }
+ propHandlerMap.set(id, this);
+ gPropHandlers.set(this.context.extension, propHandlerMap);
+ }
+
+ // Deletes the `onclick` handler for the given menu item.
+ // The `onclick` function MUST be owned by `this.context`.
+ unsetListener(id) {
+ if (!this.onclickMap.delete(id)) {
+ return;
+ }
+ if (this.onclickMap.size === 0) {
+ this.context.childManager
+ .getParentEvent("menus.onClicked")
+ .removeListener(this.dispatchEvent);
+ this.context.forgetOnClose(this);
+ }
+ let propHandlerMap = gPropHandlers.get(this.context.extension);
+ propHandlerMap.delete(id);
+ if (propHandlerMap.size === 0) {
+ gPropHandlers.delete(this.context.extension);
+ }
+ }
+
+ // Deletes the `onclick` handler for the given menu item, if any, regardless
+ // of the context where it was created.
+ unsetListenerFromAnyContext(id) {
+ let propHandlerMap = gPropHandlers.get(this.context.extension);
+ let propHandler = propHandlerMap && propHandlerMap.get(id);
+ if (propHandler) {
+ propHandler.unsetListener(id);
+ }
+ }
+
+ // Remove all `onclick` handlers of the extension.
+ deleteAllListenersFromExtension() {
+ let propHandlerMap = gPropHandlers.get(this.context.extension);
+ if (propHandlerMap) {
+ for (let [id, propHandler] of propHandlerMap) {
+ propHandler.unsetListener(id);
+ }
+ }
+ }
+
+ // Removes all `onclick` handlers from this context.
+ close() {
+ for (let id of this.onclickMap.keys()) {
+ this.unsetListener(id);
+ }
+ }
+}
+
+this.menus = class extends ExtensionAPI {
+ getAPI(context) {
+ let { extension } = context;
+ let onClickedProp = new ContextMenusClickPropHandler(context);
+ let pendingMenuEvent;
+
+ return {
+ menus: {
+ create(createProperties, callback) {
+ let caller = context.getCaller();
+
+ if (extension.persistentBackground && createProperties.id === null) {
+ createProperties.id = ++gNextMenuItemID;
+ }
+ let { onclick } = createProperties;
+ if (onclick && !context.extension.persistentBackground) {
+ throw new ExtensionError(
+ `Property "onclick" cannot be used in menus.create, replace with an "onClicked" event listener.`
+ );
+ }
+ delete createProperties.onclick;
+ context.childManager
+ .callParentAsyncFunction("menus.create", [createProperties])
+ .then(() => {
+ if (onclick) {
+ onClickedProp.setListener(createProperties.id, onclick);
+ }
+ if (callback) {
+ context.runSafeWithoutClone(callback);
+ }
+ })
+ .catch(error => {
+ context.withLastError(error, caller, () => {
+ if (callback) {
+ context.runSafeWithoutClone(callback);
+ }
+ });
+ });
+ return createProperties.id;
+ },
+
+ update(id, updateProperties) {
+ let { onclick } = updateProperties;
+ if (onclick && !context.extension.persistentBackground) {
+ throw new ExtensionError(
+ `Property "onclick" cannot be used in menus.update, replace with an "onClicked" event listener.`
+ );
+ }
+ delete updateProperties.onclick;
+ return context.childManager
+ .callParentAsyncFunction("menus.update", [id, updateProperties])
+ .then(() => {
+ if (onclick) {
+ onClickedProp.setListener(id, onclick);
+ } else if (onclick === null) {
+ onClickedProp.unsetListenerFromAnyContext(id);
+ }
+ // else onclick is not set so it should not be changed.
+ });
+ },
+
+ remove(id) {
+ onClickedProp.unsetListenerFromAnyContext(id);
+ return context.childManager.callParentAsyncFunction("menus.remove", [
+ id,
+ ]);
+ },
+
+ removeAll() {
+ onClickedProp.deleteAllListenersFromExtension();
+
+ return context.childManager.callParentAsyncFunction(
+ "menus.removeAll",
+ []
+ );
+ },
+
+ overrideContext(contextOptions) {
+ let checkValidArg = (contextType, propKey) => {
+ if (contextOptions.context !== contextType) {
+ if (contextOptions[propKey]) {
+ throw new ExtensionError(
+ `Property "${propKey}" can only be used with context "${contextType}"`
+ );
+ }
+ return false;
+ }
+ if (contextOptions.showDefaults) {
+ throw new ExtensionError(
+ `Property "showDefaults" cannot be used with context "${contextType}"`
+ );
+ }
+ if (!contextOptions[propKey]) {
+ throw new ExtensionError(
+ `Property "${propKey}" is required for context "${contextType}"`
+ );
+ }
+ return true;
+ };
+ if (checkValidArg("tab", "tabId")) {
+ if (!context.extension.hasPermission("tabs")) {
+ throw new ExtensionError(
+ `The "tab" context requires the "tabs" permission.`
+ );
+ }
+ }
+ if (checkValidArg("bookmark", "bookmarkId")) {
+ if (!context.extension.hasPermission("bookmarks")) {
+ throw new ExtensionError(
+ `The "bookmark" context requires the "bookmarks" permission.`
+ );
+ }
+ }
+
+ let webExtContextData = {
+ extensionId: context.extension.id,
+ showDefaults: contextOptions.showDefaults,
+ overrideContext: contextOptions.context,
+ bookmarkId: contextOptions.bookmarkId,
+ tabId: contextOptions.tabId,
+ };
+
+ if (pendingMenuEvent) {
+ // overrideContext is called more than once during the same event.
+ pendingMenuEvent.webExtContextData = webExtContextData;
+ return;
+ }
+ pendingMenuEvent = {
+ webExtContextData,
+ observe(subject, topic, data) {
+ pendingMenuEvent = null;
+ Services.obs.removeObserver(this, "on-prepare-contextmenu");
+ subject = subject.wrappedJSObject;
+ if (context.principal.subsumes(subject.principal)) {
+ subject.setWebExtContextData(this.webExtContextData);
+ }
+ },
+ run() {
+ // "on-prepare-contextmenu" is expected to be observed before the
+ // end of the "contextmenu" event dispatch. This task is queued
+ // in case that does not happen, e.g. when the menu is not shown.
+ // ... or if the method was not called during a contextmenu event.
+ if (pendingMenuEvent === this) {
+ pendingMenuEvent = null;
+ Services.obs.removeObserver(this, "on-prepare-contextmenu");
+ }
+ },
+ };
+ Services.obs.addObserver(pendingMenuEvent, "on-prepare-contextmenu");
+ Services.tm.dispatchToMainThread(pendingMenuEvent);
+ },
+
+ onClicked: new EventManager({
+ context,
+ name: "menus.onClicked",
+ register: fire => {
+ let listener = (info, tab) => {
+ withHandlingUserInput(context.contentWindow, () =>
+ fire.sync(info, tab)
+ );
+ };
+
+ let event = context.childManager.getParentEvent("menus.onClicked");
+ event.addListener(listener);
+ return () => {
+ event.removeListener(listener);
+ };
+ },
+ }).api(),
+ },
+ };
+ }
+};
diff --git a/comm/mail/components/extensions/child/ext-tabs.js b/comm/mail/components/extensions/child/ext-tabs.js
new file mode 100644
index 0000000000..173c2b5f63
--- /dev/null
+++ b/comm/mail/components/extensions/child/ext-tabs.js
@@ -0,0 +1,23 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+this.tabs = class extends ExtensionAPI {
+ getAPI(context) {
+ return {
+ tabs: {
+ connect(tabId, options) {
+ let { frameId = null, name = "" } = options || {};
+ return context.messenger.connect({ name, tabId, frameId });
+ },
+
+ sendMessage(tabId, message, options, callback) {
+ let arg = { tabId, frameId: options?.frameId, message, callback };
+ return context.messenger.sendRuntimeMessage(arg);
+ },
+ },
+ };
+ }
+};