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

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

Issue 2867023002: [ServiceWorker] waitUntil() should wait until all promises got resolved/rejected. (Closed)
Patch Set: More 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 #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 12
13 namespace blink { 13 namespace blink {
14 14
15 class ExceptionState; 15 class ExceptionState;
16 class ExecutionContext; 16 class ExecutionContext;
17 class ScriptPromise; 17 class ScriptPromise;
18 class ScriptState; 18 class ScriptState;
19 class ScriptValue;
20 19
21 // Created for each ExtendableEvent instance. 20 // Created for each ExtendableEvent instance.
22 class MODULES_EXPORT WaitUntilObserver final 21 class MODULES_EXPORT WaitUntilObserver final
23 : public GarbageCollectedFinalized<WaitUntilObserver> { 22 : public GarbageCollectedFinalized<WaitUntilObserver> {
24 public: 23 public:
25 enum EventType { 24 enum EventType {
26 kActivate, 25 kActivate,
27 kFetch, 26 kFetch,
28 kInstall, 27 kInstall,
29 kMessage, 28 kMessage,
30 kNotificationClick, 29 kNotificationClick,
31 kNotificationClose, 30 kNotificationClose,
32 kPaymentRequest, 31 kPaymentRequest,
33 kPush, 32 kPush,
34 kSync, 33 kSync,
35 kBackgroundFetchAbort, 34 kBackgroundFetchAbort,
36 kBackgroundFetchClick, 35 kBackgroundFetchClick,
37 kBackgroundFetchFail, 36 kBackgroundFetchFail,
38 kBackgroundFetched 37 kBackgroundFetched
39 }; 38 };
40 39
41 static WaitUntilObserver* Create(ExecutionContext*, EventType, int event_id); 40 static WaitUntilObserver* Create(ExecutionContext*, EventType, int event_id);
42 41
43 // Must be called before and after dispatching the event. 42 // Must be called before dispatching the event.
44 void WillDispatchEvent(); 43 void WillDispatchEvent();
45 void DidDispatchEvent(bool error_occurred); 44 // Must be called after dispatching the event. If |event_dispatch_failed| is
45 // true, then DidDispatchEvent() immediately reports to
46 // ServiceWorkerGlobalScopeClient that the event finished, without waiting for
47 // all waitUntil promises to settle.
48 void DidDispatchEvent(bool event_dispatch_failed);
46 49
47 // Observes the promise and delays calling the continuation until 50 // Observes the promise and delays calling the continuation until
48 // the given promise is resolved or rejected. 51 // the given promise is resolved or rejected.
49 void WaitUntil(ScriptState*, ScriptPromise, ExceptionState&); 52 void WaitUntil(ScriptState*, ScriptPromise, ExceptionState&);
50 53
51 // These methods can be called when the lifecycle of ExtendableEvent 54 // These methods can be called when the lifecycle of ExtendableEvent
52 // observed by this WaitUntilObserver should be extended by other reason 55 // observed by this WaitUntilObserver should be extended by other reason
53 // than ExtendableEvent.waitUntil. 56 // than ExtendableEvent.waitUntil.
54 // Note: There is no need to call decrementPendingActivity() after the context 57 // Note: There is no need to call decrementPendingActivity() after the context
55 // is being destroyed. 58 // is being destroyed.
56 void IncrementPendingActivity(); 59 void IncrementPendingActivity();
57 void DecrementPendingActivity(); 60 void DecrementPendingActivity();
58 61
59 DECLARE_VIRTUAL_TRACE(); 62 DECLARE_VIRTUAL_TRACE();
60 63
61 private: 64 private:
62 friend class InternalsServiceWorker; 65 friend class InternalsServiceWorker;
63 class ThenFunction; 66 class ThenFunction;
64 67
68 enum class EventDispatchState {
69 // Event dispatch has not yet finished.
70 kInitial,
71 // Event dispatch completed. There may still be outstanding waitUntil
72 // promises that must settle before notifying ServiceWorkerGlobalScopeClient
73 // that the event finished.
74 kCompleted,
75 // Event dispatch failed. Any outstanding waitUntil promises are ignored.
76 kFailed
77 };
78
65 WaitUntilObserver(ExecutionContext*, EventType, int event_id); 79 WaitUntilObserver(ExecutionContext*, EventType, int event_id);
66 80
67 void ReportError(const ScriptValue&); 81 // Called when a promise passed to a waitUntil() call that is associated with
82 // this observer was fulfilled.
83 void OnPromiseFulfilled();
84 // Called when a promise passed to a waitUntil() call that is associated with
85 // this observer was rejected.
86 void OnPromiseRejected();
68 87
69 void ConsumeWindowInteraction(TimerBase*); 88 void ConsumeWindowInteraction(TimerBase*);
70 89
71 Member<ExecutionContext> execution_context_; 90 Member<ExecutionContext> execution_context_;
72 EventType type_; 91 EventType type_;
73 int event_id_; 92 int event_id_;
74 int pending_activity_ = 0; 93 int pending_activity_ = 0;
75 bool has_error_ = false; 94 EventDispatchState event_dispatch_state_ = EventDispatchState::kInitial;
76 bool event_dispatched_ = false; 95 bool has_rejected_promise_ = false;
77 double event_dispatch_time_ = 0; 96 double event_dispatch_time_ = 0;
78 TaskRunnerTimer<WaitUntilObserver> consume_window_interaction_timer_; 97 TaskRunnerTimer<WaitUntilObserver> consume_window_interaction_timer_;
79 }; 98 };
80 99
81 } // namespace blink 100 } // namespace blink
82 101
83 #endif // WaitUntilObserver_h 102 #endif // WaitUntilObserver_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698