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

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

Issue 2715663002: ServiceWorker: Factor out FetchEvent related logics from RespondWithObserver. (Closed)
Patch Set: ServiceWorker: Factor out FetchEvent related logics from RespondWithObserver. Created 3 years, 9 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 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 // Called when the respondWith() promise was rejected.
53 virtual void responseWasFulfilled(const ScriptValue&); 46 virtual void onResponseRejected(WebServiceWorkerResponseError) = 0;
47
48 // Called when the respondWith() promise was fulfilled.
49 virtual void onResponseFulfilled(const ScriptValue&) = 0;
50
51 // Called when the event handler finished without calling respondWith().
52 virtual void onNoResponse() = 0;
54 53
55 DECLARE_VIRTUAL_TRACE(); 54 DECLARE_VIRTUAL_TRACE();
56 55
57 protected: 56 protected:
58 RespondWithObserver(ExecutionContext*, 57 RespondWithObserver(ExecutionContext*, int eventID, WaitUntilObserver*);
59 int fetchEventID, 58 const int m_eventID;
60 const KURL& requestURL, 59 double m_eventDispatchTime = 0;
61 WebURLRequest::FetchRequestMode,
62 WebURLRequest::FetchRedirectMode,
63 WebURLRequest::FrameType,
64 WebURLRequest::RequestContext,
65 WaitUntilObserver*);
66 60
67 private: 61 private:
68 class ThenFunction; 62 class ThenFunction;
69 63
70 const int m_fetchEventID; 64 void responseWasRejected(WebServiceWorkerResponseError);
71 const KURL m_requestURL; 65 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 66
79 enum State { Initial, Pending, Done }; 67 enum State { Initial, Pending, Done };
80 State m_state; 68 State m_state;
81 69
82 // RespondWith should ensure the ExtendableEvent is alive until the promise 70 // RespondWith should ensure the ExtendableEvent is alive until the promise
83 // passed to RespondWith is resolved. The lifecycle of the ExtendableEvent 71 // passed to RespondWith is resolved. The lifecycle of the ExtendableEvent
84 // is controlled by WaitUntilObserver, so not only 72 // is controlled by WaitUntilObserver, so not only
85 // WaitUntilObserver::ThenFunction but RespondWith needs to have a strong 73 // WaitUntilObserver::ThenFunction but RespondWith needs to have a strong
86 // reference to the WaitUntilObserver. 74 // reference to the WaitUntilObserver.
87 Member<WaitUntilObserver> m_observer; 75 Member<WaitUntilObserver> m_observer;
88 }; 76 };
89 77
90 } // namespace blink 78 } // namespace blink
91 79
92 #endif // RespondWithObserver_h 80 #endif // RespondWithObserver_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698