From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- .../actors/AboutMessagePreviewChild.sys.mjs | 58 ++++++++++++++ .../actors/AboutMessagePreviewParent.sys.mjs | 93 ++++++++++++++++++++++ browser/components/messagepreview/jar.mn | 9 +++ browser/components/messagepreview/limelight.svg | 4 + .../components/messagepreview/messagepreview.css | 33 ++++++++ .../components/messagepreview/messagepreview.html | 29 +++++++ .../components/messagepreview/messagepreview.js | 39 +++++++++ browser/components/messagepreview/moz.build | 17 ++++ 8 files changed, 282 insertions(+) create mode 100644 browser/components/messagepreview/actors/AboutMessagePreviewChild.sys.mjs create mode 100644 browser/components/messagepreview/actors/AboutMessagePreviewParent.sys.mjs create mode 100644 browser/components/messagepreview/jar.mn create mode 100644 browser/components/messagepreview/limelight.svg create mode 100644 browser/components/messagepreview/messagepreview.css create mode 100644 browser/components/messagepreview/messagepreview.html create mode 100644 browser/components/messagepreview/messagepreview.js create mode 100644 browser/components/messagepreview/moz.build (limited to 'browser/components/messagepreview') 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..7651170fef --- /dev/null +++ b/browser/components/messagepreview/actors/AboutMessagePreviewParent.sys.mjs @@ -0,0 +1,93 @@ +/* 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 { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; +import { JsonSchema } from "resource://gre/modules/JsonSchema.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + SpecialMessageActions: + "resource://messaging-system/lib/SpecialMessageActions.sys.mjs", +}); +XPCOMUtils.defineLazyModuleGetters(lazy, { + InfoBar: "resource://activity-stream/lib/InfoBar.jsm", + Spotlight: "resource://activity-stream/lib/Spotlight.jsm", + CFRPageActions: "resource://activity-stream/lib/CFRPageActions.jsm", +}); + +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 + ); + } + + async showMessage(data) { + let message; + try { + message = JSON.parse(data); + } catch (e) { + console.error("Could not parse message", e); + return; + } + + const schema = await fetch( + "resource://activity-stream/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; + 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.`); + } + } +} diff --git a/browser/components/messagepreview/jar.mn b/browser/components/messagepreview/jar.mn new file mode 100644 index 0000000000..110476d794 --- /dev/null +++ b/browser/components/messagepreview/jar.mn @@ -0,0 +1,9 @@ +# 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/. + +browser.jar: + content/browser/messagepreview/messagepreview.html + content/browser/messagepreview/messagepreview.js + content/browser/messagepreview/limelight.svg + content/browser/messagepreview/messagepreview.css diff --git a/browser/components/messagepreview/limelight.svg b/browser/components/messagepreview/limelight.svg new file mode 100644 index 0000000000..938a17b3b2 --- /dev/null +++ b/browser/components/messagepreview/limelight.svg @@ -0,0 +1,4 @@ + + diff --git a/browser/components/messagepreview/messagepreview.css b/browser/components/messagepreview/messagepreview.css new file mode 100644 index 0000000000..f81c2d2623 --- /dev/null +++ b/browser/components/messagepreview/messagepreview.css @@ -0,0 +1,33 @@ +/* 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/. */ + +html { + position: fixed; +} + +html, +body { + height: 100%; + width: 100%; +} + +body { + background: url(chrome://browser/content/messagepreview/limelight.svg) + center/contain no-repeat; + -moz-context-properties: fill, fill-opacity; + fill: var(--in-content-icon-color); + fill-opacity: 0.2; +} + +.hint-box { + display: flex; + align-items: center; + justify-content: center; +} + +.hint { + max-width: 40em; + font-size: 1.2em; + text-align: center; +} diff --git a/browser/components/messagepreview/messagepreview.html b/browser/components/messagepreview/messagepreview.html new file mode 100644 index 0000000000..57169d3403 --- /dev/null +++ b/browser/components/messagepreview/messagepreview.html @@ -0,0 +1,29 @@ + + + + + + + + about:messagepreview + + + + + + + + diff --git a/browser/components/messagepreview/messagepreview.js b/browser/components/messagepreview/messagepreview.js new file mode 100644 index 0000000000..48e5fb1ff5 --- /dev/null +++ b/browser/components/messagepreview/messagepreview.js @@ -0,0 +1,39 @@ +/* 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/. */ + +/* global MPShowMessage, MPIsEnabled, MPShouldShowHint */ + +"use strict"; + +function decodeMessageFromUrl() { + const url = new URL(document.location.href); + + if (url.searchParams.has("json")) { + const encodedMessage = url.searchParams.get("json"); + + return atob(encodedMessage); + } + return null; +} + +function showHint() { + document.body.classList.add("hint-box"); + document.body.innerHTML = `
Message preview is not enabled. Enable it in about:config by setting browser.newtabpage.activity-stream.asrouter.devtoolsEnabled to true.
`; +} + +const message = decodeMessageFromUrl(); + +if (message) { + // If message preview is enabled, show the message. + if (MPIsEnabled()) { + MPShowMessage(message); + } else if (MPShouldShowHint()) { + // If running in a local build, show a hint about how to enable preview. + if (document.body) { + showHint(); + } else { + document.addEventListener("DOMContentLoaded", showHint, { once: true }); + } + } +} diff --git a/browser/components/messagepreview/moz.build b/browser/components/messagepreview/moz.build new file mode 100644 index 0000000000..5b528d73f7 --- /dev/null +++ b/browser/components/messagepreview/moz.build @@ -0,0 +1,17 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +JAR_MANIFESTS += ["jar.mn"] + +FINAL_LIBRARY = "browsercomps" + +with Files("**"): + BUG_COMPONENT = ("Firefox", "Messaging System") + +FINAL_TARGET_FILES.actors += [ + "actors/AboutMessagePreviewChild.sys.mjs", + "actors/AboutMessagePreviewParent.sys.mjs", +] -- cgit v1.2.3