Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: third_party/WebKit/Source/modules/serviceworkers/WaitUntilObserver.cpp

Issue 2867023002: [ServiceWorker] waitUntil() should wait until all promises got resolved/rejected. (Closed)
Patch Set: Clean up Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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);
69 value = 68 value =
70 ScriptPromise::Reject(value.GetScriptState(), value).GetScriptValue(); 69 ScriptPromise::Reject(value.GetScriptState(), value).GetScriptValue();
71 } 70 }
72 observer_->DecrementPendingActivity(); 71 observer_->OnPromiseCompleted(resolve_type_ == kRejected ? true : false);
73 observer_ = nullptr; 72 observer_ = nullptr;
74 return value; 73 return value;
75 } 74 }
76 75
77 Member<WaitUntilObserver> observer_; 76 Member<WaitUntilObserver> observer_;
78 ResolveType resolve_type_; 77 ResolveType resolve_type_;
79 }; 78 };
80 79
81 WaitUntilObserver* WaitUntilObserver::Create(ExecutionContext* context, 80 WaitUntilObserver* WaitUntilObserver::Create(ExecutionContext* context,
82 EventType type, 81 EventType type,
83 int event_id) { 82 int event_id) {
84 return new WaitUntilObserver(context, type, event_id); 83 return new WaitUntilObserver(context, type, event_id);
85 } 84 }
86 85
87 void WaitUntilObserver::WillDispatchEvent() { 86 void WaitUntilObserver::WillDispatchEvent() {
88 event_dispatch_time_ = WTF::CurrentTime(); 87 event_dispatch_time_ = WTF::CurrentTime();
89 // When handling a notificationclick or paymentrequest event, we want to 88 // When handling a notificationclick or paymentrequest event, we want to
90 // allow one window to be focused or opened. These calls are allowed between 89 // allow one window to be focused or opened. These calls are allowed between
91 // the call to willDispatchEvent() and the last call to 90 // the call to willDispatchEvent() and the last call to
92 // decrementPendingActivity(). If waitUntil() isn't called, that means 91 // decrementPendingActivity(). If waitUntil() isn't called, that means
93 // between willDispatchEvent() and didDispatchEvent(). 92 // between willDispatchEvent() and didDispatchEvent().
94 if (type_ == kNotificationClick || type_ == kPaymentRequest) 93 if (type_ == kNotificationClick || type_ == kPaymentRequest)
95 execution_context_->AllowWindowInteraction(); 94 execution_context_->AllowWindowInteraction();
96 95
97 IncrementPendingActivity(); 96 IncrementPendingActivity();
98 } 97 }
99 98
100 void WaitUntilObserver::DidDispatchEvent(bool error_occurred) { 99 void WaitUntilObserver::DidDispatchEvent(bool event_dispatch_failed) {
101 if (error_occurred) 100 event_dispatch_state_ = event_dispatch_failed ? kFailed : kCompleted;
102 has_error_ = true;
103 DecrementPendingActivity(); 101 DecrementPendingActivity();
104 event_dispatched_ = true;
105 } 102 }
106 103
107 void WaitUntilObserver::WaitUntil(ScriptState* script_state, 104 void WaitUntilObserver::WaitUntil(ScriptState* script_state,
108 ScriptPromise script_promise, 105 ScriptPromise script_promise,
109 ExceptionState& exception_state) { 106 ExceptionState& exception_state) {
110 if (event_dispatched_) { 107 if (event_dispatch_state_ != kInitial) {
111 exception_state.ThrowDOMException(kInvalidStateError, 108 exception_state.ThrowDOMException(kInvalidStateError,
112 "The event handler is already finished."); 109 "The event handler is already finished.");
113 return; 110 return;
114 } 111 }
115 112
116 if (!execution_context_) 113 if (!execution_context_)
117 return; 114 return;
118 115
119 // When handling a notificationclick event, we want to allow one window to 116 // When handling a notificationclick event, we want to allow one window to
120 // be focused or opened. See comments in ::willDispatchEvent(). When 117 // be focused or opened. See comments in ::willDispatchEvent(). When
(...skipping 15 matching lines...) Expand all
136 EventType type, 133 EventType type,
137 int event_id) 134 int event_id)
138 : execution_context_(context), 135 : execution_context_(context),
139 type_(type), 136 type_(type),
140 event_id_(event_id), 137 event_id_(event_id),
141 consume_window_interaction_timer_( 138 consume_window_interaction_timer_(
142 Platform::Current()->CurrentThread()->GetWebTaskRunner(), 139 Platform::Current()->CurrentThread()->GetWebTaskRunner(),
143 this, 140 this,
144 &WaitUntilObserver::ConsumeWindowInteraction) {} 141 &WaitUntilObserver::ConsumeWindowInteraction) {}
145 142
146 void WaitUntilObserver::ReportError(const ScriptValue& value) { 143 void WaitUntilObserver::OnPromiseCompleted(bool rejected) {
falken 2017/05/10 05:25:12 OnPromiseSettled is aligned with promise terminolo
leonhsl(Using Gerrit) 2017/05/10 07:07:52 Done.
147 // FIXME: Propagate error message to the client for onerror handling. 144 if (rejected)
148 NOTIMPLEMENTED(); 145 has_rejected_promise_ = true;
149 146 DecrementPendingActivity();
150 has_error_ = true;
151 } 147 }
152 148
153 void WaitUntilObserver::IncrementPendingActivity() { 149 void WaitUntilObserver::IncrementPendingActivity() {
154 ++pending_activity_; 150 ++pending_activity_;
155 } 151 }
156 152
157 void WaitUntilObserver::DecrementPendingActivity() { 153 void WaitUntilObserver::DecrementPendingActivity() {
158 ASSERT(pending_activity_ > 0); 154 ASSERT(pending_activity_ > 0);
159 if (!execution_context_ || (!has_error_ && --pending_activity_)) 155 if (!execution_context_ ||
156 (event_dispatch_state_ != kFailed && --pending_activity_))
160 return; 157 return;
161 158
162 ServiceWorkerGlobalScopeClient* client = 159 ServiceWorkerGlobalScopeClient* client =
163 ServiceWorkerGlobalScopeClient::From(execution_context_); 160 ServiceWorkerGlobalScopeClient::From(execution_context_);
164 WebServiceWorkerEventResult result = 161 WebServiceWorkerEventResult result =
165 has_error_ ? kWebServiceWorkerEventResultRejected 162 (event_dispatch_state_ == kFailed || has_rejected_promise_)
166 : kWebServiceWorkerEventResultCompleted; 163 ? kWebServiceWorkerEventResultRejected
164 : kWebServiceWorkerEventResultCompleted;
167 switch (type_) { 165 switch (type_) {
168 case kActivate: 166 case kActivate:
169 client->DidHandleActivateEvent(event_id_, result, event_dispatch_time_); 167 client->DidHandleActivateEvent(event_id_, result, event_dispatch_time_);
170 break; 168 break;
171 case kFetch: 169 case kFetch:
172 client->DidHandleFetchEvent(event_id_, result, event_dispatch_time_); 170 client->DidHandleFetchEvent(event_id_, result, event_dispatch_time_);
173 break; 171 break;
174 case kInstall: 172 case kInstall:
175 client->DidHandleInstallEvent(event_id_, result, event_dispatch_time_); 173 client->DidHandleInstallEvent(event_id_, result, event_dispatch_time_);
176 break; 174 break;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 if (!execution_context_) 220 if (!execution_context_)
223 return; 221 return;
224 execution_context_->ConsumeWindowInteraction(); 222 execution_context_->ConsumeWindowInteraction();
225 } 223 }
226 224
227 DEFINE_TRACE(WaitUntilObserver) { 225 DEFINE_TRACE(WaitUntilObserver) {
228 visitor->Trace(execution_context_); 226 visitor->Trace(execution_context_);
229 } 227 }
230 228
231 } // namespace blink 229 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698