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