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 #ifndef WaitUntilObserver_h | 5 #ifndef WaitUntilObserver_h |
6 #define WaitUntilObserver_h | 6 #define WaitUntilObserver_h |
7 | 7 |
8 #include "modules/ModulesExport.h" | 8 #include "modules/ModulesExport.h" |
9 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 9 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
10 #include "platform/Timer.h" | 10 #include "platform/Timer.h" |
11 #include "platform/wtf/Forward.h" | 11 #include "platform/wtf/Forward.h" |
| 12 #include "platform/wtf/Functional.h" |
12 | 13 |
13 namespace blink { | 14 namespace blink { |
14 | 15 |
15 class ExceptionState; | 16 class ExceptionState; |
16 class ExecutionContext; | 17 class ExecutionContext; |
17 class ScriptPromise; | 18 class ScriptPromise; |
18 class ScriptState; | 19 class ScriptState; |
19 | 20 |
20 // Created for each ExtendableEvent instance. | 21 // Created for each ExtendableEvent instance. |
21 class MODULES_EXPORT WaitUntilObserver final | 22 class MODULES_EXPORT WaitUntilObserver final |
22 : public GarbageCollectedFinalized<WaitUntilObserver> { | 23 : public GarbageCollectedFinalized<WaitUntilObserver> { |
23 public: | 24 public: |
| 25 using PromiseSettledCallback = Function<void(const ScriptValue&)>; |
| 26 |
24 enum EventType { | 27 enum EventType { |
25 kActivate, | 28 kActivate, |
26 kFetch, | 29 kFetch, |
27 kInstall, | 30 kInstall, |
28 kMessage, | 31 kMessage, |
29 kNotificationClick, | 32 kNotificationClick, |
30 kNotificationClose, | 33 kNotificationClose, |
31 kPaymentRequest, | 34 kPaymentRequest, |
32 kPush, | 35 kPush, |
33 kSync, | 36 kSync, |
34 kBackgroundFetchAbort, | 37 kBackgroundFetchAbort, |
35 kBackgroundFetchClick, | 38 kBackgroundFetchClick, |
36 kBackgroundFetchFail, | 39 kBackgroundFetchFail, |
37 kBackgroundFetched | 40 kBackgroundFetched |
38 }; | 41 }; |
39 | 42 |
40 static WaitUntilObserver* Create(ExecutionContext*, EventType, int event_id); | 43 static WaitUntilObserver* Create(ExecutionContext*, EventType, int event_id); |
41 | 44 |
42 // Must be called before dispatching the event. | 45 // Must be called before dispatching the event. |
43 void WillDispatchEvent(); | 46 void WillDispatchEvent(); |
44 // Must be called after dispatching the event. If |event_dispatch_failed| is | 47 // Must be called after dispatching the event. If |event_dispatch_failed| is |
45 // true, then DidDispatchEvent() immediately reports to | 48 // true, then DidDispatchEvent() immediately reports to |
46 // ServiceWorkerGlobalScopeClient that the event finished, without waiting for | 49 // ServiceWorkerGlobalScopeClient that the event finished, without waiting for |
47 // all waitUntil promises to settle. | 50 // all waitUntil promises to settle. |
48 void DidDispatchEvent(bool event_dispatch_failed); | 51 void DidDispatchEvent(bool event_dispatch_failed); |
49 | 52 |
50 // Observes the promise and delays calling the continuation until | 53 // Observes the promise and delays reporting to ServiceWorkerGlobalScopeClient |
51 // the given promise is resolved or rejected. | 54 // that the event completed until the given promise is resolved or rejected. |
52 void WaitUntil(ScriptState*, ScriptPromise, ExceptionState&); | 55 // WaitUntil may be called multiple times. The event is extended until all |
53 | 56 // promises have settled. |
54 // These methods can be called when the lifecycle of ExtendableEvent | 57 // If provided, |on_promise_fulfilled| or |on_promise_rejected| is invoked |
55 // observed by this WaitUntilObserver should be extended by other reason | 58 // once |script_promise| fulfills or rejects. This enables the caller to do |
56 // than ExtendableEvent.waitUntil. | 59 // custom handling. |
57 // Note: There is no need to call decrementPendingActivity() after the context | 60 void WaitUntil( |
58 // is being destroyed. | 61 ScriptState*, |
59 void IncrementPendingActivity(); | 62 ScriptPromise /* script_promise */, |
60 void DecrementPendingActivity(); | 63 ExceptionState&, |
| 64 std::unique_ptr<PromiseSettledCallback> on_promise_fulfilled = nullptr, |
| 65 std::unique_ptr<PromiseSettledCallback> on_promise_rejected = nullptr); |
61 | 66 |
62 DECLARE_VIRTUAL_TRACE(); | 67 DECLARE_VIRTUAL_TRACE(); |
63 | 68 |
64 private: | 69 private: |
65 friend class InternalsServiceWorker; | 70 friend class InternalsServiceWorker; |
66 class ThenFunction; | 71 class ThenFunction; |
67 | 72 |
68 enum class EventDispatchState { | 73 enum class EventDispatchState { |
69 // Event dispatch has not yet finished. | 74 // Event dispatch has not yet started. |
70 kInitial, | 75 kInitial, |
| 76 // Event dispatch has started but not yet finished. |
| 77 kDispatching, |
71 // Event dispatch completed. There may still be outstanding waitUntil | 78 // Event dispatch completed. There may still be outstanding waitUntil |
72 // promises that must settle before notifying ServiceWorkerGlobalScopeClient | 79 // promises that must settle before notifying ServiceWorkerGlobalScopeClient |
73 // that the event finished. | 80 // that the event finished. |
74 kCompleted, | 81 kDispatched, |
75 // Event dispatch failed. Any outstanding waitUntil promises are ignored. | 82 // Event dispatch failed. Any outstanding waitUntil promises are ignored. |
76 kFailed | 83 kFailed |
77 }; | 84 }; |
78 | 85 |
79 WaitUntilObserver(ExecutionContext*, EventType, int event_id); | 86 WaitUntilObserver(ExecutionContext*, EventType, int event_id); |
80 | 87 |
81 // Called when a promise passed to a waitUntil() call that is associated with | 88 void IncrementPendingPromiseCount(); |
82 // this observer was fulfilled. | 89 void DecrementPendingPromiseCount(); |
| 90 |
| 91 // Enqueued as a microtask when a promise passed to a waitUntil() call that is |
| 92 // associated with this observer was fulfilled. |
83 void OnPromiseFulfilled(); | 93 void OnPromiseFulfilled(); |
84 // Called when a promise passed to a waitUntil() call that is associated with | 94 // Enqueued as a microtask when a promise passed to a waitUntil() call that is |
85 // this observer was rejected. | 95 // associated with this observer was rejected. |
86 void OnPromiseRejected(); | 96 void OnPromiseRejected(); |
87 | 97 |
88 void ConsumeWindowInteraction(TimerBase*); | 98 void ConsumeWindowInteraction(TimerBase*); |
89 | 99 |
| 100 void MaybeCompleteEvent(); |
| 101 |
90 Member<ExecutionContext> execution_context_; | 102 Member<ExecutionContext> execution_context_; |
91 EventType type_; | 103 EventType type_; |
92 int event_id_; | 104 int event_id_; |
93 int pending_activity_ = 0; | 105 int pending_promises_ = 0; |
94 EventDispatchState event_dispatch_state_ = EventDispatchState::kInitial; | 106 EventDispatchState event_dispatch_state_ = EventDispatchState::kInitial; |
95 bool has_rejected_promise_ = false; | 107 bool has_rejected_promise_ = false; |
96 double event_dispatch_time_ = 0; | 108 double event_dispatch_time_ = 0; |
97 TaskRunnerTimer<WaitUntilObserver> consume_window_interaction_timer_; | 109 TaskRunnerTimer<WaitUntilObserver> consume_window_interaction_timer_; |
98 }; | 110 }; |
99 | 111 |
100 } // namespace blink | 112 } // namespace blink |
101 | 113 |
102 #endif // WaitUntilObserver_h | 114 #endif // WaitUntilObserver_h |
OLD | NEW |