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 "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" | 
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" | 
| 12 #include "core/dom/ExecutionContext.h" | 12 #include "core/dom/ExecutionContext.h" | 
| 13 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" | 13 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" | 
| 14 #include "platform/LayoutTestSupport.h" | 14 #include "platform/LayoutTestSupport.h" | 
| 15 #include "platform/bindings/Microtask.h" | |
| 15 #include "platform/wtf/Assertions.h" | 16 #include "platform/wtf/Assertions.h" | 
| 17 #include "platform/wtf/Functional.h" | |
| 16 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" | 
| 17 #include "public/platform/modules/serviceworker/WebServiceWorkerEventResult.h" | 19 #include "public/platform/modules/serviceworker/WebServiceWorkerEventResult.h" | 
| 18 #include "v8/include/v8.h" | 20 #include "v8/include/v8.h" | 
| 19 | 21 | 
| 20 namespace blink { | 22 namespace blink { | 
| 21 | 23 | 
| 22 namespace { | 24 namespace { | 
| 23 | 25 | 
| 24 // Timeout before a service worker that was given window interaction | 26 // Timeout before a service worker that was given window interaction | 
| 25 // permission loses them. The unit is seconds. | 27 // permission loses them. The unit is seconds. | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 resolve_type_(type) {} | 64 resolve_type_(type) {} | 
| 63 | 65 | 
| 64 ScriptValue Call(ScriptValue value) override { | 66 ScriptValue Call(ScriptValue value) override { | 
| 65 DCHECK(observer_); | 67 DCHECK(observer_); | 
| 66 DCHECK(resolve_type_ == kFulfilled || resolve_type_ == kRejected); | 68 DCHECK(resolve_type_ == kFulfilled || resolve_type_ == kRejected); | 
| 67 if (resolve_type_ == kRejected) { | 69 if (resolve_type_ == kRejected) { | 
| 68 observer_->OnPromiseRejected(); | 70 observer_->OnPromiseRejected(); | 
| 69 value = | 71 value = | 
| 70 ScriptPromise::Reject(value.GetScriptState(), value).GetScriptValue(); | 72 ScriptPromise::Reject(value.GetScriptState(), value).GetScriptValue(); | 
| 71 } else { | 73 } else { | 
| 72 observer_->OnPromiseFulfilled(); | 74 // At this time point the microtask A running resolver function of this | 
| 75 // promise has already been queued, in order to allow microtask A to call | |
| 76 // waitUntil, we enqueue another microtask B to delay the promise resolved | |
| 77 // notification, thus A will run before B so A can call waitUntil well, | |
| 78 // but any other microtask C possibly enqueued by A will run after B so | |
| 79 // C maybe can't call waitUntil if there has no any extend lifetime | |
| 80 // promise at that time. | |
| 81 Microtask::EnqueueMicrotask( | |
| 82 WTF::Bind(&WaitUntilObserver::OnPromiseFulfilled, | |
| 83 WrapPersistent(observer_.Get()))); | |
| 
 
leonhsl(Using Gerrit)
2017/05/22 13:23:02
I'm not so familiar with WTF::Bind compared with b
 
shimazu
2017/05/23 09:08:43
I think it's good as is because the task should ha
 
leonhsl(Using Gerrit)
2017/05/23 09:40:18
Got it. Thanks!
 
 | |
| 73 } | 84 } | 
| 74 observer_ = nullptr; | 85 observer_ = nullptr; | 
| 75 return value; | 86 return value; | 
| 76 } | 87 } | 
| 77 | 88 | 
| 78 Member<WaitUntilObserver> observer_; | 89 Member<WaitUntilObserver> observer_; | 
| 79 ResolveType resolve_type_; | 90 ResolveType resolve_type_; | 
| 80 }; | 91 }; | 
| 81 | 92 | 
| 82 WaitUntilObserver* WaitUntilObserver::Create(ExecutionContext* context, | 93 WaitUntilObserver* WaitUntilObserver::Create(ExecutionContext* context, | 
| (...skipping 18 matching lines...) Expand all Loading... | |
| 101 void WaitUntilObserver::DidDispatchEvent(bool event_dispatch_failed) { | 112 void WaitUntilObserver::DidDispatchEvent(bool event_dispatch_failed) { | 
| 102 event_dispatch_state_ = event_dispatch_failed | 113 event_dispatch_state_ = event_dispatch_failed | 
| 103 ? EventDispatchState::kFailed | 114 ? EventDispatchState::kFailed | 
| 104 : EventDispatchState::kCompleted; | 115 : EventDispatchState::kCompleted; | 
| 105 DecrementPendingActivity(); | 116 DecrementPendingActivity(); | 
| 106 } | 117 } | 
| 107 | 118 | 
| 108 void WaitUntilObserver::WaitUntil(ScriptState* script_state, | 119 void WaitUntilObserver::WaitUntil(ScriptState* script_state, | 
| 109 ScriptPromise script_promise, | 120 ScriptPromise script_promise, | 
| 110 ExceptionState& exception_state) { | 121 ExceptionState& exception_state) { | 
| 111 if (event_dispatch_state_ != EventDispatchState::kInitial) { | 122 if (pending_activity_ == 0) { | 
| 112 exception_state.ThrowDOMException(kInvalidStateError, | 123 exception_state.ThrowDOMException(kInvalidStateError, | 
| 113 "The event handler is already finished."); | 124 "The event handler is already finished " | 
| 125 "and no any extend lifetime promises are " | |
| 126 "outstanding."); | |
| 114 return; | 127 return; | 
| 115 } | 128 } | 
| 116 | 129 | 
| 130 if (pending_activity_ == 1 && | |
| 131 event_dispatch_state_ == EventDispatchState::kInitial && | |
| 132 v8::MicrotasksScope::IsRunningMicrotasks(script_state->GetIsolate())) { | |
| 
 
shimazu
2017/05/22 03:48:41
Could you explain the purpose of this branch?
I mi
 
leonhsl(Using Gerrit)
2017/05/22 05:01:43
This is to forbid calling waitUntil in such a case
 
shimazu
2017/05/22 08:09:05
I see, I could understand what this is for, thanks
 
leonhsl(Using Gerrit)
2017/05/22 13:23:02
The above suggestion reminded me, I considered a l
 
 | |
| 133 exception_state.ThrowDOMException( | |
| 134 kInvalidStateError, | |
| 135 "Although the event handler is not finished yet, it's running " | |
| 136 "microtask, and no any extend lifetime promises are outstanding."); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 117 if (!execution_context_) | 140 if (!execution_context_) | 
| 118 return; | 141 return; | 
| 119 | 142 | 
| 120 // When handling a notificationclick event, we want to allow one window to | 143 // When handling a notificationclick event, we want to allow one window to | 
| 121 // be focused or opened. See comments in ::willDispatchEvent(). When | 144 // be focused or opened. See comments in ::willDispatchEvent(). When | 
| 122 // waitUntil() is being used, opening or closing a window must happen in a | 145 // waitUntil() is being used, opening or closing a window must happen in a | 
| 123 // timeframe specified by windowInteractionTimeout(), otherwise the calls | 146 // timeframe specified by windowInteractionTimeout(), otherwise the calls | 
| 124 // will fail. | 147 // will fail. | 
| 125 if (type_ == kNotificationClick) | 148 if (type_ == kNotificationClick) | 
| 126 consume_window_interaction_timer_.StartOneShot(WindowInteractionTimeout(), | 149 consume_window_interaction_timer_.StartOneShot(WindowInteractionTimeout(), | 
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 if (!execution_context_) | 252 if (!execution_context_) | 
| 230 return; | 253 return; | 
| 231 execution_context_->ConsumeWindowInteraction(); | 254 execution_context_->ConsumeWindowInteraction(); | 
| 232 } | 255 } | 
| 233 | 256 | 
| 234 DEFINE_TRACE(WaitUntilObserver) { | 257 DEFINE_TRACE(WaitUntilObserver) { | 
| 235 visitor->Trace(execution_context_); | 258 visitor->Trace(execution_context_); | 
| 236 } | 259 } | 
| 237 | 260 | 
| 238 } // namespace blink | 261 } // namespace blink | 
| OLD | NEW |