Request persistent storage on Firefox (#1480)

This commit is contained in:
toasted-nutbread 2021-03-02 22:27:53 -05:00 committed by GitHub
parent b0bc7dc08f
commit 3fe825cf30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -183,6 +183,8 @@ class Backend {
const onPermissionsChanged = this._onWebExtensionEventWrapper(this._onPermissionsChanged.bind(this)); const onPermissionsChanged = this._onWebExtensionEventWrapper(this._onPermissionsChanged.bind(this));
chrome.permissions.onAdded.addListener(onPermissionsChanged); chrome.permissions.onAdded.addListener(onPermissionsChanged);
chrome.permissions.onRemoved.addListener(onPermissionsChanged); chrome.permissions.onRemoved.addListener(onPermissionsChanged);
chrome.runtime.onInstalled.addListener(this._onInstalled.bind(this));
} }
async _prepareInternal() { async _prepareInternal() {
@ -372,6 +374,11 @@ class Backend {
this._checkPermissions(); this._checkPermissions();
} }
_onInstalled({reason}) {
if (reason !== 'install') { return; }
this._requestPersistentStorage();
}
// Message handlers // Message handlers
_onApiRequestBackendReadySignal(_params, sender) { _onApiRequestBackendReadySignal(_params, sender) {
@ -2103,4 +2110,25 @@ class Backend {
_hasRequiredPermissionsForSettings(options) { _hasRequiredPermissionsForSettings(options) {
return this._permissions === null || this._permissionsUtil.hasRequiredPermissionsForOptions(this._permissions, options); return this._permissions === null || this._permissionsUtil.hasRequiredPermissionsForOptions(this._permissions, options);
} }
async _requestPersistentStorage() {
try {
if (await navigator.storage.persisted()) { return; }
// Only request this permission for Firefox versions >= 77.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1630413
const {vendor, version} = await browser.runtime.getBrowserInfo();
if (vendor !== 'Mozilla') { return; }
const match = /^\d+/.exec(version);
if (match === null) { return; }
const versionNumber = Number.parseInt(match[0]);
if (!(Number.isFinite(versionNumber) && versionNumber >= 77)) { return; }
await navigator.storage.persist();
} catch (e) {
// NOP
}
}
} }