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