2012-05-21 15:12:37 +04: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/. */
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
#include "nsAsyncStreamCopier.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsIOService.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsIEventTarget.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsStreamUtils.h"
|
2014-04-09 04:26:00 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsIBufferedStreams.h"
|
|
|
|
#include "nsIRequestObserver.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-11-21 02:55:44 +04:00
|
|
|
#undef LOG
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
2016-05-26 02:13:54 +03:00
|
|
|
// MOZ_LOG=nsStreamCopier:5
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
2015-11-03 07:35:29 +03:00
|
|
|
static LazyLogModule gStreamCopierLog("nsStreamCopier");
|
2015-06-04 01:25:57 +03:00
|
|
|
#define LOG(args) MOZ_LOG(gStreamCopierLog, mozilla::LogLevel::Debug, args)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
/**
|
|
|
|
* An event used to perform initialization off the main thread.
|
|
|
|
*/
|
2016-04-26 03:23:21 +03:00
|
|
|
class AsyncApplyBufferingPolicyEvent final: public Runnable
|
2014-04-09 04:26:00 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aCopier
|
|
|
|
* The nsAsyncStreamCopier requesting the information.
|
|
|
|
*/
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit AsyncApplyBufferingPolicyEvent(nsAsyncStreamCopier* aCopier)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("AsyncApplyBufferingPolicyEvent")
|
|
|
|
, mCopier(aCopier)
|
2017-06-01 23:43:44 +03:00
|
|
|
, mTarget(GetCurrentThreadEventTarget())
|
2017-06-12 22:34:10 +03:00
|
|
|
{}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-04-09 04:26:00 +04:00
|
|
|
{
|
|
|
|
nsresult rv = mCopier->ApplyBufferingPolicy();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCopier->Cancel(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
rv = mTarget->Dispatch(NewRunnableMethod("nsAsyncStreamCopier::AsyncCopyInternal",
|
|
|
|
mCopier,
|
2016-05-05 11:45:00 +03:00
|
|
|
&nsAsyncStreamCopier::AsyncCopyInternal),
|
|
|
|
NS_DISPATCH_NORMAL);
|
2014-04-09 04:26:00 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCopier->Cancel(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-06-12 22:34:10 +03:00
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsAsyncStreamCopier> mCopier;
|
2014-04-09 04:26:00 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsAsyncStreamCopier::nsAsyncStreamCopier()
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
: mLock("nsAsyncStreamCopier.mLock")
|
2003-10-06 05:46:31 +04:00
|
|
|
, mMode(NS_ASYNCCOPY_VIA_READSEGMENTS)
|
2010-04-20 20:32:28 +04:00
|
|
|
, mChunkSize(nsIOService::gDefaultSegmentSize)
|
2003-01-18 04:27:53 +03:00
|
|
|
, mStatus(NS_OK)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mIsPending(false)
|
2014-04-09 04:26:00 +04:00
|
|
|
, mShouldSniffBuffering(false)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("Creating nsAsyncStreamCopier @%p\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAsyncStreamCopier::~nsAsyncStreamCopier()
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("Destroying nsAsyncStreamCopier @%p\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2003-01-18 04:27:53 +03:00
|
|
|
nsAsyncStreamCopier::IsComplete(nsresult *status)
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (status)
|
|
|
|
*status = mStatus;
|
|
|
|
return !mIsPending;
|
|
|
|
}
|
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
nsIRequest*
|
|
|
|
nsAsyncStreamCopier::AsRequest()
|
|
|
|
{
|
|
|
|
return static_cast<nsIRequest*>(static_cast<nsIAsyncStreamCopier*>(this));
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
void
|
2003-10-06 05:46:31 +04:00
|
|
|
nsAsyncStreamCopier::Complete(nsresult status)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsAsyncStreamCopier::Complete [this=%p status=%" PRIx32 "]\n", this,
|
|
|
|
static_cast<uint32_t>(status)));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRequestObserver> observer;
|
|
|
|
nsCOMPtr<nsISupports> ctx;
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-07-30 18:20:58 +04:00
|
|
|
mCopierCtx = nullptr;
|
2009-03-30 23:10:13 +04:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
if (mIsPending) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsPending = false;
|
2003-10-06 05:46:31 +04:00
|
|
|
mStatus = status;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
// setup OnStopRequest callback and release references...
|
|
|
|
observer = mObserver;
|
2012-07-30 18:20:58 +04:00
|
|
|
mObserver = nullptr;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (observer) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" calling OnStopRequest [status=%" PRIx32 "]\n",
|
|
|
|
static_cast<uint32_t>(status)));
|
2014-04-09 04:26:00 +04:00
|
|
|
observer->OnStopRequest(AsRequest(), ctx, status);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
void
|
|
|
|
nsAsyncStreamCopier::OnAsyncCopyComplete(void *closure, nsresult status)
|
|
|
|
{
|
|
|
|
nsAsyncStreamCopier *self = (nsAsyncStreamCopier *) closure;
|
|
|
|
self->Complete(status);
|
|
|
|
NS_RELEASE(self); // addref'd in AsyncCopy
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsISupports
|
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
// We cannot use simply NS_IMPL_ISUPPORTSx as both
|
|
|
|
// nsIAsyncStreamCopier and nsIAsyncStreamCopier2 implement nsIRequest
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsAsyncStreamCopier)
|
|
|
|
NS_IMPL_RELEASE(nsAsyncStreamCopier)
|
|
|
|
NS_INTERFACE_TABLE_HEAD(nsAsyncStreamCopier)
|
|
|
|
NS_INTERFACE_TABLE_BEGIN
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsAsyncStreamCopier, nsIAsyncStreamCopier)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsAsyncStreamCopier, nsIAsyncStreamCopier2)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(nsAsyncStreamCopier, nsIRequest, nsIAsyncStreamCopier)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(nsAsyncStreamCopier, nsISupports, nsIAsyncStreamCopier)
|
|
|
|
NS_INTERFACE_TABLE_END
|
|
|
|
NS_INTERFACE_TABLE_TAIL
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIRequest
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::GetName(nsACString &name)
|
|
|
|
{
|
2003-10-06 05:46:31 +04:00
|
|
|
name.Truncate();
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsAsyncStreamCopier::IsPending(bool *result)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
*result = !IsComplete();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::GetStatus(nsresult *status)
|
|
|
|
{
|
|
|
|
IsComplete(status);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::Cancel(nsresult status)
|
|
|
|
{
|
2009-03-30 23:10:13 +04:00
|
|
|
nsCOMPtr<nsISupports> copierCtx;
|
|
|
|
{
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-03-30 23:10:13 +04:00
|
|
|
if (!mIsPending)
|
|
|
|
return NS_OK;
|
|
|
|
copierCtx.swap(mCopierCtx);
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(status)) {
|
|
|
|
NS_WARNING("cancel with non-failure status code");
|
|
|
|
status = NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
|
2009-03-30 23:10:13 +04:00
|
|
|
if (copierCtx)
|
|
|
|
NS_CancelAsyncCopy(copierCtx, status);
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::Suspend()
|
|
|
|
{
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_NOTREACHED("nsAsyncStreamCopier::Suspend");
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::Resume()
|
|
|
|
{
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_NOTREACHED("nsAsyncStreamCopier::Resume");
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::GetLoadFlags(nsLoadFlags *aLoadFlags)
|
|
|
|
{
|
|
|
|
*aLoadFlags = LOAD_NORMAL;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::SetLoadFlags(nsLoadFlags aLoadFlags)
|
|
|
|
{
|
2003-10-06 05:46:31 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::GetLoadGroup(nsILoadGroup **aLoadGroup)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aLoadGroup = nullptr;
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::SetLoadGroup(nsILoadGroup *aLoadGroup)
|
|
|
|
{
|
2003-10-06 05:46:31 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
nsresult
|
|
|
|
nsAsyncStreamCopier::InitInternal(nsIInputStream *source,
|
|
|
|
nsIOutputStream *sink,
|
|
|
|
nsIEventTarget *target,
|
|
|
|
uint32_t chunkSize,
|
|
|
|
bool closeSource,
|
|
|
|
bool closeSink)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mSource && !mSink, "Init() called more than once");
|
|
|
|
if (chunkSize == 0) {
|
|
|
|
chunkSize = nsIOService::gDefaultSegmentSize;
|
|
|
|
}
|
|
|
|
mChunkSize = chunkSize;
|
|
|
|
|
|
|
|
mSource = source;
|
|
|
|
mSink = sink;
|
|
|
|
mCloseSource = closeSource;
|
|
|
|
mCloseSink = closeSink;
|
|
|
|
|
|
|
|
if (target) {
|
|
|
|
mTarget = target;
|
|
|
|
} else {
|
|
|
|
nsresult rv;
|
|
|
|
mTarget = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIAsyncStreamCopier
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::Init(nsIInputStream *source,
|
|
|
|
nsIOutputStream *sink,
|
2003-10-06 05:46:31 +04:00
|
|
|
nsIEventTarget *target,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool sourceBuffered,
|
|
|
|
bool sinkBuffered,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t chunkSize,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool closeSource,
|
|
|
|
bool closeSink)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_ASSERTION(sourceBuffered || sinkBuffered, "at least one stream must be buffered");
|
2014-04-09 04:26:00 +04:00
|
|
|
mMode = sourceBuffered ? NS_ASYNCCOPY_VIA_READSEGMENTS
|
|
|
|
: NS_ASYNCCOPY_VIA_WRITESEGMENTS;
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
return InitInternal(source, sink, target, chunkSize, closeSource, closeSink);
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIAsyncStreamCopier2
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::Init(nsIInputStream *source,
|
|
|
|
nsIOutputStream *sink,
|
|
|
|
nsIEventTarget *target,
|
|
|
|
uint32_t chunkSize,
|
|
|
|
bool closeSource,
|
|
|
|
bool closeSink)
|
|
|
|
{
|
|
|
|
mShouldSniffBuffering = true;
|
|
|
|
|
|
|
|
return InitInternal(source, sink, target, chunkSize, closeSource, closeSink);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect whether the input or the output stream is buffered,
|
|
|
|
* bufferize one of them if neither is buffered.
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsAsyncStreamCopier::ApplyBufferingPolicy()
|
|
|
|
{
|
|
|
|
// This function causes I/O, it must not be executed on the main
|
|
|
|
// thread.
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
|
|
|
if (NS_OutputStreamIsBuffered(mSink)) {
|
|
|
|
// Sink is buffered, no need to perform additional buffering
|
|
|
|
mMode = NS_ASYNCCOPY_VIA_WRITESEGMENTS;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (NS_InputStreamIsBuffered(mSource)) {
|
|
|
|
// Source is buffered, no need to perform additional buffering
|
|
|
|
mMode = NS_ASYNCCOPY_VIA_READSEGMENTS;
|
|
|
|
return NS_OK;
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|
2014-04-09 04:26:00 +04:00
|
|
|
|
|
|
|
// No buffering, let's buffer the sink
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIBufferedOutputStream> sink =
|
|
|
|
do_CreateInstance(NS_BUFFEREDOUTPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = sink->Init(mSink, mChunkSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMode = NS_ASYNCCOPY_VIA_WRITESEGMENTS;
|
|
|
|
mSink = sink;
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Both nsIAsyncStreamCopier and nsIAsyncStreamCopier2
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAsyncStreamCopier::AsyncCopy(nsIRequestObserver *observer, nsISupports *ctx)
|
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsAsyncStreamCopier::AsyncCopy [this=%p observer=%p]\n", this, observer));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
NS_ASSERTION(mSource && mSink, "not initialized");
|
2003-01-18 04:27:53 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
if (observer) {
|
|
|
|
// build proxy for observer events
|
2013-03-19 20:04:57 +04:00
|
|
|
rv = NS_NewRequestObserverProxy(getter_AddRefs(mObserver), observer, ctx);
|
2003-10-06 05:46:31 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// from this point forward, AsyncCopy is going to return NS_OK. any errors
|
|
|
|
// will be reported via OnStopRequest.
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsPending = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
if (mObserver) {
|
2014-04-09 04:26:00 +04:00
|
|
|
rv = mObserver->OnStartRequest(AsRequest(), nullptr);
|
2003-10-06 05:46:31 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
Cancel(rv);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2012-05-23 08:23:11 +04:00
|
|
|
|
2014-04-09 04:26:00 +04:00
|
|
|
if (!mShouldSniffBuffering) {
|
|
|
|
// No buffer sniffing required, let's proceed
|
|
|
|
AsyncCopyInternal();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
// Don't perform buffer sniffing on the main thread
|
2014-05-08 23:08:38 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event = new AsyncApplyBufferingPolicyEvent(this);
|
2014-04-09 04:26:00 +04:00
|
|
|
rv = mTarget->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cancel(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're not going to block the main thread, so let's sniff here
|
|
|
|
rv = ApplyBufferingPolicy();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Cancel(rv);
|
|
|
|
}
|
|
|
|
AsyncCopyInternal();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Launch async copy.
|
|
|
|
// All errors are reported through the observer.
|
|
|
|
void
|
|
|
|
nsAsyncStreamCopier::AsyncCopyInternal()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMode == NS_ASYNCCOPY_VIA_READSEGMENTS
|
|
|
|
|| mMode == NS_ASYNCCOPY_VIA_WRITESEGMENTS);
|
|
|
|
|
|
|
|
nsresult rv;
|
2003-10-06 05:46:31 +04:00
|
|
|
// we want to receive progress notifications; release happens in
|
|
|
|
// OnAsyncCopyComplete.
|
|
|
|
NS_ADDREF_THIS();
|
2012-06-11 03:44:50 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
rv = NS_AsyncCopy(mSource, mSink, mTarget, mMode, mChunkSize,
|
|
|
|
OnAsyncCopyComplete, this, mCloseSource, mCloseSink,
|
|
|
|
getter_AddRefs(mCopierCtx));
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
Cancel(rv);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
}
|
2014-04-09 04:26:00 +04:00
|
|
|
|
|
|
|
|