1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/service_worker/wait_until_observer.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom-blink.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/bindings/core/v8/script_function.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/core/execution_context/agent.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope.h"
#include "third_party/blink/renderer/platform/scheduler/public/event_loop.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/web_test_support.h"
#include "v8/include/v8.h"
namespace blink {
namespace {
// Timeout before a service worker that was given window interaction
// permission loses them. The unit is seconds.
const unsigned kWindowInteractionTimeout = 10;
const unsigned kWindowInteractionTimeoutForTest = 1;
base::TimeDelta WindowInteractionTimeout() {
return base::Seconds(WebTestSupport::IsRunningWebTest()
? kWindowInteractionTimeoutForTest
: kWindowInteractionTimeout);
}
} // anonymous namespace
// According from step 4 of ExtendableEvent::waitUntil() in spec:
// https://w3c.github.io/ServiceWorker/#dom-extendableevent-waituntil
// "Upon fulfillment or rejection of f, queue a microtask to run these
// substeps: Decrement the pending promises count by one."
class WaitUntilObserver::ThenFulfilled final
: public ThenCallable<IDLUndefined, ThenFulfilled> {
public:
explicit ThenFulfilled(WaitUntilObserver* observer) : observer_(observer) {}
void Trace(Visitor* visitor) const override {
visitor->Trace(observer_);
ThenCallable<IDLUndefined, ThenFulfilled>::Trace(visitor);
}
void React(ScriptState*) {
DCHECK(observer_);
observer_->OnPromiseFulfilled();
observer_ = nullptr;
}
private:
Member<WaitUntilObserver> observer_;
};
class WaitUntilObserver::ThenRejected final
: public ThenCallable<IDLAny, ThenRejected, IDLPromise<IDLAny>> {
public:
explicit ThenRejected(WaitUntilObserver* observer) : observer_(observer) {}
void Trace(Visitor* visitor) const override {
visitor->Trace(observer_);
ThenCallable<IDLAny, ThenRejected, IDLPromise<IDLAny>>::Trace(visitor);
}
ScriptPromise<IDLAny> React(ScriptState* script_state, ScriptValue value) {
DCHECK(observer_);
observer_->OnPromiseRejected();
observer_ = nullptr;
return ScriptPromise<IDLAny>::Reject(script_state, value);
}
private:
Member<WaitUntilObserver> observer_;
};
void WaitUntilObserver::WillDispatchEvent() {
DCHECK(GetExecutionContext());
// When handling a notificationclick, paymentrequest, or backgroundfetchclick
// event, we want to allow one window to be focused or opened. These calls are
// allowed between the call to willDispatchEvent() and the last call to
// DecrementPendingPromiseCount(). If waitUntil() isn't called, that means
// between willDispatchEvent() and didDispatchEvent().
if (type_ == kNotificationClick || type_ == kPaymentRequest ||
type_ == kBackgroundFetchClick) {
GetExecutionContext()->AllowWindowInteraction();
}
DCHECK_EQ(EventDispatchState::kInitial, event_dispatch_state_);
event_dispatch_state_ = EventDispatchState::kDispatching;
}
void WaitUntilObserver::DidDispatchEvent(bool event_dispatch_failed) {
event_dispatch_state_ = event_dispatch_failed
? EventDispatchState::kFailed
: EventDispatchState::kDispatched;
MaybeCompleteEvent();
}
// https://w3c.github.io/ServiceWorker/#dom-extendableevent-waituntil
bool WaitUntilObserver::WaitUntil(
ScriptState* script_state,
const ScriptPromise<IDLUndefined>& script_promise,
ExceptionState& exception_state) {
DCHECK_NE(event_dispatch_state_, EventDispatchState::kInitial);
// 1. `If the isTrusted attribute is false, throw an "InvalidStateError"
// DOMException.`
// This might not yet be implemented.
// 2. `If not active, throw an "InvalidStateError" DOMException.`
if (!IsEventActive()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kInvalidStateError,
"The event handler is already finished and no extend lifetime "
"promises are outstanding.");
return false;
}
// When handling a notificationclick event, we want to allow one window to
// be focused or opened. See comments in ::willDispatchEvent(). When
// waitUntil() is being used, opening or closing a window must happen in a
// timeframe specified by windowInteractionTimeout(), otherwise the calls
// will fail.
if (type_ == kNotificationClick) {
consume_window_interaction_timer_.StartOneShot(WindowInteractionTimeout(),
FROM_HERE);
}
// 3. `Add f to the extend lifetime promises.`
// 4. `Increment the pending promises count by one.`
IncrementPendingPromiseCount();
script_promise.Then(script_state, MakeGarbageCollected<ThenFulfilled>(this),
MakeGarbageCollected<ThenRejected>(this));
return true;
}
// https://w3c.github.io/ServiceWorker/#extendableevent-active
bool WaitUntilObserver::IsEventActive() const {
// `An ExtendableEvent object is said to be active when its timed out flag is
// unset and either its pending promises count is greater than zero or its
// dispatch flag is set.`
return pending_promises_ > 0 || IsDispatchingEvent();
}
bool WaitUntilObserver::IsDispatchingEvent() const {
return event_dispatch_state_ == EventDispatchState::kDispatching;
}
WaitUntilObserver::WaitUntilObserver(ExecutionContext* context,
EventType type,
int event_id)
: ExecutionContextClient(context),
type_(type),
event_id_(event_id),
consume_window_interaction_timer_(
context->GetTaskRunner(TaskType::kUserInteraction),
this,
&WaitUntilObserver::ConsumeWindowInteraction) {}
void WaitUntilObserver::OnPromiseFulfilled() {
DecrementPendingPromiseCount();
}
void WaitUntilObserver::OnPromiseRejected() {
has_rejected_promise_ = true;
DecrementPendingPromiseCount();
}
void WaitUntilObserver::IncrementPendingPromiseCount() {
++pending_promises_;
}
void WaitUntilObserver::DecrementPendingPromiseCount() {
DCHECK_GT(pending_promises_, 0);
--pending_promises_;
MaybeCompleteEvent();
}
void WaitUntilObserver::MaybeCompleteEvent() {
if (!GetExecutionContext())
return;
switch (event_dispatch_state_) {
case EventDispatchState::kInitial:
NOTREACHED();
case EventDispatchState::kDispatching:
// Still dispatching, do not complete the event.
return;
case EventDispatchState::kDispatched:
// Still waiting for a promise, do not complete the event.
if (pending_promises_ != 0)
return;
// Dispatch finished and there are no pending promises, complete the
// event.
break;
case EventDispatchState::kFailed:
// Dispatch had some error, complete the event immediately.
break;
}
ServiceWorkerGlobalScope* service_worker_global_scope =
To<ServiceWorkerGlobalScope>(GetExecutionContext());
mojom::ServiceWorkerEventStatus status =
(event_dispatch_state_ == EventDispatchState::kFailed ||
has_rejected_promise_)
? mojom::ServiceWorkerEventStatus::REJECTED
: mojom::ServiceWorkerEventStatus::COMPLETED;
switch (type_) {
case kAbortPayment:
service_worker_global_scope->DidHandleAbortPaymentEvent(event_id_,
status);
break;
case kActivate:
service_worker_global_scope->DidHandleActivateEvent(event_id_, status);
break;
case kCanMakePayment:
service_worker_global_scope->DidHandleCanMakePaymentEvent(event_id_,
status);
break;
case kCookieChange:
service_worker_global_scope->DidHandleCookieChangeEvent(event_id_,
status);
break;
case kFetch:
service_worker_global_scope->DidHandleFetchEvent(event_id_, status);
break;
case kInstall:
To<ServiceWorkerGlobalScope>(*GetExecutionContext())
.SetIsInstalling(false);
service_worker_global_scope->DidHandleInstallEvent(event_id_, status);
break;
case kMessage:
service_worker_global_scope->DidHandleExtendableMessageEvent(event_id_,
status);
break;
case kMessageerror:
service_worker_global_scope->DidHandleExtendableMessageEvent(event_id_,
status);
break;
case kNotificationClick:
service_worker_global_scope->DidHandleNotificationClickEvent(event_id_,
status);
consume_window_interaction_timer_.Stop();
ConsumeWindowInteraction(nullptr);
break;
case kNotificationClose:
service_worker_global_scope->DidHandleNotificationCloseEvent(event_id_,
status);
break;
case kPush:
service_worker_global_scope->DidHandlePushEvent(event_id_, status);
break;
case kPushSubscriptionChange:
service_worker_global_scope->DidHandlePushSubscriptionChangeEvent(
event_id_, status);
break;
case kSync:
service_worker_global_scope->DidHandleSyncEvent(event_id_, status);
break;
case kPeriodicSync:
service_worker_global_scope->DidHandlePeriodicSyncEvent(event_id_,
status);
break;
case kPaymentRequest:
service_worker_global_scope->DidHandlePaymentRequestEvent(event_id_,
status);
break;
case kBackgroundFetchAbort:
service_worker_global_scope->DidHandleBackgroundFetchAbortEvent(event_id_,
status);
break;
case kBackgroundFetchClick:
service_worker_global_scope->DidHandleBackgroundFetchClickEvent(event_id_,
status);
break;
case kBackgroundFetchFail:
service_worker_global_scope->DidHandleBackgroundFetchFailEvent(event_id_,
status);
break;
case kBackgroundFetchSuccess:
service_worker_global_scope->DidHandleBackgroundFetchSuccessEvent(
event_id_, status);
break;
case kContentDelete:
service_worker_global_scope->DidHandleContentDeleteEvent(event_id_,
status);
break;
}
}
void WaitUntilObserver::ConsumeWindowInteraction(TimerBase*) {
if (ExecutionContext* context = GetExecutionContext())
context->ConsumeWindowInteraction();
}
void WaitUntilObserver::Trace(Visitor* visitor) const {
visitor->Trace(consume_window_interaction_timer_);
ExecutionContextClient::Trace(visitor);
}
} // namespace blink
|