1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/src/nsBaseChannel.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,287 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsBaseChannel_h__ 1.10 +#define nsBaseChannel_h__ 1.11 + 1.12 +#include "nsString.h" 1.13 +#include "nsAutoPtr.h" 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsHashPropertyBag.h" 1.16 +#include "nsInputStreamPump.h" 1.17 + 1.18 +#include "nsIChannel.h" 1.19 +#include "nsIURI.h" 1.20 +#include "nsILoadGroup.h" 1.21 +#include "nsIStreamListener.h" 1.22 +#include "nsIInterfaceRequestor.h" 1.23 +#include "nsIProgressEventSink.h" 1.24 +#include "nsITransport.h" 1.25 +#include "nsIAsyncVerifyRedirectCallback.h" 1.26 +#include "PrivateBrowsingChannel.h" 1.27 +#include "nsThreadUtils.h" 1.28 +#include "nsNetUtil.h" 1.29 + 1.30 +class nsIInputStream; 1.31 + 1.32 +//----------------------------------------------------------------------------- 1.33 +// nsBaseChannel is designed to be subclassed. The subclass is responsible for 1.34 +// implementing the OpenContentStream method, which will be called by the 1.35 +// nsIChannel::AsyncOpen and nsIChannel::Open implementations. 1.36 +// 1.37 +// nsBaseChannel implements nsIInterfaceRequestor to provide a convenient way 1.38 +// for subclasses to query both the nsIChannel::notificationCallbacks and 1.39 +// nsILoadGroup::notificationCallbacks for supported interfaces. 1.40 +// 1.41 +// nsBaseChannel implements nsITransportEventSink to support progress & status 1.42 +// notifications generated by the transport layer. 1.43 + 1.44 +class nsBaseChannel : public nsHashPropertyBag 1.45 + , public nsIChannel 1.46 + , public nsIInterfaceRequestor 1.47 + , public nsITransportEventSink 1.48 + , public nsIAsyncVerifyRedirectCallback 1.49 + , public mozilla::net::PrivateBrowsingChannel<nsBaseChannel> 1.50 + , protected nsIStreamListener 1.51 +{ 1.52 +public: 1.53 + NS_DECL_ISUPPORTS_INHERITED 1.54 + NS_DECL_NSIREQUEST 1.55 + NS_DECL_NSICHANNEL 1.56 + NS_DECL_NSIINTERFACEREQUESTOR 1.57 + NS_DECL_NSITRANSPORTEVENTSINK 1.58 + NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK 1.59 + 1.60 + nsBaseChannel(); 1.61 + 1.62 + // This method must be called to initialize the basechannel instance. 1.63 + nsresult Init() { 1.64 + return NS_OK; 1.65 + } 1.66 + 1.67 +protected: 1.68 + // ----------------------------------------------- 1.69 + // Methods to be implemented by the derived class: 1.70 + 1.71 + virtual ~nsBaseChannel() {} 1.72 + 1.73 +private: 1.74 + // Implemented by subclass to supply data stream. The parameter, async, is 1.75 + // true when called from nsIChannel::AsyncOpen and false otherwise. When 1.76 + // async is true, the resulting stream will be used with a nsIInputStreamPump 1.77 + // instance. This means that if it is a non-blocking stream that supports 1.78 + // nsIAsyncInputStream that it will be read entirely on the main application 1.79 + // thread, and its AsyncWait method will be called whenever ReadSegments 1.80 + // returns NS_BASE_STREAM_WOULD_BLOCK. Otherwise, if the stream is blocking, 1.81 + // then it will be read on one of the background I/O threads, and it does not 1.82 + // need to implement ReadSegments. If async is false, this method may return 1.83 + // NS_ERROR_NOT_IMPLEMENTED to cause the basechannel to implement Open in 1.84 + // terms of AsyncOpen (see NS_ImplementChannelOpen). 1.85 + // A callee is allowed to return an nsIChannel instead of an nsIInputStream. 1.86 + // That case will be treated as a redirect to the new channel. By default 1.87 + // *channel will be set to null by the caller, so callees who don't want to 1.88 + // return one an just not touch it. 1.89 + virtual nsresult OpenContentStream(bool async, nsIInputStream **stream, 1.90 + nsIChannel** channel) = 0; 1.91 + 1.92 + // The basechannel calls this method from its OnTransportStatus method to 1.93 + // determine whether to call nsIProgressEventSink::OnStatus in addition to 1.94 + // nsIProgressEventSink::OnProgress. This method may be overriden by the 1.95 + // subclass to enable nsIProgressEventSink::OnStatus events. If this method 1.96 + // returns true, then the statusArg out param specifies the "statusArg" value 1.97 + // to pass to the OnStatus method. By default, OnStatus messages are 1.98 + // suppressed. The status parameter passed to this method is the status value 1.99 + // from the OnTransportStatus method. 1.100 + virtual bool GetStatusArg(nsresult status, nsString &statusArg) { 1.101 + return false; 1.102 + } 1.103 + 1.104 + // Called when the callbacks available to this channel may have changed. 1.105 + virtual void OnCallbacksChanged() { 1.106 + } 1.107 + 1.108 + // Called when our channel is done, to allow subclasses to drop resources. 1.109 + virtual void OnChannelDone() { 1.110 + } 1.111 + 1.112 +public: 1.113 + // ---------------------------------------------- 1.114 + // Methods provided for use by the derived class: 1.115 + 1.116 + // Redirect to another channel. This method takes care of notifying 1.117 + // observers of this redirect as well as of opening the new channel, if asked 1.118 + // to do so. It also cancels |this| with the status code 1.119 + // NS_BINDING_REDIRECTED. A failure return from this method means that the 1.120 + // redirect could not be performed (no channel was opened; this channel 1.121 + // wasn't canceled.) The redirectFlags parameter consists of the flag values 1.122 + // defined on nsIChannelEventSink. 1.123 + nsresult Redirect(nsIChannel *newChannel, uint32_t redirectFlags, 1.124 + bool openNewChannel); 1.125 + 1.126 + // Tests whether a type hint was set. Subclasses can use this to decide 1.127 + // whether to call SetContentType. 1.128 + // NOTE: This is only reliable if the subclass didn't itself call 1.129 + // SetContentType, and should also not be called after OpenContentStream. 1.130 + bool HasContentTypeHint() const; 1.131 + 1.132 + // The URI member should be initialized before the channel is used, and then 1.133 + // it should never be changed again until the channel is destroyed. 1.134 + nsIURI *URI() { 1.135 + return mURI; 1.136 + } 1.137 + void SetURI(nsIURI *uri) { 1.138 + NS_ASSERTION(uri, "must specify a non-null URI"); 1.139 + NS_ASSERTION(!mURI, "must not modify URI"); 1.140 + NS_ASSERTION(!mOriginalURI, "how did that get set so early?"); 1.141 + mURI = uri; 1.142 + mOriginalURI = uri; 1.143 + } 1.144 + nsIURI *OriginalURI() { 1.145 + return mOriginalURI; 1.146 + } 1.147 + 1.148 + // The security info is a property of the transport-layer, which should be 1.149 + // assigned by the subclass. 1.150 + nsISupports *SecurityInfo() { 1.151 + return mSecurityInfo; 1.152 + } 1.153 + void SetSecurityInfo(nsISupports *info) { 1.154 + mSecurityInfo = info; 1.155 + } 1.156 + 1.157 + // Test the load flags 1.158 + bool HasLoadFlag(uint32_t flag) { 1.159 + return (mLoadFlags & flag) != 0; 1.160 + } 1.161 + 1.162 + // This is a short-cut to calling nsIRequest::IsPending() 1.163 + virtual bool Pending() const { 1.164 + return mPump || mWaitingOnAsyncRedirect; 1.165 + } 1.166 + 1.167 + // Helper function for querying the channel's notification callbacks. 1.168 + template <class T> void GetCallback(nsCOMPtr<T> &result) { 1.169 + GetInterface(NS_GET_TEMPLATE_IID(T), getter_AddRefs(result)); 1.170 + } 1.171 + 1.172 + // Helper function for calling QueryInterface on this. 1.173 + nsQueryInterface do_QueryInterface() { 1.174 + return nsQueryInterface(static_cast<nsIChannel *>(this)); 1.175 + } 1.176 + // MSVC needs this: 1.177 + nsQueryInterface do_QueryInterface(nsISupports *obj) { 1.178 + return nsQueryInterface(obj); 1.179 + } 1.180 + 1.181 + // If a subclass does not want to feed transport-layer progress events to the 1.182 + // base channel via nsITransportEventSink, then it may set this flag to cause 1.183 + // the base channel to synthesize progress events when it receives data from 1.184 + // the content stream. By default, progress events are not synthesized. 1.185 + void EnableSynthesizedProgressEvents(bool enable) { 1.186 + mSynthProgressEvents = enable; 1.187 + } 1.188 + 1.189 + // Some subclasses may wish to manually insert a stream listener between this 1.190 + // and the channel's listener. The following methods make that possible. 1.191 + void SetStreamListener(nsIStreamListener *listener) { 1.192 + mListener = listener; 1.193 + } 1.194 + nsIStreamListener *StreamListener() { 1.195 + return mListener; 1.196 + } 1.197 + 1.198 + // Pushes a new stream converter in front of the channel's stream listener. 1.199 + // The fromType and toType values are passed to nsIStreamConverterService's 1.200 + // AsyncConvertData method. If invalidatesContentLength is true, then the 1.201 + // channel's content-length property will be assigned a value of -1. This is 1.202 + // necessary when the converter changes the length of the resulting data 1.203 + // stream, which is almost always the case for a "stream converter" ;-) 1.204 + // This function optionally returns a reference to the new converter. 1.205 + nsresult PushStreamConverter(const char *fromType, const char *toType, 1.206 + bool invalidatesContentLength = true, 1.207 + nsIStreamListener **converter = nullptr); 1.208 + 1.209 +private: 1.210 + NS_DECL_NSISTREAMLISTENER 1.211 + NS_DECL_NSIREQUESTOBSERVER 1.212 + 1.213 + // Called to setup mPump and call AsyncRead on it. 1.214 + nsresult BeginPumpingData(); 1.215 + 1.216 + // Called when the callbacks available to this channel may have changed. 1.217 + void CallbacksChanged() { 1.218 + mProgressSink = nullptr; 1.219 + mQueriedProgressSink = false; 1.220 + OnCallbacksChanged(); 1.221 + } 1.222 + 1.223 + // Called when our channel is done. This should drop no-longer-needed pointers. 1.224 + void ChannelDone() { 1.225 + mListener = nullptr; 1.226 + mListenerContext = nullptr; 1.227 + OnChannelDone(); 1.228 + } 1.229 + 1.230 + // Handle an async redirect callback. This will only be called if we 1.231 + // returned success from AsyncOpen while posting a redirect runnable. 1.232 + void HandleAsyncRedirect(nsIChannel* newChannel); 1.233 + void ContinueHandleAsyncRedirect(nsresult result); 1.234 + nsresult ContinueRedirect(); 1.235 + 1.236 + // start URI classifier if requested 1.237 + void ClassifyURI(); 1.238 + 1.239 + class RedirectRunnable : public nsRunnable 1.240 + { 1.241 + public: 1.242 + RedirectRunnable(nsBaseChannel* chan, nsIChannel* newChannel) 1.243 + : mChannel(chan), mNewChannel(newChannel) 1.244 + { 1.245 + NS_PRECONDITION(newChannel, "Must have channel to redirect to"); 1.246 + } 1.247 + 1.248 + NS_IMETHOD Run() 1.249 + { 1.250 + mChannel->HandleAsyncRedirect(mNewChannel); 1.251 + return NS_OK; 1.252 + } 1.253 + 1.254 + private: 1.255 + nsRefPtr<nsBaseChannel> mChannel; 1.256 + nsCOMPtr<nsIChannel> mNewChannel; 1.257 + }; 1.258 + friend class RedirectRunnable; 1.259 + 1.260 + nsRefPtr<nsInputStreamPump> mPump; 1.261 + nsCOMPtr<nsIProgressEventSink> mProgressSink; 1.262 + nsCOMPtr<nsIURI> mOriginalURI; 1.263 + nsCOMPtr<nsISupports> mOwner; 1.264 + nsCOMPtr<nsISupports> mSecurityInfo; 1.265 + nsCOMPtr<nsIChannel> mRedirectChannel; 1.266 + nsCString mContentType; 1.267 + nsCString mContentCharset; 1.268 + uint32_t mLoadFlags; 1.269 + bool mQueriedProgressSink; 1.270 + bool mSynthProgressEvents; 1.271 + bool mWasOpened; 1.272 + bool mWaitingOnAsyncRedirect; 1.273 + bool mOpenRedirectChannel; 1.274 + uint32_t mRedirectFlags; 1.275 + 1.276 +protected: 1.277 + nsCOMPtr<nsIURI> mURI; 1.278 + nsCOMPtr<nsILoadGroup> mLoadGroup; 1.279 + nsCOMPtr<nsIInterfaceRequestor> mCallbacks; 1.280 + nsCOMPtr<nsIStreamListener> mListener; 1.281 + nsCOMPtr<nsISupports> mListenerContext; 1.282 + nsresult mStatus; 1.283 + uint32_t mContentDispositionHint; 1.284 + nsAutoPtr<nsString> mContentDispositionFilename; 1.285 + int64_t mContentLength; 1.286 + 1.287 + friend class mozilla::net::PrivateBrowsingChannel<nsBaseChannel>; 1.288 +}; 1.289 + 1.290 +#endif // !nsBaseChannel_h__