diff --git a/browser/base/content/test/performance/browser_startup.js b/browser/base/content/test/performance/browser_startup.js index 05a106933074..b7d328612567 100644 --- a/browser/base/content/test/performance/browser_startup.js +++ b/browser/base/content/test/performance/browser_startup.js @@ -49,9 +49,6 @@ const startupPhases = { // We are at this phase after creating the first browser window (ie. after final-ui-startup). "before opening first browser window": {blacklist: { - components: new Set([ - "nsAsyncShutdown.js", - ]), modules: new Set([ "resource://gre/modules/PlacesBackups.jsm", "resource://gre/modules/PlacesUtils.jsm", diff --git a/dom/workers/ServiceWorkerRegistrar.cpp b/dom/workers/ServiceWorkerRegistrar.cpp index 249aee540f75..f90bb735e064 100644 --- a/dom/workers/ServiceWorkerRegistrar.cpp +++ b/dom/workers/ServiceWorkerRegistrar.cpp @@ -54,7 +54,8 @@ StaticRefPtr gServiceWorkerRegistrar; } // namespace NS_IMPL_ISUPPORTS(ServiceWorkerRegistrar, - nsIObserver) + nsIObserver, + nsIAsyncShutdownBlocker) void ServiceWorkerRegistrar::Initialize() @@ -73,10 +74,6 @@ ServiceWorkerRegistrar::Initialize() DebugOnly rv = obs->AddObserver(gServiceWorkerRegistrar, "profile-after-change", false); MOZ_ASSERT(NS_SUCCEEDED(rv)); - - rv = obs->AddObserver(gServiceWorkerRegistrar, "profile-before-change", - false); - MOZ_ASSERT(NS_SUCCEEDED(rv)); } } @@ -94,7 +91,6 @@ ServiceWorkerRegistrar::ServiceWorkerRegistrar() : mMonitor("ServiceWorkerRegistrar.mMonitor") , mDataLoaded(false) , mShuttingDown(false) - , mShutdownCompleteFlag(nullptr) , mRunnableCounter(0) { MOZ_ASSERT(NS_IsMainThread()); @@ -829,8 +825,8 @@ ServiceWorkerRegistrar::ShutdownCompleted() { MOZ_ASSERT(NS_IsMainThread()); - MOZ_ASSERT(mShutdownCompleteFlag && !*mShutdownCompleteFlag); - *mShutdownCompleteFlag = true; + DebugOnly rv = GetShutdownPhase()->RemoveBlocker(this); + MOZ_ASSERT(NS_SUCCEEDED(rv)); } void @@ -1027,6 +1023,13 @@ ServiceWorkerRegistrar::ProfileStarted() return; } + rv = GetShutdownPhase()->AddBlocker( + this, NS_LITERAL_STRING(__FILE__), __LINE__, + NS_LITERAL_STRING("ServiceWorkerRegistrar: Flushing data")); + if (NS_WARN_IF(NS_FAILED(rv))) { + return; + } + nsCOMPtr target = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); MOZ_ASSERT(target, "Must have stream transport service"); @@ -1061,12 +1064,41 @@ ServiceWorkerRegistrar::ProfileStopped() return; } - bool completed = false; - mShutdownCompleteFlag = &completed; - child->SendShutdownServiceWorkerRegistrar(); +} - MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return completed; })); +// Async shutdown blocker methods + +NS_IMETHODIMP +ServiceWorkerRegistrar::BlockShutdown(nsIAsyncShutdownClient* aClient) +{ + ProfileStopped(); + return NS_OK; +} + +NS_IMETHODIMP +ServiceWorkerRegistrar::GetName(nsAString& aName) +{ + aName = NS_LITERAL_STRING("ServiceWorkerRegistrar: Flushing data"); + return NS_OK; +} + +NS_IMETHODIMP +ServiceWorkerRegistrar::GetState(nsIPropertyBag**) +{ + return NS_OK; +} + +nsCOMPtr +ServiceWorkerRegistrar::GetShutdownPhase() const +{ + nsCOMPtr svc = services::GetAsyncShutdown(); + MOZ_RELEASE_ASSERT(svc); + + nsCOMPtr client; + Unused << svc->GetProfileBeforeChange(getter_AddRefs(client)); + MOZ_RELEASE_ASSERT(client); + return Move(client); } void @@ -1097,17 +1129,6 @@ ServiceWorkerRegistrar::Observe(nsISupports* aSubject, const char* aTopic, return NS_OK; } - if (!strcmp(aTopic, "profile-before-change")) { - nsCOMPtr observerService = - services::GetObserverService(); - observerService->RemoveObserver(this, "profile-before-change"); - - // Shutting down, let's sync the data. - ProfileStopped(); - - return NS_OK; - } - MOZ_ASSERT(false, "ServiceWorkerRegistrar got unexpected topic!"); return NS_ERROR_UNEXPECTED; } diff --git a/dom/workers/ServiceWorkerRegistrar.h b/dom/workers/ServiceWorkerRegistrar.h index e90fe32fafb7..be7452ce0fa1 100644 --- a/dom/workers/ServiceWorkerRegistrar.h +++ b/dom/workers/ServiceWorkerRegistrar.h @@ -10,6 +10,7 @@ #include "mozilla/Monitor.h" #include "mozilla/Telemetry.h" #include "nsClassHashtable.h" +#include "nsIAsyncShutdown.h" #include "nsIObserver.h" #include "nsCOMPtr.h" #include "nsString.h" @@ -34,12 +35,14 @@ namespace dom { class ServiceWorkerRegistrationData; class ServiceWorkerRegistrar : public nsIObserver + , public nsIAsyncShutdownBlocker { friend class ServiceWorkerRegistrarSaveDataRunnable; public: NS_DECL_THREADSAFE_ISUPPORTS NS_DECL_NSIOBSERVER + NS_DECL_NSIASYNCSHUTDOWNBLOCKER static void Initialize(); @@ -79,6 +82,8 @@ private: void ShutdownCompleted(); void MaybeScheduleShutdownCompleted(); + nsCOMPtr GetShutdownPhase() const; + bool IsSupportedVersion(const nsACString& aVersion) const; mozilla::Monitor mMonitor; @@ -91,7 +96,6 @@ protected: // PBackground thread only bool mShuttingDown; - bool* mShutdownCompleteFlag; uint32_t mRunnableCounter; };