Merge pull request #9936 from Snuffleupagus/BasePreferences-validate

Validate the Preferences when fetching them from storage
This commit is contained in:
Tim van der Meij 2018-07-29 16:16:48 +02:00 committed by GitHub
commit d19e13ee2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 20 deletions

View File

@ -635,8 +635,6 @@ let PDFViewerApplication = {
if (this.pdfLoadingTask) {
// We need to destroy already opened document.
return this.close().then(() => {
// Reload the preferences if a document was previously opened.
this.preferences.reload();
// ... and repeat the open() call.
return this.open(file, args);
});

View File

@ -60,9 +60,19 @@ class BasePreferences {
this.prefs = Object.assign(Object.create(null), defaults);
return this._readFromStorage(defaults);
}).then((prefObj) => {
if (prefObj) {
this.prefs = prefObj;
}).then((prefs) => {
if (!prefs) {
return;
}
for (let name in prefs) {
const defaultValue = this.defaults[name], prefValue = prefs[name];
// Ignore preferences not present in, or whose types don't match,
// the default values.
if (defaultValue === undefined ||
typeof prefValue !== typeof defaultValue) {
continue;
}
this.prefs[name] = prefValue;
}
});
}
@ -99,21 +109,6 @@ class BasePreferences {
});
}
/**
* Replace the current preference values with the ones from storage.
* @return {Promise} A promise that is resolved when the preference values
* have been updated.
*/
reload() {
return this._initializedPromise.then(() => {
return this._readFromStorage(this.defaults);
}).then((prefObj) => {
if (prefObj) {
this.prefs = prefObj;
}
});
}
/**
* Set the value of a preference.
* @param {string} name The name of the preference that should be changed.