summaryrefslogtreecommitdiffstats
path: root/browser/components/messagepreview/actors
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /browser/components/messagepreview/actors
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/messagepreview/actors')
-rw-r--r--browser/components/messagepreview/actors/AboutMessagePreviewChild.sys.mjs58
-rw-r--r--browser/components/messagepreview/actors/AboutMessagePreviewParent.sys.mjs107
2 files changed, 165 insertions, 0 deletions
diff --git a/browser/components/messagepreview/actors/AboutMessagePreviewChild.sys.mjs b/browser/components/messagepreview/actors/AboutMessagePreviewChild.sys.mjs
new file mode 100644
index 0000000000..15c100328b
--- /dev/null
+++ b/browser/components/messagepreview/actors/AboutMessagePreviewChild.sys.mjs
@@ -0,0 +1,58 @@
+/* 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/. */
+
+import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
+
+export class AboutMessagePreviewChild extends JSWindowActorChild {
+ handleEvent(event) {
+ console.log(`Received page event ${event.type}`);
+ }
+
+ actorCreated() {
+ this.exportFunctions();
+ }
+
+ exportFunctions() {
+ if (this.contentWindow) {
+ for (const name of ["MPShowMessage", "MPIsEnabled", "MPShouldShowHint"]) {
+ Cu.exportFunction(this[name].bind(this), this.contentWindow, {
+ defineAs: name,
+ });
+ }
+ }
+ }
+
+ /**
+ * Check if the Message Preview feature is enabled. This reflects the value of
+ * the pref `browser.newtabpage.activity-stream.asrouter.devtoolsEnabled`.
+ *
+ * @returns {boolean}
+ */
+ MPIsEnabled() {
+ return Services.prefs.getBoolPref(
+ "browser.newtabpage.activity-stream.asrouter.devtoolsEnabled",
+ false
+ );
+ }
+
+ /**
+ * Route a message to the parent process to be displayed with the relevant
+ * messaging surface.
+ *
+ * @param {object} message
+ */
+ MPShowMessage(message) {
+ this.sendAsyncMessage(`MessagePreview:SHOW_MESSAGE`, message);
+ }
+
+ /**
+ * Check if a hint should be shown about how to enable Message Preview. The
+ * hint is only displayed in local/unofficial builds.
+ *
+ * @returns {boolean}
+ */
+ MPShouldShowHint() {
+ return !this.MPIsEnabled() && !AppConstants.MOZILLA_OFFICIAL;
+ }
+}
diff --git a/browser/components/messagepreview/actors/AboutMessagePreviewParent.sys.mjs b/browser/components/messagepreview/actors/AboutMessagePreviewParent.sys.mjs
new file mode 100644
index 0000000000..d19eb4329f
--- /dev/null
+++ b/browser/components/messagepreview/actors/AboutMessagePreviewParent.sys.mjs
@@ -0,0 +1,107 @@
+/* vim: set ts=2 sw=2 sts=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/. */
+
+import { JsonSchema } from "resource://gre/modules/JsonSchema.sys.mjs";
+
+const lazy = {};
+
+ChromeUtils.defineESModuleGetters(lazy, {
+ CFRPageActions: "resource:///modules/asrouter/CFRPageActions.sys.mjs",
+ FeatureCalloutBroker:
+ "resource:///modules/asrouter/FeatureCalloutBroker.sys.mjs",
+ InfoBar: "resource:///modules/asrouter/InfoBar.sys.mjs",
+ SpecialMessageActions:
+ "resource://messaging-system/lib/SpecialMessageActions.sys.mjs",
+ Spotlight: "resource:///modules/asrouter/Spotlight.sys.mjs",
+});
+
+function dispatchCFRAction({ type, data }, browser) {
+ if (type === "USER_ACTION") {
+ lazy.SpecialMessageActions.handleAction(data, browser);
+ }
+}
+
+export class AboutMessagePreviewParent extends JSWindowActorParent {
+ showInfoBar(message, browser) {
+ lazy.InfoBar.showInfoBarMessage(browser, message, dispatchCFRAction);
+ }
+
+ showSpotlight(message, browser) {
+ lazy.Spotlight.showSpotlightDialog(browser, message, () => {});
+ }
+
+ showCFR(message, browser) {
+ lazy.CFRPageActions.forceRecommendation(
+ browser,
+ message,
+ dispatchCFRAction
+ );
+ }
+
+ showFeatureCallout(message, browser) {
+ switch (message.trigger.id) {
+ case "featureCalloutCheck":
+ // For messagePreview, force the trigger to be something we can show
+ message.trigger.id = "nthTabClosed";
+ lazy.FeatureCalloutBroker.showFeatureCallout(browser, message);
+ break;
+ default:
+ lazy.FeatureCalloutBroker.showFeatureCallout(browser, message);
+ }
+ }
+
+ async showMessage(data) {
+ let message;
+ try {
+ message = JSON.parse(data);
+ } catch (e) {
+ console.error("Could not parse message", e);
+ return;
+ }
+
+ const schema = await fetch(
+ "chrome://browser/content/asrouter/schemas/MessagingExperiment.schema.json",
+ { credentials: "omit" }
+ ).then(rsp => rsp.json());
+
+ const result = JsonSchema.validate(message, schema);
+ if (!result.valid) {
+ console.error(
+ `Invalid message: ${JSON.stringify(result.errors, undefined, 2)}`
+ );
+ }
+
+ const browser =
+ this.browsingContext.topChromeWindow.gBrowser.selectedBrowser;
+ switch (message.template) {
+ case "infobar":
+ this.showInfoBar(message, browser);
+ return;
+ case "spotlight":
+ this.showSpotlight(message, browser);
+ return;
+ case "cfr_doorhanger":
+ this.showCFR(message, browser);
+ return;
+ case "feature_callout":
+ this.showFeatureCallout(message, browser);
+ return;
+ default:
+ console.error(`Unsupported message template ${message.template}`);
+ }
+ }
+
+ receiveMessage(message) {
+ const { name, data } = message;
+
+ switch (name) {
+ case "MessagePreview:SHOW_MESSAGE":
+ this.showMessage(data);
+ break;
+ default:
+ console.log(`Unexpected event ${name} was not handled.`);
+ }
+ }
+}