From 44427fa7b2d3ff240f8a824b513eb8d7efdc8a55 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 21 Mar 2024 16:25:44 +0100 Subject: [PATCH] Allow listening for preference changes in the Firefox PDF viewer (bug 1886586) --- web/app.js | 6 +----- web/preferences.js | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/web/app.js b/web/app.js index 2e87b64ce5f90..03d98ca2d2bb0 100644 --- a/web/app.js +++ b/web/app.js @@ -799,11 +799,7 @@ const PDFViewerApplication = { }, get supportsCaretBrowsingMode() { - return shadow( - this, - "supportsCaretBrowsingMode", - AppOptions.get("supportsCaretBrowsingMode") - ); + return AppOptions.get("supportsCaretBrowsingMode"); }, moveCaret(isUp, select) { diff --git a/web/preferences.js b/web/preferences.js index 312176fa864f5..9b764d90867cd 100644 --- a/web/preferences.js +++ b/web/preferences.js @@ -21,6 +21,12 @@ import { AppOptions, OptionKind } from "./app_options.js"; * or every time the viewer is loaded. */ class BasePreferences { + #browserDefaults = Object.freeze( + typeof PDFJSDev === "undefined" + ? AppOptions.getAll(OptionKind.BROWSER, /* defaultOnly = */ true) + : PDFJSDev.eval("BROWSER_PREFERENCES") + ); + #defaults = Object.freeze( typeof PDFJSDev === "undefined" ? AppOptions.getAll(OptionKind.PREFERENCE, /* defaultOnly = */ true) @@ -46,13 +52,11 @@ class BasePreferences { this.#initializedPromise = this._readFromStorage(this.#defaults).then( ({ browserPrefs, prefs }) => { - const BROWSER_PREFS = - typeof PDFJSDev === "undefined" - ? AppOptions.getAll(OptionKind.BROWSER, /* defaultOnly = */ true) - : PDFJSDev.eval("BROWSER_PREFERENCES"); const options = Object.create(null); - for (const [name, defaultVal] of Object.entries(BROWSER_PREFS)) { + for (const [name, defaultVal] of Object.entries( + this.#browserDefaults + )) { const prefVal = browserPrefs?.[name]; options[name] = typeof prefVal === typeof defaultVal ? prefVal : defaultVal; @@ -64,6 +68,12 @@ class BasePreferences { typeof prefVal === typeof defaultVal ? prefVal : defaultVal; } AppOptions.setAll(options, /* init = */ true); + + if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { + window.addEventListener("updatedPreference", evt => { + this.#updatePref(evt.detail); + }); + } } ); } @@ -88,6 +98,26 @@ class BasePreferences { throw new Error("Not implemented: _readFromStorage"); } + #updatePref({ name, value }) { + if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { + throw new Error("Not implemented: #updatePref"); + } + + if (name in this.#browserDefaults) { + if (typeof value !== typeof this.#browserDefaults[name]) { + return; // Invalid preference value. + } + } else if (name in this.#defaults) { + if (typeof value !== typeof this.#defaults[name]) { + return; // Invalid preference value. + } + this.#prefs[name] = value; + } else { + return; // Invalid preference. + } + AppOptions.set(name, value); + } + /** * Reset the preferences to their default values and update storage. * @returns {Promise} A promise that is resolved when the preference values