From e98b9b019a1238871189e49cb7fa6a4c3816e2b1 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 9 Feb 2024 12:01:59 +0100 Subject: [PATCH] Break import cycles, in the viewer, for `PDFViewerApplication` Currently the `web/app.js` file pulls in various build-specific dependencies, via the use of import maps, and those files in turn import from `web/app.js` thus creating undesirable import cycles. To avoid this we instead pass in a `PDFViewerApplication`-reference, immediately after it's been created, to the relevant code. Note that we use an ESLint plugin rule, see `import/no-cycle`, that is normally able to catch import cycles. However, in this case import maps are involved which is why this wasn't caught. --- web/app.js | 8 +++++++- web/chromecom.js | 29 ++++++++++++++--------------- web/firefoxcom.js | 32 ++++++++++++++++++-------------- web/genericcom.js | 4 ++-- web/interfaces.js | 2 ++ web/pdf_print_service.js | 8 ++++++-- 6 files changed, 49 insertions(+), 34 deletions(-) diff --git a/web/app.js b/web/app.js index ea7ec64d1..39d73a096 100644 --- a/web/app.js +++ b/web/app.js @@ -53,12 +53,12 @@ import { } from "pdfjs-lib"; import { AppOptions, OptionKind } from "./app_options.js"; import { AutomationEventBus, EventBus } from "./event_utils.js"; +import { ExternalServices, initCom } from "web-external_services"; import { LinkTarget, PDFLinkService } from "./pdf_link_service.js"; import { AltTextManager } from "web-alt_text_manager"; import { AnnotationEditorParams } from "web-annotation_editor_params"; import { CaretBrowsingMode } from "./caret_browsing.js"; import { DownloadManager } from "web-download_manager"; -import { ExternalServices } from "web-external_services"; import { OverlayManager } from "./overlay_manager.js"; import { PasswordPrompt } from "./password_prompt.js"; import { PDFAttachmentViewer } from "web-pdf_attachment_viewer"; @@ -2146,6 +2146,12 @@ const PDFViewerApplication = { }, }; +initCom(PDFViewerApplication); + +if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { + PDFPrintServiceFactory.initGlobals(PDFViewerApplication); +} + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { const HOSTED_VIEWER_ORIGINS = [ "null", diff --git a/web/chromecom.js b/web/chromecom.js index 6ba500509..c1e3b3f10 100644 --- a/web/chromecom.js +++ b/web/chromecom.js @@ -19,7 +19,6 @@ import { BaseExternalServices } from "./external_services.js"; import { BasePreferences } from "./preferences.js"; import { GenericL10n } from "./genericl10n.js"; import { GenericScripting } from "./generic_scripting.js"; -import { PDFViewerApplication } from "./app.js"; if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) { throw new Error( @@ -42,10 +41,16 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("CHROME")) { } AppOptions.set("defaultUrl", defaultUrl); +})(); + +let viewerApp = { initialized: false }; +function initCom(app) { + viewerApp = app; + // Ensure that PDFViewerApplication.initialBookmark reflects the current hash, // in case the URL rewrite above results in a different hash. - PDFViewerApplication.initialBookmark = location.hash.slice(1); -})(); + viewerApp.initialBookmark = location.hash.slice(1); +} const ChromeCom = { /** @@ -77,10 +82,9 @@ const ChromeCom = { * Resolves a PDF file path and attempts to detects length. * * @param {string} file - Absolute URL of PDF file. - * @param {OverlayManager} overlayManager - Manager for the viewer overlays. * @param {Function} callback - A callback with resolved URL and file length. */ - resolvePDFFile(file, overlayManager, callback) { + resolvePDFFile(file, callback) { // Expand drive:-URLs to filesystem URLs (Chrome OS) file = file.replace( /^drive:/i, @@ -104,13 +108,9 @@ const ChromeCom = { // Even without this check, the file load in frames is still blocked, // but this may change in the future (https://crbug.com/550151). if (origin && !/^file:|^chrome-extension:/.test(origin)) { - PDFViewerApplication._documentError( - "Blocked " + - origin + - " from loading " + - file + - ". Refused to load a local file in a non-local page " + - "for security reasons." + viewerApp._documentError( + `Blocked ${origin} from loading ${file}. Refused to load ` + + "a local file in a non-local page for security reasons." ); return; } @@ -118,7 +118,7 @@ const ChromeCom = { if (isAllowedAccess) { callback(file); } else { - requestAccessToLocalFile(file, overlayManager, callback); + requestAccessToLocalFile(file, viewerApp.overlayManager, callback); } }); }); @@ -420,7 +420,6 @@ class ExternalServices extends BaseExternalServices { // defaultUrl is set in viewer.js ChromeCom.resolvePDFFile( AppOptions.get("defaultUrl"), - PDFViewerApplication.overlayManager, function (url, length, originalUrl) { callbacks.onOpenWithURL(url, length, originalUrl); } @@ -436,4 +435,4 @@ class ExternalServices extends BaseExternalServices { } } -export { ChromeCom, ExternalServices, Preferences }; +export { ExternalServices, initCom, Preferences }; diff --git a/web/firefoxcom.js b/web/firefoxcom.js index b788f2f29..e16238664 100644 --- a/web/firefoxcom.js +++ b/web/firefoxcom.js @@ -18,7 +18,6 @@ import { BaseExternalServices } from "./external_services.js"; import { BasePreferences } from "./preferences.js"; import { DEFAULT_SCALE_VALUE } from "./ui_utils.js"; import { L10n } from "./l10n.js"; -import { PDFViewerApplication } from "./app.js"; if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { throw new Error( @@ -26,6 +25,11 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { ); } +let viewerApp = { initialized: false }; +function initCom(app) { + viewerApp = app; +} + class FirefoxCom { /** * Creates an event that the extension is listening for and will @@ -167,14 +171,14 @@ class Preferences extends BasePreferences { const findLen = "find".length; const handleEvent = function ({ type, detail }) { - if (!PDFViewerApplication.initialized) { + if (!viewerApp.initialized) { return; } if (type === "findbarclose") { - PDFViewerApplication.eventBus.dispatch(type, { source: window }); + viewerApp.eventBus.dispatch(type, { source: window }); return; } - PDFViewerApplication.eventBus.dispatch("find", { + viewerApp.eventBus.dispatch("find", { source: window, type: type.substring(findLen), query: detail.query, @@ -194,18 +198,18 @@ class Preferences extends BasePreferences { (function listenZoomEvents() { const events = ["zoomin", "zoomout", "zoomreset"]; const handleEvent = function ({ type, detail }) { - if (!PDFViewerApplication.initialized) { + if (!viewerApp.initialized) { return; } // Avoid attempting to needlessly reset the zoom level *twice* in a row, // when using the `Ctrl + 0` keyboard shortcut. if ( type === "zoomreset" && - PDFViewerApplication.pdfViewer.currentScaleValue === DEFAULT_SCALE_VALUE + viewerApp.pdfViewer.currentScaleValue === DEFAULT_SCALE_VALUE ) { return; } - PDFViewerApplication.eventBus.dispatch(type, { source: window }); + viewerApp.eventBus.dispatch(type, { source: window }); }; for (const event of events) { @@ -215,10 +219,10 @@ class Preferences extends BasePreferences { (function listenSaveEvent() { const handleEvent = function ({ type, detail }) { - if (!PDFViewerApplication.initialized) { + if (!viewerApp.initialized) { return; } - PDFViewerApplication.eventBus.dispatch("download", { source: window }); + viewerApp.eventBus.dispatch("download", { source: window }); }; window.addEventListener("save", handleEvent); @@ -226,10 +230,10 @@ class Preferences extends BasePreferences { (function listenEditingEvent() { const handleEvent = function ({ detail }) { - if (!PDFViewerApplication.initialized) { + if (!viewerApp.initialized) { return; } - PDFViewerApplication.eventBus.dispatch("editingaction", { + viewerApp.eventBus.dispatch("editingaction", { source: window, name: detail.name, }); @@ -242,9 +246,9 @@ if (PDFJSDev.test("GECKOVIEW")) { (function listenQueryEvents() { window.addEventListener("pdf.js.query", async ({ detail: { queryId } }) => { let result = null; - if (queryId === "canDownloadInsteadOfPrint") { + if (viewerApp.initialized && queryId === "canDownloadInsteadOfPrint") { result = false; - const { pdfDocument, pdfViewer } = PDFViewerApplication; + const { pdfDocument, pdfViewer } = viewerApp; if (pdfDocument) { try { const hasUnchangedAnnotations = @@ -411,4 +415,4 @@ class ExternalServices extends BaseExternalServices { } } -export { DownloadManager, ExternalServices, FirefoxCom, Preferences }; +export { DownloadManager, ExternalServices, initCom, Preferences }; diff --git a/web/genericcom.js b/web/genericcom.js index f4c48f478..def4988a3 100644 --- a/web/genericcom.js +++ b/web/genericcom.js @@ -25,7 +25,7 @@ if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) { ); } -const GenericCom = {}; +function initCom(app) {} class Preferences extends BasePreferences { async _writeToStorage(prefObj) { @@ -47,4 +47,4 @@ class ExternalServices extends BaseExternalServices { } } -export { ExternalServices, GenericCom, Preferences }; +export { ExternalServices, initCom, Preferences }; diff --git a/web/interfaces.js b/web/interfaces.js index 10c9bb023..4c4403128 100644 --- a/web/interfaces.js +++ b/web/interfaces.js @@ -221,6 +221,8 @@ class IL10n { * @interface */ class IPDFPrintServiceFactory { + static initGlobals() {} + static get supportsPrinting() { return false; } diff --git a/web/pdf_print_service.js b/web/pdf_print_service.js index 389418f66..2e2af18d2 100644 --- a/web/pdf_print_service.js +++ b/web/pdf_print_service.js @@ -15,11 +15,11 @@ import { AnnotationMode, PixelsPerInch, shadow } from "pdfjs-lib"; import { getXfaHtmlForPrinting } from "./print_utils.js"; -import { PDFViewerApplication } from "./app.js"; let activeService = null; let dialog = null; let overlayManager = null; +let viewerApp = { initialized: false }; // Renders the page to the canvas of the given print service, and returns // the suggested dimensions of the output page. @@ -338,7 +338,7 @@ function ensureOverlay() { ); } if (!overlayPromise) { - overlayManager = PDFViewerApplication.overlayManager; + overlayManager = viewerApp.overlayManager; if (!overlayManager) { throw new Error("The overlay manager has not yet been initialized."); } @@ -359,6 +359,10 @@ function ensureOverlay() { * @implements {IPDFPrintServiceFactory} */ class PDFPrintServiceFactory { + static initGlobals(app) { + viewerApp = app; + } + static get supportsPrinting() { return shadow(this, "supportsPrinting", true); }