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_FTPChannelParent_h
9 #define mozilla_net_FTPChannelParent_h
10 
11 #include "ADivertableParentChannel.h"
12 #include "mozilla/net/PFTPChannelParent.h"
13 #include "mozilla/net/NeckoParent.h"
14 #include "nsIParentChannel.h"
15 #include "nsIInterfaceRequestor.h"
16 #include "nsIChannelEventSink.h"
17 #include "nsIFTPChannelParentInternal.h"
18 
19 class nsILoadContext;
20 
21 namespace mozilla {
22 
23 namespace dom {
24 class TabParent;
25 class PBrowserOrId;
26 } // namespace dom
27 
28 namespace net {
29 class ChannelEventQueue;
30 
31 class FTPChannelParent final : public PFTPChannelParent
32                              , public nsIParentChannel
33                              , public nsIInterfaceRequestor
34                              , public ADivertableParentChannel
35                              , public nsIChannelEventSink
36                              , public nsIFTPChannelParentInternal
37 {
38 public:
39   NS_DECL_ISUPPORTS
40   NS_DECL_NSIREQUESTOBSERVER
41   NS_DECL_NSISTREAMLISTENER
42   NS_DECL_NSIPARENTCHANNEL
43   NS_DECL_NSIINTERFACEREQUESTOR
44   NS_DECL_NSICHANNELEVENTSINK
45 
46   FTPChannelParent(const dom::PBrowserOrId& aIframeEmbedding,
47                    nsILoadContext* aLoadContext,
48                    PBOverrideStatus aOverrideStatus);
49 
50   bool Init(const FTPChannelCreationArgs& aOpenArgs);
51 
52   // ADivertableParentChannel functions.
53   void DivertTo(nsIStreamListener *aListener) override;
54   nsresult SuspendForDiversion() override;
55   nsresult SuspendMessageDiversion() override;
56   nsresult ResumeMessageDiversion() override;
57 
58   // Calls OnStartRequest for "DivertTo" listener, then notifies child channel
59   // that it should divert OnDataAvailable and OnStopRequest calls to this
60   // parent channel.
61   void StartDiversion();
62 
63   // Handles calling OnStart/Stop if there are errors during diversion.
64   // Called asynchronously from FailDiversion.
65   void NotifyDiversionFailed(nsresult aErrorCode, bool aSkipResume = true);
66 
67   NS_IMETHOD SetErrorMsg(const char *aMsg, bool aUseUTF8) override;
68 
69 protected:
70   virtual ~FTPChannelParent();
71 
72   // private, supporting function for ADivertableParentChannel.
73   nsresult ResumeForDiversion();
74 
75   // Asynchronously calls NotifyDiversionFailed.
76   void FailDiversion(nsresult aErrorCode, bool aSkipResume = true);
77 
78   bool DoAsyncOpen(const URIParams& aURI, const uint64_t& aStartPos,
79                    const nsCString& aEntityID,
80                    const OptionalInputStreamParams& aUploadStream,
81                    const OptionalLoadInfoArgs& aLoadInfoArgs);
82 
83   // used to connect redirected-to channel in parent with just created
84   // ChildChannel.  Used during HTTP->FTP redirects.
85   bool ConnectChannel(const uint32_t& channelId);
86 
87   void DivertOnDataAvailable(const nsCString& data,
88                              const uint64_t& offset,
89                              const uint32_t& count);
90   void DivertOnStopRequest(const nsresult& statusCode);
91   void DivertComplete();
92 
93   friend class FTPDivertDataAvailableEvent;
94   friend class FTPDivertStopRequestEvent;
95   friend class FTPDivertCompleteEvent;
96 
97   virtual bool RecvCancel(const nsresult& status) override;
98   virtual bool RecvSuspend() override;
99   virtual bool RecvResume() override;
100   virtual bool RecvDivertOnDataAvailable(const nsCString& data,
101                                          const uint64_t& offset,
102                                          const uint32_t& count) override;
103   virtual bool RecvDivertOnStopRequest(const nsresult& statusCode) override;
104   virtual bool RecvDivertComplete() override;
105 
106   nsresult SuspendChannel();
107   nsresult ResumeChannel();
108 
109   virtual void ActorDestroy(ActorDestroyReason why) override;
110 
111   // if configured to use HTTP proxy for FTP, this can an an HTTP channel.
112   nsCOMPtr<nsIChannel> mChannel;
113 
114   bool mIPCClosed;
115 
116   nsCOMPtr<nsILoadContext> mLoadContext;
117 
118   PBOverrideStatus mPBOverride;
119 
120   // If OnStart/OnData/OnStop have been diverted from the child, forward them to
121   // this listener.
122   nsCOMPtr<nsIStreamListener> mDivertToListener;
123   // Set to the canceled status value if the main channel was canceled.
124   nsresult mStatus;
125   // Once set, no OnStart/OnData/OnStop calls should be accepted; conversely, it
126   // must be set when RecvDivertOnData/~DivertOnStop/~DivertComplete are
127   // received from the child channel.
128   bool mDivertingFromChild;
129   // Set if OnStart|StopRequest was called during a diversion from the child.
130   bool mDivertedOnStartRequest;
131 
132   // Set if we successfully suspended the nsHttpChannel for diversion. Unset
133   // when we call ResumeForDiversion.
134   bool mSuspendedForDiversion;
135   RefPtr<mozilla::dom::TabParent> mTabParent;
136 
137   RefPtr<ChannelEventQueue> mEventQ;
138 
139   nsCString mErrorMsg;
140   bool mUseUTF8;
141 };
142 
143 } // namespace net
144 } // namespace mozilla
145 
146 #endif // mozilla_net_FTPChannelParent_h
147