2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsBaseContentStream.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsBaseContentStream::DispatchCallback(bool async)
|
2007-03-22 10:30:00 -07: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) {
|
2013-04-04 00:05:18 -07:00
|
|
|
callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
|
2012-07-30 07:20:58 -07:00
|
|
|
mCallback = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
callback.swap(mCallback);
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mCallbackTarget = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
callback->OnInputStreamReady(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseContentStream::nsISupports
|
|
|
|
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_IMPL_ADDREF(nsBaseContentStream)
|
|
|
|
NS_IMPL_RELEASE(nsBaseContentStream)
|
2007-03-22 10:30:00 -07: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-23 14:56:32 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
|
2007-03-22 10:30:00 -07: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 08:56:38 -07:00
|
|
|
nsBaseContentStream::Available(uint64_t *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*result = 0;
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsBaseContentStream::Read(char *buf, uint32_t count, uint32_t *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, buf, count, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::ReadSegments(nsWriteSegmentFun fun, void *closure,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count, uint32_t *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*result = 0;
|
|
|
|
|
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// No data yet
|
|
|
|
if (!IsClosed() && IsNonBlocking())
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsBaseContentStream::IsNonBlocking(bool *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*result = mNonBlocking;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseContentStream::nsIAsyncInputStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::CloseWithStatus(nsresult status)
|
|
|
|
{
|
|
|
|
if (IsClosed())
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(NS_FAILED(status));
|
|
|
|
mStatus = status;
|
|
|
|
|
|
|
|
DispatchCallback();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::AsyncWait(nsIInputStreamCallback *callback,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flags, uint32_t requestedCount,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIEventTarget *target)
|
|
|
|
{
|
|
|
|
// 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-28 23:19:26 -07:00
|
|
|
bool correctThread;
|
2007-03-22 10:30:00 -07:00
|
|
|
target->IsOnCurrentThread(&correctThread);
|
|
|
|
NS_ASSERTION(correctThread, "event target must be on the current thread");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mCallback = callback;
|
|
|
|
mCallbackTarget = target;
|
|
|
|
|
|
|
|
if (!mCallback)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// If we're already closed, then dispatch this callback immediately.
|
|
|
|
if (IsClosed()) {
|
|
|
|
DispatchCallback();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
OnCallbackPending();
|
|
|
|
return NS_OK;
|
|
|
|
}
|