Merge pull request #17646 from Snuffleupagus/app-break-import-cycles

Break import cycles, in the viewer, for `PDFViewerApplication`
This commit is contained in:
Jonas Jenwald 2024-02-10 23:22:49 +01:00 committed by GitHub
commit 485e9cecd7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 96 additions and 77 deletions

View File

@ -269,7 +269,6 @@ function createWebpackConfig(
const viewerAlias = {
"web-alt_text_manager": "web/alt_text_manager.js",
"web-annotation_editor_params": "web/annotation_editor_params.js",
"web-com": "",
"web-download_manager": "",
"web-external_services": "",
"web-null_l10n": "",
@ -291,7 +290,6 @@ function createWebpackConfig(
libraryAlias["display-fetch_stream"] = "src/display/fetch_stream.js";
libraryAlias["display-network"] = "src/display/network.js";
viewerAlias["web-com"] = "web/chromecom.js";
viewerAlias["web-download_manager"] = "web/download_manager.js";
viewerAlias["web-external_services"] = "web/chromecom.js";
viewerAlias["web-null_l10n"] = "web/l10n.js";
@ -306,7 +304,6 @@ function createWebpackConfig(
libraryAlias["display-node_stream"] = "src/display/node_stream.js";
libraryAlias["display-node_utils"] = "src/display/node_utils.js";
viewerAlias["web-com"] = "web/genericcom.js";
viewerAlias["web-download_manager"] = "web/download_manager.js";
viewerAlias["web-external_services"] = "web/genericcom.js";
viewerAlias["web-null_l10n"] = "web/genericl10n.js";
@ -321,7 +318,6 @@ function createWebpackConfig(
viewerAlias[key] = gvAlias[key] || "web/stubs-geckoview.js";
}
}
viewerAlias["web-com"] = "web/firefoxcom.js";
viewerAlias["web-download_manager"] = "web/firefoxcom.js";
viewerAlias["web-external_services"] = "web/firefoxcom.js";
viewerAlias["web-null_l10n"] = "web/l10n.js";

View File

@ -27,7 +27,6 @@
"web-alt_text_manager": "../../web/alt_text_manager.js",
"web-annotation_editor_params": "../../web/annotation_editor_params.js",
"web-com": "../../web/genericcom.js",
"web-download_manager": "../../web/download_manager.js",
"web-external_services": "../../web/genericcom.js",
"web-null_l10n": "../../web/genericl10n.js",

View File

@ -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";
@ -70,6 +70,7 @@ import { PDFHistory } from "./pdf_history.js";
import { PDFLayerViewer } from "web-pdf_layer_viewer";
import { PDFOutlineViewer } from "web-pdf_outline_viewer";
import { PDFPresentationMode } from "web-pdf_presentation_mode";
import { PDFPrintServiceFactory } from "web-print_service";
import { PDFRenderingQueue } from "./pdf_rendering_queue.js";
import { PDFScriptingManager } from "./pdf_scripting_manager.js";
import { PDFSidebar } from "web-pdf_sidebar";
@ -731,7 +732,7 @@ const PDFViewerApplication = {
},
get supportsPrinting() {
return PDFPrintServiceFactory.instance.supportsPrinting;
return PDFPrintServiceFactory.supportsPrinting;
},
get supportsFullscreen() {
@ -1786,7 +1787,7 @@ const PDFViewerApplication = {
const optionalContentConfigPromise =
this.pdfViewer.optionalContentConfigPromise;
const printService = PDFPrintServiceFactory.instance.createPrintService(
const printService = PDFPrintServiceFactory.createPrintService(
this.pdfDocument,
pagesOverview,
printContainer,
@ -2145,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",
@ -3234,14 +3241,4 @@ function webViewerReportTelemetry({ details }) {
PDFViewerApplication.externalServices.reportTelemetry(details);
}
/* Abstract factory for the print service. */
const PDFPrintServiceFactory = {
instance: {
supportsPrinting: false,
createPrintService() {
throw new Error("Not implemented: createPrintService");
},
},
};
export { PDFPrintServiceFactory, PDFViewerApplication };
export { PDFViewerApplication };

View File

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

View File

@ -20,7 +20,6 @@ import {
shadow,
} from "pdfjs-lib";
import { getXfaHtmlForPrinting } from "./print_utils.js";
import { PDFPrintServiceFactory } from "./app.js";
// Creates a placeholder with div and canvas with right size for the page.
function composePage(
@ -194,15 +193,16 @@ class FirefoxPrintService {
}
}
PDFPrintServiceFactory.instance = {
get supportsPrinting() {
/**
* @implements {IPDFPrintServiceFactory}
*/
class PDFPrintServiceFactory {
static get supportsPrinting() {
const canvas = document.createElement("canvas");
const value = "mozPrintCallback" in canvas;
return shadow(this, "supportsPrinting", "mozPrintCallback" in canvas);
}
return shadow(this, "supportsPrinting", value);
},
createPrintService(
static createPrintService(
pdfDocument,
pagesOverview,
printContainer,
@ -218,7 +218,7 @@ PDFPrintServiceFactory.instance = {
optionalContentConfigPromise,
printAnnotationStoragePromise
);
},
};
}
}
export { FirefoxPrintService };
export { PDFPrintServiceFactory };

View File

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

View File

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

View File

@ -217,4 +217,25 @@ class IL10n {
resume() {}
}
export { IDownloadManager, IL10n, IPDFLinkService, IRenderableView };
/**
* @interface
*/
class IPDFPrintServiceFactory {
static initGlobals() {}
static get supportsPrinting() {
return false;
}
static createPrintService() {
throw new Error("Not implemented: createPrintService");
}
}
export {
IDownloadManager,
IL10n,
IPDFLinkService,
IPDFPrintServiceFactory,
IRenderableView,
};

View File

@ -13,13 +13,13 @@
* limitations under the License.
*/
import { AnnotationMode, PixelsPerInch } from "pdfjs-lib";
import { PDFPrintServiceFactory, PDFViewerApplication } from "./app.js";
import { AnnotationMode, PixelsPerInch, shadow } from "pdfjs-lib";
import { getXfaHtmlForPrinting } from "./print_utils.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.");
}
@ -355,10 +355,19 @@ function ensureOverlay() {
return overlayPromise;
}
PDFPrintServiceFactory.instance = {
supportsPrinting: true,
/**
* @implements {IPDFPrintServiceFactory}
*/
class PDFPrintServiceFactory {
static initGlobals(app) {
viewerApp = app;
}
createPrintService(
static get supportsPrinting() {
return shadow(this, "supportsPrinting", true);
}
static createPrintService(
pdfDocument,
pagesOverview,
printContainer,
@ -378,7 +387,7 @@ PDFPrintServiceFactory.instance = {
printAnnotationStoragePromise
);
return activeService;
},
};
}
}
export { PDFPrintService };
export { PDFPrintServiceFactory };

View File

@ -60,7 +60,6 @@ See https://github.com/adobe-type-tools/cmap-resources
"web-alt_text_manager": "./stubs-geckoview.js",
"web-annotation_editor_params": "./stubs-geckoview.js",
"web-com": "./genericcom.js",
"web-download_manager": "./download_manager.js",
"web-external_services": "./genericcom.js",
"web-null_l10n": "./genericl10n.js",

View File

@ -13,8 +13,6 @@
* limitations under the License.
*/
import "web-com";
import "web-print_service";
import { RenderingStates, ScrollMode, SpreadMode } from "./ui_utils.js";
import { AppOptions } from "./app_options.js";
import { LinkTarget } from "./pdf_link_service.js";

View File

@ -69,7 +69,6 @@ See https://github.com/adobe-type-tools/cmap-resources
"web-alt_text_manager": "./alt_text_manager.js",
"web-annotation_editor_params": "./annotation_editor_params.js",
"web-com": "./genericcom.js",
"web-download_manager": "./download_manager.js",
"web-external_services": "./genericcom.js",
"web-null_l10n": "./genericl10n.js",

View File

@ -13,8 +13,6 @@
* limitations under the License.
*/
import "web-com";
import "web-print_service";
import { RenderingStates, ScrollMode, SpreadMode } from "./ui_utils.js";
import { AppOptions } from "./app_options.js";
import { LinkTarget } from "./pdf_link_service.js";