Chromium Code Reviews| 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(m_state == Pending); | |
|
falken
2016/06/22 05:53:33
nit: DCHECK_EQ gives a nicer error message.
shimazu
2016/06/22 07:13:39
Done.
| |
| 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) { |
| 166 responseWasRejected(WebServiceWorkerResponseErrorDefaultPrevented); | 171 responseWasRejected(WebServiceWorkerResponseErrorDefaultPrevented); |
| 167 return; | 172 return; |
| 168 } | 173 } |
| 169 | 174 |
| 170 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->didHandleFetchE vent(m_eventID); | 175 ServiceWorkerGlobalScopeClient::from(getExecutionContext())->respondToFetchE vent(m_eventID); |
| 171 m_state = Done; | 176 m_state = Done; |
| 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 Loading... | |
| 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 |
| OLD | NEW |