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 "nsFtpProtocolHandler.h"
|
2014-07-10 07:13:00 -07:00
|
|
|
#include "nsIEncodedChannel.h"
|
|
|
|
#include "nsIHttpChannelInternal.h"
|
|
|
|
#include "nsIForcePendingChannel.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"
|
2014-03-10 15:04:28 -07:00
|
|
|
#include "mozilla/unused.h"
|
2013-10-18 13:57:55 -07:00
|
|
|
#include "SerializedLoadContext.h"
|
2014-09-21 09:40:27 -07:00
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
2014-08-22 20:05:56 -07:00
|
|
|
#include "nsIOService.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 {
|
|
|
|
|
2012-12-29 01:02:16 -08:00
|
|
|
FTPChannelParent::FTPChannelParent(nsILoadContext* aLoadContext, PBOverrideStatus aOverrideStatus)
|
2012-07-22 15:35:33 -07:00
|
|
|
: mIPCClosed(false)
|
2012-12-29 01:02:16 -08:00
|
|
|
, mLoadContext(aLoadContext)
|
|
|
|
, mPBOverride(aOverrideStatus)
|
2014-03-10 15:04:28 -07:00
|
|
|
, mStatus(NS_OK)
|
|
|
|
, mDivertingFromChild(false)
|
|
|
|
, mDivertedOnStartRequest(false)
|
|
|
|
, mSuspendedForDiversion(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");
|
2014-08-22 20:05:56 -07:00
|
|
|
|
|
|
|
mObserver = new OfflineObserver(this);
|
2010-08-10 11:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
FTPChannelParent::~FTPChannelParent()
|
|
|
|
{
|
|
|
|
gFtpHandler->Release();
|
2014-08-22 20:05:56 -07:00
|
|
|
if (mObserver) {
|
|
|
|
mObserver->RemoveObserver();
|
|
|
|
}
|
2010-08-10 11:47:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(FTPChannelParent,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIParentChannel,
|
|
|
|
nsIInterfaceRequestor,
|
2014-07-10 07:13:00 -07:00
|
|
|
nsIRequestObserver,
|
|
|
|
nsIChannelEventSink)
|
2010-08-10 11:47:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::PFTPChannelParent
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-06-22 09:09:19 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent methods
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool
|
|
|
|
FTPChannelParent::Init(const FTPChannelCreationArgs& aArgs)
|
|
|
|
{
|
|
|
|
switch (aArgs.type()) {
|
|
|
|
case FTPChannelCreationArgs::TFTPChannelOpenArgs:
|
|
|
|
{
|
|
|
|
const FTPChannelOpenArgs& a = aArgs.get_FTPChannelOpenArgs();
|
2014-09-21 09:40:27 -07:00
|
|
|
return DoAsyncOpen(a.uri(), a.startPos(), a.entityID(), a.uploadStream(),
|
|
|
|
a.requestingPrincipalInfo(), a.securityFlags(),
|
|
|
|
a.contentPolicyType());
|
2013-06-22 09:09:19 -07:00
|
|
|
}
|
|
|
|
case FTPChannelCreationArgs::TFTPChannelConnectArgs:
|
|
|
|
{
|
|
|
|
const FTPChannelConnectArgs& cArgs = aArgs.get_FTPChannelConnectArgs();
|
|
|
|
return ConnectChannel(cArgs.channelId());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("unknown open type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-10 11:47:00 -07:00
|
|
|
bool
|
2013-06-22 09:09:19 -07:00
|
|
|
FTPChannelParent::DoAsyncOpen(const URIParams& aURI,
|
|
|
|
const uint64_t& aStartPos,
|
|
|
|
const nsCString& aEntityID,
|
2014-09-21 09:40:27 -07:00
|
|
|
const OptionalInputStreamParams& aUploadStream,
|
|
|
|
const ipc::PrincipalInfo& aRequestingPrincipalInfo,
|
|
|
|
const uint32_t& aSecurityFlags,
|
|
|
|
const uint32_t& aContentPolicyType)
|
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);
|
2013-06-22 09:09:19 -07:00
|
|
|
LOG(("FTPChannelParent DoAsyncOpen [this=%p uri=%s]\n",
|
2010-08-10 11:47:00 -07:00
|
|
|
this, uriSpec.get()));
|
|
|
|
#endif
|
|
|
|
|
2014-08-22 20:05:56 -07:00
|
|
|
bool app_offline = false;
|
|
|
|
uint32_t appId = GetAppId();
|
|
|
|
if (appId != NECKO_UNKNOWN_APP_ID &&
|
|
|
|
appId != NECKO_NO_APP_ID) {
|
|
|
|
gIOService->IsAppOffline(appId, &app_offline);
|
|
|
|
LOG(("FTP app id %u is offline %d\n", appId, app_offline));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (app_offline)
|
|
|
|
return SendFailedAsyncOpen(NS_ERROR_OFFLINE);
|
|
|
|
|
2010-08-10 11:47:00 -07:00
|
|
|
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
|
|
|
|
2014-09-21 09:40:27 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> requestingPrincipal =
|
|
|
|
mozilla::ipc::PrincipalInfoToPrincipal(aRequestingPrincipalInfo, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return SendFailedAsyncOpen(rv);
|
|
|
|
}
|
|
|
|
|
2010-08-10 11:47:00 -07:00
|
|
|
nsCOMPtr<nsIChannel> chan;
|
2014-09-21 09:40:27 -07:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(chan),
|
|
|
|
uri,
|
|
|
|
requestingPrincipal,
|
|
|
|
aSecurityFlags,
|
|
|
|
aContentPolicyType,
|
|
|
|
nullptr, // aLoadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
nsIRequest::LOAD_NORMAL,
|
|
|
|
ios);
|
|
|
|
|
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
|
|
|
|
2014-07-10 07:13:00 -07:00
|
|
|
mChannel = chan;
|
|
|
|
|
|
|
|
// later on mChannel may become an HTTP channel (we'll be redirected to one
|
|
|
|
// if we're using a proxy), but for now this is safe
|
|
|
|
nsFtpChannel* ftpChan = static_cast<nsFtpChannel*>(mChannel.get());
|
2012-12-29 01:02:16 -08:00
|
|
|
|
|
|
|
if (mPBOverride != kPBOverride_Unset) {
|
2014-07-10 07:13:00 -07:00
|
|
|
ftpChan->SetPrivate(mPBOverride == kPBOverride_Private ? true : false);
|
2012-12-29 01:02:16 -08:00
|
|
|
}
|
2014-07-10 07:13:00 -07:00
|
|
|
rv = ftpChan->SetNotificationCallbacks(this);
|
2013-01-11 06:40:25 -08:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return SendFailedAsyncOpen(rv);
|
|
|
|
|
2014-03-25 11:37:13 -07:00
|
|
|
nsTArray<mozilla::ipc::FileDescriptor> fds;
|
|
|
|
nsCOMPtr<nsIInputStream> upload = DeserializeInputStream(aUploadStream, fds);
|
2010-08-10 11:47:00 -07:00
|
|
|
if (upload) {
|
|
|
|
// contentType and contentLength are ignored
|
2014-07-10 07:13:00 -07:00
|
|
|
rv = ftpChan->SetUploadStream(upload, EmptyCString(), 0);
|
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
|
|
|
}
|
|
|
|
|
2014-07-10 07:13:00 -07:00
|
|
|
rv = ftpChan->ResumeAt(aStartPos, aEntityID);
|
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
|
|
|
|
2014-07-10 07:13:00 -07:00
|
|
|
rv = ftpChan->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
|
2013-06-22 09:09:19 -07:00
|
|
|
FTPChannelParent::ConnectChannel(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))
|
2014-07-10 07:13:00 -07:00
|
|
|
mChannel = channel;
|
2010-11-23 14:56:06 -08:00
|
|
|
|
|
|
|
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);
|
2014-08-22 20:05:56 -07:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-10 15:04:28 -07:00
|
|
|
bool
|
|
|
|
FTPChannelParent::RecvDivertOnDataAvailable(const nsCString& data,
|
|
|
|
const uint64_t& offset,
|
|
|
|
const uint32_t& count)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mDivertingFromChild)) {
|
|
|
|
MOZ_ASSERT(mDivertingFromChild,
|
|
|
|
"Cannot RecvDivertOnDataAvailable if diverting is not set!");
|
|
|
|
FailDiversion(NS_ERROR_UNEXPECTED);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop OnDataAvailables if the parent was canceled already.
|
|
|
|
if (NS_FAILED(mStatus)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stringStream;
|
|
|
|
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stringStream), data.get(),
|
|
|
|
count, NS_ASSIGNMENT_DEPEND);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->Cancel(rv);
|
|
|
|
}
|
|
|
|
mStatus = rv;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = OnDataAvailable(mChannel, nullptr, stringStream, offset, count);
|
|
|
|
|
|
|
|
stringStream->Close();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->Cancel(rv);
|
|
|
|
}
|
|
|
|
mStatus = rv;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FTPChannelParent::RecvDivertOnStopRequest(const nsresult& statusCode)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mDivertingFromChild)) {
|
|
|
|
MOZ_ASSERT(mDivertingFromChild,
|
|
|
|
"Cannot RecvDivertOnStopRequest if diverting is not set!");
|
|
|
|
FailDiversion(NS_ERROR_UNEXPECTED);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Honor the channel's status even if the underlying transaction completed.
|
|
|
|
nsresult status = NS_FAILED(mStatus) ? mStatus : statusCode;
|
|
|
|
|
|
|
|
// Reset fake pending status in case OnStopRequest has already been called.
|
|
|
|
if (mChannel) {
|
2014-07-10 07:13:00 -07:00
|
|
|
nsCOMPtr<nsIForcePendingChannel> forcePendingIChan = do_QueryInterface(mChannel);
|
|
|
|
if (forcePendingIChan) {
|
|
|
|
forcePendingIChan->ForcePending(false);
|
|
|
|
}
|
2014-03-10 15:04:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
OnStopRequest(mChannel, nullptr, status);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FTPChannelParent::RecvDivertComplete()
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mDivertingFromChild)) {
|
|
|
|
MOZ_ASSERT(mDivertingFromChild,
|
|
|
|
"Cannot RecvDivertComplete if diverting is not set!");
|
|
|
|
FailDiversion(NS_ERROR_UNEXPECTED);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = ResumeForDiversion();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
FailDiversion(NS_ERROR_UNEXPECTED);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-10 11:47:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::nsIRequestObserver
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FTPChannelParent::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
|
|
|
{
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("FTPChannelParent::OnStartRequest [this=%p]\n", this));
|
2010-08-10 11:47:00 -07:00
|
|
|
|
2014-03-10 15:04:28 -07:00
|
|
|
if (mDivertingFromChild) {
|
|
|
|
MOZ_RELEASE_ASSERT(mDivertToListener,
|
|
|
|
"Cannot divert if listener is unset!");
|
|
|
|
return mDivertToListener->OnStartRequest(aRequest, aContext);
|
|
|
|
}
|
|
|
|
|
2013-09-11 01:10:59 -07:00
|
|
|
nsCOMPtr<nsIChannel> chan = do_QueryInterface(aRequest);
|
|
|
|
MOZ_ASSERT(chan);
|
|
|
|
NS_ENSURE_TRUE(chan, NS_ERROR_UNEXPECTED);
|
|
|
|
|
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);
|
2013-09-11 01:10:59 -07:00
|
|
|
|
2010-08-10 11:47:00 -07:00
|
|
|
nsCString entityID;
|
2013-09-11 01:10:59 -07:00
|
|
|
nsCOMPtr<nsIResumableChannel> resChan = do_QueryInterface(aRequest);
|
|
|
|
MOZ_ASSERT(resChan); // both FTP and HTTP should implement nsIResumableChannel
|
|
|
|
if (resChan) {
|
|
|
|
resChan->GetEntityID(entityID);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRTime lastModified = 0;
|
2014-07-10 07:13:00 -07:00
|
|
|
nsCOMPtr<nsIFTPChannel> ftpChan = do_QueryInterface(aRequest);
|
2013-09-11 01:10:59 -07:00
|
|
|
if (ftpChan) {
|
|
|
|
ftpChan->GetLastModifiedTime(&lastModified);
|
2014-07-10 07:13:00 -07:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsIHttpChannelInternal> httpChan = do_QueryInterface(aRequest);
|
|
|
|
if (httpChan) {
|
|
|
|
httpChan->GetLastModifiedTime(&lastModified);
|
2013-09-11 01:10:59 -07:00
|
|
|
}
|
2010-08-10 11:47:00 -07:00
|
|
|
|
2013-09-11 01:10:59 -07:00
|
|
|
URIParams uriparam;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
chan->GetURI(getter_AddRefs(uri));
|
|
|
|
SerializeURI(uri, uriparam);
|
2012-08-23 12:33:46 -07:00
|
|
|
|
2014-04-07 11:26:22 -07:00
|
|
|
if (mIPCClosed || !SendOnStartRequest(mStatus, contentLength, contentType,
|
|
|
|
lastModified, entityID, uriparam)) {
|
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)
|
|
|
|
{
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("FTPChannelParent::OnStopRequest: [this=%p status=%ul]\n",
|
2010-08-10 11:47:00 -07:00
|
|
|
this, aStatusCode));
|
|
|
|
|
2014-03-10 15:04:28 -07:00
|
|
|
if (mDivertingFromChild) {
|
|
|
|
MOZ_RELEASE_ASSERT(mDivertToListener,
|
|
|
|
"Cannot divert if listener is unset!");
|
|
|
|
return mDivertToListener->OnStopRequest(aRequest, aContext, aStatusCode);
|
|
|
|
}
|
|
|
|
|
2010-08-10 11:47:00 -07:00
|
|
|
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
|
|
|
{
|
2013-06-04 16:32:31 -07:00
|
|
|
LOG(("FTPChannelParent::OnDataAvailable [this=%p]\n", this));
|
2014-03-10 15:04:28 -07:00
|
|
|
|
|
|
|
if (mDivertingFromChild) {
|
|
|
|
MOZ_RELEASE_ASSERT(mDivertToListener,
|
|
|
|
"Cannot divert if listener is unset!");
|
|
|
|
return mDivertToListener->OnDataAvailable(aRequest, aContext, aInputStream,
|
|
|
|
aOffset, aCount);
|
|
|
|
}
|
|
|
|
|
2010-08-10 11:47:00 -07:00
|
|
|
nsCString data;
|
|
|
|
nsresult rv = NS_ReadInputStreamToString(aInputStream, data, aCount);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2014-04-07 11:26:22 -07:00
|
|
|
if (mIPCClosed || !SendOnDataAvailable(mStatus, data, aOffset, aCount))
|
2010-08-10 11:47:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-23 14:56:06 -08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::nsIParentChannel
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-03-27 13:11:50 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
FTPChannelParent::SetParentListener(HttpChannelParentListener* aListener)
|
|
|
|
{
|
|
|
|
// Do not need ptr to HttpChannelParentListener.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-23 14:56:06 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-03-10 15:04:28 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::ADivertableParentChannel
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
nsresult
|
|
|
|
FTPChannelParent::SuspendForDiversion()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mChannel);
|
|
|
|
if (NS_WARN_IF(mDivertingFromChild)) {
|
|
|
|
MOZ_ASSERT(!mDivertingFromChild, "Already suspended for diversion!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try suspending the channel. Allow it to fail, since OnStopRequest may have
|
|
|
|
// been called and thus the channel may not be pending.
|
|
|
|
nsresult rv = mChannel->Suspend();
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv) || rv == NS_ERROR_NOT_AVAILABLE);
|
|
|
|
mSuspendedForDiversion = NS_SUCCEEDED(rv);
|
|
|
|
|
|
|
|
// Once this is set, no more OnStart/OnData/OnStop callbacks should be sent
|
|
|
|
// to the child.
|
|
|
|
mDivertingFromChild = true;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* private, supporting function for ADivertableParentChannel */
|
|
|
|
nsresult
|
|
|
|
FTPChannelParent::ResumeForDiversion()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mChannel);
|
|
|
|
MOZ_ASSERT(mDivertToListener);
|
|
|
|
if (NS_WARN_IF(!mDivertingFromChild)) {
|
|
|
|
MOZ_ASSERT(mDivertingFromChild,
|
|
|
|
"Cannot ResumeForDiversion if not diverting!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSuspendedForDiversion) {
|
|
|
|
nsresult rv = mChannel->Resume();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
FailDiversion(NS_ERROR_UNEXPECTED, true);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mSuspendedForDiversion = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete() will tear down IPDL, but ref from underlying nsFTPChannel will
|
|
|
|
// keep us alive if there's more data to be delivered to listener.
|
|
|
|
if (NS_WARN_IF(NS_FAILED(Delete()))) {
|
|
|
|
FailDiversion(NS_ERROR_UNEXPECTED);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FTPChannelParent::DivertTo(nsIStreamListener *aListener)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aListener);
|
|
|
|
if (NS_WARN_IF(!mDivertingFromChild)) {
|
|
|
|
MOZ_ASSERT(mDivertingFromChild,
|
|
|
|
"Cannot DivertTo new listener if diverting is not set!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(mIPCClosed || !SendFlushedForDiversion())) {
|
|
|
|
FailDiversion(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDivertToListener = aListener;
|
|
|
|
|
|
|
|
// Call OnStartRequest and SendDivertMessages asynchronously to avoid
|
|
|
|
// reentering client context.
|
|
|
|
NS_DispatchToCurrentThread(
|
|
|
|
NS_NewRunnableMethod(this, &FTPChannelParent::StartDiversion));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FTPChannelParent::StartDiversion()
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!mDivertingFromChild)) {
|
|
|
|
MOZ_ASSERT(mDivertingFromChild,
|
|
|
|
"Cannot StartDiversion if diverting is not set!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fake pending status in case OnStopRequest has already been called.
|
|
|
|
if (mChannel) {
|
2014-07-10 07:13:00 -07:00
|
|
|
nsCOMPtr<nsIForcePendingChannel> forcePendingIChan = do_QueryInterface(mChannel);
|
|
|
|
if (forcePendingIChan) {
|
|
|
|
forcePendingIChan->ForcePending(true);
|
|
|
|
}
|
2014-03-10 15:04:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call OnStartRequest for the "DivertTo" listener.
|
|
|
|
nsresult rv = OnStartRequest(mChannel, nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->Cancel(rv);
|
|
|
|
}
|
|
|
|
mStatus = rv;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// After OnStartRequest has been called, tell FTPChannelChild to divert the
|
|
|
|
// OnDataAvailables and OnStopRequest to this FTPChannelParent.
|
|
|
|
if (NS_WARN_IF(mIPCClosed || !SendDivertMessages())) {
|
|
|
|
FailDiversion(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class FTPFailDiversionEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FTPFailDiversionEvent(FTPChannelParent *aChannelParent,
|
|
|
|
nsresult aErrorCode,
|
|
|
|
bool aSkipResume)
|
|
|
|
: mChannelParent(aChannelParent)
|
|
|
|
, mErrorCode(aErrorCode)
|
|
|
|
, mSkipResume(aSkipResume)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(aChannelParent);
|
|
|
|
MOZ_RELEASE_ASSERT(NS_FAILED(aErrorCode));
|
|
|
|
}
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mChannelParent->NotifyDiversionFailed(mErrorCode, mSkipResume);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<FTPChannelParent> mChannelParent;
|
|
|
|
nsresult mErrorCode;
|
|
|
|
bool mSkipResume;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
FTPChannelParent::FailDiversion(nsresult aErrorCode,
|
|
|
|
bool aSkipResume)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(NS_FAILED(aErrorCode));
|
|
|
|
MOZ_RELEASE_ASSERT(mDivertingFromChild);
|
|
|
|
MOZ_RELEASE_ASSERT(mDivertToListener);
|
|
|
|
MOZ_RELEASE_ASSERT(mChannel);
|
|
|
|
|
|
|
|
NS_DispatchToCurrentThread(
|
|
|
|
new FTPFailDiversionEvent(this, aErrorCode, aSkipResume));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FTPChannelParent::NotifyDiversionFailed(nsresult aErrorCode,
|
|
|
|
bool aSkipResume)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(NS_FAILED(aErrorCode));
|
|
|
|
MOZ_RELEASE_ASSERT(mDivertingFromChild);
|
|
|
|
MOZ_RELEASE_ASSERT(mDivertToListener);
|
|
|
|
MOZ_RELEASE_ASSERT(mChannel);
|
|
|
|
|
|
|
|
mChannel->Cancel(aErrorCode);
|
2014-07-10 07:13:00 -07:00
|
|
|
nsCOMPtr<nsIForcePendingChannel> forcePendingIChan = do_QueryInterface(mChannel);
|
|
|
|
if (forcePendingIChan) {
|
|
|
|
forcePendingIChan->ForcePending(false);
|
|
|
|
}
|
2014-03-10 15:04:28 -07:00
|
|
|
|
|
|
|
bool isPending = false;
|
|
|
|
nsresult rv = mChannel->IsPending(&isPending);
|
|
|
|
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
// Resume only we suspended earlier.
|
|
|
|
if (mSuspendedForDiversion) {
|
|
|
|
mChannel->Resume();
|
|
|
|
}
|
|
|
|
// Channel has already sent OnStartRequest to the child, so ensure that we
|
|
|
|
// call it here if it hasn't already been called.
|
|
|
|
if (!mDivertedOnStartRequest) {
|
2014-07-10 07:13:00 -07:00
|
|
|
nsCOMPtr<nsIForcePendingChannel> forcePendingIChan = do_QueryInterface(mChannel);
|
|
|
|
if (forcePendingIChan) {
|
|
|
|
forcePendingIChan->ForcePending(true);
|
|
|
|
}
|
2014-03-10 15:04:28 -07:00
|
|
|
mDivertToListener->OnStartRequest(mChannel, nullptr);
|
2014-07-10 07:13:00 -07:00
|
|
|
|
|
|
|
if (forcePendingIChan) {
|
|
|
|
forcePendingIChan->ForcePending(false);
|
|
|
|
}
|
2014-03-10 15:04:28 -07:00
|
|
|
}
|
|
|
|
// If the channel is pending, it will call OnStopRequest itself; otherwise, do
|
|
|
|
// it here.
|
|
|
|
if (!isPending) {
|
|
|
|
mDivertToListener->OnStopRequest(mChannel, nullptr, aErrorCode);
|
|
|
|
}
|
|
|
|
mDivertToListener = nullptr;
|
|
|
|
mChannel = nullptr;
|
|
|
|
|
|
|
|
if (!mIPCClosed) {
|
|
|
|
unused << SendDeleteSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 20:05:56 -07:00
|
|
|
void
|
|
|
|
FTPChannelParent::OfflineDisconnect()
|
|
|
|
{
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->Cancel(NS_ERROR_OFFLINE);
|
|
|
|
}
|
|
|
|
mStatus = NS_ERROR_OFFLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
FTPChannelParent::GetAppId()
|
|
|
|
{
|
|
|
|
uint32_t appId = NECKO_UNKNOWN_APP_ID;
|
|
|
|
if (mLoadContext) {
|
|
|
|
mLoadContext->GetAppId(&appId);
|
|
|
|
}
|
|
|
|
return appId;
|
|
|
|
}
|
|
|
|
|
2014-07-10 07:13:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// FTPChannelParent::nsIChannelEventSink
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FTPChannelParent::AsyncOnChannelRedirect(
|
|
|
|
nsIChannel *oldChannel,
|
|
|
|
nsIChannel *newChannel,
|
|
|
|
uint32_t redirectFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* callback)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFTPChannel> ftpChan = do_QueryInterface(newChannel);
|
|
|
|
if (!ftpChan) {
|
|
|
|
// when FTP is set to use HTTP proxying, we wind up getting redirected to an HTTP channel.
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChan = do_QueryInterface(newChannel);
|
|
|
|
if (!httpChan)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
mChannel = newChannel;
|
|
|
|
callback->OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-06 21:47:48 -07:00
|
|
|
//---------------------
|
2010-08-10 11:47:00 -07:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|