Add type validation to the default_preferences
generation (PR 10548 follow-up)
The generated `default_preferences.json` file is necessary when initializing the Firefox preferences, which only supports certain types, hence this patch adds additional validation to help prevent run-time errors in Firefox. Given that these changes add a code-path to `AppOptions.getAll` which could throw, the `OptionKind.PREFERENCE` branch is also modified to require *exact* matching to prevent (future) errors in the viewer. Finally the conditionally defined `defaultOptions` will no longer (potentially) be considered during the `gulp default_preferences` task, to make it more difficult for them to be accidentally included.
This commit is contained in:
parent
c43396c2b7
commit
f7cc331654
@ -535,7 +535,7 @@ gulp.task('default_preferences-pre', function() {
|
|||||||
], { base: 'src/', }),
|
], { base: 'src/', }),
|
||||||
gulp.src([
|
gulp.src([
|
||||||
'web/*.js',
|
'web/*.js',
|
||||||
'!web/{pdfjs,preferences,viewer}.js',
|
'!web/{app,pdfjs,preferences,viewer}.js',
|
||||||
], { base: '.', }),
|
], { base: '.', }),
|
||||||
]).pipe(transform('utf8', preprocess))
|
]).pipe(transform('utf8', preprocess))
|
||||||
.pipe(gulp.dest(DEFAULT_PREFERENCES_DIR + 'lib/'));
|
.pipe(gulp.dest(DEFAULT_PREFERENCES_DIR + 'lib/'));
|
||||||
|
@ -187,7 +187,9 @@ let PDFViewerApplication = {
|
|||||||
for (const name in prefs) {
|
for (const name in prefs) {
|
||||||
AppOptions.set(name, prefs[name]);
|
AppOptions.set(name, prefs[name]);
|
||||||
}
|
}
|
||||||
} catch (reason) { }
|
} catch (reason) {
|
||||||
|
console.error(`_readPreferences: "${reason.message}".`);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -222,7 +222,7 @@ const defaultOptions = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
if (typeof PDFJSDev === 'undefined' ||
|
if (typeof PDFJSDev === 'undefined' ||
|
||||||
PDFJSDev.test('!PRODUCTION || GENERIC')) {
|
PDFJSDev.test('!PRODUCTION || (GENERIC && !LIB)')) {
|
||||||
defaultOptions.disablePreferences = {
|
defaultOptions.disablePreferences = {
|
||||||
/** @type {boolean} */
|
/** @type {boolean} */
|
||||||
value: false,
|
value: false,
|
||||||
@ -262,9 +262,15 @@ class AppOptions {
|
|||||||
if ((kind & defaultOption.kind) === 0) {
|
if ((kind & defaultOption.kind) === 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ((kind & OptionKind.PREFERENCE) !== 0) {
|
if (kind === OptionKind.PREFERENCE) {
|
||||||
options[name] = defaultOption.value;
|
const value = defaultOption.value, valueType = typeof value;
|
||||||
continue;
|
|
||||||
|
if (valueType === 'boolean' || valueType === 'string' ||
|
||||||
|
(valueType === 'number' && Number.isInteger(value))) {
|
||||||
|
options[name] = value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
throw new Error(`Invalid type for preference: ${name}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const userOption = userOptions[name];
|
const userOption = userOptions[name];
|
||||||
|
@ -35,8 +35,6 @@ function getDefaultPreferences() {
|
|||||||
}
|
}
|
||||||
}).then(function({ AppOptions, OptionKind, }) {
|
}).then(function({ AppOptions, OptionKind, }) {
|
||||||
return AppOptions.getAll(OptionKind.PREFERENCE);
|
return AppOptions.getAll(OptionKind.PREFERENCE);
|
||||||
}, function(reason) {
|
|
||||||
console.error(reason);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user