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

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