Skip to content

Commit

Permalink
Merge pull request #17208 from Snuffleupagus/browser-prefs-async-init
Browse files Browse the repository at this point in the history
[Firefox] Fetch browser preferences/options together with the viewer preferences (bug 1862192)
  • Loading branch information
Snuffleupagus authored Nov 1, 2023
2 parents 50c0fcc + ce9cfa2 commit 6df8972
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 83 deletions.
21 changes: 21 additions & 0 deletions gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ function createWebpackConfig(
BUNDLE_VERSION: versionInfo.version,
BUNDLE_BUILD: versionInfo.commit,
TESTING: defines.TESTING ?? process.env.TESTING === "true",
BROWSER_PREFERENCES: defaultPreferencesDir
? getBrowserPreferences(defaultPreferencesDir)
: {},
DEFAULT_PREFERENCES: defaultPreferencesDir
? getDefaultPreferences(defaultPreferencesDir)
: {},
Expand Down Expand Up @@ -811,17 +814,32 @@ async function parseDefaultPreferences(dir) {
"./" + DEFAULT_PREFERENCES_DIR + dir + "app_options.mjs"
);

const browserPrefs = AppOptions.getAll(OptionKind.BROWSER);
if (Object.keys(browserPrefs).length === 0) {
throw new Error("No browser preferences found.");
}
const prefs = AppOptions.getAll(OptionKind.PREFERENCE);
if (Object.keys(prefs).length === 0) {
throw new Error("No default preferences found.");
}

fs.writeFileSync(
DEFAULT_PREFERENCES_DIR + dir + "browser_preferences.json",
JSON.stringify(browserPrefs)
);
fs.writeFileSync(
DEFAULT_PREFERENCES_DIR + dir + "default_preferences.json",
JSON.stringify(prefs)
);
}

function getBrowserPreferences(dir) {
const str = fs
.readFileSync(DEFAULT_PREFERENCES_DIR + dir + "browser_preferences.json")
.toString();
return JSON.parse(str);
}

