2018-11-30 19:52:05 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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 18:17:19 +00:00
|
|
|
|
|
|
|
#include "nsBaseContentStream.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
void nsBaseContentStream::DispatchCallback(bool async) {
|
2005-11-12 18:17:19 +00: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-12 23:24:15 +00:00
|
|
|
callback = NS_NewInputStreamReadyEvent(
|
|
|
|
"nsBaseContentStream::DispatchCallback", mCallback, mCallbackTarget);
|
2012-07-30 14:20:58 +00:00
|
|
|
mCallback = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
} else {
|
|
|
|
callback.swap(mCallback);
|
|
|
|
}
|
2012-07-30 14:20:58 +00:00
|
|
|
mCallbackTarget = nullptr;
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
callback->OnInputStreamReady(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseContentStream::nsISupports
|
|
|
|
|
2013-07-19 02:24:13 +00:00
|
|
|
NS_IMPL_ADDREF(nsBaseContentStream)
|
|
|
|
NS_IMPL_RELEASE(nsBaseContentStream)
|
2005-11-12 18:17:19 +00: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 21:56:32 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
|
2018-02-12 19:36:46 +00:00
|
|
|
NS_INTERFACE_MAP_END
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseContentStream::nsIInputStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::Close() {
|
|
|
|
return IsClosed() ? NS_OK : CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsBaseContentStream::Available(uint64_t* result) {
|
2005-11-12 18:17:19 +00:00
|
|
|
*result = 0;
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
Bug 1818305 - Part 2: Add a streamStatus method to nsIInputStream, r=xpcom-reviewers,necko-reviewers,geckoview-reviewers,valentin,jesup,m_kato,mccr8
This is semantically similar to the existing available() method, however will
not block, and doesn't need to do the work to actually determine the number of
available bytes.
As part of this patch, I also fixed one available() implementation which was
incorrectly throwing NS_BASE_STREAM_WOULD_BLOCK.
Differential Revision: https://phabricator.services.mozilla.com/D170697
2023-03-15 19:52:34 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::StreamStatus() { return mStatus; }
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsBaseContentStream::Read(char* buf, uint32_t count, uint32_t* result) {
|
2005-11-12 18:17:19 +00:00
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, buf, count, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::ReadSegments(nsWriteSegmentFun fun, void* closure,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count, uint32_t* result) {
|
2005-11-12 18:17:19 +00:00
|
|
|
*result = 0;
|
2006-01-02 02:30:32 +00:00
|
|
|
|
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED) return NS_OK;
|
|
|
|
|
2006-03-03 16:27:35 +00:00
|
|
|
// No data yet
|
|
|
|
if (!IsClosed() && IsNonBlocking()) return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsBaseContentStream::IsNonBlocking(bool* result) {
|
2005-11-12 18:17:19 +00:00
|
|
|
*result = mNonBlocking;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseContentStream::nsIAsyncInputStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBaseContentStream::CloseWithStatus(nsresult status) {
|
|
|
|
if (IsClosed()) return NS_OK;
|
|
|
|
|
2006-01-02 02:30:32 +00:00
|
|
|
NS_ENSURE_ARG(NS_FAILED(status));
|
2005-11-12 18:17:19 +00:00
|
|
|
mStatus = status;
|
|
|
|
|
|
|
|
DispatchCallback();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2006-05-10 17:30:15 +00:00
|
|
|
nsBaseContentStream::AsyncWait(nsIInputStreamCallback* callback, uint32_t flags,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t requestedCount,
|
2006-05-10 17:30:15 +00:00
|
|
|
nsIEventTarget* target) {
|
2005-11-12 18:17:19 +00: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 06:19:26 +00:00
|
|
|
bool correctThread;
|
2005-11-12 18:17:19 +00:00
|
|
|
target->IsOnCurrentThread(&correctThread);
|
|
|
|
NS_ASSERTION(correctThread, "event target must be on the current thread");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mCallback = callback;
|
|
|
|
mCallbackTarget = target;
|
|
|
|
|
2006-03-03 16:27:35 +00:00
|
|
|
if (!mCallback) return NS_OK;
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
// If we're already closed, then dispatch this callback immediately.
|
2006-03-03 16:27:35 +00:00
|
|
|
if (IsClosed()) {
|
2005-11-12 18:17:19 +00:00
|
|
|
DispatchCallback();
|
2006-03-03 16:27:35 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2006-03-03 16:27:35 +00:00
|
|
|
OnCallbackPending();
|
2005-11-12 18:17:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|