| OLD | NEW |
| 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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 } | 167 } |
| 168 | 168 |
| 169 Member<RespondWithObserver> m_observer; | 169 Member<RespondWithObserver> m_observer; |
| 170 ResolveType m_resolveType; | 170 ResolveType m_resolveType; |
| 171 }; | 171 }; |
| 172 | 172 |
| 173 RespondWithObserver::~RespondWithObserver() {} | 173 RespondWithObserver::~RespondWithObserver() {} |
| 174 | 174 |
| 175 RespondWithObserver* RespondWithObserver::create( | 175 RespondWithObserver* RespondWithObserver::create( |
| 176 ExecutionContext* context, | 176 ExecutionContext* context, |
| 177 int eventID, | 177 int fetchEventID, |
| 178 const KURL& requestURL, | 178 const KURL& requestURL, |
| 179 WebURLRequest::FetchRequestMode requestMode, | 179 WebURLRequest::FetchRequestMode requestMode, |
| 180 WebURLRequest::FrameType frameType, | 180 WebURLRequest::FrameType frameType, |
| 181 WebURLRequest::RequestContext requestContext, | 181 WebURLRequest::RequestContext requestContext, |
| 182 WaitUntilObserver* observer) { | 182 WaitUntilObserver* observer) { |
| 183 return new RespondWithObserver(context, eventID, requestURL, requestMode, | 183 return new RespondWithObserver(context, fetchEventID, requestURL, requestMode, |
| 184 frameType, requestContext, observer); | 184 frameType, requestContext, observer); |
| 185 } | 185 } |
| 186 | 186 |
| 187 void RespondWithObserver::contextDestroyed() { | 187 void RespondWithObserver::contextDestroyed() { |
| 188 ContextLifecycleObserver::contextDestroyed(); | 188 ContextLifecycleObserver::contextDestroyed(); |
| 189 if (m_observer) { | 189 if (m_observer) { |
| 190 DCHECK_EQ(Pending, m_state); | 190 DCHECK_EQ(Pending, m_state); |
| 191 m_observer.clear(); | 191 m_observer.clear(); |
| 192 } | 192 } |
| 193 m_state = Done; | 193 m_state = Done; |
| 194 } | 194 } |
| 195 | 195 |
| 196 void RespondWithObserver::willDispatchEvent() { | 196 void RespondWithObserver::willDispatchEvent() { |
| 197 m_eventDispatchTime = WTF::currentTime(); | 197 m_eventDispatchTime = WTF::currentTime(); |
| 198 } | 198 } |
| 199 | 199 |
| 200 void RespondWithObserver::didDispatchEvent(DispatchEventResult dispatchResult) { | 200 void RespondWithObserver::didDispatchEvent(DispatchEventResult dispatchResult) { |
| 201 ASSERT(getExecutionContext()); | 201 ASSERT(getExecutionContext()); |
| 202 if (m_state != Initial) | 202 if (m_state != Initial) |
| 203 return; | 203 return; |
| 204 | 204 |
| 205 if (dispatchResult != DispatchEventResult::NotCanceled) { | 205 if (dispatchResult != DispatchEventResult::NotCanceled) { |
| 206 m_observer->incrementPendingActivity(); | 206 m_observer->incrementPendingActivity(); |
| 207 responseWasRejected(WebServiceWorkerResponseErrorDefaultPrevented); | 207 responseWasRejected(WebServiceWorkerResponseErrorDefaultPrevented); |
| 208 return; | 208 return; |
| 209 } | 209 } |
| 210 | 210 |
| 211 ServiceWorkerGlobalScopeClient::from(getExecutionContext()) | 211 ServiceWorkerGlobalScopeClient::from(getExecutionContext()) |
| 212 ->respondToFetchEvent(m_eventID, m_eventDispatchTime); | 212 ->respondToFetchEvent(m_fetchEventID, m_eventDispatchTime); |
| 213 m_state = Done; | 213 m_state = Done; |
| 214 m_observer.clear(); | 214 m_observer.clear(); |
| 215 } | 215 } |
| 216 | 216 |
| 217 void RespondWithObserver::respondWith(ScriptState* scriptState, | 217 void RespondWithObserver::respondWith(ScriptState* scriptState, |
| 218 ScriptPromise scriptPromise, | 218 ScriptPromise scriptPromise, |
| 219 ExceptionState& exceptionState) { | 219 ExceptionState& exceptionState) { |
| 220 if (m_state != Initial) { | 220 if (m_state != Initial) { |
| 221 exceptionState.throwDOMException( | 221 exceptionState.throwDOMException( |
| 222 InvalidStateError, "The fetch event has already been responded to."); | 222 InvalidStateError, "The fetch event has already been responded to."); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 235 ASSERT(getExecutionContext()); | 235 ASSERT(getExecutionContext()); |
| 236 getExecutionContext()->addConsoleMessage( | 236 getExecutionContext()->addConsoleMessage( |
| 237 ConsoleMessage::create(JSMessageSource, WarningMessageLevel, | 237 ConsoleMessage::create(JSMessageSource, WarningMessageLevel, |
| 238 getMessageForResponseError(error, m_requestURL))); | 238 getMessageForResponseError(error, m_requestURL))); |
| 239 | 239 |
| 240 // The default value of WebServiceWorkerResponse's status is 0, which maps | 240 // The default value of WebServiceWorkerResponse's status is 0, which maps |
| 241 // to a network error. | 241 // to a network error. |
| 242 WebServiceWorkerResponse webResponse; | 242 WebServiceWorkerResponse webResponse; |
| 243 webResponse.setError(error); | 243 webResponse.setError(error); |
| 244 ServiceWorkerGlobalScopeClient::from(getExecutionContext()) | 244 ServiceWorkerGlobalScopeClient::from(getExecutionContext()) |
| 245 ->respondToFetchEvent(m_eventID, webResponse, m_eventDispatchTime); | 245 ->respondToFetchEvent(m_fetchEventID, webResponse, m_eventDispatchTime); |
| 246 m_state = Done; | 246 m_state = Done; |
| 247 m_observer->decrementPendingActivity(); | 247 m_observer->decrementPendingActivity(); |
| 248 m_observer.clear(); | 248 m_observer.clear(); |
| 249 } | 249 } |
| 250 | 250 |
| 251 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) { | 251 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) { |
| 252 ASSERT(getExecutionContext()); | 252 ASSERT(getExecutionContext()); |
| 253 if (!V8Response::hasInstance(value.v8Value(), | 253 if (!V8Response::hasInstance(value.v8Value(), |
| 254 toIsolate(getExecutionContext()))) { | 254 toIsolate(getExecutionContext()))) { |
| 255 responseWasRejected(WebServiceWorkerResponseErrorNoV8Instance); | 255 responseWasRejected(WebServiceWorkerResponseErrorNoV8Instance); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 if (blobDataHandle) { | 308 if (blobDataHandle) { |
| 309 webResponse.setBlobDataHandle(blobDataHandle); | 309 webResponse.setBlobDataHandle(blobDataHandle); |
| 310 } else { | 310 } else { |
| 311 Stream* outStream = Stream::create(getExecutionContext(), ""); | 311 Stream* outStream = Stream::create(getExecutionContext(), ""); |
| 312 webResponse.setStreamURL(outStream->url()); | 312 webResponse.setStreamURL(outStream->url()); |
| 313 buffer->startLoading(FetchDataLoader::createLoaderAsStream(outStream), | 313 buffer->startLoading(FetchDataLoader::createLoaderAsStream(outStream), |
| 314 new NoopLoaderClient); | 314 new NoopLoaderClient); |
| 315 } | 315 } |
| 316 } | 316 } |
| 317 ServiceWorkerGlobalScopeClient::from(getExecutionContext()) | 317 ServiceWorkerGlobalScopeClient::from(getExecutionContext()) |
| 318 ->respondToFetchEvent(m_eventID, webResponse, m_eventDispatchTime); | 318 ->respondToFetchEvent(m_fetchEventID, webResponse, m_eventDispatchTime); |
| 319 m_state = Done; | 319 m_state = Done; |
| 320 m_observer->decrementPendingActivity(); | 320 m_observer->decrementPendingActivity(); |
| 321 m_observer.clear(); | 321 m_observer.clear(); |
| 322 } | 322 } |
| 323 | 323 |
| 324 RespondWithObserver::RespondWithObserver( | 324 RespondWithObserver::RespondWithObserver( |
| 325 ExecutionContext* context, | 325 ExecutionContext* context, |
| 326 int eventID, | 326 int fetchEventID, |
| 327 const KURL& requestURL, | 327 const KURL& requestURL, |
| 328 WebURLRequest::FetchRequestMode requestMode, | 328 WebURLRequest::FetchRequestMode requestMode, |
| 329 WebURLRequest::FrameType frameType, | 329 WebURLRequest::FrameType frameType, |
| 330 WebURLRequest::RequestContext requestContext, | 330 WebURLRequest::RequestContext requestContext, |
| 331 WaitUntilObserver* observer) | 331 WaitUntilObserver* observer) |
| 332 : ContextLifecycleObserver(context), | 332 : ContextLifecycleObserver(context), |
| 333 m_eventID(eventID), | 333 m_fetchEventID(fetchEventID), |
| 334 m_requestURL(requestURL), | 334 m_requestURL(requestURL), |
| 335 m_requestMode(requestMode), | 335 m_requestMode(requestMode), |
| 336 m_frameType(frameType), | 336 m_frameType(frameType), |
| 337 m_requestContext(requestContext), | 337 m_requestContext(requestContext), |
| 338 m_state(Initial), | 338 m_state(Initial), |
| 339 m_observer(observer) {} | 339 m_observer(observer) {} |
| 340 | 340 |
| 341 DEFINE_TRACE(RespondWithObserver) { | 341 DEFINE_TRACE(RespondWithObserver) { |
| 342 visitor->trace(m_observer); | 342 visitor->trace(m_observer); |
| 343 ContextLifecycleObserver::trace(visitor); | 343 ContextLifecycleObserver::trace(visitor); |
| 344 } | 344 } |
| 345 | 345 |
| 346 } // namespace blink | 346 } // namespace blink |
| OLD | NEW |