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

Merge pull request #18332 from calixteman/bug1904585

Add the possibility to dispatch some pdf.js events at the chrome level (bug 1904585)
This commit is contained in:
calixteman 2024-06-25 21:15:15 +02:00 committed by GitHub
commit 2fbd61944b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 72 additions and 24 deletions

View file

@ -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();

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) {
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 };

View file

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

View file

@ -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 };