2010-08-10 11:47:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
|
|
|
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-08-10 11:47:00 -07:00
|
|
|
|
|
|
|
#include "mozilla/net/FTPChannelParent.h"
|
|
|
|
#include "nsFTPChannel.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsISupportsPriority.h"
|
2010-11-23 14:56:06 -08:00
|
|
|
#include "nsIRedirectChannelRegistrar.h"
|
2010-08-10 11:47:00 -07:00
|
|
|
#include "nsFtpProtocolHandler.h"
|
2012-08-06 21:47:48 -07:00
|
|
|
#include "mozilla/LoadContext.h"
|
2012-08-22 19:13:54 -07:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2012-08-23 12:33:46 -07:00
|
|
|
#include "mozilla/ipc/URIUtils.h"
|
2012-08-22 19:13:54 -07:00
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2010-08-10 11:47:00 -07:00
|
|
|
|
|
|
|
#undef LOG
|
|
|
|
#define LOG(args) PR_LOG(gFTPLog, PR_LOG_DEBUG, args)
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
FTPChannelParent::FTPChannelParent()
|
2012-07-22 15:35:33 -07:00
|
|
|
: mIPCClosed(false)
|
2010-08-10 11:47:00 -07:00
|
|
|
{
|
|
|
|
nsIProtocolHandler* handler;
|
|
|
|
CallGetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "ftp", &handler);
|
|
|
|
NS_ASSERTION(handler, "no ftp handler");
|
|
|
|
}
|
|
|
|
|
|
|
|
FTPChannelParent::~FTPChannelParent()
|
|
|
|
{
|
|
|
|
gFtpHandler->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FTPChannelParent::ActorDestroy(ActorDestroyReason why)
|
|
|
|
{
|
|
|
|
// We may still have refcount>0 if the channel hasn't called OnStopRequest
|
|
|
|
// yet, but we must not send any more msgs to child.
|
|
|
|
mIPCClosed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-08-06 21:47:48 -07:00
|
|
|
NS_IMPL_ISUPPORTS4(FTPChannelParent,
|
2010-08-10 11:47:00 -07:00
|
|
|
nsIStreamListener,
|
2010-11-23 14:56:06 -08:00
|
|
|
nsIParentChannel,
|
2010-08-10 11:47:00 -07:00
|
|
|
nsIInterfaceRequestor,
|
2012-08-07 13:40:00 -07:00
|
|
|
nsIRequestObserver)
|
2010-08-10 11:47:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::PFTPChannelParent
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool
|
2012-08-23 12:33:46 -07:00
|
|
|
FTPChannelParent::RecvAsyncOpen(const URIParams& aURI,
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint64_t& aStartPos,
|
2010-08-10 11:47:00 -07:00
|
|
|
const nsCString& aEntityID,
|
2012-08-22 19:13:54 -07:00
|
|
|
const OptionalInputStreamParams& aUploadStream,
|
2012-08-06 21:47:48 -07:00
|
|
|
const IPC::SerializedLoadContext& loadContext)
|
2010-08-10 11:47:00 -07:00
|
|
|
{
|
2012-08-23 12:33:46 -07:00
|
|
|
nsCOMPtr<nsIURI> uri = DeserializeURI(aURI);
|
|
|
|
if (!uri)
|
|
|
|
return false;
|
2010-08-10 11:47:00 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCString uriSpec;
|
|
|
|
uri->GetSpec(uriSpec);
|
|
|
|
LOG(("FTPChannelParent RecvAsyncOpen [this=%x uri=%s]\n",
|
|
|
|
this, uriSpec.get()));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> ios(do_GetIOService(&rv));
|
|
|
|
if (NS_FAILED(rv))
|
2012-01-10 23:19:17 -08:00
|
|
|
return SendFailedAsyncOpen(rv);
|
2010-08-10 11:47:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> chan;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(chan), uri, ios);
|
|
|
|
if (NS_FAILED(rv))
|
2012-01-10 23:19:17 -08:00
|
|
|
return SendFailedAsyncOpen(rv);
|
2010-08-10 11:47:00 -07:00
|
|
|
|
|
|
|
mChannel = static_cast<nsFtpChannel*>(chan.get());
|
|
|
|
|
2012-08-22 19:13:54 -07:00
|
|
|
nsCOMPtr<nsIInputStream> upload = DeserializeInputStream(aUploadStream);
|
2010-08-10 11:47:00 -07:00
|
|
|
if (upload) {
|
|
|
|
// contentType and contentLength are ignored
|
|
|
|
rv = mChannel->SetUploadStream(upload, EmptyCString(), 0);
|
|
|
|
if (NS_FAILED(rv))
|
2012-01-10 23:19:17 -08:00
|
|
|
return SendFailedAsyncOpen(rv);
|
2010-08-10 11:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = mChannel->ResumeAt(aStartPos, aEntityID);
|
|
|
|
if (NS_FAILED(rv))
|
2012-01-10 23:19:17 -08:00
|
|
|
return SendFailedAsyncOpen(rv);
|
2010-08-10 11:47:00 -07:00
|
|
|
|
2012-08-06 21:47:48 -07:00
|
|
|
if (loadContext.IsNotNull())
|
|
|
|
mLoadContext = new LoadContext(loadContext);
|
2012-09-04 17:37:45 -07:00
|
|
|
else if (loadContext.IsPrivateBitValid()) {
|
|
|
|
nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(chan);
|
|
|
|
if (pbChannel)
|
|
|
|
pbChannel->SetPrivate(loadContext.mUsePrivateBrowsing);
|
|
|
|
}
|
2012-05-24 08:31:54 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = mChannel->AsyncOpen(this, nullptr);
|
2010-08-10 11:47:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
2012-01-10 23:19:17 -08:00
|
|
|
return SendFailedAsyncOpen(rv);
|
2010-08-10 11:47:00 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-23 14:56:06 -08:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
FTPChannelParent::RecvConnectChannel(const uint32_t& channelId)
|
2010-11-23 14:56:06 -08:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
LOG(("Looking for a registered channel [this=%p, id=%d]", this, channelId));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_LinkRedirectChannels(channelId, this, getter_AddRefs(channel));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mChannel = static_cast<nsFtpChannel*>(channel.get());
|
|
|
|
|
|
|
|
LOG((" found channel %p, rv=%08x", mChannel.get(), rv));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-10 11:47:00 -07:00
|
|
|
bool
|
|
|
|
FTPChannelParent::RecvCancel(const nsresult& status)
|
|
|
|
{
|
2012-01-10 23:19:17 -08:00
|
|
|
if (mChannel)
|
|
|
|
mChannel->Cancel(status);
|
2010-08-10 11:47:00 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FTPChannelParent::RecvSuspend()
|
|
|
|
{
|
2012-01-10 23:19:17 -08:00
|
|
|
if (mChannel)
|
|
|
|
mChannel->Suspend();
|
2010-08-10 11:47:00 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FTPChannelParent::RecvResume()
|
|
|
|
{
|
2012-01-10 23:19:17 -08:00
|
|
|
if (mChannel)
|
|
|
|
mChannel->Resume();
|
2010-08-10 11:47:00 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::nsIRequestObserver
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FTPChannelParent::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
|
|
|
{
|
|
|
|
LOG(("FTPChannelParent::OnStartRequest [this=%x]\n", this));
|
|
|
|
|
|
|
|
nsFtpChannel* chan = static_cast<nsFtpChannel*>(aRequest);
|
2012-10-22 10:51:07 -07:00
|
|
|
int64_t contentLength;
|
|
|
|
chan->GetContentLength(&contentLength);
|
2010-08-10 11:47:00 -07:00
|
|
|
nsCString contentType;
|
|
|
|
chan->GetContentType(contentType);
|
|
|
|
nsCString entityID;
|
|
|
|
chan->GetEntityID(entityID);
|
|
|
|
PRTime lastModified;
|
|
|
|
chan->GetLastModifiedTime(&lastModified);
|
|
|
|
|
2012-08-23 12:33:46 -07:00
|
|
|
URIParams uri;
|
|
|
|
SerializeURI(chan->URI(), uri);
|
|
|
|
|
2012-10-22 10:51:07 -07:00
|
|
|
if (mIPCClosed || !SendOnStartRequest(contentLength, contentType,
|
2012-08-23 12:33:46 -07:00
|
|
|
lastModified, entityID, uri)) {
|
2010-08-10 11:47:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FTPChannelParent::OnStopRequest(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatusCode)
|
|
|
|
{
|
|
|
|
LOG(("FTPChannelParent::OnStopRequest: [this=%x status=%ul]\n",
|
|
|
|
this, aStatusCode));
|
|
|
|
|
|
|
|
if (mIPCClosed || !SendOnStopRequest(aStatusCode)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::nsIStreamListener
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FTPChannelParent::OnDataAvailable(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIInputStream* aInputStream,
|
2012-09-05 19:41:02 -07:00
|
|
|
uint64_t aOffset,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCount)
|
2010-08-10 11:47:00 -07:00
|
|
|
{
|
|
|
|
LOG(("FTPChannelParent::OnDataAvailable [this=%x]\n", this));
|
|
|
|
|
|
|
|
nsCString data;
|
|
|
|
nsresult rv = NS_ReadInputStreamToString(aInputStream, data, aCount);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (mIPCClosed || !SendOnDataAvailable(data, aOffset, aCount))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-23 14:56:06 -08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::nsIParentChannel
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FTPChannelParent::Delete()
|
|
|
|
{
|
|
|
|
if (mIPCClosed || !SendDeleteSelf())
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-10 11:47:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::nsIInterfaceRequestor
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FTPChannelParent::GetInterface(const nsIID& uuid, void** result)
|
|
|
|
{
|
2012-07-22 15:35:33 -07:00
|
|
|
// Only support nsILoadContext if child channel's callbacks did too
|
2012-08-06 21:47:48 -07:00
|
|
|
if (uuid.Equals(NS_GET_IID(nsILoadContext)) && mLoadContext) {
|
|
|
|
NS_ADDREF(mLoadContext);
|
|
|
|
*result = static_cast<nsILoadContext*>(mLoadContext);
|
|
|
|
return NS_OK;
|
2012-07-22 15:35:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-23 14:56:06 -08:00
|
|
|
return QueryInterface(uuid, result);
|
2010-08-10 11:47:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-06 21:47:48 -07:00
|
|
|
//---------------------
|
2010-08-10 11:47:00 -07:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|