From d1080e785a2cdbfb275195c650054faf9360c3cd Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 26 Jan 2024 12:04:54 +0100 Subject: [PATCH] Remove the `createPreferences` method from `DefaultExternalServices` Given the simplicity of the `createPreferences` method, we can leverage import maps to directly initialize the correct `Preferences`-instance depending on the build. --- gulpfile.mjs | 4 ++++ test/unit/unit_test.html | 1 + web/app.js | 7 ++----- web/chromecom.js | 8 ++------ web/firefoxcom.js | 8 ++------ web/genericcom.js | 8 ++------ web/viewer-geckoview.html | 1 + web/viewer.html | 1 + 8 files changed, 15 insertions(+), 23 deletions(-) diff --git a/gulpfile.mjs b/gulpfile.mjs index 58e78c7d7..2923f7935 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -281,6 +281,7 @@ function createWebpackConfig( "web-pdf_presentation_mode": "web/pdf_presentation_mode.js", "web-pdf_sidebar": "web/pdf_sidebar.js", "web-pdf_thumbnail_viewer": "web/pdf_thumbnail_viewer.js", + "web-preferences": "", "web-print_service": "", "web-secondary_toolbar": "web/secondary_toolbar.js", "web-toolbar": "web/toolbar.js", @@ -291,6 +292,7 @@ function createWebpackConfig( viewerAlias["web-com"] = "web/chromecom.js"; viewerAlias["web-download_manager"] = "web/download_manager.js"; + viewerAlias["web-preferences"] = "web/chromecom.js"; viewerAlias["web-print_service"] = "web/pdf_print_service.js"; } else if (bundleDefines.GENERIC) { // Aliases defined here must also be replicated in the paths section of @@ -304,6 +306,7 @@ function createWebpackConfig( viewerAlias["web-com"] = "web/genericcom.js"; viewerAlias["web-download_manager"] = "web/download_manager.js"; viewerAlias["web-l10n_utils"] = "web/l10n_utils.js"; + viewerAlias["web-preferences"] = "web/genericcom.js"; viewerAlias["web-print_service"] = "web/pdf_print_service.js"; } else if (bundleDefines.MOZCENTRAL) { if (bundleDefines.GECKOVIEW) { @@ -317,6 +320,7 @@ function createWebpackConfig( } viewerAlias["web-com"] = "web/firefoxcom.js"; viewerAlias["web-download_manager"] = "web/firefoxcom.js"; + viewerAlias["web-preferences"] = "web/firefoxcom.js"; viewerAlias["web-print_service"] = "web/firefox_print_service.js"; } const alias = { ...basicAlias, ...libraryAlias, ...viewerAlias }; diff --git a/test/unit/unit_test.html b/test/unit/unit_test.html index 804f3eaf8..4066ba2a7 100644 --- a/test/unit/unit_test.html +++ b/test/unit/unit_test.html @@ -39,6 +39,7 @@ "web-pdf_presentation_mode": "../../web/pdf_presentation_mode.js", "web-pdf_sidebar": "../../web/pdf_sidebar.js", "web-pdf_thumbnail_viewer": "../../web/pdf_thumbnail_viewer.js", + "web-preferences": "../../web/genericcom.js", "web-print_service": "../../web/pdf_print_service.js", "web-secondary_toolbar": "../../web/secondary_toolbar.js", "web-toolbar": "../../web/toolbar.js" diff --git a/web/app.js b/web/app.js index 0e258d2f3..1d50adfea 100644 --- a/web/app.js +++ b/web/app.js @@ -73,6 +73,7 @@ import { PDFScriptingManager } from "./pdf_scripting_manager.js"; import { PDFSidebar } from "web-pdf_sidebar"; import { PDFThumbnailViewer } from "web-pdf_thumbnail_viewer"; import { PDFViewer } from "./pdf_viewer.js"; +import { Preferences } from "web-preferences"; import { SecondaryToolbar } from "web-secondary_toolbar"; import { Toolbar } from "web-toolbar"; import { ViewHistory } from "./view_history.js"; @@ -99,10 +100,6 @@ class DefaultExternalServices { static reportTelemetry(data) {} - static createPreferences() { - throw new Error("Not implemented: createPreferences"); - } - static async createL10n() { throw new Error("Not implemented: createL10n"); } @@ -617,7 +614,7 @@ const PDFViewerApplication = { }, async run(config) { - this.preferences = this.externalServices.createPreferences(); + this.preferences = new Preferences(); await this.initialize(config); const { appConfig, eventBus } = this; diff --git a/web/chromecom.js b/web/chromecom.js index 97df08e12..e8f6b7952 100644 --- a/web/chromecom.js +++ b/web/chromecom.js @@ -325,7 +325,7 @@ function setReferer(url, callback) { // chrome.storage.local to chrome.storage.sync when needed. const storageArea = chrome.storage.sync || chrome.storage.local; -class ChromePreferences extends BasePreferences { +class Preferences extends BasePreferences { async _writeToStorage(prefObj) { return new Promise(resolve => { if (prefObj === this.defaults) { @@ -426,10 +426,6 @@ class ChromeExternalServices extends DefaultExternalServices { ); } - static createPreferences() { - return new ChromePreferences(); - } - static async createL10n() { return new GenericL10n(navigator.language); } @@ -440,4 +436,4 @@ class ChromeExternalServices extends DefaultExternalServices { } PDFViewerApplication.externalServices = ChromeExternalServices; -export { ChromeCom }; +export { ChromeCom, Preferences }; diff --git a/web/firefoxcom.js b/web/firefoxcom.js index 7fb802e19..2c2966b44 100644 --- a/web/firefoxcom.js +++ b/web/firefoxcom.js @@ -147,7 +147,7 @@ class DownloadManager { } } -class FirefoxPreferences extends BasePreferences { +class Preferences extends BasePreferences { async _readFromStorage(prefObj) { return FirefoxCom.requestAsync("getPreferences", prefObj); } @@ -382,10 +382,6 @@ class FirefoxExternalServices extends DefaultExternalServices { FirefoxCom.request("reportTelemetry", JSON.stringify(data)); } - static createPreferences() { - return new FirefoxPreferences(); - } - static updateEditorStates(data) { FirefoxCom.request("updateEditorStates", data); } @@ -412,4 +408,4 @@ class FirefoxExternalServices extends DefaultExternalServices { } PDFViewerApplication.externalServices = FirefoxExternalServices; -export { DownloadManager, FirefoxCom }; +export { DownloadManager, FirefoxCom, Preferences }; diff --git a/web/genericcom.js b/web/genericcom.js index 65bc4e63d..1ad633f5e 100644 --- a/web/genericcom.js +++ b/web/genericcom.js @@ -27,7 +27,7 @@ if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) { const GenericCom = {}; -class GenericPreferences extends BasePreferences { +class Preferences extends BasePreferences { async _writeToStorage(prefObj) { localStorage.setItem("pdfjs.preferences", JSON.stringify(prefObj)); } @@ -38,10 +38,6 @@ class GenericPreferences extends BasePreferences { } class GenericExternalServices extends DefaultExternalServices { - static createPreferences() { - return new GenericPreferences(); - } - static async createL10n() { return new GenericL10n(AppOptions.get("locale")); } @@ -52,4 +48,4 @@ class GenericExternalServices extends DefaultExternalServices { } PDFViewerApplication.externalServices = GenericExternalServices; -export { GenericCom }; +export { GenericCom, Preferences }; diff --git a/web/viewer-geckoview.html b/web/viewer-geckoview.html index 3e58d1673..1f72425e6 100644 --- a/web/viewer-geckoview.html +++ b/web/viewer-geckoview.html @@ -72,6 +72,7 @@ See https://github.com/adobe-type-tools/cmap-resources "web-pdf_presentation_mode": "./stubs-geckoview.js", "web-pdf_sidebar": "./stubs-geckoview.js", "web-pdf_thumbnail_viewer": "./stubs-geckoview.js", + "web-preferences": "./genericcom.js", "web-print_service": "./pdf_print_service.js", "web-secondary_toolbar": "./stubs-geckoview.js", "web-toolbar": "./toolbar-geckoview.js" diff --git a/web/viewer.html b/web/viewer.html index 84e8f1432..fa0c55c1d 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -81,6 +81,7 @@ See https://github.com/adobe-type-tools/cmap-resources "web-pdf_presentation_mode": "./pdf_presentation_mode.js", "web-pdf_sidebar": "./pdf_sidebar.js", "web-pdf_thumbnail_viewer": "./pdf_thumbnail_viewer.js", + "web-preferences": "./genericcom.js", "web-print_service": "./pdf_print_service.js", "web-secondary_toolbar": "./secondary_toolbar.js", "web-toolbar": "./toolbar.js"