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/WaitUntilObserver.h" | 5 #include "modules/serviceworkers/WaitUntilObserver.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/V8BindingForCore.h" | 10 #include "bindings/core/v8/V8BindingForCore.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 WaitUntilObserver* observer, | 58 WaitUntilObserver* observer, |
59 ResolveType type) | 59 ResolveType type) |
60 : ScriptFunction(script_state), | 60 : ScriptFunction(script_state), |
61 observer_(observer), | 61 observer_(observer), |
62 resolve_type_(type) {} | 62 resolve_type_(type) {} |
63 | 63 |
64 ScriptValue Call(ScriptValue value) override { | 64 ScriptValue Call(ScriptValue value) override { |
65 ASSERT(observer_); | 65 ASSERT(observer_); |
66 ASSERT(resolve_type_ == kFulfilled || resolve_type_ == kRejected); | 66 ASSERT(resolve_type_ == kFulfilled || resolve_type_ == kRejected); |
67 if (resolve_type_ == kRejected) { | 67 if (resolve_type_ == kRejected) { |
68 observer_->ReportError(value); | 68 observer_->OnPromiseRejected(); |
69 value = | 69 value = |
70 ScriptPromise::Reject(value.GetScriptState(), value).GetScriptValue(); | 70 ScriptPromise::Reject(value.GetScriptState(), value).GetScriptValue(); |
| 71 } else { |
| 72 observer_->OnPromiseFulfilled(); |
71 } | 73 } |
72 observer_->DecrementPendingActivity(); | |
73 observer_ = nullptr; | 74 observer_ = nullptr; |
74 return value; | 75 return value; |
75 } | 76 } |
76 | 77 |
77 Member<WaitUntilObserver> observer_; | 78 Member<WaitUntilObserver> observer_; |
78 ResolveType resolve_type_; | 79 ResolveType resolve_type_; |
79 }; | 80 }; |
80 | 81 |
81 WaitUntilObserver* WaitUntilObserver::Create(ExecutionContext* context, | 82 WaitUntilObserver* WaitUntilObserver::Create(ExecutionContext* context, |
82 EventType type, | 83 EventType type, |
83 int event_id) { | 84 int event_id) { |
84 return new WaitUntilObserver(context, type, event_id); | 85 return new WaitUntilObserver(context, type, event_id); |
85 } | 86 } |
86 | 87 |
87 void WaitUntilObserver::WillDispatchEvent() { | 88 void WaitUntilObserver::WillDispatchEvent() { |
88 event_dispatch_time_ = WTF::CurrentTime(); | 89 event_dispatch_time_ = WTF::CurrentTime(); |
89 // When handling a notificationclick or paymentrequest event, we want to | 90 // When handling a notificationclick or paymentrequest event, we want to |
90 // allow one window to be focused or opened. These calls are allowed between | 91 // allow one window to be focused or opened. These calls are allowed between |
91 // the call to willDispatchEvent() and the last call to | 92 // the call to willDispatchEvent() and the last call to |
92 // decrementPendingActivity(). If waitUntil() isn't called, that means | 93 // decrementPendingActivity(). If waitUntil() isn't called, that means |
93 // between willDispatchEvent() and didDispatchEvent(). | 94 // between willDispatchEvent() and didDispatchEvent(). |
94 if (type_ == kNotificationClick || type_ == kPaymentRequest) | 95 if (type_ == kNotificationClick || type_ == kPaymentRequest) |
95 execution_context_->AllowWindowInteraction(); | 96 execution_context_->AllowWindowInteraction(); |
96 | 97 |
97 IncrementPendingActivity(); | 98 IncrementPendingActivity(); |
98 } | 99 } |
99 | 100 |
100 void WaitUntilObserver::DidDispatchEvent(bool error_occurred) { | 101 void WaitUntilObserver::DidDispatchEvent(bool event_dispatch_failed) { |
101 if (error_occurred) | 102 event_dispatch_state_ = event_dispatch_failed |
102 has_error_ = true; | 103 ? EventDispatchState::kFailed |
| 104 : EventDispatchState::kCompleted; |
103 DecrementPendingActivity(); | 105 DecrementPendingActivity(); |
104 event_dispatched_ = true; | |
105 } | 106 } |
106 | 107 |
107 void WaitUntilObserver::WaitUntil(ScriptState* script_state, | 108 void WaitUntilObserver::WaitUntil(ScriptState* script_state, |
108 ScriptPromise script_promise, | 109 ScriptPromise script_promise, |
109 ExceptionState& exception_state) { | 110 ExceptionState& exception_state) { |
110 if (event_dispatched_) { | 111 if (event_dispatch_state_ != EventDispatchState::kInitial) { |
111 exception_state.ThrowDOMException(kInvalidStateError, | 112 exception_state.ThrowDOMException(kInvalidStateError, |
112 "The event handler is already finished."); | 113 "The event handler is already finished."); |
113 return; | 114 return; |
114 } | 115 } |
115 | 116 |
116 if (!execution_context_) | 117 if (!execution_context_) |
117 return; | 118 return; |
118 | 119 |
119 // When handling a notificationclick event, we want to allow one window to | 120 // When handling a notificationclick event, we want to allow one window to |
120 // be focused or opened. See comments in ::willDispatchEvent(). When | 121 // be focused or opened. See comments in ::willDispatchEvent(). When |
(...skipping 15 matching lines...) Expand all Loading... |
136 EventType type, | 137 EventType type, |
137 int event_id) | 138 int event_id) |
138 : execution_context_(context), | 139 : execution_context_(context), |
139 type_(type), | 140 type_(type), |
140 event_id_(event_id), | 141 event_id_(event_id), |
141 consume_window_interaction_timer_( | 142 consume_window_interaction_timer_( |
142 Platform::Current()->CurrentThread()->GetWebTaskRunner(), | 143 Platform::Current()->CurrentThread()->GetWebTaskRunner(), |
143 this, | 144 this, |
144 &WaitUntilObserver::ConsumeWindowInteraction) {} | 145 &WaitUntilObserver::ConsumeWindowInteraction) {} |
145 | 146 |
146 void WaitUntilObserver::ReportError(const ScriptValue& value) { | 147 void WaitUntilObserver::OnPromiseFulfilled() { |
147 // FIXME: Propagate error message to the client for onerror handling. | 148 DecrementPendingActivity(); |
148 NOTIMPLEMENTED(); | 149 } |
149 | 150 |
150 has_error_ = true; | 151 void WaitUntilObserver::OnPromiseRejected() { |
| 152 has_rejected_promise_ = true; |
| 153 DecrementPendingActivity(); |
151 } | 154 } |
152 | 155 |
153 void WaitUntilObserver::IncrementPendingActivity() { | 156 void WaitUntilObserver::IncrementPendingActivity() { |
154 ++pending_activity_; | 157 ++pending_activity_; |
155 } | 158 } |
156 | 159 |
157 void WaitUntilObserver::DecrementPendingActivity() { | 160 void WaitUntilObserver::DecrementPendingActivity() { |
158 ASSERT(pending_activity_ > 0); | 161 ASSERT(pending_activity_ > 0); |
159 if (!execution_context_ || (!has_error_ && --pending_activity_)) | 162 if (!execution_context_ || |
| 163 (event_dispatch_state_ != EventDispatchState::kFailed && |
| 164 --pending_activity_)) |
160 return; | 165 return; |
161 | 166 |
162 ServiceWorkerGlobalScopeClient* client = | 167 ServiceWorkerGlobalScopeClient* client = |
163 ServiceWorkerGlobalScopeClient::From(execution_context_); | 168 ServiceWorkerGlobalScopeClient::From(execution_context_); |
164 WebServiceWorkerEventResult result = | 169 WebServiceWorkerEventResult result = |
165 has_error_ ? kWebServiceWorkerEventResultRejected | 170 (event_dispatch_state_ == EventDispatchState::kFailed || |
166 : kWebServiceWorkerEventResultCompleted; | 171 has_rejected_promise_) |
| 172 ? kWebServiceWorkerEventResultRejected |
| 173 : kWebServiceWorkerEventResultCompleted; |
167 switch (type_) { | 174 switch (type_) { |
168 case kActivate: | 175 case kActivate: |
169 client->DidHandleActivateEvent(event_id_, result, event_dispatch_time_); | 176 client->DidHandleActivateEvent(event_id_, result, event_dispatch_time_); |
170 break; | 177 break; |
171 case kFetch: | 178 case kFetch: |
172 client->DidHandleFetchEvent(event_id_, result, event_dispatch_time_); | 179 client->DidHandleFetchEvent(event_id_, result, event_dispatch_time_); |
173 break; | 180 break; |
174 case kInstall: | 181 case kInstall: |
175 client->DidHandleInstallEvent(event_id_, result, event_dispatch_time_); | 182 client->DidHandleInstallEvent(event_id_, result, event_dispatch_time_); |
176 break; | 183 break; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 if (!execution_context_) | 229 if (!execution_context_) |
223 return; | 230 return; |
224 execution_context_->ConsumeWindowInteraction(); | 231 execution_context_->ConsumeWindowInteraction(); |
225 } | 232 } |
226 | 233 |
227 DEFINE_TRACE(WaitUntilObserver) { | 234 DEFINE_TRACE(WaitUntilObserver) { |
228 visitor->Trace(execution_context_); | 235 visitor->Trace(execution_context_); |
229 } | 236 } |
230 | 237 |
231 } // namespace blink | 238 } // namespace blink |
OLD | NEW |