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 if (m_state != Initial || !executionContext()) |
| 78 sendResponse(nullptr); | 79 return; |
| 80 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID); | |
| 81 m_state = Done; | |
| 79 } | 82 } |
| 80 | 83 |
| 81 void RespondWithObserver::respondWith(ScriptState* scriptState, const ScriptValu e& value, ExceptionState& exceptionState) | 84 void RespondWithObserver::respondWith(ScriptState* scriptState, const ScriptValu e& value, ExceptionState& exceptionState) |
| 82 { | 85 { |
| 83 if (m_state != Initial) { | 86 if (m_state != Initial) { |
| 84 exceptionState.throwDOMException(InvalidStateError, "respondWith is alre ady called."); | 87 exceptionState.throwDOMException(InvalidStateError, "respondWith is alre ady called."); |
| 85 return; | 88 return; |
| 86 } | 89 } |
| 87 | 90 |
| 88 m_state = Pending; | 91 m_state = Pending; |
| 89 ScriptPromise::cast(scriptState, value).then( | 92 ScriptPromise::cast(scriptState, value).then( |
| 90 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) , | 93 ThenFunction::createFunction(scriptState, this, ThenFunction::Fulfilled) , |
| 91 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ; | 94 ThenFunction::createFunction(scriptState, this, ThenFunction::Rejected)) ; |
| 92 } | 95 } |
| 93 | 96 |
| 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() | 97 void RespondWithObserver::responseWasRejected() |
| 103 { | 98 { |
| 104 // FIXME: Throw a NetworkError to service worker's execution context. | 99 if (!executionContext()) |
|
Mike West
2014/09/16 11:01:38
Nit: Can the response be rejected outside of an ex
horo
2014/09/16 11:17:40
I think we can use ASSERT here.
Done.
| |
| 105 sendResponse(nullptr); | 100 return; |
| 101 // The default value of WebServiceWorkerResponse's status is 0 that means a network error. | |
|
Mike West
2014/09/16 11:01:38
Nit: I'd suggest "... is 0, which maps to a networ
horo
2014/09/16 11:17:41
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 if (!executionContext()) |
| 111 return; | 110 return; |
| 112 if (!V8Response::hasInstance(value.v8Value(), toIsolate(executionContext())) ) { | 111 if (!V8Response::hasInstance(value.v8Value(), toIsolate(executionContext())) ) { |
| 113 responseWasRejected(); | 112 responseWasRejected(); |
| 114 return; | 113 return; |
| 115 } | 114 } |
| 116 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value.v8Value() ); | 115 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value.v8Value() ); |
| 117 sendResponse(V8Response::toImpl(object)); | 116 WebServiceWorkerResponse webResponse; |
| 117 V8Response::toImpl(object)->populateWebServiceWorkerResponse(webResponse); | |
|
Mike West
2014/09/16 11:01:38
Nit: You could use 'toImplWithTypeCheck' to skip t
horo
2014/09/16 11:17:40
Done.
| |
| 118 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse); | |
| 119 m_state = Done; | |
| 118 } | 120 } |
| 119 | 121 |
| 120 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID) | 122 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID) |
| 121 : ContextLifecycleObserver(context) | 123 : ContextLifecycleObserver(context) |
| 122 , m_eventID(eventID) | 124 , m_eventID(eventID) |
| 123 , m_state(Initial) | 125 , m_state(Initial) |
| 124 { | 126 { |
| 125 } | 127 } |
| 126 | 128 |
| 127 } // namespace blink | 129 } // namespace blink |
| OLD | NEW |