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 calling the continuation until |
falken
2017/05/25 07:23:24
s/the continuation/reporting to ServiceWorkerGloba
leonhsl(Using Gerrit)
2017/05/25 10:11:36
Done.
| |
51 // the given promise is resolved or rejected. | 54 // the given promise is resolved or rejected. |
falken
2017/05/25 07:23:24
WaitUntil may be called multiple times. The event
leonhsl(Using Gerrit)
2017/05/25 10:11:36
Done.
Thanks! Exactly what we should say here ;-)
| |
52 void WaitUntil(ScriptState*, ScriptPromise, ExceptionState&); | 55 void WaitUntil( |
53 | 56 ScriptState*, |
54 // These methods can be called when the lifecycle of ExtendableEvent | 57 ScriptPromise, |
falken
2017/05/25 07:23:24
|promise|
leonhsl(Using Gerrit)
2017/05/25 10:11:36
Done.
| |
55 // observed by this WaitUntilObserver should be extended by other reason | 58 ExceptionState&, |
56 // than ExtendableEvent.waitUntil. | 59 std::unique_ptr<PromiseSettledCallback> on_promise_fulfilled = nullptr, |
57 // Note: There is no need to call decrementPendingActivity() after the context | 60 std::unique_ptr<PromiseSettledCallback> on_promise_rejected = nullptr); |
58 // is being destroyed. | |
59 void IncrementPendingActivity(); | |
60 void DecrementPendingActivity(); | |
61 | 61 |
62 DECLARE_VIRTUAL_TRACE(); | 62 DECLARE_VIRTUAL_TRACE(); |
63 | 63 |
64 private: | 64 private: |
65 friend class InternalsServiceWorker; | 65 friend class InternalsServiceWorker; |
66 class ThenFunction; | 66 class ThenFunction; |
67 | 67 |
68 enum class EventDispatchState { | 68 enum class EventDispatchState { |
69 // Event dispatch has not yet finished. | 69 // Event dispatch has not yet started. |
70 kInitial, | 70 kInitial, |
71 // Event dispatch has started but not yet finished. | |
72 kDispatching, | |
71 // Event dispatch completed. There may still be outstanding waitUntil | 73 // Event dispatch completed. There may still be outstanding waitUntil |
72 // promises that must settle before notifying ServiceWorkerGlobalScopeClient | 74 // promises that must settle before notifying ServiceWorkerGlobalScopeClient |
73 // that the event finished. | 75 // that the event finished. |
74 kCompleted, | 76 kDispatched, |
75 // Event dispatch failed. Any outstanding waitUntil promises are ignored. | 77 // Event dispatch failed. Any outstanding waitUntil promises are ignored. |
76 kFailed | 78 kFailed |
77 }; | 79 }; |
78 | 80 |
79 WaitUntilObserver(ExecutionContext*, EventType, int event_id); | 81 WaitUntilObserver(ExecutionContext*, EventType, int event_id); |
80 | 82 |
81 // Called when a promise passed to a waitUntil() call that is associated with | 83 void IncrementPendingPromise(); |
82 // this observer was fulfilled. | 84 void DecrementPendingPromise(); |
falken
2017/05/25 07:23:24
IncrementPendingPromiseCount
DecrementPrendingProm
leonhsl(Using Gerrit)
2017/05/25 10:11:36
Done.
| |
85 | |
86 // Enqueued as a microtask when a promise passed to a waitUntil() call that is | |
87 // associated with this observer was fulfilled. | |
83 void OnPromiseFulfilled(); | 88 void OnPromiseFulfilled(); |
84 // Called when a promise passed to a waitUntil() call that is associated with | 89 // Enqueued as a microtask when a promise passed to a waitUntil() call that is |
85 // this observer was rejected. | 90 // associated with this observer was rejected. |
86 void OnPromiseRejected(); | 91 void OnPromiseRejected(); |
87 | 92 |
88 void ConsumeWindowInteraction(TimerBase*); | 93 void ConsumeWindowInteraction(TimerBase*); |
89 | 94 |
95 void MaybeCompleteEvent(); | |
96 | |
90 Member<ExecutionContext> execution_context_; | 97 Member<ExecutionContext> execution_context_; |
91 EventType type_; | 98 EventType type_; |
92 int event_id_; | 99 int event_id_; |
93 int pending_activity_ = 0; | 100 int pending_promises_ = 0; |
94 EventDispatchState event_dispatch_state_ = EventDispatchState::kInitial; | 101 EventDispatchState event_dispatch_state_ = EventDispatchState::kInitial; |
95 bool has_rejected_promise_ = false; | 102 bool has_rejected_promise_ = false; |
96 double event_dispatch_time_ = 0; | 103 double event_dispatch_time_ = 0; |
97 TaskRunnerTimer<WaitUntilObserver> consume_window_interaction_timer_; | 104 TaskRunnerTimer<WaitUntilObserver> consume_window_interaction_timer_; |
98 }; | 105 }; |
99 | 106 |
100 } // namespace blink | 107 } // namespace blink |
101 | 108 |
102 #endif // WaitUntilObserver_h | 109 #endif // WaitUntilObserver_h |
OLD | NEW |