Bug 1878930 - s/RawBuffer/Span/: TexImage: GetRangeFromData. r=gfx-reviewers,lsalzman
[gecko.git] / netwerk / base / nsStreamListenerWrapper.h
bloba95083729718a97b406841df29ffd5ab5a5f8bf5
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__
8 #include "nsCOMPtr.h"
9 #include "nsIRequest.h"
10 #include "nsIStreamListener.h"
11 #include "nsIThreadRetargetableStreamListener.h"
12 #include "nsIMultiPartChannel.h"
13 #include "mozilla/Attributes.h"
15 namespace mozilla {
16 namespace net {
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 {
23 public:
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
38 // listeners)
39 nsCOMPtr<nsIMultiPartChannel> multiPartChannel =
40 do_QueryInterface(aRequest);
41 if (multiPartChannel) {
42 mIsMulti = true;
44 return mListener->OnStartRequest(aRequest);
46 NS_IMETHOD OnStopRequest(nsIRequest* aRequest,
47 nsresult aStatusCode) override {
48 nsresult rv = mListener->OnStopRequest(aRequest, aStatusCode);
49 if (!mIsMulti) {
50 // Multipart channels can call OnStartRequest again
51 mListener = nullptr;
53 return rv;
56 private:
57 bool mIsMulti{false};
58 ~nsStreamListenerWrapper() = default;
59 nsCOMPtr<nsIStreamListener> mListener;
62 } // namespace net
63 } // namespace mozilla
65 #endif // nsStreamListenerWrapper_h__