From b7cd7c515dc2933decc98a18e2a85ecdbb21d28c Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Thu, 21 Sep 2017 21:32:00 +0200 Subject: [PATCH] Bug 1047098 - "Clear Recent History" must clean up all the ServiceWorkers, r=bkelly This includes minor shutdown fixes by :asuth as discussed on https://bugzilla.mozilla.org/show_bug.cgi?id=1047098#c56 and c57. --HG-- extra : rebase_source : d1a230cc005b2a6a71f16ef84a55851ee2f4f66e extra : source : e89d2565799b4b02d5ee2c56da8d44dc0067f26a --- browser/base/content/sanitize.js | 11 ++++++++ dom/workers/ServiceWorkerRegistrar.cpp | 28 +++++++++++++++++++ .../components/places/tests/head_common.js | 2 +- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/browser/base/content/sanitize.js b/browser/base/content/sanitize.js index 56779e3429e2..faafaa686299 100644 --- a/browser/base/content/sanitize.js +++ b/browser/base/content/sanitize.js @@ -20,6 +20,9 @@ XPCOMUtils.defineLazyModuleGetters(this, { }); +XPCOMUtils.defineLazyServiceGetter(this, "serviceWorkerManager", + "@mozilla.org/serviceworkers/manager;1", + "nsIServiceWorkerManager"); XPCOMUtils.defineLazyServiceGetter(this, "quotaManagerService", "@mozilla.org/dom/quota-manager-service;1", "nsIQuotaManagerService"); @@ -293,6 +296,14 @@ Sanitizer.prototype = { // LocalStorage Services.obs.notifyObservers(null, "extension:purge-localStorage"); + // ServiceWorkers + let serviceWorkers = serviceWorkerManager.getAllRegistrations(); + for (let i = 0; i < serviceWorkers.length; i++) { + let sw = serviceWorkers.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo); + let host = sw.principal.URI.host; + serviceWorkerManager.removeAndPropagate(host); + } + // QuotaManager let promises = []; await new Promise(resolve => { diff --git a/dom/workers/ServiceWorkerRegistrar.cpp b/dom/workers/ServiceWorkerRegistrar.cpp index f90bb735e064..0e3a570ef6c6 100644 --- a/dom/workers/ServiceWorkerRegistrar.cpp +++ b/dom/workers/ServiceWorkerRegistrar.cpp @@ -1061,6 +1061,34 @@ ServiceWorkerRegistrar::ProfileStopped() PBackgroundChild* child = BackgroundChild::GetForCurrentThread(); if (!child) { + // Mutations to the ServiceWorkerRegistrar happen on the PBackground thread, + // issued by the ServiceWorkerManagerService, so the appropriate place to + // trigger shutdown is on that thread. + // + // However, it's quite possible that the PBackground thread was not brought + // into existence for xpcshell tests. We don't cause it to be created + // ourselves for any reason, for example. + // + // In this scenario, we know that: + // - We will receive exactly one call to ourself from BlockShutdown() and + // BlockShutdown() will be called (at most) once. + // - The only way our Shutdown() method gets called is via + // BackgroundParentImpl::RecvShutdownServiceWorkerRegistrar() being + // invoked, which only happens if we get to that send below here that we + // can't get to. + // - All Shutdown() does is set mShuttingDown=true (essential for + // invariants) and invoke MaybeScheduleShutdownCompleted(). + // - Since there is no PBackground thread, mRunnableCounter must be 0 + // because only ScheduleSaveData() increments it and it only runs on the + // background thread, so it cannot have run. And so we would expect + // MaybeScheduleShutdownCompleted() to schedule an invocation of + // ShutdownCompleted on the main thread. + // + // So it's appropriate for us to set mShuttingDown=true (as Shutdown would + // do) and directly invoke ShutdownCompleted() (as Shutdown would indirectly + // do via MaybeScheduleShutdownCompleted). + mShuttingDown = true; + ShutdownCompleted(); return; } diff --git a/toolkit/components/places/tests/head_common.js b/toolkit/components/places/tests/head_common.js index 70288c6514be..610cb597ce52 100644 --- a/toolkit/components/places/tests/head_common.js +++ b/toolkit/components/places/tests/head_common.js @@ -75,7 +75,7 @@ XPCOMUtils.defineLazyGetter(this, "SMALLSVG_DATA_URI", function() { var gTestDir = do_get_cwd(); // Initialize profile. -var gProfD = do_get_profile(); +var gProfD = do_get_profile(true); Services.prefs.setBoolPref("browser.urlbar.usepreloadedtopurls.enabled", false); do_register_cleanup(() =>