function getDefaultPreferences(dir) {
const str = fs
.readFileSync(DEFAULT_PREFERENCES_DIR + dir + "default_preferences.json")
Expand Down Expand Up @@ -1581,6 +1599,9 @@ function buildLib(defines, dir) {
BUNDLE_VERSION: versionInfo.version,
BUNDLE_BUILD: versionInfo.commit,
TESTING: defines.TESTING ?? process.env.TESTING === "true",
BROWSER_PREFERENCES: getBrowserPreferences(
defines.SKIP_BABEL ? "lib/" : "lib-legacy/"
),
DEFAULT_PREFERENCES: getDefaultPreferences(
defines.SKIP_BABEL ? "lib/" : "lib-legacy/"
),
Expand Down
72 changes: 31 additions & 41 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,37 +120,10 @@ class DefaultExternalServices {
throw new Error("Not implemented: createScripting");
}

static get supportsPinchToZoom() {
return shadow(this, "supportsPinchToZoom", true);
}

static get supportsIntegratedFind() {
return shadow(this, "supportsIntegratedFind", false);
}

static get supportsDocumentFonts() {
return shadow(this, "supportsDocumentFonts", true);
}

static get supportedMouseWheelZoomModifierKeys() {
return shadow(this, "supportedMouseWheelZoomModifierKeys", {
ctrlKey: true,
metaKey: true,
});
}

static get isInAutomation() {
return shadow(this, "isInAutomation", false);
}

static updateEditorStates(data) {
throw new Error("Not implemented: updateEditorStates");
}

static get canvasMaxAreaInBytes() {
return shadow(this, "canvasMaxAreaInBytes", -1);
}

static getNimbusExperimentData() {
return shadow(this, "getNimbusExperimentData", Promise.resolve(null));
}
Expand Down Expand Up @@ -421,7 +394,7 @@ const PDFViewerApplication = {
async _initializeViewerComponents() {
const { appConfig, externalServices, l10n } = this;

const eventBus = externalServices.isInAutomation
const eventBus = AppOptions.get("isInAutomation")
? new AutomationEventBus()
: new EventBus();
this.eventBus = eventBus;
Expand Down Expand Up @@ -722,7 +695,7 @@ const PDFViewerApplication = {
});
}

if (!this.supportsDocumentFonts) {
if (!AppOptions.get("supportsDocumentFonts")) {
AppOptions.set("disableFontFace", true);
this.l10n.get("pdfjs-web-fonts-disabled").then(msg => {
console.warn(msg);
Expand Down Expand Up @@ -825,15 +798,19 @@ const PDFViewerApplication = {
},

get supportsPinchToZoom() {
return this.externalServices.supportsPinchToZoom;
return shadow(
this,
"supportsPinchToZoom",
AppOptions.get("supportsPinchToZoom")
);
},

get supportsIntegratedFind() {
return this.externalServices.supportsIntegratedFind;
},

get supportsDocumentFonts() {
return this.externalServices.supportsDocumentFonts;
return shadow(
this,
"supportsIntegratedFind",
AppOptions.get("supportsIntegratedFind")
);
},

get loadingBar() {
Expand All @@ -842,8 +819,20 @@ const PDFViewerApplication = {
return shadow(this, "loadingBar", bar);
},

get supportedMouseWheelZoomModifierKeys() {
return this.externalServices.supportedMouseWheelZoomModifierKeys;
get supportsMouseWheelZoomCtrlKey() {
return shadow(
this,
"supportsMouseWheelZoomCtrlKey",
AppOptions.get("supportsMouseWheelZoomCtrlKey")
);
},

get supportsMouseWheelZoomMetaKey() {
return shadow(
this,
"supportsMouseWheelZoomMetaKey",
AppOptions.get("supportsMouseWheelZoomMetaKey")
);
},

initPassiveLoading(file) {
Expand Down Expand Up @@ -1034,7 +1023,7 @@ const PDFViewerApplication = {
// Set the necessary API parameters, using all the available options.
const apiParams = AppOptions.getAll(OptionKind.API);
const params = {
canvasMaxAreaInBytes: this.externalServices.canvasMaxAreaInBytes,
canvasMaxAreaInBytes: AppOptions.get("canvasMaxAreaInBytes"),
...apiParams,
...args,
};
Expand Down Expand Up @@ -2648,7 +2637,8 @@ function setZoomDisabledTimeout() {
function webViewerWheel(evt) {
const {
pdfViewer,
supportedMouseWheelZoomModifierKeys,
supportsMouseWheelZoomCtrlKey,
supportsMouseWheelZoomMetaKey,
supportsPinchToZoom,
} = PDFViewerApplication;

Expand Down Expand Up @@ -2687,8 +2677,8 @@ function webViewerWheel(evt) {

if (
isPinchToZoom ||
(evt.ctrlKey && supportedMouseWheelZoomModifierKeys.ctrlKey) ||
(evt.metaKey && supportedMouseWheelZoomModifierKeys.metaKey)
(evt.ctrlKey && supportsMouseWheelZoomCtrlKey) ||
(evt.metaKey && supportsMouseWheelZoomMetaKey)
) {
// Only zoom the pages, not the entire viewer.
evt.preventDefault();
Expand Down
47 changes: 45 additions & 2 deletions web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
}

const OptionKind = {
BROWSER: 0x01,
VIEWER: 0x02,
API: 0x04,
WORKER: 0x08,
Expand All @@ -53,6 +54,42 @@ const OptionKind = {
* primitive types and cannot rely on any imported types.
*/
const defaultOptions = {
canvasMaxAreaInBytes: {
/** @type {number} */
value: -1,
kind: OptionKind.BROWSER,
},
isInAutomation: {
/** @type {boolean} */
value: false,
kind: OptionKind.BROWSER,
},
supportsDocumentFonts: {
/** @type {boolean} */
value: true,
kind: OptionKind.BROWSER,
},
supportsIntegratedFind: {
/** @type {boolean} */
value: false,
kind: OptionKind.BROWSER,
},
supportsMouseWheelZoomCtrlKey: {
/** @type {boolean} */
value: true,
kind: OptionKind.BROWSER,
},
supportsMouseWheelZoomMetaKey: {
/** @type {boolean} */
value: true,
kind: OptionKind.BROWSER,
},
supportsPinchToZoom: {
/** @type {boolean} */
value: true,
kind: OptionKind.BROWSER,
},

annotationEditorMode: {
/** @type {number} */
value: 0,
Expand Down Expand Up @@ -363,10 +400,16 @@ class AppOptions {
for (const name in defaultOptions) {
const defaultOption = defaultOptions[name];
if (kind) {
if ((kind & defaultOption.kind) === 0) {
if (!(kind & defaultOption.kind)) {
continue;
}
if (kind === OptionKind.PREFERENCE) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("LIB")) &&
kind === OptionKind.PREFERENCE
) {
if (defaultOption.kind & OptionKind.BROWSER) {
throw new Error(`Invalid kind for preference: ${name}`);
}
const value = defaultOption.value,
valueType = typeof value;

Expand Down
2 changes: 1 addition & 1 deletion web/chromecom.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class ChromePreferences extends BasePreferences {
defaultPrefs = this.defaults;
}
storageArea.get(defaultPrefs, function (readPrefs) {
resolve(readPrefs);
resolve({ prefs: readPrefs });
});
};

Expand Down
36 changes: 1 addition & 35 deletions web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
import { isPdfFile, PDFDataRangeTransport, shadow } from "pdfjs-lib";
import { isPdfFile, PDFDataRangeTransport } from "pdfjs-lib";
import { BasePreferences } from "./preferences.js";
import { DEFAULT_SCALE_VALUE } from "./ui_utils.js";
import { L10n } from "./l10n.js";
Expand Down Expand Up @@ -434,40 +434,6 @@ class FirefoxExternalServices extends DefaultExternalServices {
return FirefoxScripting;
}

static get supportsPinchToZoom() {
const support = FirefoxCom.requestSync("supportsPinchToZoom");
return shadow(this, "supportsPinchToZoom", support);
}

static get supportsIntegratedFind() {
const support = FirefoxCom.requestSync("supportsIntegratedFind");
return shadow(this, "supportsIntegratedFind", support);
}

static get supportsDocumentFonts() {
const support = FirefoxCom.requestSync("supportsDocumentFonts");
return shadow(this, "supportsDocumentFonts", support);
}

static get supportedMouseWheelZoomModifierKeys() {
const support = FirefoxCom.requestSync(
"supportedMouseWheelZoomModifierKeys"
);
return shadow(this, "supportedMouseWheelZoomModifierKeys", support);
}

static get isInAutomation() {
// Returns the value of `Cu.isInAutomation`, which is only `true` when e.g.
// various test-suites are running in mozilla-central.
const isInAutomation = FirefoxCom.requestSync("isInAutomation");
return shadow(this, "isInAutomation", isInAutomation);
}

static get canvasMaxAreaInBytes() {
const maxArea = FirefoxCom.requestSync("getCanvasMaxArea");
return shadow(this, "canvasMaxAreaInBytes", maxArea);
}

static async getNimbusExperimentData() {
const nimbusData = await FirefoxCom.requestAsync(
"getNimbusExperimentData",
Expand Down
2 changes: 1 addition & 1 deletion web/genericcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class GenericPreferences extends BasePreferences {
}

async _readFromStorage(prefObj) {
return JSON.parse(localStorage.getItem("pdfjs.preferences"));
return { prefs: JSON.parse(localStorage.getItem("pdfjs.preferences")) };
}
}

Expand Down
17 changes: 14 additions & 3 deletions web/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,25 @@ class BasePreferences {
}

this.#initializedPromise = this._readFromStorage(this.#defaults).then(
prefs => {
({ browserPrefs, prefs }) => {
const BROWSER_PREFS =
typeof PDFJSDev === "undefined"
? AppOptions.getAll(OptionKind.BROWSER)
: PDFJSDev.eval("BROWSER_PREFERENCES");
const options = Object.create(null);

for (const [name, defaultVal] of Object.entries(BROWSER_PREFS)) {
const prefVal = browserPrefs?.[name];
options[name] =
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
}
for (const [name, defaultVal] of Object.entries(this.#defaults)) {
const prefVal = prefs?.[name];
// Ignore preferences whose types don't match the default values.
this.#prefs[name] =
options[name] = this.#prefs[name] =
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
}
AppOptions.setAll(this.#prefs, /* init = */ true);
AppOptions.setAll(options, /* init = */ true);
}
);
}
Expand Down

0 comments on commit 6df8972

Please sign in to comment.