Bug 1509459 - Get the flexbox highlighter state if the highlighter is ready in the...
[gecko.git] / netwerk / protocol / ftp / nsFTPChannel.cpp
blob91bd8515e22771dcc2e5c44a068353848773dca9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=4 sts=2 sw=2 et cin: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsFTPChannel.h"
8 #include "nsFtpConnectionThread.h" // defines nsFtpState
10 #include "nsThreadUtils.h"
11 #include "mozilla/Attributes.h"
13 using namespace mozilla;
14 using namespace mozilla::net;
15 extern LazyLogModule gFTPLog;
17 // There are two transport connections established for an
18 // ftp connection. One is used for the command channel , and
19 // the other for the data channel. The command channel is the first
20 // connection made and is used to negotiate the second, data, channel.
21 // The data channel is driven by the command channel and is either
22 // initiated by the server (PORT command) or by the client (PASV command).
23 // Client initiation is the most common case and is attempted first.
25 //-----------------------------------------------------------------------------
27 NS_IMPL_ISUPPORTS_INHERITED(nsFtpChannel, nsBaseChannel, nsIUploadChannel,
28 nsIResumableChannel, nsIFTPChannel,
29 nsIProxiedChannel, nsIForcePendingChannel,
30 nsIChannelWithDivertableParentListener)
32 //-----------------------------------------------------------------------------
34 NS_IMETHODIMP
35 nsFtpChannel::SetUploadStream(nsIInputStream *stream,
36 const nsACString &contentType,
37 int64_t contentLength) {
38 NS_ENSURE_TRUE(!Pending(), NS_ERROR_IN_PROGRESS);
40 mUploadStream = stream;
42 // NOTE: contentLength is intentionally ignored here.
44 return NS_OK;
47 NS_IMETHODIMP
48 nsFtpChannel::GetUploadStream(nsIInputStream **stream) {
49 NS_ENSURE_ARG_POINTER(stream);
50 *stream = mUploadStream;
51 NS_IF_ADDREF(*stream);
52 return NS_OK;
55 //-----------------------------------------------------------------------------
57 NS_IMETHODIMP
58 nsFtpChannel::ResumeAt(uint64_t aStartPos, const nsACString &aEntityID) {
59 NS_ENSURE_TRUE(!Pending(), NS_ERROR_IN_PROGRESS);
60 mEntityID = aEntityID;
61 mStartPos = aStartPos;
62 mResumeRequested = (mStartPos || !mEntityID.IsEmpty());
63 return NS_OK;
66 NS_IMETHODIMP
67 nsFtpChannel::GetEntityID(nsACString &entityID) {
68 if (mEntityID.IsEmpty()) return NS_ERROR_NOT_RESUMABLE;
70 entityID = mEntityID;
71 return NS_OK;
74 //-----------------------------------------------------------------------------
75 NS_IMETHODIMP
76 nsFtpChannel::GetProxyInfo(nsIProxyInfo **aProxyInfo) {
77 *aProxyInfo = ProxyInfo();
78 NS_IF_ADDREF(*aProxyInfo);
79 return NS_OK;
82 //-----------------------------------------------------------------------------
84 nsresult nsFtpChannel::OpenContentStream(bool async, nsIInputStream **result,
85 nsIChannel **channel) {
86 if (!async) return NS_ERROR_NOT_IMPLEMENTED;
88 nsFtpState *state = new nsFtpState();
89 if (!state) return NS_ERROR_OUT_OF_MEMORY;
90 NS_ADDREF(state);
92 nsresult rv = state->Init(this);
93 if (NS_FAILED(rv)) {
94 NS_RELEASE(state);
95 return rv;
98 *result = state;
99 return NS_OK;
102 bool nsFtpChannel::GetStatusArg(nsresult status, nsString &statusArg) {
103 nsAutoCString host;
104 URI()->GetHost(host);
105 CopyUTF8toUTF16(host, statusArg);
106 return true;
109 void nsFtpChannel::OnCallbacksChanged() { mFTPEventSink = nullptr; }
111 //-----------------------------------------------------------------------------
113 namespace {
115 class FTPEventSinkProxy final : public nsIFTPEventSink {
116 ~FTPEventSinkProxy() = default;
118 public:
119 explicit FTPEventSinkProxy(nsIFTPEventSink *aTarget)
120 : mTarget(aTarget), mEventTarget(GetCurrentThreadEventTarget()) {}
122 NS_DECL_THREADSAFE_ISUPPORTS
123 NS_DECL_NSIFTPEVENTSINK
125 class OnFTPControlLogRunnable : public Runnable {
126 public:
127 OnFTPControlLogRunnable(nsIFTPEventSink *aTarget, bool aServer,
128 const char *aMessage)
129 : mozilla::Runnable("FTPEventSinkProxy::OnFTPControlLogRunnable"),
130 mTarget(aTarget),
131 mServer(aServer),
132 mMessage(aMessage) {}
134 NS_DECL_NSIRUNNABLE
136 private:
137 nsCOMPtr<nsIFTPEventSink> mTarget;
138 bool mServer;
139 nsCString mMessage;
142 private:
143 nsCOMPtr<nsIFTPEventSink> mTarget;
144 nsCOMPtr<nsIEventTarget> mEventTarget;
147 NS_IMPL_ISUPPORTS(FTPEventSinkProxy, nsIFTPEventSink)
149 NS_IMETHODIMP
150 FTPEventSinkProxy::OnFTPControlLog(bool aServer, const char *aMsg) {
151 RefPtr<OnFTPControlLogRunnable> r =
152 new OnFTPControlLogRunnable(mTarget, aServer, aMsg);
153 return mEventTarget->Dispatch(r, NS_DISPATCH_NORMAL);
156 NS_IMETHODIMP
157 FTPEventSinkProxy::OnFTPControlLogRunnable::Run() {
158 mTarget->OnFTPControlLog(mServer, mMessage.get());
159 return NS_OK;
162 } // namespace
164 void nsFtpChannel::GetFTPEventSink(nsCOMPtr<nsIFTPEventSink> &aResult) {
165 if (!mFTPEventSink) {
166 nsCOMPtr<nsIFTPEventSink> ftpSink;
167 GetCallback(ftpSink);
168 if (ftpSink) {
169 mFTPEventSink = new FTPEventSinkProxy(ftpSink);
172 aResult = mFTPEventSink;
175 NS_IMETHODIMP
176 nsFtpChannel::ForcePending(bool aForcePending) {
177 // Set true here so IsPending will return true.
178 // Required for callback diversion from child back to parent. In such cases
179 // OnStopRequest can be called in the parent before callbacks are diverted
180 // back from the child to the listener in the parent.
181 mForcePending = aForcePending;
183 return NS_OK;
186 NS_IMETHODIMP
187 nsFtpChannel::IsPending(bool *result) {
188 *result = Pending();
189 return NS_OK;
192 bool nsFtpChannel::Pending() const {
193 return nsBaseChannel::Pending() || mForcePending;
196 NS_IMETHODIMP
197 nsFtpChannel::Suspend() {
198 LOG(("nsFtpChannel::Suspend [this=%p]\n", this));
200 nsresult rv = SuspendInternal();
202 nsresult rvParentChannel = NS_OK;
203 if (mParentChannel) {
204 rvParentChannel = mParentChannel->SuspendMessageDiversion();
207 return NS_FAILED(rv) ? rv : rvParentChannel;
210 NS_IMETHODIMP
211 nsFtpChannel::Resume() {
212 LOG(("nsFtpChannel::Resume [this=%p]\n", this));
214 nsresult rv = ResumeInternal();
216 nsresult rvParentChannel = NS_OK;
217 if (mParentChannel) {
218 rvParentChannel = mParentChannel->ResumeMessageDiversion();
221 return NS_FAILED(rv) ? rv : rvParentChannel;
224 //-----------------------------------------------------------------------------
225 // AChannelHasDivertableParentChannelAsListener internal functions
226 //-----------------------------------------------------------------------------
228 NS_IMETHODIMP
229 nsFtpChannel::MessageDiversionStarted(
230 ADivertableParentChannel *aParentChannel) {
231 MOZ_ASSERT(!mParentChannel);
232 mParentChannel = aParentChannel;
233 // If the channel is suspended, propagate that info to the parent's mEventQ.
234 uint32_t suspendCount = mSuspendCount;
235 while (suspendCount--) {
236 mParentChannel->SuspendMessageDiversion();
238 return NS_OK;
241 NS_IMETHODIMP
242 nsFtpChannel::MessageDiversionStop() {
243 LOG(("nsFtpChannel::MessageDiversionStop [this=%p]", this));
244 MOZ_ASSERT(mParentChannel);
245 mParentChannel = nullptr;
246 return NS_OK;
249 NS_IMETHODIMP
250 nsFtpChannel::SuspendInternal() {
251 LOG(("nsFtpChannel::SuspendInternal [this=%p]\n", this));
252 ++mSuspendCount;
253 return nsBaseChannel::Suspend();
256 NS_IMETHODIMP
257 nsFtpChannel::ResumeInternal() {
258 LOG(("nsFtpChannel::ResumeInternal [this=%p]\n", this));
259 NS_ENSURE_TRUE(mSuspendCount > 0, NS_ERROR_UNEXPECTED);
260 --mSuspendCount;
261 return nsBaseChannel::Resume();