netwerk/base/ArrayBufferInputStream.cpp
author Andreea Pavel <apavel@mozilla.com>
Wed, 18 Sep 2019 13:53:34 +0300
changeset 493713 a3a081ae714f1123bdc23c9d9ef53dfaa783a8de
parent 472056 e1993a1f09ac53cd1a04fdf6a87f8cad8e44f73e
permissions -rw-r--r--
Backed out 9 changesets (bug 1578661) for lints failure at ServoCSSPropList.py a=backout Backed out changeset d16463e5698c (bug 1578661) Backed out changeset c6d64ac858ba (bug 1578661) Backed out changeset db306f1467f7 (bug 1578661) Backed out changeset 273535aab82d (bug 1578661) Backed out changeset f643262a8c25 (bug 1578661) Backed out changeset b0db409ada96 (bug 1578661) Backed out changeset dc96d13728e0 (bug 1578661) Backed out changeset 11e1e8f0a1b7 (bug 1578661) Backed out changeset 6dd7a0d914d9 (bug 1578661)

/* -*- 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"
#include "js/ArrayBuffer.h"  // JS::{GetArrayBuffer{ByteLength,Data},IsArrayBufferObject}
#include "js/RootingAPI.h"  // JS::{Handle,Rooted}
#include "js/Value.h"       // JS::Value
#include "mozilla/UniquePtrExtensions.h"
#include "mozilla/dom/ScriptSettings.h"

using mozilla::dom::RootingCx;

NS_IMPL_ISUPPORTS(ArrayBufferInputStream, nsIArrayBufferInputStream,
                  nsIInputStream);

ArrayBufferInputStream::ArrayBufferInputStream()
    : mBufferLength(0), mPos(0), mClosed(false) {}

NS_IMETHODIMP
ArrayBufferInputStream::SetData(JS::Handle<JS::Value> aBuffer,
                                uint32_t aByteOffset, uint32_t aLength) {
  NS_ASSERT_OWNINGTHREAD(ArrayBufferInputStream);

  if (!aBuffer.isObject()) {
    return NS_ERROR_FAILURE;
  }
  JS::Rooted<JSObject*> arrayBuffer(RootingCx(), &aBuffer.toObject());
  if (!JS::IsArrayBufferObject(arrayBuffer)) {
    return NS_ERROR_FAILURE;
  }

  uint32_t buflen = JS::GetArrayBufferByteLength(arrayBuffer);
  uint32_t offset = std::min(buflen, aByteOffset);
  uint32_t bufferLength = std::min(buflen - offset, aLength);

  mArrayBuffer = mozilla::MakeUniqueFallible<char[]>(bufferLength);
  if (!mArrayBuffer) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  mBufferLength = bufferLength;

  JS::AutoCheckCannotGC nogc;
  bool isShared;
  char* src =
      (char*)JS::GetArrayBufferData(arrayBuffer, &isShared, nogc) + offset;
  memcpy(&mArrayBuffer[0], src, mBufferLength);
  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;
  }
  if (mArrayBuffer) {
    *aCount = mBufferLength ? mBufferLength - mPos : 0;
  } else {
    *aCount = 0;
  }
  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");
  NS_ASSERTION(mBufferLength >= mPos, "bad stream state");

  if (mClosed) {
    return NS_BASE_STREAM_CLOSED;
  }

  MOZ_ASSERT(mArrayBuffer || (mPos == mBufferLength),
             "stream inited incorrectly");

  *result = 0;
  while (mPos < mBufferLength) {
    uint32_t remaining = mBufferLength - mPos;
    MOZ_ASSERT(mArrayBuffer);

    uint32_t count = std::min(aCount, remaining);
    if (count == 0) {
      break;
    }

    uint32_t written;
    nsresult rv = writer(this, closure, &mArrayBuffer[0] + mPos, *result, count,
                         &written);
    if (NS_FAILED(rv)) {
      // InputStreams do not propagate errors to caller.
      return NS_OK;
    }

    NS_ASSERTION(written <= count,
                 "writer should not write more than we asked it to write");
    mPos += written;
    *result += written;
    aCount -= written;
  }

  return NS_OK;
}

NS_IMETHODIMP
ArrayBufferInputStream::IsNonBlocking(bool* aNonBlocking) {
  *aNonBlocking = true;
  return NS_OK;
}