/work/obj-fuzz/dist/include/mozilla/dom/FetchDriver.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef mozilla_dom_FetchDriver_h |
8 | | #define mozilla_dom_FetchDriver_h |
9 | | |
10 | | #include "nsIChannelEventSink.h" |
11 | | #include "nsICacheInfoChannel.h" |
12 | | #include "nsIInterfaceRequestor.h" |
13 | | #include "nsIStreamListener.h" |
14 | | #include "nsIThreadRetargetableStreamListener.h" |
15 | | #include "mozilla/ConsoleReportCollector.h" |
16 | | #include "mozilla/dom/AbortSignal.h" |
17 | | #include "mozilla/dom/SRIMetadata.h" |
18 | | #include "mozilla/RefPtr.h" |
19 | | |
20 | | #include "mozilla/DebugOnly.h" |
21 | | #include "mozilla/net/ReferrerPolicy.h" |
22 | | |
23 | | class nsIConsoleReportCollector; |
24 | | class nsIDocument; |
25 | | class nsIEventTarget; |
26 | | class nsIOutputStream; |
27 | | class nsILoadGroup; |
28 | | class nsIPrincipal; |
29 | | |
30 | | namespace mozilla { |
31 | | namespace dom { |
32 | | |
33 | | class InternalRequest; |
34 | | class InternalResponse; |
35 | | class PerformanceStorage; |
36 | | |
37 | | /** |
38 | | * Provides callbacks to be called when response is available or on error. |
39 | | * Implemenations usually resolve or reject the promise returned from fetch(). |
40 | | * The callbacks can be called synchronously or asynchronously from FetchDriver::Fetch. |
41 | | */ |
42 | | class FetchDriverObserver |
43 | | { |
44 | | public: |
45 | | FetchDriverObserver() : mReporter(new ConsoleReportCollector()) |
46 | | , mGotResponseAvailable(false) |
47 | 0 | { } |
48 | | |
49 | | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FetchDriverObserver); |
50 | | void OnResponseAvailable(InternalResponse* aResponse) |
51 | 0 | { |
52 | 0 | MOZ_ASSERT(!mGotResponseAvailable); |
53 | 0 | mGotResponseAvailable = true; |
54 | 0 | OnResponseAvailableInternal(aResponse); |
55 | 0 | } |
56 | | |
57 | | enum EndReason |
58 | | { |
59 | | eAborted, |
60 | | eByNetworking, |
61 | | }; |
62 | | |
63 | | virtual void OnResponseEnd(EndReason aReason) |
64 | 0 | { }; |
65 | | |
66 | | nsIConsoleReportCollector* GetReporter() const |
67 | 0 | { |
68 | 0 | return mReporter; |
69 | 0 | } |
70 | | |
71 | | virtual void FlushConsoleReport() = 0; |
72 | | |
73 | | // Called in OnStartRequest() to determine if the OnDataAvailable() method |
74 | | // needs to be called. Invoking that method may generate additional main |
75 | | // thread runnables. |
76 | | virtual bool NeedOnDataAvailable() = 0; |
77 | | |
78 | | // Called once when the first byte of data is received iff |
79 | | // NeedOnDataAvailable() returned true when called in OnStartRequest(). |
80 | | virtual void OnDataAvailable() = 0; |
81 | | |
82 | | protected: |
83 | | virtual ~FetchDriverObserver() |
84 | 0 | { }; |
85 | | |
86 | | virtual void OnResponseAvailableInternal(InternalResponse* aResponse) = 0; |
87 | | |
88 | | nsCOMPtr<nsIConsoleReportCollector> mReporter; |
89 | | private: |
90 | | bool mGotResponseAvailable; |
91 | | }; |
92 | | |
93 | | class AlternativeDataStreamListener; |
94 | | |
95 | | class FetchDriver final : public nsIStreamListener, |
96 | | public nsIChannelEventSink, |
97 | | public nsIInterfaceRequestor, |
98 | | public nsIThreadRetargetableStreamListener, |
99 | | public AbortFollower |
100 | | { |
101 | | public: |
102 | | NS_DECL_THREADSAFE_ISUPPORTS |
103 | | NS_DECL_NSIREQUESTOBSERVER |
104 | | NS_DECL_NSISTREAMLISTENER |
105 | | NS_DECL_NSICHANNELEVENTSINK |
106 | | NS_DECL_NSIINTERFACEREQUESTOR |
107 | | NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER |
108 | | |
109 | | FetchDriver(InternalRequest* aRequest, |
110 | | nsIPrincipal* aPrincipal, |
111 | | nsILoadGroup* aLoadGroup, |
112 | | nsIEventTarget* aMainThreadEventTarget, |
113 | | PerformanceStorage* aPerformanceStorage, |
114 | | bool aIsTrackingFetch); |
115 | | |
116 | | nsresult Fetch(AbortSignalImpl* aSignalImpl, |
117 | | FetchDriverObserver* aObserver); |
118 | | |
119 | | void |
120 | | SetDocument(nsIDocument* aDocument); |
121 | | |
122 | | void |
123 | | SetClientInfo(const ClientInfo& aClientInfo); |
124 | | |
125 | | void |
126 | | SetController(const Maybe<ServiceWorkerDescriptor>& aController); |
127 | | |
128 | | void |
129 | | SetWorkerScript(const nsACString& aWorkerScirpt) |
130 | 0 | { |
131 | 0 | MOZ_ASSERT(!aWorkerScirpt.IsEmpty()); |
132 | 0 | mWorkerScript = aWorkerScirpt; |
133 | 0 | } |
134 | | |
135 | | // AbortFollower |
136 | | void |
137 | | Abort() override; |
138 | | |
139 | | private: |
140 | | nsCOMPtr<nsIPrincipal> mPrincipal; |
141 | | nsCOMPtr<nsILoadGroup> mLoadGroup; |
142 | | RefPtr<InternalRequest> mRequest; |
143 | | RefPtr<InternalResponse> mResponse; |
144 | | nsCOMPtr<nsIOutputStream> mPipeOutputStream; |
145 | | RefPtr<FetchDriverObserver> mObserver; |
146 | | nsCOMPtr<nsIDocument> mDocument; |
147 | | Maybe<ClientInfo> mClientInfo; |
148 | | Maybe<ServiceWorkerDescriptor> mController; |
149 | | nsCOMPtr<nsIChannel> mChannel; |
150 | | nsAutoPtr<SRICheckDataVerifier> mSRIDataVerifier; |
151 | | nsCOMPtr<nsIEventTarget> mMainThreadEventTarget; |
152 | | |
153 | | // This is set only when Fetch is used in workers. |
154 | | RefPtr<PerformanceStorage> mPerformanceStorage; |
155 | | |
156 | | SRIMetadata mSRIMetadata; |
157 | | nsCString mWorkerScript; |
158 | | |
159 | | // This is written once in OnStartRequest on the main thread and then |
160 | | // written/read in OnDataAvailable() on any thread. Necko guarantees |
161 | | // that these do not overlap. |
162 | | bool mNeedToObserveOnDataAvailable; |
163 | | |
164 | | bool mIsTrackingFetch; |
165 | | |
166 | | RefPtr<AlternativeDataStreamListener> mAltDataListener; |
167 | | bool mOnStopRequestCalled; |
168 | | |
169 | | #ifdef DEBUG |
170 | | bool mResponseAvailableCalled; |
171 | | bool mFetchCalled; |
172 | | #endif |
173 | | |
174 | | friend class AlternativeDataStreamListener; |
175 | | |
176 | | FetchDriver() = delete; |
177 | | FetchDriver(const FetchDriver&) = delete; |
178 | | FetchDriver& operator=(const FetchDriver&) = delete; |
179 | | ~FetchDriver(); |
180 | | |
181 | | |
182 | | nsresult HttpFetch(const nsACString& aPreferredAlternativeDataType = EmptyCString()); |
183 | | // Returns the filtered response sent to the observer. |
184 | | already_AddRefed<InternalResponse> |
185 | | BeginAndGetFilteredResponse(InternalResponse* aResponse, |
186 | | bool aFoundOpaqueRedirect); |
187 | | // Utility since not all cases need to do any post processing of the filtered |
188 | | // response. |
189 | | void FailWithNetworkError(nsresult rv); |
190 | | |
191 | | void SetRequestHeaders(nsIHttpChannel* aChannel) const; |
192 | | |
193 | | nsresult FinishOnStopRequest(AlternativeDataStreamListener* aAltDataListener); |
194 | | }; |
195 | | |
196 | | } // namespace dom |
197 | | } // namespace mozilla |
198 | | |
199 | | #endif // mozilla_dom_FetchDriver_h |