diff --git a/web/app.js b/web/app.js index d55ce59c9be9c..45dc9fd7e221b 100644 --- a/web/app.js +++ b/web/app.js @@ -57,7 +57,7 @@ import { version, } from "pdfjs-lib"; import { AppOptions, OptionKind } from "./app_options.js"; -import { AutomationEventBus, EventBus } from "./event_utils.js"; +import { EventBus, FirefoxEventBus } from "./event_utils.js"; import { ExternalServices, initCom, MLManager } from "web-external_services"; import { LinkTarget, PDFLinkService } from "./pdf_link_service.js"; import { AltTextManager } from "web-alt_text_manager"; @@ -156,6 +156,7 @@ const PDFViewerApplication = { isViewerEmbedded: window.parent !== window, url: "", baseUrl: "", + _allowedGlobalEventsPromise: null, _downloadUrl: "", _eventBusAbortController: null, _windowAbortController: null, @@ -186,6 +187,10 @@ const PDFViewerApplication = { // initialize the `L10n`-instance as soon as possible. if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) { l10nPromise = this.externalServices.createL10n(); + if (PDFJSDev.test("MOZCENTRAL")) { + this._allowedGlobalEventsPromise = + this.externalServices.getGlobalEventNames(); + } } this.appConfig = appConfig; @@ -386,10 +391,17 @@ const PDFViewerApplication = { */ async _initializeViewerComponents() { const { appConfig, externalServices, l10n } = this; - - const eventBus = AppOptions.get("isInAutomation") - ? new AutomationEventBus() - : new EventBus(); + let eventBus; + if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { + eventBus = new FirefoxEventBus( + await this._allowedGlobalEventsPromise, + externalServices, + AppOptions.get("isInAutomation") + ); + this._allowedGlobalEventsPromise = null; + } else { + eventBus = new EventBus(); + } this.eventBus = eventBus; this.overlayManager = new OverlayManager(); diff --git a/web/event_utils.js b/web/event_utils.js index 29c30166e9f3c..dfca497d53db9 100644 --- a/web/event_utils.js +++ b/web/event_utils.js @@ -170,35 +170,57 @@ class EventBus { } /** - * NOTE: Only used to support various PDF viewer tests in `mozilla-central`. + * NOTE: Only used in the Firefox build-in pdf viewer. */ -class AutomationEventBus extends EventBus { +class FirefoxEventBus extends EventBus { + #externalServices; + + #globalEventNames; + + #isInAutomation; + + constructor(globalEventNames, externalServices, isInAutomation) { + super(); + this.#globalEventNames = globalEventNames; + this.#externalServices = externalServices; + this.#isInAutomation = isInAutomation; + } + dispatch(eventName, data) { if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("MOZCENTRAL")) { - throw new Error("Not implemented: AutomationEventBus.dispatch"); + throw new Error("Not implemented: FirefoxEventBus.dispatch"); } super.dispatch(eventName, data); - const detail = Object.create(null); - if (data) { - for (const key in data) { - const value = data[key]; - if (key === "source") { - if (value === window || value === document) { - return; // No need to re-dispatch (already) global events. + if (this.#isInAutomation) { + const detail = Object.create(null); + if (data) { + for (const key in data) { + const value = data[key]; + if (key === "source") { + if (value === window || value === document) { + return; // No need to re-dispatch (already) global events. + } + continue; // Ignore the `source` property. } - continue; // Ignore the `source` property. + detail[key] = value; } - detail[key] = value; } + const event = new CustomEvent(eventName, { + bubbles: true, + cancelable: true, + detail, + }); + document.dispatchEvent(event); + } + + if (this.#globalEventNames?.has(eventName)) { + this.#externalServices.dispatchGlobalEvent({ + eventName, + detail: data, + }); } - const event = new CustomEvent(eventName, { - bubbles: true, - cancelable: true, - detail, - }); - document.dispatchEvent(event); } } -export { AutomationEventBus, EventBus, waitOnEventOrTimeout, WaitOnType }; +export { EventBus, FirefoxEventBus, waitOnEventOrTimeout, WaitOnType }; diff --git a/web/external_services.js b/web/external_services.js index fa1f086b484f1..8a3043833039e 100644 --- a/web/external_services.js +++ b/web/external_services.js @@ -46,6 +46,12 @@ class BaseExternalServices { } async getNimbusExperimentData() {} + + async getGlobalEventNames() { + return null; + } + + dispatchGlobalEvent(_event) {} } export { BaseExternalServices }; diff --git a/web/firefoxcom.js b/web/firefoxcom.js index babd142ee0eea..e431f4a081853 100644 --- a/web/firefoxcom.js +++ b/web/firefoxcom.js @@ -407,6 +407,14 @@ class ExternalServices extends BaseExternalServices { ); return nimbusData && JSON.parse(nimbusData); } + + async getGlobalEventNames() { + return FirefoxCom.requestAsync("getGlobalEventNames", null); + } + + dispatchGlobalEvent(event) { + FirefoxCom.request("dispatchGlobalEvent", event); + } } export { DownloadManager, ExternalServices, initCom, MLManager, Preferences };