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 RespondWithObserver_h | 5 #ifndef RespondWithObserver_h |
6 #define RespondWithObserver_h | 6 #define RespondWithObserver_h |
7 | 7 |
8 #include "core/dom/ContextLifecycleObserver.h" | 8 #include "core/dom/ContextLifecycleObserver.h" |
9 #include "core/events/EventTarget.h" | 9 #include "core/events/EventTarget.h" |
10 #include "modules/ModulesExport.h" | 10 #include "modules/ModulesExport.h" |
11 #include "modules/serviceworkers/WaitUntilObserver.h" | |
12 #include "platform/heap/Handle.h" | 11 #include "platform/heap/Handle.h" |
13 #include "public/platform/WebURLRequest.h" | |
14 #include "public/platform/modules/serviceworker/WebServiceWorkerResponseError.h" | 12 #include "public/platform/modules/serviceworker/WebServiceWorkerResponseError.h" |
15 | 13 |
16 namespace blink { | 14 namespace blink { |
17 | 15 |
18 class ExceptionState; | 16 class ExceptionState; |
19 class ExecutionContext; | 17 class ExecutionContext; |
20 class ScriptPromise; | 18 class ScriptPromise; |
21 class ScriptState; | 19 class ScriptState; |
22 class ScriptValue; | 20 class ScriptValue; |
21 class WaitUntilObserver; | |
23 | 22 |
24 // This class observes the service worker's handling of a FetchEvent and | 23 // This is a base class to implement respondWith. The respondWith has the three |
25 // notifies the client. | 24 // types of results: fulfilled, rejected and not called. Derived classes for |
25 // each event should implement the procedure of the three behaviors by | |
26 // overriding onResponseFulfilled, onResponseRejected and onNoResponse. | |
26 class MODULES_EXPORT RespondWithObserver | 27 class MODULES_EXPORT RespondWithObserver |
27 : public GarbageCollectedFinalized<RespondWithObserver>, | 28 : public GarbageCollectedFinalized<RespondWithObserver>, |
28 public ContextLifecycleObserver { | 29 public ContextLifecycleObserver { |
29 USING_GARBAGE_COLLECTED_MIXIN(RespondWithObserver); | 30 USING_GARBAGE_COLLECTED_MIXIN(RespondWithObserver); |
30 | 31 |
31 public: | 32 public: |
32 virtual ~RespondWithObserver(); | 33 virtual ~RespondWithObserver() = default; |
33 | |
34 static RespondWithObserver* create(ExecutionContext*, | |
35 int fetchEventID, | |
36 const KURL& requestURL, | |
37 WebURLRequest::FetchRequestMode, | |
38 WebURLRequest::FetchRedirectMode, | |
39 WebURLRequest::FrameType, | |
40 WebURLRequest::RequestContext, | |
41 WaitUntilObserver*); | |
42 | 34 |
43 void contextDestroyed(ExecutionContext*) override; | 35 void contextDestroyed(ExecutionContext*) override; |
44 | 36 |
45 void willDispatchEvent(); | 37 void willDispatchEvent(); |
46 void didDispatchEvent(DispatchEventResult dispatchResult); | 38 void didDispatchEvent(DispatchEventResult dispatchResult); |
47 | 39 |
48 // Observes the promise and delays calling didHandleFetchEvent() until the | 40 // The respondWith() observes the promise until the given promise is resolved |
49 // given promise is resolved or rejected. | 41 // or rejected and then delays calling ServiceWorkerGlobalScopeClient:: |
42 // didHandle*Event() in order to notify the result to the client. | |
50 void respondWith(ScriptState*, ScriptPromise, ExceptionState&); | 43 void respondWith(ScriptState*, ScriptPromise, ExceptionState&); |
51 | 44 |
52 void responseWasRejected(WebServiceWorkerResponseError); | 45 virtual void onResponseRejected(WebServiceWorkerResponseError) = 0; |
falken
2017/03/14 07:33:20
Can you add lightweight comments for these:
// Ca
zino
2017/03/14 14:10:05
Done.
| |
53 virtual void responseWasFulfilled(const ScriptValue&); | 46 virtual void onResponseFulfilled(const ScriptValue&) = 0; |
47 virtual void onNoResponse() = 0; | |
54 | 48 |
55 DECLARE_VIRTUAL_TRACE(); | 49 DECLARE_VIRTUAL_TRACE(); |
56 | 50 |
57 protected: | 51 protected: |
58 RespondWithObserver(ExecutionContext*, | 52 RespondWithObserver(ExecutionContext*, int eventID, WaitUntilObserver*); |
59 int fetchEventID, | 53 const int m_eventID; |
60 const KURL& requestURL, | 54 double m_eventDispatchTime = 0; |
61 WebURLRequest::FetchRequestMode, | |
62 WebURLRequest::FetchRedirectMode, | |
63 WebURLRequest::FrameType, | |
64 WebURLRequest::RequestContext, | |
65 WaitUntilObserver*); | |
66 | 55 |
67 private: | 56 private: |
68 class ThenFunction; | 57 class ThenFunction; |
69 | 58 |
70 const int m_fetchEventID; | 59 void responseWasRejected(WebServiceWorkerResponseError); |
71 const KURL m_requestURL; | 60 void responseWasFulfilled(const ScriptValue&); |
72 const WebURLRequest::FetchRequestMode m_requestMode; | |
73 const WebURLRequest::FetchRedirectMode m_redirectMode; | |
74 const WebURLRequest::FrameType m_frameType; | |
75 const WebURLRequest::RequestContext m_requestContext; | |
76 | |
77 double m_eventDispatchTime = 0; | |
78 | 61 |
79 enum State { Initial, Pending, Done }; | 62 enum State { Initial, Pending, Done }; |
80 State m_state; | 63 State m_state; |
81 | 64 |
82 // RespondWith should ensure the ExtendableEvent is alive until the promise | 65 // RespondWith should ensure the ExtendableEvent is alive until the promise |
83 // passed to RespondWith is resolved. The lifecycle of the ExtendableEvent | 66 // passed to RespondWith is resolved. The lifecycle of the ExtendableEvent |
84 // is controlled by WaitUntilObserver, so not only | 67 // is controlled by WaitUntilObserver, so not only |
85 // WaitUntilObserver::ThenFunction but RespondWith needs to have a strong | 68 // WaitUntilObserver::ThenFunction but RespondWith needs to have a strong |
86 // reference to the WaitUntilObserver. | 69 // reference to the WaitUntilObserver. |
87 Member<WaitUntilObserver> m_observer; | 70 Member<WaitUntilObserver> m_observer; |
88 }; | 71 }; |
89 | 72 |
90 } // namespace blink | 73 } // namespace blink |
91 | 74 |
92 #endif // RespondWithObserver_h | 75 #endif // RespondWithObserver_h |
OLD | NEW |