1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsStreamListenerWrapper_h__
6 #define nsStreamListenerWrapper_h__
9 #include "nsIRequest.h"
10 #include "nsIStreamListener.h"
11 #include "nsIThreadRetargetableStreamListener.h"
12 #include "nsIMultiPartChannel.h"
13 #include "mozilla/Attributes.h"
18 // Wrapper class to make replacement of nsHttpChannel's listener
19 // from JavaScript possible. It is workaround for bug 433711 and 682305.
20 class nsStreamListenerWrapper final
21 : public nsIMultiPartChannelListener
,
22 public nsIThreadRetargetableStreamListener
{
24 explicit nsStreamListenerWrapper(nsIStreamListener
* listener
)
25 : mListener(listener
) {
26 MOZ_ASSERT(mListener
, "no stream listener specified");
29 NS_DECL_THREADSAFE_ISUPPORTS
30 NS_FORWARD_SAFE_NSISTREAMLISTENER(mListener
)
31 NS_DECL_NSIMULTIPARTCHANNELLISTENER
32 NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
34 // Don't use NS_FORWARD_NSIREQUESTOBSERVER(mListener->) here, because we need
35 // to release mListener in OnStopRequest, and IDL-generated function doesn't.
36 NS_IMETHOD
OnStartRequest(nsIRequest
* aRequest
) override
{
37 // OnStartRequest can come after OnStopRequest in certain cases (multipart
39 nsCOMPtr
<nsIMultiPartChannel
> multiPartChannel
=
40 do_QueryInterface(aRequest
);
41 if (multiPartChannel
) {
44 return mListener
->OnStartRequest(aRequest
);
46 NS_IMETHOD
OnStopRequest(nsIRequest
* aRequest
,
47 nsresult aStatusCode
) override
{
48 nsresult rv
= mListener
->OnStopRequest(aRequest
, aStatusCode
);
50 // Multipart channels can call OnStartRequest again
58 ~nsStreamListenerWrapper() = default;
59 nsCOMPtr
<nsIStreamListener
> mListener
;
63 } // namespace mozilla
65 #endif // nsStreamListenerWrapper_h__