2013-04-19 13:25:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include "ArrayBufferInputStream.h"
|
|
|
|
#include "nsStreamUtils.h"
|
2013-08-22 09:28:12 +04:00
|
|
|
#include "jsapi.h"
|
2013-04-19 13:25:33 +04:00
|
|
|
#include "jsfriendapi.h"
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ArrayBufferInputStream, nsIArrayBufferInputStream, nsIInputStream);
|
2013-04-19 13:25:33 +04:00
|
|
|
|
|
|
|
ArrayBufferInputStream::ArrayBufferInputStream()
|
2014-10-07 21:44:07 +04:00
|
|
|
: mBufferLength(0)
|
2013-05-07 20:46:09 +04:00
|
|
|
, mPos(0)
|
2013-04-19 13:25:33 +04:00
|
|
|
, mClosed(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 21:39:36 +04:00
|
|
|
ArrayBufferInputStream::SetData(JS::Handle<JS::Value> aBuffer,
|
2013-04-19 13:25:33 +04:00
|
|
|
uint32_t aByteOffset,
|
|
|
|
uint32_t aLength,
|
|
|
|
JSContext* aCx)
|
|
|
|
{
|
|
|
|
if (!aBuffer.isObject()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-04-22 21:46:40 +04:00
|
|
|
JS::RootedObject arrayBuffer(aCx, &aBuffer.toObject());
|
|
|
|
if (!JS_IsArrayBufferObject(arrayBuffer)) {
|
2013-04-19 13:25:33 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-04-22 21:46:40 +04:00
|
|
|
|
|
|
|
uint32_t buflen = JS_GetArrayBufferByteLength(arrayBuffer);
|
2016-11-01 00:48:04 +03:00
|
|
|
uint32_t offset = std::min(buflen, aByteOffset);
|
|
|
|
mBufferLength = std::min(buflen - offset, aLength);
|
|
|
|
|
|
|
|
mArrayBuffer = mozilla::MakeUnique<char[]>(mBufferLength);
|
|
|
|
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
|
|
|
bool isShared;
|
|
|
|
char* src = (char*) JS_GetArrayBufferData(arrayBuffer, &isShared, nogc) + offset;
|
|
|
|
memcpy(&mArrayBuffer[0], src, mBufferLength);
|
2013-04-19 13:25:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ArrayBufferInputStream::Close()
|
|
|
|
{
|
|
|
|
mClosed = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ArrayBufferInputStream::Available(uint64_t* aCount)
|
|
|
|
{
|
|
|
|
if (mClosed) {
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
2014-10-14 00:42:44 +04:00
|
|
|
if (mArrayBuffer) {
|
2016-11-01 00:48:04 +03:00
|
|
|
*aCount = mBufferLength ? mBufferLength - mPos : 0;
|
2014-10-14 00:42:44 +04:00
|
|
|
} else {
|
|
|
|
*aCount = 0;
|
|
|
|
}
|
2013-04-19 13:25:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ArrayBufferInputStream::Read(char* aBuf, uint32_t aCount, uint32_t *aReadCount)
|
|
|
|
{
|
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aReadCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ArrayBufferInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
|
|
|
|
uint32_t aCount, uint32_t *result)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(result, "null ptr");
|
2013-05-07 20:46:09 +04:00
|
|
|
NS_ASSERTION(mBufferLength >= mPos, "bad stream state");
|
2013-04-19 13:25:33 +04:00
|
|
|
|
|
|
|
if (mClosed) {
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:44:07 +04:00
|
|
|
MOZ_ASSERT(mArrayBuffer || (mPos == mBufferLength), "stream inited incorrectly");
|
|
|
|
|
|
|
|
*result = 0;
|
|
|
|
while (mPos < mBufferLength) {
|
|
|
|
uint32_t remaining = mBufferLength - mPos;
|
|
|
|
MOZ_ASSERT(mArrayBuffer);
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
|
2016-11-01 00:48:04 +03:00
|
|
|
uint32_t count = std::min(aCount, remaining);
|
2014-10-07 21:44:07 +04:00
|
|
|
if (count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2013-04-19 13:25:33 +04:00
|
|
|
|
2014-10-07 21:44:07 +04:00
|
|
|
uint32_t written;
|
2016-11-01 00:48:04 +03:00
|
|
|
nsresult rv = writer(this, closure, &mArrayBuffer[0] + mPos, *result, count, &written);
|
2014-10-07 21:44:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// InputStreams do not propagate errors to caller.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(written <= count,
|
2013-04-19 13:25:33 +04:00
|
|
|
"writer should not write more than we asked it to write");
|
2014-10-07 21:44:07 +04:00
|
|
|
mPos += written;
|
|
|
|
*result += written;
|
|
|
|
aCount -= written;
|
2013-04-19 13:25:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ArrayBufferInputStream::IsNonBlocking(bool *aNonBlocking)
|
|
|
|
{
|
|
|
|
*aNonBlocking = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|