| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 if (m_observer) { |
| 157 DCHECK_EQ(Pending, m_state); |
| 158 m_observer->decrementPendingActivity(); |
| 159 m_observer.clear(); |
| 160 } |
| 156 m_state = Done; | 161 m_state = Done; |
| 157 } | 162 } |
| 158 | 163 |
| 159 void RespondWithObserver::didDispatchEvent(DispatchEventResult dispatchResult) | 164 void RespondWithObserver::didDispatchEvent(DispatchEventResult dispatchResult) |
| 160 { | 165 { |
| 161 ASSERT(getExecutionContext()); | 166 ASSERT(getExecutionContext()); |
| 162 if (m_state != Initial) | 167 if (m_state != Initial) |
| 163 return; | 168 return; |
| 164 | 169 |
| 165 if (dispatchResult != DispatchEventResult::NotCanceled) { | 170 if (dispatchResult != DispatchEventResult::NotCanceled) { |
| 171 m_observer->incrementPendingActivity(); |
| 166 responseWasRejected(WebServiceWorkerResponseErrorDefaultPrevented); | 172 responseWasRejected(WebServiceWorkerResponseErrorDefaultPrevented); |
| 167 return; | 173 return; |
| 168 } | 174 } |
| 169 | 175 |
| 170 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->didHandleFetchE
vent(m_eventID); | 176 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->respondToFetchE
vent(m_eventID); |
| 171 m_state = Done; | 177 m_state = Done; |
| 178 m_observer.clear(); |
| 172 } | 179 } |
| 173 | 180 |
| 174 void RespondWithObserver::respondWith(ScriptState* scriptState, ScriptPromise sc
riptPromise, ExceptionState& exceptionState) | 181 void RespondWithObserver::respondWith(ScriptState* scriptState, ScriptPromise sc
riptPromise, ExceptionState& exceptionState) |
| 175 { | 182 { |
| 176 if (m_state != Initial) { | 183 if (m_state != Initial) { |
| 177 exceptionState.throwDOMException(InvalidStateError, "The fetch event has
already been responded to."); | 184 exceptionState.throwDOMException(InvalidStateError, "The fetch event has
already been responded to."); |
| 178 return; | 185 return; |
| 179 } | 186 } |
| 180 | 187 |
| 181 m_state = Pending; | 188 m_state = Pending; |
| 189 m_observer->incrementPendingActivity(); |
| 182 scriptPromise.then( | 190 scriptPromise.then( |
| 183 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, | 191 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, |
| 184 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; | 192 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; |
| 185 } | 193 } |
| 186 | 194 |
| 187 void RespondWithObserver::responseWasRejected(WebServiceWorkerResponseError erro
r) | 195 void RespondWithObserver::responseWasRejected(WebServiceWorkerResponseError erro
r) |
| 188 { | 196 { |
| 189 ASSERT(getExecutionContext()); | 197 ASSERT(getExecutionContext()); |
| 190 getExecutionContext()->addConsoleMessage(ConsoleMessage::create(JSMessageSou
rce, WarningMessageLevel, getMessageForResponseError(error, m_requestURL))); | 198 getExecutionContext()->addConsoleMessage(ConsoleMessage::create(JSMessageSou
rce, WarningMessageLevel, getMessageForResponseError(error, m_requestURL))); |
| 191 | 199 |
| 192 // The default value of WebServiceWorkerResponse's status is 0, which maps | 200 // The default value of WebServiceWorkerResponse's status is 0, which maps |
| 193 // to a network error. | 201 // to a network error. |
| 194 WebServiceWorkerResponse webResponse; | 202 WebServiceWorkerResponse webResponse; |
| 195 webResponse.setError(error); | 203 webResponse.setError(error); |
| 196 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->didHandleFetchE
vent(m_eventID, webResponse); | 204 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->respondToFetchE
vent(m_eventID, webResponse); |
| 197 m_state = Done; | 205 m_state = Done; |
| 206 m_observer->decrementPendingActivity(); |
| 207 m_observer.clear(); |
| 198 } | 208 } |
| 199 | 209 |
| 200 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) | 210 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) |
| 201 { | 211 { |
| 202 ASSERT(getExecutionContext()); | 212 ASSERT(getExecutionContext()); |
| 203 if (!V8Response::hasInstance(value.v8Value(), toIsolate(getExecutionContext(
)))) { | 213 if (!V8Response::hasInstance(value.v8Value(), toIsolate(getExecutionContext(
)))) { |
| 204 responseWasRejected(WebServiceWorkerResponseErrorNoV8Instance); | 214 responseWasRejected(WebServiceWorkerResponseErrorNoV8Instance); |
| 205 return; | 215 return; |
| 206 } | 216 } |
| 207 Response* response = V8Response::toImplWithTypeCheck(toIsolate(getExecutionC
ontext()), value.v8Value()); | 217 Response* response = V8Response::toImplWithTypeCheck(toIsolate(getExecutionC
ontext()), value.v8Value()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 if (buffer) { | 260 if (buffer) { |
| 251 RefPtr<BlobDataHandle> blobDataHandle = buffer->drainAsBlobDataHandle(Fe
tchDataConsumerHandle::Reader::AllowBlobWithInvalidSize); | 261 RefPtr<BlobDataHandle> blobDataHandle = buffer->drainAsBlobDataHandle(Fe
tchDataConsumerHandle::Reader::AllowBlobWithInvalidSize); |
| 252 if (blobDataHandle) { | 262 if (blobDataHandle) { |
| 253 webResponse.setBlobDataHandle(blobDataHandle); | 263 webResponse.setBlobDataHandle(blobDataHandle); |
| 254 } else { | 264 } else { |
| 255 Stream* outStream = Stream::create(getExecutionContext(), ""); | 265 Stream* outStream = Stream::create(getExecutionContext(), ""); |
| 256 webResponse.setStreamURL(outStream->url()); | 266 webResponse.setStreamURL(outStream->url()); |
| 257 buffer->startLoading(FetchDataLoader::createLoaderAsStream(outStream
), new NoopLoaderClient); | 267 buffer->startLoading(FetchDataLoader::createLoaderAsStream(outStream
), new NoopLoaderClient); |
| 258 } | 268 } |
| 259 } | 269 } |
| 260 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->didHandleFetchE
vent(m_eventID, webResponse); | 270 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->respondToFetchE
vent(m_eventID, webResponse); |
| 261 m_state = Done; | 271 m_state = Done; |
| 272 m_observer->decrementPendingActivity(); |
| 273 m_observer.clear(); |
| 262 } | 274 } |
| 263 | 275 |
| 264 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID,
const KURL& requestURL, WebURLRequest::FetchRequestMode requestMode, WebURLRequ
est::FrameType frameType, WebURLRequest::RequestContext requestContext) | 276 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) | 277 : ContextLifecycleObserver(context) |
| 266 , m_eventID(eventID) | 278 , m_eventID(eventID) |
| 267 , m_requestURL(requestURL) | 279 , m_requestURL(requestURL) |
| 268 , m_requestMode(requestMode) | 280 , m_requestMode(requestMode) |
| 269 , m_frameType(frameType) | 281 , m_frameType(frameType) |
| 270 , m_requestContext(requestContext) | 282 , m_requestContext(requestContext) |
| 271 , m_state(Initial) | 283 , m_state(Initial) |
| 284 , m_observer(observer) |
| 272 { | 285 { |
| 273 } | 286 } |
| 274 | 287 |
| 275 DEFINE_TRACE(RespondWithObserver) | 288 DEFINE_TRACE(RespondWithObserver) |
| 276 { | 289 { |
| 290 visitor->trace(m_observer); |
| 277 ContextLifecycleObserver::trace(visitor); | 291 ContextLifecycleObserver::trace(visitor); |
| 278 } | 292 } |
| 279 | 293 |
| 280 } // namespace blink | 294 } // namespace blink |
| OLD | NEW |