1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et tw=80 : */
3 
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 
8 #ifndef mozilla_net_FTPChannelChild_h
9 #define mozilla_net_FTPChannelChild_h
10 
11 #include "mozilla/UniquePtr.h"
12 #include "mozilla/net/PFTPChannelChild.h"
13 #include "mozilla/net/ChannelEventQueue.h"
14 #include "nsBaseChannel.h"
15 #include "nsIFTPChannel.h"
16 #include "nsIUploadChannel.h"
17 #include "nsIProxiedChannel.h"
18 #include "nsIResumableChannel.h"
19 #include "nsIChildChannel.h"
20 #include "nsIDivertableChannel.h"
21 
22 #include "nsIStreamListener.h"
23 #include "PrivateBrowsingChannel.h"
24 
25 namespace mozilla {
26 namespace net {
27 
28 // This class inherits logic from nsBaseChannel that is not needed for an
29 // e10s child channel, but it works.  At some point we could slice up
30 // nsBaseChannel and have a new class that has only the common logic for
31 // nsFTPChannel/FTPChannelChild.
32 
33 class FTPChannelChild final : public PFTPChannelChild
34                             , public nsBaseChannel
35                             , public nsIFTPChannel
36                             , public nsIUploadChannel
37                             , public nsIResumableChannel
38                             , public nsIProxiedChannel
39                             , public nsIChildChannel
40                             , public nsIDivertableChannel
41 {
42 public:
43   typedef ::nsIStreamListener nsIStreamListener;
44 
45   NS_DECL_ISUPPORTS_INHERITED
46   NS_DECL_NSIFTPCHANNEL
47   NS_DECL_NSIUPLOADCHANNEL
48   NS_DECL_NSIRESUMABLECHANNEL
49   NS_DECL_NSIPROXIEDCHANNEL
50   NS_DECL_NSICHILDCHANNEL
51   NS_DECL_NSIDIVERTABLECHANNEL
52 
53   NS_IMETHOD Cancel(nsresult status) override;
54   NS_IMETHOD Suspend() override;
55   NS_IMETHOD Resume() override;
56 
57   explicit FTPChannelChild(nsIURI* uri);
58 
59   void AddIPDLReference();
60   void ReleaseIPDLReference();
61 
62   NS_IMETHOD AsyncOpen(nsIStreamListener* listener, nsISupports* aContext) override;
63 
64   // Note that we handle this ourselves, overriding the nsBaseChannel
65   // default behavior, in order to be e10s-friendly.
66   NS_IMETHOD IsPending(bool* result) override;
67 
68   nsresult OpenContentStream(bool async,
69                              nsIInputStream** stream,
70                              nsIChannel** channel) override;
71 
72   bool IsSuspended();
73 
74   void FlushedForDiversion();
75 
76 protected:
77   virtual ~FTPChannelChild();
78 
79   bool RecvOnStartRequest(const nsresult& aChannelStatus,
80                           const int64_t& aContentLength,
81                           const nsCString& aContentType,
82                           const PRTime& aLastModified,
83                           const nsCString& aEntityID,
84                           const URIParams& aURI) override;
85   bool RecvOnDataAvailable(const nsresult& channelStatus,
86                            const nsCString& data,
87                            const uint64_t& offset,
88                            const uint32_t& count) override;
89   bool RecvOnStopRequest(const nsresult& channelStatus,
90                          const nsCString &aErrorMsg,
91                          const bool &aUseUTF8) override;
92   bool RecvFailedAsyncOpen(const nsresult& statusCode) override;
93   bool RecvFlushedForDiversion() override;
94   bool RecvDivertMessages() override;
95   bool RecvDeleteSelf() override;
96 
97   void DoOnStartRequest(const nsresult& aChannelStatus,
98                         const int64_t& aContentLength,
99                         const nsCString& aContentType,
100                         const PRTime& aLastModified,
101                         const nsCString& aEntityID,
102                         const URIParams& aURI);
103   void DoOnDataAvailable(const nsresult& channelStatus,
104                          const nsCString& data,
105                          const uint64_t& offset,
106                          const uint32_t& count);
107   void MaybeDivertOnData(const nsCString& data,
108                          const uint64_t& offset,
109                          const uint32_t& count);
110   void MaybeDivertOnStop(const nsresult& statusCode);
111   void DoOnStopRequest(const nsresult& statusCode,
112                        const nsCString &aErrorMsg,
113                        bool aUseUTF8);
114   void DoFailedAsyncOpen(const nsresult& statusCode);
115   void DoDeleteSelf();
116 
117   friend class FTPStartRequestEvent;
118   friend class FTPDataAvailableEvent;
119   friend class MaybeDivertOnDataFTPEvent;
120   friend class FTPStopRequestEvent;
121   friend class MaybeDivertOnStopFTPEvent;
122   friend class FTPFailedAsyncOpenEvent;
123   friend class FTPDeleteSelfEvent;
124 
125 private:
126   nsCOMPtr<nsIInputStream> mUploadStream;
127 
128   bool mIPCOpen;
129   RefPtr<ChannelEventQueue> mEventQ;
130 
131   // If nsUnknownDecoder is involved we queue onDataAvailable (and possibly
132   // OnStopRequest) so that we can divert them if needed when the listener's
133   // OnStartRequest is finally called
134   nsTArray<UniquePtr<ChannelEvent>> mUnknownDecoderEventQ;
135   bool mUnknownDecoderInvolved;
136 
137   bool mCanceled;
138   uint32_t mSuspendCount;
139   bool mIsPending;
140 
141   PRTime mLastModifiedTime;
142   uint64_t mStartPos;
143   nsCString mEntityID;
144 
145   // Once set, OnData and possibly OnStop will be diverted to the parent.
146   bool mDivertingToParent;
147   // Once set, no OnStart/OnData/OnStop callbacks should be received from the
148   // parent channel, nor dequeued from the ChannelEventQueue.
149   bool mFlushedForDiversion;
150   // Set if SendSuspend is called. Determines if SendResume is needed when
151   // diverting callbacks to parent.
152   bool mSuspendSent;
153 };
154 
155 inline bool
IsSuspended()156 FTPChannelChild::IsSuspended()
157 {
158   return mSuspendCount != 0;
159 }
160 
161 } // namespace net
162 } // namespace mozilla
163 
164 #endif // mozilla_net_FTPChannelChild_h
165