Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(393)

Side by Side Diff: third_party/WebKit/Source/modules/serviceworkers/RespondWithObserver.cpp

Issue 2034663002: ServiceWorker: Keep the worker alive until FetchEvent.waitUntil settles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add an inline comment Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/serviceworkers/RespondWithObserver.h" 5 #include "modules/serviceworkers/RespondWithObserver.h"
6 6
7 #include "bindings/core/v8/ScriptFunction.h" 7 #include "bindings/core/v8/ScriptFunction.h"
8 #include "bindings/core/v8/ScriptPromise.h" 8 #include "bindings/core/v8/ScriptPromise.h"
9 #include "bindings/core/v8/ScriptValue.h" 9 #include "bindings/core/v8/ScriptValue.h"
10 #include "bindings/core/v8/V8Binding.h" 10 #include "bindings/core/v8/V8Binding.h"
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 m_observer = nullptr; 138 m_observer = nullptr;
139 return value; 139 return value;
140 } 140 }
141 141
142 Member<RespondWithObserver> m_observer; 142 Member<RespondWithObserver> m_observer;
143 ResolveType m_resolveType; 143 ResolveType m_resolveType;
144 }; 144 };
145 145
146 RespondWithObserver::~RespondWithObserver() {} 146 RespondWithObserver::~RespondWithObserver() {}
147 147
148 RespondWithObserver* RespondWithObserver::create(ExecutionContext* context, int eventID, const KURL& requestURL, WebURLRequest::FetchRequestMode requestMode, We bURLRequest::FrameType frameType, WebURLRequest::RequestContext requestContext) 148 RespondWithObserver* RespondWithObserver::create(ExecutionContext* context, int eventID, const KURL& requestURL, WebURLRequest::FetchRequestMode requestMode, We bURLRequest::FrameType frameType, WebURLRequest::RequestContext requestContext, WaitUntilObserver* observer)
149 { 149 {
150 return new RespondWithObserver(context, eventID, requestURL, requestMode, fr ameType, requestContext); 150 return new RespondWithObserver(context, eventID, requestURL, requestMode, fr ameType, requestContext, observer);
151 } 151 }
152 152
153 void RespondWithObserver::contextDestroyed() 153 void RespondWithObserver::contextDestroyed()
154 { 154 {
155 ContextLifecycleObserver::contextDestroyed(); 155 ContextLifecycleObserver::contextDestroyed();
156 m_state = Done; 156 m_state = Done;
157 if (m_observer) {
158 m_observer->decrementPendingActivity();
falken 2016/06/22 01:27:32 Where is the incrementPendingActivity this balance
shimazu 2016/06/22 05:41:27 m_observer is non-null only when respondWith is in
159 m_observer.clear();
160 }
157 } 161 }
158 162
159 void RespondWithObserver::didDispatchEvent(DispatchEventResult dispatchResult) 163 void RespondWithObserver::didDispatchEvent(DispatchEventResult dispatchResult)
160 { 164 {
161 ASSERT(getExecutionContext()); 165 ASSERT(getExecutionContext());
162 if (m_state != Initial) 166 if (m_state != Initial)
163 return; 167 return;
164 168
165 if (dispatchResult != DispatchEventResult::NotCanceled) { 169 if (dispatchResult != DispatchEventResult::NotCanceled) {
166 responseWasRejected(WebServiceWorkerResponseErrorDefaultPrevented); 170 responseWasRejected(WebServiceWorkerResponseErrorDefaultPrevented);
167 return; 171 return;
168 } 172 }
169 173
170 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->didHandleFetchE vent(m_eventID); 174 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->respondToFetchE vent(m_eventID);
171 m_state = Done; 175 m_state = Done;
176 m_observer->decrementPendingActivity();
177 m_observer.clear();
172 } 178 }
173 179
174 void RespondWithObserver::respondWith(ScriptState* scriptState, ScriptPromise sc riptPromise, ExceptionState& exceptionState) 180 void RespondWithObserver::respondWith(ScriptState* scriptState, ScriptPromise sc riptPromise, ExceptionState& exceptionState)
175 { 181 {
176 if (m_state != Initial) { 182 if (m_state != Initial) {
177 exceptionState.throwDOMException(InvalidStateError, "The fetch event has already been responded to."); 183 exceptionState.throwDOMException(InvalidStateError, "The fetch event has already been responded to.");
178 return; 184 return;
179 } 185 }
180 186
181 m_state = Pending; 187 m_state = Pending;
188 m_observer->incrementPendingActivity();
182 scriptPromise.then( 189 scriptPromise.then(
183 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) , 190 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) ,
184 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ; 191 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ;
185 } 192 }
186 193
187 void RespondWithObserver::responseWasRejected(WebServiceWorkerResponseError erro r) 194 void RespondWithObserver::responseWasRejected(WebServiceWorkerResponseError erro r)
188 { 195 {
189 ASSERT(getExecutionContext()); 196 ASSERT(getExecutionContext());
190 getExecutionContext()->addConsoleMessage(ConsoleMessage::create(JSMessageSou rce, WarningMessageLevel, getMessageForResponseError(error, m_requestURL))); 197 getExecutionContext()->addConsoleMessage(ConsoleMessage::create(JSMessageSou rce, WarningMessageLevel, getMessageForResponseError(error, m_requestURL)));
191 198
192 // The default value of WebServiceWorkerResponse's status is 0, which maps 199 // The default value of WebServiceWorkerResponse's status is 0, which maps
193 // to a network error. 200 // to a network error.
194 WebServiceWorkerResponse webResponse; 201 WebServiceWorkerResponse webResponse;
195 webResponse.setError(error); 202 webResponse.setError(error);
196 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->didHandleFetchE vent(m_eventID, webResponse); 203 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->respondToFetchE vent(m_eventID, webResponse);
197 m_state = Done; 204 m_state = Done;
205 m_observer->decrementPendingActivity();
206 m_observer.clear();
198 } 207 }
199 208
200 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) 209 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value)
201 { 210 {
202 ASSERT(getExecutionContext()); 211 ASSERT(getExecutionContext());
203 if (!V8Response::hasInstance(value.v8Value(), toIsolate(getExecutionContext( )))) { 212 if (!V8Response::hasInstance(value.v8Value(), toIsolate(getExecutionContext( )))) {
204 responseWasRejected(WebServiceWorkerResponseErrorNoV8Instance); 213 responseWasRejected(WebServiceWorkerResponseErrorNoV8Instance);
205 return; 214 return;
206 } 215 }
207 Response* response = V8Response::toImplWithTypeCheck(toIsolate(getExecutionC ontext()), value.v8Value()); 216 Response* response = V8Response::toImplWithTypeCheck(toIsolate(getExecutionC ontext()), value.v8Value());
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 if (buffer) { 259 if (buffer) {
251 RefPtr<BlobDataHandle> blobDataHandle = buffer->drainAsBlobDataHandle(Fe tchDataConsumerHandle::Reader::AllowBlobWithInvalidSize); 260 RefPtr<BlobDataHandle> blobDataHandle = buffer->drainAsBlobDataHandle(Fe tchDataConsumerHandle::Reader::AllowBlobWithInvalidSize);
252 if (blobDataHandle) { 261 if (blobDataHandle) {
253 webResponse.setBlobDataHandle(blobDataHandle); 262 webResponse.setBlobDataHandle(blobDataHandle);
254 } else { 263 } else {
255 Stream* outStream = Stream::create(getExecutionContext(), ""); 264 Stream* outStream = Stream::create(getExecutionContext(), "");
256 webResponse.setStreamURL(outStream->url()); 265 webResponse.setStreamURL(outStream->url());
257 buffer->startLoading(FetchDataLoader::createLoaderAsStream(outStream ), new NoopLoaderClient); 266 buffer->startLoading(FetchDataLoader::createLoaderAsStream(outStream ), new NoopLoaderClient);
258 } 267 }
259 } 268 }
260 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->didHandleFetchE vent(m_eventID, webResponse); 269 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->respondToFetchE vent(m_eventID, webResponse);
261 m_state = Done; 270 m_state = Done;
271 m_observer->decrementPendingActivity();
272 m_observer.clear();
262 } 273 }
263 274
264 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID, const KURL& requestURL, WebURLRequest::FetchRequestMode requestMode, WebURLRequ est::FrameType frameType, WebURLRequest::RequestContext requestContext) 275 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID, const KURL& requestURL, WebURLRequest::FetchRequestMode requestMode, WebURLRequ est::FrameType frameType, WebURLRequest::RequestContext requestContext, WaitUnti lObserver* observer)
265 : ContextLifecycleObserver(context) 276 : ContextLifecycleObserver(context)
266 , m_eventID(eventID) 277 , m_eventID(eventID)
267 , m_requestURL(requestURL) 278 , m_requestURL(requestURL)
268 , m_requestMode(requestMode) 279 , m_requestMode(requestMode)
269 , m_frameType(frameType) 280 , m_frameType(frameType)
270 , m_requestContext(requestContext) 281 , m_requestContext(requestContext)
271 , m_state(Initial) 282 , m_state(Initial)
283 , m_observer(observer)
272 { 284 {
273 } 285 }
274 286
275 DEFINE_TRACE(RespondWithObserver) 287 DEFINE_TRACE(RespondWithObserver)
276 { 288 {
289 visitor->trace(m_observer);
277 ContextLifecycleObserver::trace(visitor); 290 ContextLifecycleObserver::trace(visitor);
278 } 291 }
279 292
280 } // namespace blink 293 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698