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