Bug 1638099 - Unregister all extension service workers on extension shutdown if the app is not also shutting down. r=mixedpuppy

This patch introduces changes to parent/ext-backgroundPage.js and Extension's shutdown methods to make sure
that all service workers registered by an extension are unregistered when the extension is shutting down,
unless the application is also shutting down (in which case the registration is not unregistered because
for the already installed extenson the previously activeWorker is expected to be still active across browser
restarts).

These changes prevent also to hit the issue that D119532 was triggering when an extension was reloaded
and it does not need any of the changes to ContentPrincipal::AddonPolicy from D119531.

Differential Revision: https://phabricator.services.mozilla.com/D119903
This commit is contained in:
Luca Greco 2021-07-26 17:52:24 +00:00
Родитель 38acfa9c75
Коммит 4f6b894e7a
2 изменённых файлов: 41 добавлений и 19 удалений

Просмотреть файл

@ -394,12 +394,15 @@ var ExtensionAddonObserver = {
); );
// Clear all the registered service workers for the extension // Clear all the registered service workers for the extension
// principal. // principal (the one that may have been registered through the
// manifest.json file and the ones that may have been registered
// from an extension page through the service worker API).
//
// Any stored data would be cleared below (if the pref // Any stored data would be cleared below (if the pref
// "extensions.webextensions.keepStorageOnUninstall has not been // "extensions.webextensions.keepStorageOnUninstall has not been
// explicitly set to true, which is usually only done in // explicitly set to true, which is usually only done in
// tests and by some extensions developers for testing purpose). // tests and by some extensions developers for testing purpose).
if (WebExtensionPolicy.backgroundServiceWorkerEnabled) { //
// TODO: ServiceWorkerCleanUp may go away once Bug 1183245 // TODO: ServiceWorkerCleanUp may go away once Bug 1183245
// is fixed, and so this may actually go away, replaced by // is fixed, and so this may actually go away, replaced by
// marking the registration as disabled or to be removed on // marking the registration as disabled or to be removed on
@ -411,7 +414,6 @@ var ExtensionAddonObserver = {
`Clear ServiceWorkers for ${addon.id}`, `Clear ServiceWorkers for ${addon.id}`,
ServiceWorkerCleanUp.removeFromPrincipal(principal) ServiceWorkerCleanUp.removeFromPrincipal(principal)
); );
}
if (!Services.prefs.getBoolPref(LEAVE_STORAGE_PREF, false)) { if (!Services.prefs.getBoolPref(LEAVE_STORAGE_PREF, false)) {
// Clear browser.storage.local backends. // Clear browser.storage.local backends.
@ -2837,6 +2839,20 @@ class Extension extends ExtensionData {
this.updatePermissions(reason); this.updatePermissions(reason);
// The service worker registrations related to the extensions are unregistered
// only when the extension is not shutting down as part of the application
// shutdown (a previously registered service worker is expected to stay
// active across browser restarts), the service worker may have been
// registered through the manifest.json background.service_worker property
// or from an extension page through the service worker API if allowed
// through the about:config pref.
if (!isAppShutdown) {
this.state = "Shutdown: ServiceWorkers";
// TODO: ServiceWorkerCleanUp may go away once Bug 1183245 is fixed.
await ServiceWorkerCleanUp.removeFromPrincipal(this.principal);
this.state = "Shutdown: ServiceWorkers completed";
}
if (!this.manifest) { if (!this.manifest) {
this.state = "Shutdown: Complete: No manifest"; this.state = "Shutdown: Complete: No manifest";
this.policy.active = false; this.policy.active = false;

Просмотреть файл

@ -129,11 +129,17 @@ class BackgroundWorker {
); );
} }
shutdown() { shutdown(isAppShutdown) {
if (this.registrationInfo) { // All service worker registrations related to the extensions will be unregistered
// - when the extension is shutting down if the application is not also shutting down
// shutdown (in which case a previously registered service worker is expected to stay
// active across browser restarts).
// - when the extension has been uninstalled
if (!isAppShutdown && this.registrationInfo) {
this.registrationInfo.forceShutdown(); this.registrationInfo.forceShutdown();
this.registrationInfo = null;
} }
this.registrationInfo = null;
} }
} }
@ -220,9 +226,9 @@ this.backgroundPage = class extends ExtensionAPI {
}); });
} }
onShutdown() { onShutdown(isAppShutdown) {
if (this.bgInstance) { if (this.bgInstance) {
this.bgInstance.shutdown(); this.bgInstance.shutdown(isAppShutdown);
this.bgInstance = null; this.bgInstance = null;
} else { } else {
EventManager.clearPrimedListeners(this.extension, false); EventManager.clearPrimedListeners(this.extension, false);