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