diff --git a/web/app.js b/web/app.js index 68c37d5bf..9e83ab481 100644 --- a/web/app.js +++ b/web/app.js @@ -84,29 +84,52 @@ const ViewOnLoad = { INITIAL: 1, }; -const DefaultExternalServices = { - updateFindControlState(data) {}, - updateFindMatchesCount(data) {}, - initPassiveLoading(callbacks) {}, - fallback(data, callback) {}, - reportTelemetry(data) {}, - createDownloadManager(options) { +class DefaultExternalServices { + constructor() { + throw new Error("Cannot initialize DefaultExternalServices."); + } + + static updateFindControlState(data) {} + + static updateFindMatchesCount(data) {} + + static initPassiveLoading(callbacks) {} + + static fallback(data, callback) {} + + static reportTelemetry(data) {} + + static createDownloadManager(options) { throw new Error("Not implemented: createDownloadManager"); - }, - createPreferences() { + } + + static createPreferences() { throw new Error("Not implemented: createPreferences"); - }, - createL10n(options) { + } + + static createL10n(options) { throw new Error("Not implemented: createL10n"); - }, - supportsIntegratedFind: false, - supportsDocumentFonts: true, - supportsDocumentColors: true, - supportedMouseWheelZoomModifierKeys: { - ctrlKey: true, - metaKey: true, - }, -}; + } + + static get supportsIntegratedFind() { + return shadow(this, "supportsIntegratedFind", false); + } + + static get supportsDocumentFonts() { + return shadow(this, "supportsDocumentFonts", true); + } + + static get supportsDocumentColors() { + return shadow(this, "supportsDocumentColors", true); + } + + static get supportedMouseWheelZoomModifierKeys() { + return shadow(this, "supportedMouseWheelZoomModifierKeys", { + ctrlKey: true, + metaKey: true, + }); + } +} const PDFViewerApplication = { initialBookmark: document.location.hash.substring(1), diff --git a/web/chromecom.js b/web/chromecom.js index 853f773e1..2853c8ccb 100644 --- a/web/chromecom.js +++ b/web/chromecom.js @@ -404,27 +404,30 @@ class ChromePreferences extends BasePreferences { } } -const ChromeExternalServices = Object.create(DefaultExternalServices); -ChromeExternalServices.initPassiveLoading = function(callbacks) { - const { overlayManager } = PDFViewerApplication; - // defaultUrl is set in viewer.js - ChromeCom.resolvePDFFile( - AppOptions.get("defaultUrl"), - overlayManager, - function(url, length, originalUrl) { - callbacks.onOpenWithURL(url, length, originalUrl); - } - ); -}; -ChromeExternalServices.createDownloadManager = function(options) { - return new DownloadManager(options); -}; -ChromeExternalServices.createPreferences = function() { - return new ChromePreferences(); -}; -ChromeExternalServices.createL10n = function(options) { - return new GenericL10n(navigator.language); -}; +class ChromeExternalServices extends DefaultExternalServices { + static initPassiveLoading(callbacks) { + // defaultUrl is set in viewer.js + ChromeCom.resolvePDFFile( + AppOptions.get("defaultUrl"), + PDFViewerApplication.overlayManager, + function(url, length, originalUrl) { + callbacks.onOpenWithURL(url, length, originalUrl); + } + ); + } + + static createDownloadManager(options) { + return new DownloadManager(options); + } + + static createPreferences() { + return new ChromePreferences(); + } + + static createL10n(options) { + return new GenericL10n(navigator.language); + } +} PDFViewerApplication.externalServices = ChromeExternalServices; export { ChromeCom }; diff --git a/web/firefoxcom.js b/web/firefoxcom.js index 83db636ce..e80ef3331 100644 --- a/web/firefoxcom.js +++ b/web/firefoxcom.js @@ -15,9 +15,9 @@ import "../extensions/firefox/tools/l10n"; import { createObjectURL, PDFDataRangeTransport, shadow } from "pdfjs-lib"; +import { DefaultExternalServices, PDFViewerApplication } from "./app.js"; import { BasePreferences } from "./preferences.js"; import { DEFAULT_SCALE_VALUE } from "./ui_utils.js"; -import { PDFViewerApplication } from "./app.js"; if ( typeof PDFJSDev === "undefined" || @@ -241,16 +241,16 @@ class FirefoxComDataRangeTransport extends PDFDataRangeTransport { } } -PDFViewerApplication.externalServices = { - updateFindControlState(data) { +class FirefoxExternalServices extends DefaultExternalServices { + static updateFindControlState(data) { FirefoxCom.request("updateFindControlState", data); - }, + } - updateFindMatchesCount(data) { + static updateFindMatchesCount(data) { FirefoxCom.request("updateFindMatchesCount", data); - }, + } - initPassiveLoading(callbacks) { + static initPassiveLoading(callbacks) { let pdfDataRangeTransport; window.addEventListener("message", function windowMessage(e) { @@ -309,52 +309,53 @@ PDFViewerApplication.externalServices = { } }); FirefoxCom.requestSync("initPassiveLoading", null); - }, + } - fallback(data, callback) { + static fallback(data, callback) { FirefoxCom.request("fallback", data, callback); - }, + } - reportTelemetry(data) { + static reportTelemetry(data) { FirefoxCom.request("reportTelemetry", JSON.stringify(data)); - }, + } - createDownloadManager(options) { + static createDownloadManager(options) { return new DownloadManager(options); - }, + } - createPreferences() { + static createPreferences() { return new FirefoxPreferences(); - }, + } - createL10n(options) { + static createL10n(options) { const mozL10n = document.mozL10n; // TODO refactor mozL10n.setExternalLocalizerServices return new MozL10n(mozL10n); - }, + } - get supportsIntegratedFind() { + static get supportsIntegratedFind() { const support = FirefoxCom.requestSync("supportsIntegratedFind"); return shadow(this, "supportsIntegratedFind", support); - }, + } - get supportsDocumentFonts() { + static get supportsDocumentFonts() { const support = FirefoxCom.requestSync("supportsDocumentFonts"); return shadow(this, "supportsDocumentFonts", support); - }, + } - get supportsDocumentColors() { + static get supportsDocumentColors() { const support = FirefoxCom.requestSync("supportsDocumentColors"); return shadow(this, "supportsDocumentColors", support); - }, + } - get supportedMouseWheelZoomModifierKeys() { + static get supportedMouseWheelZoomModifierKeys() { const support = FirefoxCom.requestSync( "supportedMouseWheelZoomModifierKeys" ); return shadow(this, "supportedMouseWheelZoomModifierKeys", support); - }, -}; + } +} +PDFViewerApplication.externalServices = FirefoxExternalServices; // l10n.js for Firefox extension expects services to be set. document.mozL10n.setExternalLocalizerServices({ diff --git a/web/genericcom.js b/web/genericcom.js index fce57da66..ef81fabdf 100644 --- a/web/genericcom.js +++ b/web/genericcom.js @@ -37,16 +37,19 @@ class GenericPreferences extends BasePreferences { } } -const GenericExternalServices = Object.create(DefaultExternalServices); -GenericExternalServices.createDownloadManager = function(options) { - return new DownloadManager(options); -}; -GenericExternalServices.createPreferences = function() { - return new GenericPreferences(); -}; -GenericExternalServices.createL10n = function({ locale = "en-US" }) { - return new GenericL10n(locale); -}; +class GenericExternalServices extends DefaultExternalServices { + static createDownloadManager(options) { + return new DownloadManager(options); + } + + static createPreferences() { + return new GenericPreferences(); + } + + static createL10n({ locale = "en-US" }) { + return new GenericL10n(locale); + } +} PDFViewerApplication.externalServices = GenericExternalServices; export { GenericCom };