1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 06:38:07 +02:00

Add the possibility to dispatch some pdf.js events at the chrome level (bug 1904585)

This commit is contained in:
Calixte Denizet 2024-06-25 16:52:47 +02:00
parent 11cb3a8e11
commit 35474f8ef4
4 changed files with 72 additions and 24 deletions

View file

@ -57,7 +57,7 @@ import {
version, version,
} from "pdfjs-lib"; } from "pdfjs-lib";
import { AppOptions, OptionKind } from "./app_options.js"; 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 { ExternalServices, initCom, MLManager } from "web-external_services";
import { LinkTarget, PDFLinkService } from "./pdf_link_service.js"; import { LinkTarget, PDFLinkService } from "./pdf_link_service.js";
import { AltTextManager } from "web-alt_text_manager"; import { AltTextManager } from "web-alt_text_manager";
@ -156,6 +156,7 @@ const PDFViewerApplication = {
isViewerEmbedded: window.parent !== window, isViewerEmbedded: window.parent !== window,
url: "", url: "",
baseUrl: "", baseUrl: "",
_allowedGlobalEventsPromise: null,
_downloadUrl: "", _downloadUrl: "",
_eventBusAbortController: null, _eventBusAbortController: null,
_windowAbortController: null, _windowAbortController: null,
@ -186,6 +187,10 @@ const PDFViewerApplication = {
// initialize the `L10n`-instance as soon as possible. // initialize the `L10n`-instance as soon as possible.
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) {
l10nPromise = this.externalServices.createL10n(); l10nPromise = this.externalServices.createL10n();
if (PDFJSDev.test("MOZCENTRAL")) {
this._allowedGlobalEventsPromise =
this.externalServices.getGlobalEventNames();
}
} }
this.appConfig = appConfig; this.appConfig = appConfig;
@ -386,10 +391,17 @@ const PDFViewerApplication = {
*/ */
async _initializeViewerComponents() { async _initializeViewerComponents() {
const { appConfig, externalServices, l10n } = this; const { appConfig, externalServices, l10n } = this;
let eventBus;
const eventBus = AppOptions.get("isInAutomation") if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
? new AutomationEventBus() eventBus = new FirefoxEventBus(
: new EventBus(); await this._allowedGlobalEventsPromise,
externalServices,
AppOptions.get("isInAutomation")
);
this._allowedGlobalEventsPromise = null;
} else {
eventBus = new EventBus();
}
this.eventBus = eventBus; this.eventBus = eventBus;
this.overlayManager = new OverlayManager(); this.overlayManager = new OverlayManager();

View file

@ -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) { dispatch(eventName, data) {
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("MOZCENTRAL")) { 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); super.dispatch(eventName, data);
const detail = Object.create(null); if (this.#isInAutomation) {
if (data) { const detail = Object.create(null);
for (const key in data) { if (data) {
const value = data[key]; for (const key in data) {
if (key === "source") { const value = data[key];
if (value === window || value === document) { if (key === "source") {
return; // No need to re-dispatch (already) global events. 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 };

View file

@ -46,6 +46,12 @@ class BaseExternalServices {
} }
async getNimbusExperimentData() {} async getNimbusExperimentData() {}
async getGlobalEventNames() {
return null;
}
dispatchGlobalEvent(_event) {}
} }
export { BaseExternalServices }; export { BaseExternalServices };

View file

@ -407,6 +407,14 @@ class ExternalServices extends BaseExternalServices {
); );
return nimbusData && JSON.parse(nimbusData); return nimbusData && JSON.parse(nimbusData);
} }
async getGlobalEventNames() {
return FirefoxCom.requestAsync("getGlobalEventNames", null);
}
dispatchGlobalEvent(event) {
FirefoxCom.request("dispatchGlobalEvent", event);
}
} }
export { DownloadManager, ExternalServices, initCom, MLManager, Preferences }; export { DownloadManager, ExternalServices, initCom, MLManager, Preferences };