From 6dc5dafa7cb16e84f1d0ecef83c42d16bfda26b5 Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Mon, 26 Oct 2015 22:51:30 -0400 Subject: [PATCH] Bug 1218621 - Keep the service worker alive while the promise passed to FetchEvent.respondWith() settles; r=catalinb --- dom/workers/ServiceWorkerEvents.cpp | 20 ++++++-------------- dom/workers/ServiceWorkerEvents.h | 8 -------- dom/workers/ServiceWorkerPrivate.cpp | 6 +++--- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/dom/workers/ServiceWorkerEvents.cpp b/dom/workers/ServiceWorkerEvents.cpp index d7bdcb550f1..96cb7d7639e 100644 --- a/dom/workers/ServiceWorkerEvents.cpp +++ b/dom/workers/ServiceWorkerEvents.cpp @@ -59,9 +59,9 @@ CancelChannelRunnable::Run() } FetchEvent::FetchEvent(EventTarget* aOwner) -: ExtendableEvent(aOwner) -, mIsReload(false) -, mWaitToRespond(false) + : ExtendableEvent(aOwner) + , mIsReload(false) + , mWaitToRespond(false) { } @@ -436,15 +436,6 @@ FetchEvent::RespondWith(Promise& aArg, ErrorResult& aRv) return; } - // 4.5.3.2 If the respond-with entered flag is set, then: - // Throw an "InvalidStateError" exception. - // Here we use |mPromise != nullptr| as respond-with enter flag - if (mPromise) { - aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); - return; - } - mPromise = &aArg; - RefPtr ir = mRequest->GetInternalRequest(); StopImmediatePropagation(); mWaitToRespond = true; @@ -452,6 +443,8 @@ FetchEvent::RespondWith(Promise& aArg, ErrorResult& aRv) new RespondWithHandler(mChannel, mRequest->Mode(), ir->IsClientRequest(), ir->IsNavigationRequest(), mScriptSpec); aArg.AppendNativeHandler(handler); + + WaitUntil(aArg, aRv); } NS_IMPL_ADDREF_INHERITED(FetchEvent, ExtendableEvent) @@ -460,8 +453,7 @@ NS_IMPL_RELEASE_INHERITED(FetchEvent, ExtendableEvent) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FetchEvent) NS_INTERFACE_MAP_END_INHERITING(ExtendableEvent) -NS_IMPL_CYCLE_COLLECTION_INHERITED(FetchEvent, ExtendableEvent, - mRequest, mPromise) +NS_IMPL_CYCLE_COLLECTION_INHERITED(FetchEvent, ExtendableEvent, mRequest) ExtendableEvent::ExtendableEvent(EventTarget* aOwner) : Event(aOwner, nullptr, nullptr) diff --git a/dom/workers/ServiceWorkerEvents.h b/dom/workers/ServiceWorkerEvents.h index 1e095b8ff8a..0e852fb30bc 100644 --- a/dom/workers/ServiceWorkerEvents.h +++ b/dom/workers/ServiceWorkerEvents.h @@ -103,7 +103,6 @@ class FetchEvent final : public ExtendableEvent nsMainThreadPtrHandle mChannel; RefPtr mRequest; nsCString mScriptSpec; - RefPtr mPromise; bool mIsReload; bool mWaitToRespond; protected: @@ -150,13 +149,6 @@ public: void RespondWith(Promise& aArg, ErrorResult& aRv); - already_AddRefed - GetPromise() const - { - RefPtr p = mPromise; - return p.forget(); - } - already_AddRefed ForwardTo(const nsAString& aUrl); diff --git a/dom/workers/ServiceWorkerPrivate.cpp b/dom/workers/ServiceWorkerPrivate.cpp index 11504551fd1..038d3d271c9 100644 --- a/dom/workers/ServiceWorkerPrivate.cpp +++ b/dom/workers/ServiceWorkerPrivate.cpp @@ -1175,11 +1175,11 @@ private: MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(runnable))); } - RefPtr respondWithPromise = event->GetPromise(); - if (respondWithPromise) { + RefPtr waitUntilPromise = event->GetPromise(); + if (waitUntilPromise) { RefPtr keepAliveHandler = new KeepAliveHandler(mKeepAliveToken); - respondWithPromise->AppendNativeHandler(keepAliveHandler); + waitUntilPromise->AppendNativeHandler(keepAliveHandler); } // 9.8.22 If request is a non-subresource request, then: Invoke Soft Update algorithm