2005-11-12 21:17:19 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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/. */
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
#include "nsBaseContentStream.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsBaseContentStream::DispatchCallback(bool async)
|
2005-11-12 21:17:19 +03:00
|
|
|
{
|
|
|
|
if (!mCallback)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// It's important to clear mCallback and mCallbackTarget up-front because the
|
|
|
|
// OnInputStreamReady implementation may call our AsyncWait method.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback;
|
|
|
|
if (async) {
|
2017-06-13 02:24:15 +03:00
|
|
|
callback = NS_NewInputStreamReadyEvent("nsBaseContentStream::DispatchCallback",
|
|
|
|
mCallback, mCallbackTarget);
|
2012-07-30 18:20:58 +04:00
|
|
|
mCallback = nullptr;
|
2005-11-12 21:17:19 +03:00
|
|
|
} else {
|
|
|
|
callback.swap(mCallback);
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mCallbackTarget = nullptr;
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
callback->OnInputStreamReady(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseContentStream::nsISupports
|
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_IMPL_ADDREF(nsBaseContentStream)
|
|
|
|
NS_IMPL_RELEASE(nsBaseContentStream)
|
2005-11-12 21:17:19 +03:00
|
|
|
|
|
|
|
// We only support nsIAsyncInputStream when we are in non-blocking mode.
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsBaseContentStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStream, mNonBlocking)
|
2007-03-24 00:56:32 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseContentStream::nsIInputStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::Close()
|
|
|
|
{
|
|
|
|
return IsClosed() ? NS_OK : CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBaseContentStream::Available(uint64_t *result)
|
2005-11-12 21:17:19 +03:00
|
|
|
{
|
|
|
|
*result = 0;
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsBaseContentStream::Read(char *buf, uint32_t count, uint32_t *result)
|
2005-11-12 21:17:19 +03:00
|
|
|
{
|
2017-07-06 15:00:35 +03:00
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, buf, count, result);
|
2005-11-12 21:17:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::ReadSegments(nsWriteSegmentFun fun, void *closure,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count, uint32_t *result)
|
2005-11-12 21:17:19 +03:00
|
|
|
{
|
|
|
|
*result = 0;
|
2006-01-02 05:30:32 +03:00
|
|
|
|
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED)
|
|
|
|
return NS_OK;
|
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
// No data yet
|
|
|
|
if (!IsClosed() && IsNonBlocking())
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsBaseContentStream::IsNonBlocking(bool *result)
|
2005-11-12 21:17:19 +03:00
|
|
|
{
|
|
|
|
*result = mNonBlocking;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseContentStream::nsIAsyncInputStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::CloseWithStatus(nsresult status)
|
|
|
|
{
|
|
|
|
if (IsClosed())
|
|
|
|
return NS_OK;
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_ENSURE_ARG(NS_FAILED(status));
|
2005-11-12 21:17:19 +03:00
|
|
|
mStatus = status;
|
|
|
|
|
|
|
|
DispatchCallback();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2006-05-10 21:30:15 +04:00
|
|
|
nsBaseContentStream::AsyncWait(nsIInputStreamCallback *callback,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags, uint32_t requestedCount,
|
2006-05-10 21:30:15 +04:00
|
|
|
nsIEventTarget *target)
|
2005-11-12 21:17:19 +03:00
|
|
|
{
|
|
|
|
// Our _only_ consumer is nsInputStreamPump, so we simplify things here by
|
|
|
|
// making assumptions about how we will be called.
|
|
|
|
NS_ASSERTION(target, "unexpected parameter");
|
|
|
|
NS_ASSERTION(flags == 0, "unexpected parameter");
|
|
|
|
NS_ASSERTION(requestedCount == 0, "unexpected parameter");
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
bool correctThread;
|
2005-11-12 21:17:19 +03:00
|
|
|
target->IsOnCurrentThread(&correctThread);
|
|
|
|
NS_ASSERTION(correctThread, "event target must be on the current thread");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mCallback = callback;
|
|
|
|
mCallbackTarget = target;
|
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
if (!mCallback)
|
|
|
|
return NS_OK;
|
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
// If we're already closed, then dispatch this callback immediately.
|
2006-03-03 19:27:35 +03:00
|
|
|
if (IsClosed()) {
|
2005-11-12 21:17:19 +03:00
|
|
|
DispatchCallback();
|
2006-03-03 19:27:35 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2005-11-12 21:17:19 +03:00
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
OnCallbackPending();
|
2005-11-12 21:17:19 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|