2014-05-05 21:30:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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
|
|
|
|
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
|
|
|
#include "mozilla/Mutex.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsStreamUtils.h"
|
2013-04-04 11:05:18 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIPipe.h"
|
2015-02-27 00:35:09 +03:00
|
|
|
#include "nsICloneableInputStream.h"
|
2003-10-06 05:46:31 +04:00
|
|
|
#include "nsIEventTarget.h"
|
2015-12-18 11:44:00 +03:00
|
|
|
#include "nsICancelableRunnable.h"
|
2009-08-07 00:34:46 +04:00
|
|
|
#include "nsISafeOutputStream.h"
|
2005-12-10 00:35:27 +03:00
|
|
|
#include "nsString.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
2013-10-16 10:42:00 +04:00
|
|
|
#include "nsIBufferedStreams.h"
|
2015-02-11 07:55:43 +03:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2016-04-11 21:40:06 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2018-01-31 18:45:20 +03:00
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
#include "NonBlockingAsyncInputStream.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;
|
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
// This is a nsICancelableRunnable because we can dispatch it to Workers and
|
|
|
|
// those can be shut down at any time, and in these cases, Cancel() is called
|
|
|
|
// instead of Run().
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsInputStreamReadyEvent final
|
2016-04-11 21:40:06 +03:00
|
|
|
: public CancelableRunnable
|
2014-05-15 00:15:46 +04:00
|
|
|
, public nsIInputStreamCallback
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2017-06-13 02:24:15 +03:00
|
|
|
nsInputStreamReadyEvent(const char* aName,
|
|
|
|
nsIInputStreamCallback* aCallback,
|
|
|
|
nsIEventTarget* aTarget)
|
|
|
|
: CancelableRunnable(aName)
|
2017-01-18 03:50:34 +03:00
|
|
|
, mCallback(aCallback)
|
2014-05-15 00:15:46 +04:00
|
|
|
, mTarget(aTarget)
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2004-01-15 09:14:18 +03:00
|
|
|
private:
|
2014-05-05 21:30:43 +04:00
|
|
|
~nsInputStreamReadyEvent()
|
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!mCallback) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
//
|
|
|
|
// whoa!! looks like we never posted this event. take care to
|
|
|
|
// release mCallback on the correct thread. if mTarget lives on the
|
|
|
|
// calling thread, then we are ok. otherwise, we have to try to
|
|
|
|
// proxy the Release over the right thread. if that thread is dead,
|
|
|
|
// then there's nothing we can do... better to leak than crash.
|
|
|
|
//
|
|
|
|
bool val;
|
|
|
|
nsresult rv = mTarget->IsOnCurrentThread(&val);
|
|
|
|
if (NS_FAILED(rv) || !val) {
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> event =
|
2017-06-13 02:24:15 +03:00
|
|
|
NS_NewInputStreamReadyEvent("~nsInputStreamReadyEvent", mCallback, mTarget);
|
2014-05-05 21:30:43 +04:00
|
|
|
mCallback = nullptr;
|
|
|
|
if (event) {
|
|
|
|
rv = event->OnInputStreamReady(nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_NOTREACHED("leaking stream event");
|
2014-05-15 00:15:46 +04:00
|
|
|
nsISupports* sup = event;
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ADDREF(sup);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2004-01-15 09:14:18 +03:00
|
|
|
public:
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream* aStream) override
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
mStream = aStream;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
mTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Dispatch failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
if (mCallback) {
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mStream) {
|
2014-05-05 21:30:43 +04:00
|
|
|
mCallback->OnInputStreamReady(mStream);
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
mCallback = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult Cancel() override
|
2015-12-18 11:44:00 +03:00
|
|
|
{
|
|
|
|
mCallback = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
private:
|
2014-05-05 21:30:43 +04:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> mStream;
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mCallback;
|
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsInputStreamReadyEvent, CancelableRunnable,
|
2016-04-11 21:40:06 +03:00
|
|
|
nsIInputStreamCallback)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
// This is a nsICancelableRunnable because we can dispatch it to Workers and
|
|
|
|
// those can be shut down at any time, and in these cases, Cancel() is called
|
|
|
|
// instead of Run().
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsOutputStreamReadyEvent final
|
2016-04-11 21:40:06 +03:00
|
|
|
: public CancelableRunnable
|
2014-05-15 00:15:46 +04:00
|
|
|
, public nsIOutputStreamCallback
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsOutputStreamReadyEvent(nsIOutputStreamCallback* aCallback,
|
|
|
|
nsIEventTarget* aTarget)
|
2017-01-18 03:50:34 +03:00
|
|
|
: CancelableRunnable("nsOutputStreamReadyEvent")
|
|
|
|
, mCallback(aCallback)
|
2014-05-15 00:15:46 +04:00
|
|
|
, mTarget(aTarget)
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2004-01-15 09:14:18 +03:00
|
|
|
private:
|
2014-05-05 21:30:43 +04:00
|
|
|
~nsOutputStreamReadyEvent()
|
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!mCallback) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
//
|
|
|
|
// whoa!! looks like we never posted this event. take care to
|
|
|
|
// release mCallback on the correct thread. if mTarget lives on the
|
|
|
|
// calling thread, then we are ok. otherwise, we have to try to
|
|
|
|
// proxy the Release over the right thread. if that thread is dead,
|
|
|
|
// then there's nothing we can do... better to leak than crash.
|
|
|
|
//
|
|
|
|
bool val;
|
|
|
|
nsresult rv = mTarget->IsOnCurrentThread(&val);
|
|
|
|
if (NS_FAILED(rv) || !val) {
|
|
|
|
nsCOMPtr<nsIOutputStreamCallback> event =
|
|
|
|
NS_NewOutputStreamReadyEvent(mCallback, mTarget);
|
|
|
|
mCallback = nullptr;
|
|
|
|
if (event) {
|
|
|
|
rv = event->OnOutputStreamReady(nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_NOTREACHED("leaking stream event");
|
2014-05-15 00:15:46 +04:00
|
|
|
nsISupports* sup = event;
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ADDREF(sup);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2004-01-15 09:14:18 +03:00
|
|
|
public:
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream* aStream) override
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
mStream = aStream;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
mTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("PostEvent failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
if (mCallback) {
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mStream) {
|
2014-05-05 21:30:43 +04:00
|
|
|
mCallback->OnOutputStreamReady(mStream);
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
mCallback = nullptr;
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult Cancel() override
|
2015-12-18 11:44:00 +03:00
|
|
|
{
|
|
|
|
mCallback = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
private:
|
2014-05-05 21:30:43 +04:00
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mStream;
|
|
|
|
nsCOMPtr<nsIOutputStreamCallback> mCallback;
|
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsOutputStreamReadyEvent, CancelableRunnable,
|
2016-04-11 21:40:06 +03:00
|
|
|
nsIOutputStreamCallback)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2013-04-04 11:05:18 +04:00
|
|
|
already_AddRefed<nsIInputStreamCallback>
|
2017-06-13 02:24:15 +03:00
|
|
|
NS_NewInputStreamReadyEvent(const char* aName,
|
|
|
|
nsIInputStreamCallback* aCallback,
|
2014-05-15 00:15:46 +04:00
|
|
|
nsIEventTarget* aTarget)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(aCallback, "null callback");
|
|
|
|
NS_ASSERTION(aTarget, "null target");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsInputStreamReadyEvent> ev =
|
2017-06-13 02:24:15 +03:00
|
|
|
new nsInputStreamReadyEvent(aName, aCallback, aTarget);
|
2014-05-05 21:30:43 +04:00
|
|
|
return ev.forget();
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2013-04-04 11:05:18 +04:00
|
|
|
already_AddRefed<nsIOutputStreamCallback>
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_NewOutputStreamReadyEvent(nsIOutputStreamCallback* aCallback,
|
|
|
|
nsIEventTarget* aTarget)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(aCallback, "null callback");
|
|
|
|
NS_ASSERTION(aTarget, "null target");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsOutputStreamReadyEvent> ev =
|
2014-05-15 00:15:46 +04:00
|
|
|
new nsOutputStreamReadyEvent(aCallback, aTarget);
|
2014-05-05 21:30:43 +04:00
|
|
|
return ev.forget();
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// NS_AsyncCopy implementation
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
// abstract stream copier...
|
2014-05-15 00:15:46 +04:00
|
|
|
class nsAStreamCopier
|
|
|
|
: public nsIInputStreamCallback
|
|
|
|
, public nsIOutputStreamCallback
|
2016-04-11 21:40:06 +03:00
|
|
|
, public CancelableRunnable
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsAStreamCopier()
|
2017-01-18 03:50:34 +03:00
|
|
|
: CancelableRunnable("nsAStreamCopier")
|
|
|
|
, mLock("nsAStreamCopier.mLock")
|
2014-05-05 21:30:43 +04:00
|
|
|
, mCallback(nullptr)
|
|
|
|
, mProgressCallback(nullptr)
|
|
|
|
, mClosure(nullptr)
|
|
|
|
, mChunkSize(0)
|
|
|
|
, mEventInProcess(false)
|
|
|
|
, mEventIsPending(false)
|
|
|
|
, mCloseSource(true)
|
|
|
|
, mCloseSink(true)
|
|
|
|
, mCanceled(false)
|
|
|
|
, mCancelStatus(NS_OK)
|
|
|
|
{
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// kick off the async copy...
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult Start(nsIInputStream* aSource,
|
|
|
|
nsIOutputStream* aSink,
|
|
|
|
nsIEventTarget* aTarget,
|
|
|
|
nsAsyncCopyCallbackFun aCallback,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aChunksize,
|
|
|
|
bool aCloseSource,
|
|
|
|
bool aCloseSink,
|
|
|
|
nsAsyncCopyProgressFun aProgressCallback)
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
mSource = aSource;
|
|
|
|
mSink = aSink;
|
|
|
|
mTarget = aTarget;
|
|
|
|
mCallback = aCallback;
|
|
|
|
mClosure = aClosure;
|
|
|
|
mChunkSize = aChunksize;
|
|
|
|
mCloseSource = aCloseSource;
|
|
|
|
mCloseSink = aCloseSink;
|
|
|
|
mProgressCallback = aProgressCallback;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
mAsyncSource = do_QueryInterface(mSource);
|
|
|
|
mAsyncSink = do_QueryInterface(mSink);
|
|
|
|
|
|
|
|
return PostContinuationEvent();
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// implemented by subclasses, returns number of bytes copied and
|
|
|
|
// sets source and sink condition before returning.
|
2014-05-15 00:15:46 +04:00
|
|
|
virtual uint32_t DoCopy(nsresult* aSourceCondition,
|
|
|
|
nsresult* aSinkCondition) = 0;
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
void Process()
|
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!mSource || !mSink) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsresult cancelStatus;
|
|
|
|
bool canceled;
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
canceled = mCanceled;
|
|
|
|
cancelStatus = mCancelStatus;
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2015-06-06 22:35:37 +03:00
|
|
|
// If the copy was canceled before Process() was even called, then
|
|
|
|
// sourceCondition and sinkCondition should be set to error results to
|
|
|
|
// ensure we don't call Finish() on a canceled nsISafeOutputStream.
|
|
|
|
MOZ_ASSERT(NS_FAILED(cancelStatus) == canceled, "cancel needs an error");
|
|
|
|
nsresult sourceCondition = cancelStatus;
|
|
|
|
nsresult sinkCondition = cancelStatus;
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// Copy data from the source to the sink until we hit failure or have
|
|
|
|
// copied all the data.
|
|
|
|
for (;;) {
|
|
|
|
// Note: copyFailed will be true if the source or the sink have
|
|
|
|
// reported an error, or if we failed to write any bytes
|
|
|
|
// because we have consumed all of our data.
|
|
|
|
bool copyFailed = false;
|
|
|
|
if (!canceled) {
|
|
|
|
uint32_t n = DoCopy(&sourceCondition, &sinkCondition);
|
|
|
|
if (n > 0 && mProgressCallback) {
|
|
|
|
mProgressCallback(mClosure, n);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
copyFailed = NS_FAILED(sourceCondition) ||
|
|
|
|
NS_FAILED(sinkCondition) || n == 0;
|
2003-10-06 05:46:31 +04: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
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-05 21:30:43 +04:00
|
|
|
canceled = mCanceled;
|
|
|
|
cancelStatus = mCancelStatus;
|
|
|
|
}
|
|
|
|
if (copyFailed && !canceled) {
|
|
|
|
if (sourceCondition == NS_BASE_STREAM_WOULD_BLOCK && mAsyncSource) {
|
|
|
|
// need to wait for more data from source. while waiting for
|
|
|
|
// more source data, be sure to observe failures on output end.
|
|
|
|
mAsyncSource->AsyncWait(this, 0, 0, nullptr);
|
|
|
|
|
|
|
|
if (mAsyncSink)
|
|
|
|
mAsyncSink->AsyncWait(this,
|
|
|
|
nsIAsyncOutputStream::WAIT_CLOSURE_ONLY,
|
|
|
|
0, nullptr);
|
|
|
|
break;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (sinkCondition == NS_BASE_STREAM_WOULD_BLOCK && mAsyncSink) {
|
2014-05-05 21:30:43 +04:00
|
|
|
// need to wait for more room in the sink. while waiting for
|
|
|
|
// more room in the sink, be sure to observer failures on the
|
|
|
|
// input end.
|
|
|
|
mAsyncSink->AsyncWait(this, 0, 0, nullptr);
|
|
|
|
|
|
|
|
if (mAsyncSource)
|
|
|
|
mAsyncSource->AsyncWait(this,
|
|
|
|
nsIAsyncInputStream::WAIT_CLOSURE_ONLY,
|
|
|
|
0, nullptr);
|
|
|
|
break;
|
2009-03-30 23:10:13 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
if (copyFailed || canceled) {
|
|
|
|
if (mCloseSource) {
|
|
|
|
// close source
|
|
|
|
if (mAsyncSource)
|
2014-05-15 00:15:46 +04:00
|
|
|
mAsyncSource->CloseWithStatus(
|
|
|
|
canceled ? cancelStatus : sinkCondition);
|
|
|
|
else {
|
2014-05-05 21:30:43 +04:00
|
|
|
mSource->Close();
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
mAsyncSource = nullptr;
|
|
|
|
mSource = nullptr;
|
|
|
|
|
|
|
|
if (mCloseSink) {
|
|
|
|
// close sink
|
|
|
|
if (mAsyncSink)
|
2014-05-15 00:15:46 +04:00
|
|
|
mAsyncSink->CloseWithStatus(
|
|
|
|
canceled ? cancelStatus : sourceCondition);
|
2014-05-05 21:30:43 +04:00
|
|
|
else {
|
|
|
|
// If we have an nsISafeOutputStream, and our
|
|
|
|
// sourceCondition and sinkCondition are not set to a
|
|
|
|
// failure state, finish writing.
|
|
|
|
nsCOMPtr<nsISafeOutputStream> sostream =
|
|
|
|
do_QueryInterface(mSink);
|
|
|
|
if (sostream && NS_SUCCEEDED(sourceCondition) &&
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_SUCCEEDED(sinkCondition)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
sostream->Finish();
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
mSink->Close();
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mAsyncSink = nullptr;
|
|
|
|
mSink = nullptr;
|
2009-03-30 23:10:13 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// notify state complete...
|
|
|
|
if (mCallback) {
|
|
|
|
nsresult status;
|
|
|
|
if (!canceled) {
|
|
|
|
status = sourceCondition;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_SUCCEEDED(status)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
status = sinkCondition;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
if (status == NS_BASE_STREAM_CLOSED) {
|
2014-05-05 21:30:43 +04:00
|
|
|
status = NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
} else {
|
|
|
|
status = cancelStatus;
|
|
|
|
}
|
|
|
|
mCallback(mClosure, status);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-03-30 23:10:13 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2009-03-30 23:10:13 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult Cancel(nsresult aReason)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mCanceled) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_SUCCEEDED(aReason)) {
|
|
|
|
NS_WARNING("cancel with non-failure status code");
|
|
|
|
aReason = NS_BASE_STREAM_CLOSED;
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mCanceled = true;
|
|
|
|
mCancelStatus = aReason;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream* aSource) override
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
PostContinuationEvent();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream* aSink) override
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
PostContinuationEvent();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// continuation event handler
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
Process();
|
|
|
|
|
|
|
|
// clear "in process" flag and post any pending continuation event
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
mEventInProcess = false;
|
|
|
|
if (mEventIsPending) {
|
|
|
|
mEventIsPending = false;
|
|
|
|
PostContinuationEvent_Locked();
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult Cancel() MOZ_MUST_OVERRIDE override = 0;
|
2015-12-18 11:44:00 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult PostContinuationEvent()
|
|
|
|
{
|
|
|
|
// we cannot post a continuation event if there is currently
|
|
|
|
// an event in process. doing so could result in Process being
|
|
|
|
// run simultaneously on multiple threads, so we mark the event
|
|
|
|
// as pending, and if an event is already in process then we
|
|
|
|
// just let that existing event take care of posting the real
|
|
|
|
// continuation event.
|
|
|
|
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
return PostContinuationEvent_Locked();
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult PostContinuationEvent_Locked()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mEventInProcess) {
|
2014-05-05 21:30:43 +04:00
|
|
|
mEventIsPending = true;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = mTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
mEventInProcess = true;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_WARNING("unable to post continuation event");
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
|
|
|
protected:
|
2014-05-05 21:30:43 +04:00
|
|
|
nsCOMPtr<nsIInputStream> mSource;
|
|
|
|
nsCOMPtr<nsIOutputStream> mSink;
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mAsyncSource;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mAsyncSink;
|
|
|
|
nsCOMPtr<nsIEventTarget> mTarget;
|
|
|
|
Mutex mLock;
|
|
|
|
nsAsyncCopyCallbackFun mCallback;
|
|
|
|
nsAsyncCopyProgressFun mProgressCallback;
|
2014-05-15 00:15:46 +04:00
|
|
|
void* mClosure;
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t mChunkSize;
|
|
|
|
bool mEventInProcess;
|
|
|
|
bool mEventIsPending;
|
|
|
|
bool mCloseSource;
|
|
|
|
bool mCloseSink;
|
|
|
|
bool mCanceled;
|
|
|
|
nsresult mCancelStatus;
|
2014-07-01 02:11:53 +04:00
|
|
|
|
|
|
|
// virtual since subclasses call superclass Release()
|
|
|
|
virtual ~nsAStreamCopier()
|
|
|
|
{
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(nsAStreamCopier,
|
2016-04-11 21:40:06 +03:00
|
|
|
CancelableRunnable,
|
2016-04-11 21:40:06 +03:00
|
|
|
nsIInputStreamCallback,
|
|
|
|
nsIOutputStreamCallback)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsStreamCopierIB final : public nsAStreamCopier
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStreamCopierIB() : nsAStreamCopier()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~nsStreamCopierIB()
|
|
|
|
{
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2015-05-28 15:32:00 +03:00
|
|
|
struct MOZ_STACK_CLASS ReadSegmentsState
|
2014-05-15 00:15:46 +04:00
|
|
|
{
|
2015-05-28 15:32:00 +03:00
|
|
|
// the nsIOutputStream will outlive the ReadSegmentsState on the stack
|
|
|
|
nsIOutputStream* MOZ_NON_OWNING_REF mSink;
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult mSinkCondition;
|
|
|
|
};
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult ConsumeInputBuffer(nsIInputStream* aInStr,
|
|
|
|
void* aClosure,
|
|
|
|
const char* aBuffer,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aCountWritten)
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
ReadSegmentsState* state = (ReadSegmentsState*)aClosure;
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = state->mSink->Write(aBuffer, aCount, aCountWritten);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
state->mSinkCondition = rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (*aCountWritten == 0) {
|
2014-05-05 21:30:43 +04:00
|
|
|
state->mSinkCondition = NS_BASE_STREAM_CLOSED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
return state->mSinkCondition;
|
|
|
|
}
|
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
uint32_t DoCopy(nsresult* aSourceCondition,
|
|
|
|
nsresult* aSinkCondition) override
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
ReadSegmentsState state;
|
|
|
|
state.mSink = mSink;
|
|
|
|
state.mSinkCondition = NS_OK;
|
|
|
|
|
|
|
|
uint32_t n;
|
2014-05-15 00:15:46 +04:00
|
|
|
*aSourceCondition =
|
2014-05-05 21:30:43 +04:00
|
|
|
mSource->ReadSegments(ConsumeInputBuffer, &state, mChunkSize, &n);
|
2014-05-15 00:15:46 +04:00
|
|
|
*aSinkCondition = state.mSinkCondition;
|
2014-05-05 21:30:43 +04:00
|
|
|
return n;
|
|
|
|
}
|
2015-12-18 11:44:00 +03:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult Cancel() override
|
2015-12-18 11:44:00 +03:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-10-06 05:46:31 +04:00
|
|
|
};
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsStreamCopierOB final : public nsAStreamCopier
|
2003-10-06 05:46:31 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStreamCopierOB() : nsAStreamCopier()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~nsStreamCopierOB()
|
|
|
|
{
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-05-28 15:32:00 +03:00
|
|
|
struct MOZ_STACK_CLASS WriteSegmentsState
|
2014-05-15 00:15:46 +04:00
|
|
|
{
|
2015-05-28 15:32:00 +03:00
|
|
|
// the nsIInputStream will outlive the WriteSegmentsState on the stack
|
|
|
|
nsIInputStream* MOZ_NON_OWNING_REF mSource;
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult mSourceCondition;
|
|
|
|
};
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult FillOutputBuffer(nsIOutputStream* aOutStr,
|
|
|
|
void* aClosure,
|
|
|
|
char* aBuffer,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aCountRead)
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
WriteSegmentsState* state = (WriteSegmentsState*)aClosure;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = state->mSource->Read(aBuffer, aCount, aCountRead);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
state->mSourceCondition = rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (*aCountRead == 0) {
|
2014-05-05 21:30:43 +04:00
|
|
|
state->mSourceCondition = NS_BASE_STREAM_CLOSED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
return state->mSourceCondition;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-12-18 11:44:00 +03:00
|
|
|
uint32_t DoCopy(nsresult* aSourceCondition,
|
|
|
|
nsresult* aSinkCondition) override
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
WriteSegmentsState state;
|
|
|
|
state.mSource = mSource;
|
|
|
|
state.mSourceCondition = NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t n;
|
2014-05-15 00:15:46 +04:00
|
|
|
*aSinkCondition =
|
2014-05-05 21:30:43 +04:00
|
|
|
mSink->WriteSegments(FillOutputBuffer, &state, mChunkSize, &n);
|
2014-05-15 00:15:46 +04:00
|
|
|
*aSourceCondition = state.mSourceCondition;
|
2014-05-05 21:30:43 +04:00
|
|
|
return n;
|
|
|
|
}
|
2015-12-18 11:44:00 +03:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult Cancel() override
|
2015-12-18 11:44:00 +03:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_AsyncCopy(nsIInputStream* aSource,
|
|
|
|
nsIOutputStream* aSink,
|
|
|
|
nsIEventTarget* aTarget,
|
|
|
|
nsAsyncCopyMode aMode,
|
|
|
|
uint32_t aChunkSize,
|
|
|
|
nsAsyncCopyCallbackFun aCallback,
|
|
|
|
void* aClosure,
|
|
|
|
bool aCloseSource,
|
|
|
|
bool aCloseSink,
|
|
|
|
nsISupports** aCopierCtx,
|
|
|
|
nsAsyncCopyProgressFun aProgressCallback)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(aTarget, "non-null target required");
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
nsAStreamCopier* copier;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aMode == NS_ASYNCCOPY_VIA_READSEGMENTS) {
|
2014-05-05 21:30:43 +04:00
|
|
|
copier = new nsStreamCopierIB();
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
copier = new nsStreamCopierOB();
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// Start() takes an owning ref to the copier...
|
|
|
|
NS_ADDREF(copier);
|
2014-05-15 00:15:46 +04:00
|
|
|
rv = copier->Start(aSource, aSink, aTarget, aCallback, aClosure, aChunkSize,
|
|
|
|
aCloseSource, aCloseSink, aProgressCallback);
|
2009-03-30 23:10:13 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (aCopierCtx) {
|
2014-08-25 23:17:15 +04:00
|
|
|
*aCopierCtx = static_cast<nsISupports*>(static_cast<nsIRunnable*>(copier));
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ADDREF(*aCopierCtx);
|
|
|
|
}
|
|
|
|
NS_RELEASE(copier);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2005-03-26 05:20:36 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_CancelAsyncCopy(nsISupports* aCopierCtx, nsresult aReason)
|
2009-03-30 23:10:13 +04:00
|
|
|
{
|
2014-08-25 23:17:15 +04:00
|
|
|
nsAStreamCopier* copier =
|
|
|
|
static_cast<nsAStreamCopier*>(static_cast<nsIRunnable *>(aCopierCtx));
|
2009-03-30 23:10:13 +04:00
|
|
|
return copier->Cancel(aReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2014-08-25 23:17:15 +04:00
|
|
|
NS_ConsumeStream(nsIInputStream* aStream, uint32_t aMaxCount,
|
|
|
|
nsACString& aResult)
|
2005-12-10 00:35:27 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult rv = NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
aResult.Truncate();
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
while (aMaxCount) {
|
2014-05-05 21:30:43 +04:00
|
|
|
uint64_t avail64;
|
2014-05-15 00:15:46 +04:00
|
|
|
rv = aStream->Available(&avail64);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2005-12-10 00:35:27 +03:00
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
if (avail64 == 0) {
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2005-12-10 00:35:27 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
uint32_t avail = (uint32_t)XPCOM_MIN<uint64_t>(avail64, aMaxCount);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
// resize aResult buffer
|
|
|
|
uint32_t length = aResult.Length();
|
|
|
|
if (avail > UINT32_MAX - length) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_FILE_TOO_BIG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
aResult.SetLength(length + avail);
|
|
|
|
if (aResult.Length() != (length + avail)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
char* buf = aResult.BeginWriting() + length;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
uint32_t n;
|
2014-05-15 00:15:46 +04:00
|
|
|
rv = aStream->Read(buf, avail, &n);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
if (n != avail) {
|
|
|
|
aResult.SetLength(length + n);
|
|
|
|
}
|
|
|
|
if (n == 0) {
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
aMaxCount -= n;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2005-12-10 00:35:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
TestInputStream(nsIInputStream* aInStr,
|
|
|
|
void* aClosure,
|
|
|
|
const char* aBuffer,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aCountWritten)
|
2005-03-26 05:20:36 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
bool* result = static_cast<bool*>(aClosure);
|
2014-05-05 21:30:43 +04:00
|
|
|
*result = true;
|
2017-11-17 04:54:57 +03:00
|
|
|
*aCountWritten = 0;
|
|
|
|
return NS_ERROR_ABORT; // don't call me anymore
|
2005-03-26 05:20:36 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_InputStreamIsBuffered(nsIInputStream* aStream)
|
2005-03-26 05:20:36 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
nsCOMPtr<nsIBufferedInputStream> bufferedIn = do_QueryInterface(aStream);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (bufferedIn) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-16 10:42:00 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
bool result = false;
|
|
|
|
uint32_t n;
|
2014-08-25 23:17:15 +04:00
|
|
|
nsresult rv = aStream->ReadSegments(TestInputStream, &result, 1, &n);
|
2014-05-05 21:30:43 +04:00
|
|
|
return result || NS_SUCCEEDED(rv);
|
2005-03-26 05:20:36 +03:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
TestOutputStream(nsIOutputStream* aOutStr,
|
|
|
|
void* aClosure,
|
|
|
|
char* aBuffer,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aCountRead)
|
2005-03-26 05:20:36 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
bool* result = static_cast<bool*>(aClosure);
|
2014-05-05 21:30:43 +04:00
|
|
|
*result = true;
|
2017-11-17 04:54:57 +03:00
|
|
|
*aCountRead = 0;
|
|
|
|
return NS_ERROR_ABORT; // don't call me anymore
|
2005-03-26 05:20:36 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_OutputStreamIsBuffered(nsIOutputStream* aStream)
|
2005-03-26 05:20:36 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
nsCOMPtr<nsIBufferedOutputStream> bufferedOut = do_QueryInterface(aStream);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (bufferedOut) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-16 10:42:00 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
bool result = false;
|
|
|
|
uint32_t n;
|
2014-05-15 00:15:46 +04:00
|
|
|
aStream->WriteSegments(TestOutputStream, &result, 1, &n);
|
2014-05-05 21:30:43 +04:00
|
|
|
return result;
|
2005-03-26 05:20:36 +03:00
|
|
|
}
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_CopySegmentToStream(nsIInputStream* aInStr,
|
|
|
|
void* aClosure,
|
|
|
|
const char* aBuffer,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aCountWritten)
|
2005-11-12 21:17:19 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
nsIOutputStream* outStr = static_cast<nsIOutputStream*>(aClosure);
|
|
|
|
*aCountWritten = 0;
|
|
|
|
while (aCount) {
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t n;
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = outStr->Write(aBuffer, aCount, &n);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
aBuffer += n;
|
|
|
|
aCount -= n;
|
|
|
|
*aCountWritten += n;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_CopySegmentToBuffer(nsIInputStream* aInStr,
|
|
|
|
void* aClosure,
|
|
|
|
const char* aBuffer,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aCountWritten)
|
2005-11-12 21:17:19 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
char* toBuf = static_cast<char*>(aClosure);
|
|
|
|
memcpy(&toBuf[aOffset], aBuffer, aCount);
|
|
|
|
*aCountWritten = aCount;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
2006-01-11 04:50:15 +03:00
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_CopySegmentToBuffer(nsIOutputStream* aOutStr,
|
|
|
|
void* aClosure,
|
|
|
|
char* aBuffer,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aCountRead)
|
2012-06-03 20:33:52 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
const char* fromBuf = static_cast<const char*>(aClosure);
|
|
|
|
memcpy(aBuffer, &fromBuf[aOffset], aCount);
|
|
|
|
*aCountRead = aCount;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2012-06-03 20:33:52 +04:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_DiscardSegment(nsIInputStream* aInStr,
|
|
|
|
void* aClosure,
|
|
|
|
const char* aBuffer,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aCountWritten)
|
2006-01-11 04:50:15 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
*aCountWritten = aCount;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2006-01-11 04:50:15 +03:00
|
|
|
}
|
2006-03-03 19:27:35 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_WriteSegmentThunk(nsIInputStream* aInStr,
|
|
|
|
void* aClosure,
|
|
|
|
const char* aBuffer,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aCountWritten)
|
2006-03-03 19:27:35 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
nsWriteSegmentThunk* thunk = static_cast<nsWriteSegmentThunk*>(aClosure);
|
|
|
|
return thunk->mFun(thunk->mStream, thunk->mClosure, aBuffer, aOffset, aCount,
|
|
|
|
aCountWritten);
|
2006-03-03 19:27:35 +03:00
|
|
|
}
|
2013-08-20 15:03:50 +04:00
|
|
|
|
2016-08-15 07:30:32 +03:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_FillArray(FallibleTArray<char>& aDest, nsIInputStream* aInput,
|
|
|
|
uint32_t aKeep, uint32_t* aNewBytes)
|
2013-08-20 15:03:50 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aInput, "null stream");
|
|
|
|
MOZ_ASSERT(aKeep <= aDest.Length(), "illegal keep count");
|
|
|
|
|
|
|
|
char* aBuffer = aDest.Elements();
|
|
|
|
int64_t keepOffset = int64_t(aDest.Length()) - aKeep;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aKeep != 0 && keepOffset > 0) {
|
2013-08-20 15:03:50 +04:00
|
|
|
memmove(aBuffer, aBuffer + keepOffset, aKeep);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
aInput->Read(aBuffer + aKeep, aDest.Capacity() - aKeep, aNewBytes);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*aNewBytes = 0;
|
|
|
|
}
|
|
|
|
// NOTE: we rely on the fact that the new slots are NOT initialized by
|
2013-09-08 06:05:02 +04:00
|
|
|
// SetLengthAndRetainStorage here, see nsTArrayElementTraits::Construct()
|
|
|
|
// in nsTArray.h:
|
|
|
|
aDest.SetLengthAndRetainStorage(aKeep + *aNewBytes);
|
2013-08-20 15:03:50 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(aDest.Length() <= aDest.Capacity(), "buffer overflow");
|
|
|
|
return rv;
|
|
|
|
}
|
2015-02-11 07:55:43 +03:00
|
|
|
|
2015-09-15 20:15:45 +03:00
|
|
|
bool
|
|
|
|
NS_InputStreamIsCloneable(nsIInputStream* aSource)
|
|
|
|
{
|
|
|
|
if (!aSource) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsICloneableInputStream> cloneable = do_QueryInterface(aSource);
|
|
|
|
return cloneable && cloneable->GetCloneable();
|
|
|
|
}
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
nsresult
|
|
|
|
NS_CloneInputStream(nsIInputStream* aSource, nsIInputStream** aCloneOut,
|
|
|
|
nsIInputStream** aReplacementOut)
|
|
|
|
{
|
2015-05-04 23:52:23 +03:00
|
|
|
if (NS_WARN_IF(!aSource)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
// Attempt to perform the clone directly on the source stream
|
|
|
|
nsCOMPtr<nsICloneableInputStream> cloneable = do_QueryInterface(aSource);
|
|
|
|
if (cloneable && cloneable->GetCloneable()) {
|
|
|
|
if (aReplacementOut) {
|
|
|
|
*aReplacementOut = nullptr;
|
|
|
|
}
|
|
|
|
return cloneable->Clone(aCloneOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we failed the clone and the caller does not want to replace their
|
|
|
|
// original stream, then we are done. Return error.
|
|
|
|
if (!aReplacementOut) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The caller has opted-in to the fallback clone support that replaces
|
|
|
|
// the original stream. Copy the data to a pipe and return two cloned
|
|
|
|
// input streams.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> reader;
|
|
|
|
nsCOMPtr<nsIInputStream> readerClone;
|
|
|
|
nsCOMPtr<nsIOutputStream> writer;
|
|
|
|
|
|
|
|
nsresult rv = NS_NewPipe(getter_AddRefs(reader), getter_AddRefs(writer),
|
|
|
|
0, 0, // default segment size and max size
|
|
|
|
true, true); // non-blocking
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
cloneable = do_QueryInterface(reader);
|
|
|
|
MOZ_ASSERT(cloneable && cloneable->GetCloneable());
|
|
|
|
|
|
|
|
rv = cloneable->Clone(getter_AddRefs(readerClone));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> target =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
rv = NS_AsyncCopy(aSource, writer, target, NS_ASYNCCOPY_VIA_WRITESEGMENTS);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
readerClone.forget(aCloneOut);
|
|
|
|
reader.forget(aReplacementOut);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-01-31 18:45:20 +03:00
|
|
|
|
|
|
|
nsresult
|
2018-01-31 18:45:20 +03:00
|
|
|
NS_MakeAsyncNonBlockingInputStream(already_AddRefed<nsIInputStream> aSource,
|
2018-01-31 18:45:20 +03:00
|
|
|
nsIAsyncInputStream** aAsyncInputStream)
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIInputStream> source = std::move(aSource);
|
2018-01-31 18:45:20 +03:00
|
|
|
if (NS_WARN_IF(!aAsyncInputStream)) {
|
2018-01-31 18:45:20 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nonBlocking = false;
|
2018-01-31 18:45:20 +03:00
|
|
|
nsresult rv = source->IsNonBlocking(&nonBlocking);
|
2018-01-31 18:45:20 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-01-31 18:45:20 +03:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(source);
|
2018-01-31 18:45:20 +03:00
|
|
|
|
|
|
|
if (nonBlocking && asyncStream) {
|
|
|
|
// This stream is perfect!
|
|
|
|
asyncStream.forget(aAsyncInputStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nonBlocking) {
|
|
|
|
// If the stream is non-blocking but not async, we wrap it.
|
2018-01-31 18:45:20 +03:00
|
|
|
return NonBlockingAsyncInputStream::Create(source.forget(),
|
|
|
|
aAsyncInputStream);
|
2018-01-31 18:45:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(kStreamTransportServiceCID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransport> transport;
|
2018-01-31 18:45:20 +03:00
|
|
|
rv = sts->CreateInputTransport(source,
|
2018-01-31 18:45:20 +03:00
|
|
|
/* aCloseWhenDone */ true,
|
|
|
|
getter_AddRefs(transport));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> wrapper;
|
|
|
|
rv = transport->OpenInputStream(/* aFlags */ 0,
|
|
|
|
/* aSegmentSize */ 0,
|
|
|
|
/* aSegmentCount */ 0,
|
|
|
|
getter_AddRefs(wrapper));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncStream = do_QueryInterface(wrapper);
|
|
|
|
MOZ_ASSERT(asyncStream);
|
|
|
|
|
|
|
|
asyncStream.forget(aAsyncInputStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|