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()); |
nhiroki
2014/09/17 05:42:46
Is this always true? Who ensures this condition?
horo
2014/09/17 07:45:26
In the constructor of RespondWithObserver, Context
kinuko
2014/09/23 16:44:38
Drive-by nit: if executionContext() can never be n
| |
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()); |
nhiroki
2014/09/17 05:42:46
ditto.
| |
105 sendResponse(nullptr); | 101 // The default value of WebServiceWorkerResponse's status is 0, which maps |
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 if (!executionContext()) | 110 ASSERT(executionContext()); |
nhiroki
2014/09/17 05:42:46
ditto.
| |
111 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 WebServiceWorkerResponse webResponse; |
117 sendResponse(V8Response::toImpl(object)); | 116 V8Response::toImplWithTypeCheck(toIsolate(executionContext()), value.v8Value ())->populateWebServiceWorkerResponse(webResponse); |
117 ServiceWorkerGlobalScopeClient::from(executionContext())->didHandleFetchEven t(m_eventID, webResponse); | |
118 m_state = Done; | |
118 } | 119 } |
119 | 120 |
120 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID) | 121 RespondWithObserver::RespondWithObserver(ExecutionContext* context, int eventID) |
121 : ContextLifecycleObserver(context) | 122 : ContextLifecycleObserver(context) |
122 , m_eventID(eventID) | 123 , m_eventID(eventID) |
123 , m_state(Initial) | 124 , m_state(Initial) |
124 { | 125 { |
125 } | 126 } |
126 | 127 |
127 } // namespace blink | 128 } // namespace blink |
OLD | NEW |