netwerk/base/src/ArrayBufferInputStream.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <algorithm>
michael@0 7 #include "ArrayBufferInputStream.h"
michael@0 8 #include "nsStreamUtils.h"
michael@0 9 #include "jsapi.h"
michael@0 10 #include "jsfriendapi.h"
michael@0 11
michael@0 12 NS_IMPL_ISUPPORTS(ArrayBufferInputStream, nsIArrayBufferInputStream, nsIInputStream);
michael@0 13
michael@0 14 ArrayBufferInputStream::ArrayBufferInputStream()
michael@0 15 : mBuffer(nullptr)
michael@0 16 , mBufferLength(0)
michael@0 17 , mOffset(0)
michael@0 18 , mPos(0)
michael@0 19 , mClosed(false)
michael@0 20 {
michael@0 21 }
michael@0 22
michael@0 23 NS_IMETHODIMP
michael@0 24 ArrayBufferInputStream::SetData(JS::Handle<JS::Value> aBuffer,
michael@0 25 uint32_t aByteOffset,
michael@0 26 uint32_t aLength,
michael@0 27 JSContext* aCx)
michael@0 28 {
michael@0 29 if (!aBuffer.isObject()) {
michael@0 30 return NS_ERROR_FAILURE;
michael@0 31 }
michael@0 32 JS::RootedObject arrayBuffer(aCx, &aBuffer.toObject());
michael@0 33 if (!JS_IsArrayBufferObject(arrayBuffer)) {
michael@0 34 return NS_ERROR_FAILURE;
michael@0 35 }
michael@0 36
michael@0 37 mArrayBuffer.construct(aCx, aBuffer);
michael@0 38
michael@0 39 uint32_t buflen = JS_GetArrayBufferByteLength(arrayBuffer);
michael@0 40 mOffset = std::min(buflen, aByteOffset);
michael@0 41 mBufferLength = std::min(buflen - mOffset, aLength);
michael@0 42 mBuffer = JS_GetStableArrayBufferData(aCx, arrayBuffer);
michael@0 43 if (!mBuffer) {
michael@0 44 return NS_ERROR_FAILURE;
michael@0 45 }
michael@0 46 return NS_OK;
michael@0 47 }
michael@0 48
michael@0 49 NS_IMETHODIMP
michael@0 50 ArrayBufferInputStream::Close()
michael@0 51 {
michael@0 52 mClosed = true;
michael@0 53 return NS_OK;
michael@0 54 }
michael@0 55
michael@0 56 NS_IMETHODIMP
michael@0 57 ArrayBufferInputStream::Available(uint64_t* aCount)
michael@0 58 {
michael@0 59 if (mClosed) {
michael@0 60 return NS_BASE_STREAM_CLOSED;
michael@0 61 }
michael@0 62 *aCount = mBufferLength - mPos;
michael@0 63 return NS_OK;
michael@0 64 }
michael@0 65
michael@0 66 NS_IMETHODIMP
michael@0 67 ArrayBufferInputStream::Read(char* aBuf, uint32_t aCount, uint32_t *aReadCount)
michael@0 68 {
michael@0 69 return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aReadCount);
michael@0 70 }
michael@0 71
michael@0 72 NS_IMETHODIMP
michael@0 73 ArrayBufferInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
michael@0 74 uint32_t aCount, uint32_t *result)
michael@0 75 {
michael@0 76 NS_ASSERTION(result, "null ptr");
michael@0 77 NS_ASSERTION(mBufferLength >= mPos, "bad stream state");
michael@0 78
michael@0 79 if (mClosed) {
michael@0 80 return NS_BASE_STREAM_CLOSED;
michael@0 81 }
michael@0 82
michael@0 83 uint32_t remaining = mBufferLength - mPos;
michael@0 84 if (!mArrayBuffer.empty()) {
michael@0 85 JSObject* buf = &mArrayBuffer.ref().get().toObject();
michael@0 86 uint32_t byteLength = JS_GetArrayBufferByteLength(buf);
michael@0 87 if (byteLength == 0 && remaining != 0) {
michael@0 88 mClosed = true;
michael@0 89 return NS_BASE_STREAM_CLOSED;
michael@0 90 }
michael@0 91 } else {
michael@0 92 MOZ_ASSERT(remaining == 0, "stream inited incorrectly");
michael@0 93 }
michael@0 94
michael@0 95 if (!remaining) {
michael@0 96 *result = 0;
michael@0 97 return NS_OK;
michael@0 98 }
michael@0 99
michael@0 100 if (aCount > remaining) {
michael@0 101 aCount = remaining;
michael@0 102 }
michael@0 103 nsresult rv = writer(this, closure, (char*)(mBuffer + mOffset) + mPos,
michael@0 104 0, aCount, result);
michael@0 105 if (NS_SUCCEEDED(rv)) {
michael@0 106 NS_ASSERTION(*result <= aCount,
michael@0 107 "writer should not write more than we asked it to write");
michael@0 108 mPos += *result;
michael@0 109 }
michael@0 110
michael@0 111 return NS_OK;
michael@0 112 }
michael@0 113
michael@0 114 NS_IMETHODIMP
michael@0 115 ArrayBufferInputStream::IsNonBlocking(bool *aNonBlocking)
michael@0 116 {
michael@0 117 *aNonBlocking = true;
michael@0 118 return NS_OK;
michael@0 119 }

mercurial