| 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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 } | 166 } |
| 167 | 167 |
| 168 void RespondWithObserver::respondWith(ScriptState* scriptState, ScriptPromise sc
riptPromise, ExceptionState& exceptionState) | 168 void RespondWithObserver::respondWith(ScriptState* scriptState, ScriptPromise sc
riptPromise, ExceptionState& exceptionState) |
| 169 { | 169 { |
| 170 if (m_state != Initial) { | 170 if (m_state != Initial) { |
| 171 exceptionState.throwDOMException(InvalidStateError, "The fetch event has
already been responded to."); | 171 exceptionState.throwDOMException(InvalidStateError, "The fetch event has
already been responded to."); |
| 172 return; | 172 return; |
| 173 } | 173 } |
| 174 | 174 |
| 175 m_state = Pending; | 175 m_state = Pending; |
| 176 m_scriptState = scriptState; |
| 176 scriptPromise.then( | 177 scriptPromise.then( |
| 177 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, | 178 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, |
| 178 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; | 179 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; |
| 179 } | 180 } |
| 180 | 181 |
| 181 void RespondWithObserver::responseWasRejected(WebServiceWorkerResponseError erro
r) | 182 void RespondWithObserver::responseWasRejected(WebServiceWorkerResponseError erro
r) |
| 182 { | 183 { |
| 183 ASSERT(getExecutionContext()); | 184 ASSERT(getExecutionContext()); |
| 184 getExecutionContext()->addConsoleMessage(ConsoleMessage::create(JSMessageSou
rce, WarningMessageLevel, getMessageForResponseError(error, m_requestURL))); | 185 getExecutionContext()->addConsoleMessage(ConsoleMessage::create(JSMessageSou
rce, WarningMessageLevel, getMessageForResponseError(error, m_requestURL))); |
| 185 | 186 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 236 } |
| 236 if (response->bodyUsed()) { | 237 if (response->bodyUsed()) { |
| 237 responseWasRejected(WebServiceWorkerResponseErrorBodyUsed); | 238 responseWasRejected(WebServiceWorkerResponseErrorBodyUsed); |
| 238 return; | 239 return; |
| 239 } | 240 } |
| 240 | 241 |
| 241 WebServiceWorkerResponse webResponse; | 242 WebServiceWorkerResponse webResponse; |
| 242 response->populateWebServiceWorkerResponse(webResponse); | 243 response->populateWebServiceWorkerResponse(webResponse); |
| 243 BodyStreamBuffer* buffer = response->internalBodyBuffer(); | 244 BodyStreamBuffer* buffer = response->internalBodyBuffer(); |
| 244 if (buffer) { | 245 if (buffer) { |
| 245 RefPtr<BlobDataHandle> blobDataHandle = buffer->drainAsBlobDataHandle(ge
tExecutionContext(), FetchDataConsumerHandle::Reader::AllowBlobWithInvalidSize); | 246 RefPtr<BlobDataHandle> blobDataHandle = buffer->drainAsBlobDataHandle(Fe
tchDataConsumerHandle::Reader::AllowBlobWithInvalidSize); |
| 246 if (blobDataHandle) { | 247 if (blobDataHandle) { |
| 247 webResponse.setBlobDataHandle(blobDataHandle); | 248 webResponse.setBlobDataHandle(blobDataHandle); |
| 248 } else { | 249 } else { |
| 249 Stream* outStream = Stream::create(getExecutionContext(), ""); | 250 Stream* outStream = Stream::create(getExecutionContext(), ""); |
| 250 webResponse.setStreamURL(outStream->url()); | 251 webResponse.setStreamURL(outStream->url()); |
| 251 buffer->startLoading(getExecutionContext(), FetchDataLoader::createL
oaderAsStream(outStream), new NoopLoaderClient); | 252 buffer->startLoading(FetchDataLoader::createLoaderAsStream(outStream
), new NoopLoaderClient); |
| 252 } | 253 } |
| 253 } | 254 } |
| 254 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->didHandleFetchE
vent(m_eventID, webResponse); | 255 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->didHandleFetchE
vent(m_eventID, webResponse); |
| 255 m_state = Done; | 256 m_state = Done; |
| 256 } | 257 } |
| 257 | 258 |
| 258 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID,
const KURL& requestURL, WebURLRequest::FetchRequestMode requestMode, WebURLRequ
est::FrameType frameType, WebURLRequest::RequestContext requestContext) | 259 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID,
const KURL& requestURL, WebURLRequest::FetchRequestMode requestMode, WebURLRequ
est::FrameType frameType, WebURLRequest::RequestContext requestContext) |
| 259 : ContextLifecycleObserver(context) | 260 : ContextLifecycleObserver(context) |
| 260 , m_eventID(eventID) | 261 , m_eventID(eventID) |
| 261 , m_requestURL(requestURL) | 262 , m_requestURL(requestURL) |
| 262 , m_requestMode(requestMode) | 263 , m_requestMode(requestMode) |
| 263 , m_frameType(frameType) | 264 , m_frameType(frameType) |
| 264 , m_requestContext(requestContext) | 265 , m_requestContext(requestContext) |
| 265 , m_state(Initial) | 266 , m_state(Initial) |
| 266 { | 267 { |
| 267 } | 268 } |
| 268 | 269 |
| 269 DEFINE_TRACE(RespondWithObserver) | 270 DEFINE_TRACE(RespondWithObserver) |
| 270 { | 271 { |
| 271 ContextLifecycleObserver::trace(visitor); | 272 ContextLifecycleObserver::trace(visitor); |
| 272 } | 273 } |
| 273 | 274 |
| 274 } // namespace blink | 275 } // namespace blink |
| OLD | NEW |