Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -156,6 +156,7 @@ const PDFViewerApplication = {
isViewerEmbedded: window.parent !== window,
url: "",
baseUrl: "",
_allowedGlobalEventsPromise: null,
_downloadUrl: "",
_eventBusAbortController: null,
_windowAbortController: null,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
60 changes: 41 additions & 19 deletions web/event_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
calixteman marked this conversation as resolved.
Show resolved Hide resolved
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 };
6 changes: 6 additions & 0 deletions web/external_services.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class BaseExternalServices {
}

async getNimbusExperimentData() {}

async getGlobalEventNames() {
return null;
}

dispatchGlobalEvent(_event) {}
}

export { BaseExternalServices };
8 changes: 8 additions & 0 deletions web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };