2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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/. */
|
2011-05-20 03:43:37 +04:00
|
|
|
|
|
|
|
#include "nsPreloadedStream.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
|
|
|
|
#include "nsThreadUtils.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-05-20 03:43:37 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsPreloadedStream, nsIInputStream, nsIAsyncInputStream)
|
2011-05-20 03:43:37 +04:00
|
|
|
|
|
|
|
nsPreloadedStream::nsPreloadedStream(nsIAsyncInputStream *aStream,
|
2012-08-22 19:56:38 +04:00
|
|
|
const char *data, uint32_t datalen)
|
2011-05-20 03:43:37 +04:00
|
|
|
: mStream(aStream), mOffset(0), mLen(datalen) {
|
|
|
|
mBuf = (char *)moz_xmalloc(datalen);
|
|
|
|
memcpy(mBuf, data, datalen);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPreloadedStream::~nsPreloadedStream() { free(mBuf); }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPreloadedStream::Close() {
|
|
|
|
mLen = 0;
|
|
|
|
return mStream->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPreloadedStream::Available(uint64_t *_retval) {
|
|
|
|
uint64_t avail = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-05-20 03:43:37 +04:00
|
|
|
nsresult rv = mStream->Available(&avail);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
*_retval = avail + mLen;
|
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-05-20 03:43:37 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPreloadedStream::Read(char *aBuf, uint32_t aCount, uint32_t *_retval) {
|
2011-05-20 03:43:37 +04:00
|
|
|
if (!mLen) return mStream->Read(aBuf, aCount, _retval);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-05-20 03:43:37 +04:00
|
|
|
uint32_t toRead = std::min(mLen, aCount);
|
|
|
|
memcpy(aBuf, mBuf + mOffset, toRead);
|
|
|
|
mOffset += toRead;
|
|
|
|
mLen -= toRead;
|
|
|
|
*_retval = toRead;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsPreloadedStream::ReadSegments(nsWriteSegmentFun aWriter, void *aClosure,
|
|
|
|
uint32_t aCount, uint32_t *result) {
|
2011-05-20 03:43:37 +04:00
|
|
|
if (!mLen) return mStream->ReadSegments(aWriter, aClosure, aCount, result);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-05-20 03:43:37 +04:00
|
|
|
*result = 0;
|
|
|
|
while (mLen > 0 && aCount > 0) {
|
2013-01-15 16:22:03 +04:00
|
|
|
uint32_t toRead = std::min(mLen, aCount);
|
2011-05-20 03:43:37 +04:00
|
|
|
uint32_t didRead = 0;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = aWriter(this, aClosure, mBuf + mOffset, *result, toRead, &didRead);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-05-20 03:43:37 +04:00
|
|
|
*result += didRead;
|
|
|
|
mOffset += didRead;
|
|
|
|
mLen -= didRead;
|
|
|
|
aCount -= didRead;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2011-05-20 03:43:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-07-07 00:14:07 +04:00
|
|
|
nsPreloadedStream::IsNonBlocking(bool *_retval) {
|
2011-05-20 03:43:37 +04:00
|
|
|
return mStream->IsNonBlocking(_retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPreloadedStream::CloseWithStatus(nsresult aStatus) {
|
|
|
|
mLen = 0;
|
|
|
|
return mStream->CloseWithStatus(aStatus);
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class RunOnThread : public Runnable {
|
2011-05-20 03:43:37 +04:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
RunOnThread(nsIAsyncInputStream *aStream, nsIInputStreamCallback *aCallback)
|
|
|
|
: Runnable("net::RunOnThread"), mStream(aStream), mCallback(aCallback) {}
|
|
|
|
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~RunOnThread() = default;
|
2017-06-12 22:34:10 +03:00
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
mCallback->OnInputStreamReady(mStream);
|
|
|
|
return NS_OK;
|
2011-05-20 03:43:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mStream;
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPreloadedStream::AsyncWait(nsIInputStreamCallback *aCallback, uint32_t aFlags,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aRequestedCount,
|
2011-05-20 03:43:37 +04:00
|
|
|
nsIEventTarget *aEventTarget) {
|
|
|
|
if (!mLen)
|
|
|
|
return mStream->AsyncWait(aCallback, aFlags, aRequestedCount, aEventTarget);
|
|
|
|
|
2011-06-25 18:49:42 +04:00
|
|
|
if (!aCallback) return NS_OK;
|
|
|
|
|
2011-05-20 03:43:37 +04:00
|
|
|
if (!aEventTarget) return aCallback->OnInputStreamReady(this);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> event = new RunOnThread(this, aCallback);
|
|
|
|
return aEventTarget->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|