summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/ExtensionActivityLog.jsm
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/components/extensions/ExtensionActivityLog.jsm
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/extensions/ExtensionActivityLog.jsm')
-rw-r--r--toolkit/components/extensions/ExtensionActivityLog.jsm127
1 files changed, 127 insertions, 0 deletions
diff --git a/toolkit/components/extensions/ExtensionActivityLog.jsm b/toolkit/components/extensions/ExtensionActivityLog.jsm
new file mode 100644
index 0000000000..1041e6234c
--- /dev/null
+++ b/toolkit/components/extensions/ExtensionActivityLog.jsm
@@ -0,0 +1,127 @@
+/* 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";
+
+const EXPORTED_SYMBOLS = ["ExtensionActivityLog"];
+
+const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
+const { XPCOMUtils } = ChromeUtils.import(
+ "resource://gre/modules/XPCOMUtils.jsm"
+);
+const { ExtensionUtils } = ChromeUtils.import(
+ "resource://gre/modules/ExtensionUtils.jsm"
+);
+ChromeUtils.defineModuleGetter(
+ this,
+ "ExtensionParent",
+ "resource://gre/modules/ExtensionParent.jsm"
+);
+XPCOMUtils.defineLazyGetter(this, "tabTracker", () => {
+ return ExtensionParent.apiManager.global.tabTracker;
+});
+
+var { DefaultMap } = ExtensionUtils;
+
+const MSG_SET_ENABLED = "Extension:ActivityLog:SetEnabled";
+const MSG_LOG = "Extension:ActivityLog:DoLog";
+
+const ExtensionActivityLog = {
+ initialized: false,
+
+ // id => Set(callbacks)
+ listeners: new DefaultMap(() => new Set()),
+ watchedIds: new Set(),
+
+ init() {
+ if (this.initialized) {
+ return;
+ }
+
+ this.initialized = true;
+
+ Services.ppmm.sharedData.set("extensions/logging", this.watchedIds);
+
+ Services.ppmm.addMessageListener(MSG_LOG, this);
+ },
+
+ /**
+ * Notify all listeners of an extension activity.
+ *
+ * @param {string} id The ID of the extension that caused the activity.
+ * @param {string} viewType The view type the activity is in.
+ * @param {string} type The type of the activity.
+ * @param {string} name The API name or path.
+ * @param {object} data Activity specific data.
+ * @param {string} timeStamp The timestamp for the activity.
+ */
+ log(id, viewType, type, name, data, timeStamp) {
+ if (!this.initialized) {
+ return;
+ }
+ let callbacks = this.listeners.get(id);
+ if (callbacks) {
+ if (!timeStamp) {
+ timeStamp = new Date();
+ }
+
+ for (let callback of callbacks) {
+ try {
+ callback({ id, viewType, timeStamp, type, name, data });
+ } catch (e) {
+ Cu.reportError(e);
+ }
+ }
+ }
+ },
+
+ addListener(id, callback) {
+ this.init();
+ let callbacks = this.listeners.get(id);
+ if (callbacks.size === 0) {
+ this.watchedIds.add(id);
+ Services.ppmm.sharedData.set("extensions/logging", this.watchedIds);
+ Services.ppmm.sharedData.flush();
+ Services.ppmm.broadcastAsyncMessage(MSG_SET_ENABLED, { id, value: true });
+ }
+ callbacks.add(callback);
+ },
+
+ removeListener(id, callback) {
+ let callbacks = this.listeners.get(id);
+ if (callbacks.size > 0) {
+ callbacks.delete(callback);
+ if (callbacks.size === 0) {
+ this.watchedIds.delete(id);
+ Services.ppmm.sharedData.set("extensions/logging", this.watchedIds);
+ Services.ppmm.sharedData.flush();
+ Services.ppmm.broadcastAsyncMessage(MSG_SET_ENABLED, {
+ id,
+ value: false,
+ });
+ }
+ }
+ },
+
+ receiveMessage({ name, data }) {
+ if (name === MSG_LOG) {
+ let { viewType, browsingContextId } = data;
+ if (browsingContextId && (!viewType || viewType == "tab")) {
+ let browser = BrowsingContext.get(browsingContextId).top
+ .embedderElement;
+ let browserData = tabTracker.getBrowserData(browser);
+ if (browserData && browserData.tabId !== undefined) {
+ data.data.tabId = browserData.tabId;
+ }
+ }
+ this.log(
+ data.id,
+ data.viewType,
+ data.type,
+ data.name,
+ data.data,
+ new Date(data.timeStamp)
+ );
+ }
+ },
+};