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"
|
2019-03-05 02:19:16 +03:00
|
|
|
#include "js/ArrayBuffer.h" // JS::{GetArrayBuffer{ByteLength,Data},IsArrayBufferObject}
|
|
|
|
#include "js/RootingAPI.h" // JS::{Handle,Rooted}
|
|
|
|
#include "js/Value.h" // JS::Value
|
2017-03-24 09:59:00 +03:00
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2018-08-09 11:48:29 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
|
|
|
|
|
|
|
using mozilla::dom::RootingCx;
|
2013-04-19 13:25:33 +04:00
|
|
|
|
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), mPos(0), mClosed(false) {}
|
2013-04-19 13:25:33 +04:00
|
|
|
|
|
|
|
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) {
|
2018-01-19 15:31:18 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(ArrayBufferInputStream);
|
|
|
|
|
2013-04-19 13:25:33 +04:00
|
|
|
if (!aBuffer.isObject()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2019-05-01 11:47:10 +03:00
|
|
|
JS::Rooted<JSObject*> arrayBuffer(RootingCx(), &aBuffer.toObject());
|
2019-03-05 02:19:16 +03:00
|
|
|
if (!JS::IsArrayBufferObject(arrayBuffer)) {
|
2013-04-19 13:25:33 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-04-22 21:46:40 +04:00
|
|
|
|
2019-03-05 02:19:16 +03:00
|
|
|
uint32_t buflen = JS::GetArrayBufferByteLength(arrayBuffer);
|
2016-11-01 00:48:04 +03:00
|
|
|
uint32_t offset = std::min(buflen, aByteOffset);
|
2017-03-24 09:59:00 +03:00
|
|
|
uint32_t bufferLength = std::min(buflen - offset, aLength);
|
2016-11-01 00:48:04 +03:00
|
|
|
|
2017-03-24 09:59:00 +03:00
|
|
|
mArrayBuffer = mozilla::MakeUniqueFallible<char[]>(bufferLength);
|
|
|
|
if (!mArrayBuffer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBufferLength = bufferLength;
|
2016-11-01 00:48:04 +03:00
|
|
|
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
|
|
|
bool isShared;
|
2019-05-01 11:47:10 +03:00
|
|
|
char* src =
|
|
|
|
(char*)JS::GetArrayBufferData(arrayBuffer, &isShared, nogc) + offset;
|
2016-11-01 00:48:04 +03:00
|
|
|
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
|
2019-05-01 11:47:10 +03:00
|
|
|
ArrayBufferInputStream::Available(uint64_t* aCount) {
|
2013-04-19 13:25:33 +04:00
|
|
|
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
|
2019-05-01 11:47:10 +03:00
|
|
|
ArrayBufferInputStream::Read(char* aBuf, uint32_t aCount,
|
|
|
|
uint32_t* aReadCount) {
|
2013-04-19 13:25:33 +04:00
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aReadCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
ArrayBufferInputStream::ReadSegments(nsWriteSegmentFun writer, void* closure,
|
|
|
|
uint32_t aCount, uint32_t* result) {
|
2013-04-19 13:25:33 +04:00
|
|
|
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
|
2019-05-01 11:47:10 +03:00
|
|
|
ArrayBufferInputStream::IsNonBlocking(bool* aNonBlocking) {
|
2013-04-19 13:25:33 +04:00
|
|
|
*aNonBlocking = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|