DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "StreamBlobImpl.h"
#include "nsStringStream.h"
#include "nsICloneableInputStream.h"

namespace mozilla {
namespace dom {

NS_IMPL_ISUPPORTS_INHERITED(StreamBlobImpl, BlobImpl, nsIMemoryReporter)

/* static */ already_AddRefed<StreamBlobImpl> StreamBlobImpl::Create(
    nsIInputStream* aInputStream, const nsAString& aContentType,
    uint64_t aLength) {
  RefPtr<StreamBlobImpl> blobImplStream =
      new StreamBlobImpl(aInputStream, aContentType, aLength);
  blobImplStream->MaybeRegisterMemoryReporter();
  return blobImplStream.forget();
}

/* static */ already_AddRefed<StreamBlobImpl> StreamBlobImpl::Create(
    nsIInputStream* aInputStream, const nsAString& aName,
    const nsAString& aContentType, int64_t aLastModifiedDate,
    uint64_t aLength) {
  RefPtr<StreamBlobImpl> blobImplStream = new StreamBlobImpl(
      aInputStream, aName, aContentType, aLastModifiedDate, aLength);
  blobImplStream->MaybeRegisterMemoryReporter();
  return blobImplStream.forget();
}

StreamBlobImpl::StreamBlobImpl(nsIInputStream* aInputStream,
                               const nsAString& aContentType, uint64_t aLength)
    : BaseBlobImpl(aContentType, aLength),
      mInputStream(aInputStream),
      mIsDirectory(false),
      mFileId(-1) {
  mImmutable = true;
}

StreamBlobImpl::StreamBlobImpl(nsIInputStream* aInputStream,
                               const nsAString& aName,
                               const nsAString& aContentType,
                               int64_t aLastModifiedDate, uint64_t aLength)
    : BaseBlobImpl(aName, aContentType, aLength, aLastModifiedDate),
      mInputStream(aInputStream),
      mIsDirectory(false),
      mFileId(-1) {
  mImmutable = true;
}

StreamBlobImpl::~StreamBlobImpl() { UnregisterWeakMemoryReporter(this); }

void StreamBlobImpl::CreateInputStream(nsIInputStream** aStream,
                                       ErrorResult& aRv) {
  nsCOMPtr<nsIInputStream> clonedStream;
  nsCOMPtr<nsIInputStream> replacementStream;

  aRv = NS_CloneInputStream(mInputStream, getter_AddRefs(clonedStream),
                            getter_AddRefs(replacementStream));
  if (NS_WARN_IF(aRv.Failed())) {
    return;
  }

  if (replacementStream) {
    mInputStream = replacementStream.forget();
  }

  clonedStream.forget(aStream);
}

already_AddRefed<BlobImpl> StreamBlobImpl::CreateSlice(
    uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
    ErrorResult& aRv) {
  if (!aLength) {
    RefPtr<BlobImpl> impl = new EmptyBlobImpl(aContentType);
    return impl.forget();
  }

  nsCOMPtr<nsIInputStream> clonedStream;

  nsCOMPtr<nsICloneableInputStreamWithRange> stream =
      do_QueryInterface(mInputStream);
  if (stream) {
    aRv = stream->CloneWithRange(aStart, aLength, getter_AddRefs(clonedStream));
    if (NS_WARN_IF(aRv.Failed())) {
      return nullptr;
    }
  } else {
    CreateInputStream(getter_AddRefs(clonedStream), aRv);
    if (NS_WARN_IF(aRv.Failed())) {
      return nullptr;
    }

    clonedStream =
        new SlicedInputStream(clonedStream.forget(), aStart, aLength);
  }

  MOZ_ASSERT(clonedStream);

  RefPtr<BlobImpl> impl =
      new StreamBlobImpl(clonedStream, aContentType, aLength);
  return impl.forget();
}

void StreamBlobImpl::MaybeRegisterMemoryReporter() {
  // We report only stringInputStream.
  nsCOMPtr<nsIStringInputStream> stringInputStream =
      do_QueryInterface(mInputStream);
  if (!stringInputStream) {
    return;
  }

  RegisterWeakMemoryReporter(this);
}

NS_IMETHODIMP
StreamBlobImpl::CollectReports(nsIHandleReportCallback* aHandleReport,
                               nsISupports* aData, bool aAnonymize) {
  nsCOMPtr<nsIStringInputStream> stringInputStream =
      do_QueryInterface(mInputStream);
  if (!stringInputStream) {
    return NS_OK;
  }

  MOZ_COLLECT_REPORT(
      "explicit/dom/memory-file-data/stream", KIND_HEAP, UNITS_BYTES,
      stringInputStream->SizeOfIncludingThis(MallocSizeOf),
      "Memory used to back a File/Blob based on an input stream.");

  return NS_OK;
}

size_t StreamBlobImpl::GetAllocationSize() const {
  nsCOMPtr<nsIStringInputStream> stringInputStream =
      do_QueryInterface(mInputStream);
  if (!stringInputStream) {
    return 0;
  }

  return stringInputStream->SizeOfIncludingThis(MallocSizeOf);
}

}  // namespace dom
}  // namespace mozilla