|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsBaseChannel_h__ |
|
7 #define nsBaseChannel_h__ |
|
8 |
|
9 #include "nsString.h" |
|
10 #include "nsAutoPtr.h" |
|
11 #include "nsCOMPtr.h" |
|
12 #include "nsHashPropertyBag.h" |
|
13 #include "nsInputStreamPump.h" |
|
14 |
|
15 #include "nsIChannel.h" |
|
16 #include "nsIURI.h" |
|
17 #include "nsILoadGroup.h" |
|
18 #include "nsIStreamListener.h" |
|
19 #include "nsIInterfaceRequestor.h" |
|
20 #include "nsIProgressEventSink.h" |
|
21 #include "nsITransport.h" |
|
22 #include "nsIAsyncVerifyRedirectCallback.h" |
|
23 #include "PrivateBrowsingChannel.h" |
|
24 #include "nsThreadUtils.h" |
|
25 #include "nsNetUtil.h" |
|
26 |
|
27 class nsIInputStream; |
|
28 |
|
29 //----------------------------------------------------------------------------- |
|
30 // nsBaseChannel is designed to be subclassed. The subclass is responsible for |
|
31 // implementing the OpenContentStream method, which will be called by the |
|
32 // nsIChannel::AsyncOpen and nsIChannel::Open implementations. |
|
33 // |
|
34 // nsBaseChannel implements nsIInterfaceRequestor to provide a convenient way |
|
35 // for subclasses to query both the nsIChannel::notificationCallbacks and |
|
36 // nsILoadGroup::notificationCallbacks for supported interfaces. |
|
37 // |
|
38 // nsBaseChannel implements nsITransportEventSink to support progress & status |
|
39 // notifications generated by the transport layer. |
|
40 |
|
41 class nsBaseChannel : public nsHashPropertyBag |
|
42 , public nsIChannel |
|
43 , public nsIInterfaceRequestor |
|
44 , public nsITransportEventSink |
|
45 , public nsIAsyncVerifyRedirectCallback |
|
46 , public mozilla::net::PrivateBrowsingChannel<nsBaseChannel> |
|
47 , protected nsIStreamListener |
|
48 { |
|
49 public: |
|
50 NS_DECL_ISUPPORTS_INHERITED |
|
51 NS_DECL_NSIREQUEST |
|
52 NS_DECL_NSICHANNEL |
|
53 NS_DECL_NSIINTERFACEREQUESTOR |
|
54 NS_DECL_NSITRANSPORTEVENTSINK |
|
55 NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK |
|
56 |
|
57 nsBaseChannel(); |
|
58 |
|
59 // This method must be called to initialize the basechannel instance. |
|
60 nsresult Init() { |
|
61 return NS_OK; |
|
62 } |
|
63 |
|
64 protected: |
|
65 // ----------------------------------------------- |
|
66 // Methods to be implemented by the derived class: |
|
67 |
|
68 virtual ~nsBaseChannel() {} |
|
69 |
|
70 private: |
|
71 // Implemented by subclass to supply data stream. The parameter, async, is |
|
72 // true when called from nsIChannel::AsyncOpen and false otherwise. When |
|
73 // async is true, the resulting stream will be used with a nsIInputStreamPump |
|
74 // instance. This means that if it is a non-blocking stream that supports |
|
75 // nsIAsyncInputStream that it will be read entirely on the main application |
|
76 // thread, and its AsyncWait method will be called whenever ReadSegments |
|
77 // returns NS_BASE_STREAM_WOULD_BLOCK. Otherwise, if the stream is blocking, |
|
78 // then it will be read on one of the background I/O threads, and it does not |
|
79 // need to implement ReadSegments. If async is false, this method may return |
|
80 // NS_ERROR_NOT_IMPLEMENTED to cause the basechannel to implement Open in |
|
81 // terms of AsyncOpen (see NS_ImplementChannelOpen). |
|
82 // A callee is allowed to return an nsIChannel instead of an nsIInputStream. |
|
83 // That case will be treated as a redirect to the new channel. By default |
|
84 // *channel will be set to null by the caller, so callees who don't want to |
|
85 // return one an just not touch it. |
|
86 virtual nsresult OpenContentStream(bool async, nsIInputStream **stream, |
|
87 nsIChannel** channel) = 0; |
|
88 |
|
89 // The basechannel calls this method from its OnTransportStatus method to |
|
90 // determine whether to call nsIProgressEventSink::OnStatus in addition to |
|
91 // nsIProgressEventSink::OnProgress. This method may be overriden by the |
|
92 // subclass to enable nsIProgressEventSink::OnStatus events. If this method |
|
93 // returns true, then the statusArg out param specifies the "statusArg" value |
|
94 // to pass to the OnStatus method. By default, OnStatus messages are |
|
95 // suppressed. The status parameter passed to this method is the status value |
|
96 // from the OnTransportStatus method. |
|
97 virtual bool GetStatusArg(nsresult status, nsString &statusArg) { |
|
98 return false; |
|
99 } |
|
100 |
|
101 // Called when the callbacks available to this channel may have changed. |
|
102 virtual void OnCallbacksChanged() { |
|
103 } |
|
104 |
|
105 // Called when our channel is done, to allow subclasses to drop resources. |
|
106 virtual void OnChannelDone() { |
|
107 } |
|
108 |
|
109 public: |
|
110 // ---------------------------------------------- |
|
111 // Methods provided for use by the derived class: |
|
112 |
|
113 // Redirect to another channel. This method takes care of notifying |
|
114 // observers of this redirect as well as of opening the new channel, if asked |
|
115 // to do so. It also cancels |this| with the status code |
|
116 // NS_BINDING_REDIRECTED. A failure return from this method means that the |
|
117 // redirect could not be performed (no channel was opened; this channel |
|
118 // wasn't canceled.) The redirectFlags parameter consists of the flag values |
|
119 // defined on nsIChannelEventSink. |
|
120 nsresult Redirect(nsIChannel *newChannel, uint32_t redirectFlags, |
|
121 bool openNewChannel); |
|
122 |
|
123 // Tests whether a type hint was set. Subclasses can use this to decide |
|
124 // whether to call SetContentType. |
|
125 // NOTE: This is only reliable if the subclass didn't itself call |
|
126 // SetContentType, and should also not be called after OpenContentStream. |
|
127 bool HasContentTypeHint() const; |
|
128 |
|
129 // The URI member should be initialized before the channel is used, and then |
|
130 // it should never be changed again until the channel is destroyed. |
|
131 nsIURI *URI() { |
|
132 return mURI; |
|
133 } |
|
134 void SetURI(nsIURI *uri) { |
|
135 NS_ASSERTION(uri, "must specify a non-null URI"); |
|
136 NS_ASSERTION(!mURI, "must not modify URI"); |
|
137 NS_ASSERTION(!mOriginalURI, "how did that get set so early?"); |
|
138 mURI = uri; |
|
139 mOriginalURI = uri; |
|
140 } |
|
141 nsIURI *OriginalURI() { |
|
142 return mOriginalURI; |
|
143 } |
|
144 |
|
145 // The security info is a property of the transport-layer, which should be |
|
146 // assigned by the subclass. |
|
147 nsISupports *SecurityInfo() { |
|
148 return mSecurityInfo; |
|
149 } |
|
150 void SetSecurityInfo(nsISupports *info) { |
|
151 mSecurityInfo = info; |
|
152 } |
|
153 |
|
154 // Test the load flags |
|
155 bool HasLoadFlag(uint32_t flag) { |
|
156 return (mLoadFlags & flag) != 0; |
|
157 } |
|
158 |
|
159 // This is a short-cut to calling nsIRequest::IsPending() |
|
160 virtual bool Pending() const { |
|
161 return mPump || mWaitingOnAsyncRedirect; |
|
162 } |
|
163 |
|
164 // Helper function for querying the channel's notification callbacks. |
|
165 template <class T> void GetCallback(nsCOMPtr<T> &result) { |
|
166 GetInterface(NS_GET_TEMPLATE_IID(T), getter_AddRefs(result)); |
|
167 } |
|
168 |
|
169 // Helper function for calling QueryInterface on this. |
|
170 nsQueryInterface do_QueryInterface() { |
|
171 return nsQueryInterface(static_cast<nsIChannel *>(this)); |
|
172 } |
|
173 // MSVC needs this: |
|
174 nsQueryInterface do_QueryInterface(nsISupports *obj) { |
|
175 return nsQueryInterface(obj); |
|
176 } |
|
177 |
|
178 // If a subclass does not want to feed transport-layer progress events to the |
|
179 // base channel via nsITransportEventSink, then it may set this flag to cause |
|
180 // the base channel to synthesize progress events when it receives data from |
|
181 // the content stream. By default, progress events are not synthesized. |
|
182 void EnableSynthesizedProgressEvents(bool enable) { |
|
183 mSynthProgressEvents = enable; |
|
184 } |
|
185 |
|
186 // Some subclasses may wish to manually insert a stream listener between this |
|
187 // and the channel's listener. The following methods make that possible. |
|
188 void SetStreamListener(nsIStreamListener *listener) { |
|
189 mListener = listener; |
|
190 } |
|
191 nsIStreamListener *StreamListener() { |
|
192 return mListener; |
|
193 } |
|
194 |
|
195 // Pushes a new stream converter in front of the channel's stream listener. |
|
196 // The fromType and toType values are passed to nsIStreamConverterService's |
|
197 // AsyncConvertData method. If invalidatesContentLength is true, then the |
|
198 // channel's content-length property will be assigned a value of -1. This is |
|
199 // necessary when the converter changes the length of the resulting data |
|
200 // stream, which is almost always the case for a "stream converter" ;-) |
|
201 // This function optionally returns a reference to the new converter. |
|
202 nsresult PushStreamConverter(const char *fromType, const char *toType, |
|
203 bool invalidatesContentLength = true, |
|
204 nsIStreamListener **converter = nullptr); |
|
205 |
|
206 private: |
|
207 NS_DECL_NSISTREAMLISTENER |
|
208 NS_DECL_NSIREQUESTOBSERVER |
|
209 |
|
210 // Called to setup mPump and call AsyncRead on it. |
|
211 nsresult BeginPumpingData(); |
|
212 |
|
213 // Called when the callbacks available to this channel may have changed. |
|
214 void CallbacksChanged() { |
|
215 mProgressSink = nullptr; |
|
216 mQueriedProgressSink = false; |
|
217 OnCallbacksChanged(); |
|
218 } |
|
219 |
|
220 // Called when our channel is done. This should drop no-longer-needed pointers. |
|
221 void ChannelDone() { |
|
222 mListener = nullptr; |
|
223 mListenerContext = nullptr; |
|
224 OnChannelDone(); |
|
225 } |
|
226 |
|
227 // Handle an async redirect callback. This will only be called if we |
|
228 // returned success from AsyncOpen while posting a redirect runnable. |
|
229 void HandleAsyncRedirect(nsIChannel* newChannel); |
|
230 void ContinueHandleAsyncRedirect(nsresult result); |
|
231 nsresult ContinueRedirect(); |
|
232 |
|
233 // start URI classifier if requested |
|
234 void ClassifyURI(); |
|
235 |
|
236 class RedirectRunnable : public nsRunnable |
|
237 { |
|
238 public: |
|
239 RedirectRunnable(nsBaseChannel* chan, nsIChannel* newChannel) |
|
240 : mChannel(chan), mNewChannel(newChannel) |
|
241 { |
|
242 NS_PRECONDITION(newChannel, "Must have channel to redirect to"); |
|
243 } |
|
244 |
|
245 NS_IMETHOD Run() |
|
246 { |
|
247 mChannel->HandleAsyncRedirect(mNewChannel); |
|
248 return NS_OK; |
|
249 } |
|
250 |
|
251 private: |
|
252 nsRefPtr<nsBaseChannel> mChannel; |
|
253 nsCOMPtr<nsIChannel> mNewChannel; |
|
254 }; |
|
255 friend class RedirectRunnable; |
|
256 |
|
257 nsRefPtr<nsInputStreamPump> mPump; |
|
258 nsCOMPtr<nsIProgressEventSink> mProgressSink; |
|
259 nsCOMPtr<nsIURI> mOriginalURI; |
|
260 nsCOMPtr<nsISupports> mOwner; |
|
261 nsCOMPtr<nsISupports> mSecurityInfo; |
|
262 nsCOMPtr<nsIChannel> mRedirectChannel; |
|
263 nsCString mContentType; |
|
264 nsCString mContentCharset; |
|
265 uint32_t mLoadFlags; |
|
266 bool mQueriedProgressSink; |
|
267 bool mSynthProgressEvents; |
|
268 bool mWasOpened; |
|
269 bool mWaitingOnAsyncRedirect; |
|
270 bool mOpenRedirectChannel; |
|
271 uint32_t mRedirectFlags; |
|
272 |
|
273 protected: |
|
274 nsCOMPtr<nsIURI> mURI; |
|
275 nsCOMPtr<nsILoadGroup> mLoadGroup; |
|
276 nsCOMPtr<nsIInterfaceRequestor> mCallbacks; |
|
277 nsCOMPtr<nsIStreamListener> mListener; |
|
278 nsCOMPtr<nsISupports> mListenerContext; |
|
279 nsresult mStatus; |
|
280 uint32_t mContentDispositionHint; |
|
281 nsAutoPtr<nsString> mContentDispositionFilename; |
|
282 int64_t mContentLength; |
|
283 |
|
284 friend class mozilla::net::PrivateBrowsingChannel<nsBaseChannel>; |
|
285 }; |
|
286 |
|
287 #endif // !nsBaseChannel_h__ |