Skip to content

Commit

Permalink
Merge pull request #18332 from calixteman/bug1904585
Browse files Browse the repository at this point in the history
Add the possibility to dispatch some pdf.js events at the chrome level (bug 1904585)
  • Loading branch information
calixteman authored Jun 25, 2024
2 parents 7f58618 + 35474f8 commit 2fbd619
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 24 deletions.
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) {
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 };

0 comments on commit 2fbd619

Please sign in to comment.