| 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 "config.h" | 5 #include "config.h" |
| 6 #include "modules/serviceworkers/RespondWithObserver.h" | 6 #include "modules/serviceworkers/RespondWithObserver.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/ScriptFunction.h" | 8 #include "bindings/core/v8/ScriptFunction.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptValue.h" | 10 #include "bindings/core/v8/ScriptValue.h" |
| 11 #include "bindings/core/v8/V8Binding.h" | 11 #include "bindings/core/v8/V8Binding.h" |
| 12 #include "bindings/modules/v8/V8Response.h" | 12 #include "bindings/modules/v8/V8Response.h" |
| 13 #include "core/dom/ExecutionContext.h" | 13 #include "core/dom/ExecutionContext.h" |
| 14 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 14 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
| 15 #include "public/platform/WebServiceWorkerResponse.h" | |
| 16 #include "wtf/Assertions.h" | 15 #include "wtf/Assertions.h" |
| 17 #include "wtf/RefPtr.h" | 16 #include "wtf/RefPtr.h" |
| 18 #include <v8.h> | 17 #include <v8.h> |
| 19 | 18 |
| 20 namespace blink { | 19 namespace blink { |
| 21 | 20 |
| 22 class RespondWithObserver::ThenFunction FINAL : public ScriptFunction { | 21 class RespondWithObserver::ThenFunction FINAL : public ScriptFunction { |
| 23 public: | 22 public: |
| 24 enum ResolveType { | 23 enum ResolveType { |
| 25 Fulfilled, | 24 Fulfilled, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 } | 67 } |
| 69 | 68 |
| 70 void RespondWithObserver::contextDestroyed() | 69 void RespondWithObserver::contextDestroyed() |
| 71 { | 70 { |
| 72 ContextLifecycleObserver::contextDestroyed(); | 71 ContextLifecycleObserver::contextDestroyed(); |
| 73 m_state = Done; | 72 m_state = Done; |
| 74 } | 73 } |
| 75 | 74 |
| 76 void RespondWithObserver::didDispatchEvent() | 75 void RespondWithObserver::didDispatchEvent() |
| 77 { | 76 { |
| 78 ASSERT(executionContext()); | 77 if (m_state == Initial) |
| 79 if (m_state != Initial) | 78 sendResponse(nullptr); |
| 80 return; | |
| 81 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven
t(m_eventID); | |
| 82 m_state = Done; | |
| 83 } | 79 } |
| 84 | 80 |
| 85 void RespondWithObserver::respondWith(ScriptState* scriptState, const ScriptValu
e& value, ExceptionState& exceptionState) | 81 void RespondWithObserver::respondWith(ScriptState* scriptState, const ScriptValu
e& value, ExceptionState& exceptionState) |
| 86 { | 82 { |
| 87 if (m_state != Initial) { | 83 if (m_state != Initial) { |
| 88 exceptionState.throwDOMException(InvalidStateError, "respondWith is alre
ady called."); | 84 exceptionState.throwDOMException(InvalidStateError, "respondWith is alre
ady called."); |
| 89 return; | 85 return; |
| 90 } | 86 } |
| 91 | 87 |
| 92 m_state = Pending; | 88 m_state = Pending; |
| 93 ScriptPromise::cast(scriptState, value).then( | 89 ScriptPromise::cast(scriptState, value).then( |
| 94 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, | 90 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled)
, |
| 95 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; | 91 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected))
; |
| 96 } | 92 } |
| 97 | 93 |
| 94 void RespondWithObserver::sendResponse(Response* response) |
| 95 { |
| 96 if (!executionContext()) |
| 97 return; |
| 98 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven
t(m_eventID, response); |
| 99 m_state = Done; |
| 100 } |
| 101 |
| 98 void RespondWithObserver::responseWasRejected() | 102 void RespondWithObserver::responseWasRejected() |
| 99 { | 103 { |
| 100 ASSERT(executionContext()); | 104 // FIXME: Throw a NetworkError to service worker's execution context. |
| 101 // The default value of WebServiceWorkerResponse's status is 0, which maps | 105 sendResponse(nullptr); |
| 102 // to a network error. | |
| 103 WebServiceWorkerResponse webResponse; | |
| 104 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven
t(m_eventID, webResponse); | |
| 105 m_state = Done; | |
| 106 } | 106 } |
| 107 | 107 |
| 108 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) | 108 void RespondWithObserver::responseWasFulfilled(const ScriptValue& value) |
| 109 { | 109 { |
| 110 ASSERT(executionContext()); | 110 if (!executionContext()) |
| 111 return; |
| 111 if (!V8Response::hasInstance(value.v8Value(), toIsolate(executionContext()))
) { | 112 if (!V8Response::hasInstance(value.v8Value(), toIsolate(executionContext()))
) { |
| 112 responseWasRejected(); | 113 responseWasRejected(); |
| 113 return; | 114 return; |
| 114 } | 115 } |
| 115 WebServiceWorkerResponse webResponse; | 116 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value.v8Value()
); |
| 116 V8Response::toImplWithTypeCheck(toIsolate(executionContext()), value.v8Value
())->populateWebServiceWorkerResponse(webResponse); | 117 sendResponse(V8Response::toImpl(object)); |
| 117 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven
t(m_eventID, webResponse); | |
| 118 m_state = Done; | |
| 119 } | 118 } |
| 120 | 119 |
| 121 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID) | 120 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID) |
| 122 : ContextLifecycleObserver(context) | 121 : ContextLifecycleObserver(context) |
| 123 , m_eventID(eventID) | 122 , m_eventID(eventID) |
| 124 , m_state(Initial) | 123 , m_state(Initial) |
| 125 { | 124 { |
| 126 } | 125 } |
| 127 | 126 |
| 128 } // namespace blink | 127 } // namespace blink |
| OLD | NEW |