Bug 1791729 - Expose StreamBufferSource implementations; r=xpcom-reviewers,nika

Differential Revision: https://phabricator.services.mozilla.com/D157791
This commit is contained in:
Jan Varga 2022-10-26 18:01:48 +00:00
parent c18827cbba
commit b03f79878a
4 changed files with 105 additions and 76 deletions

View File

@ -7,9 +7,12 @@
#ifndef mozilla_StreamBufferSource_h
#define mozilla_StreamBufferSource_h
#include <cstddef>
#include "ErrorList.h"
#include "mozilla/MemoryReporting.h"
#include "nsString.h"
#include "mozilla/Span.h"
#include "nsISupportsImpl.h"
#include "nsString.h"
namespace mozilla {

View File

@ -0,0 +1,82 @@
/* -*- 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/. */
#ifndef mozilla_StreamBufferSourceImpl_h
#define mozilla_StreamBufferSourceImpl_h
#include "mozilla/StreamBufferSource.h"
#include "nsTArray.h"
namespace mozilla {
class nsTArraySource final : public StreamBufferSource {
public:
explicit nsTArraySource(nsTArray<uint8_t>&& aArray)
: mArray(std::move(aArray)) {}
Span<const char> Data() override {
return Span{reinterpret_cast<const char*>(mArray.Elements()),
mArray.Length()};
}
bool Owning() override { return true; }
size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf) override {
return mArray.ShallowSizeOfExcludingThis(aMallocSizeOf);
}
private:
const nsTArray<uint8_t> mArray;
};
class nsCStringSource final : public StreamBufferSource {
public:
explicit nsCStringSource(nsACString&& aString)
: mString(std::move(aString)) {}
Span<const char> Data() override { return mString; }
nsresult GetData(nsACString& aString) override {
if (!aString.Assign(mString, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
bool Owning() override { return true; }
size_t SizeOfExcludingThisIfUnshared(MallocSizeOf aMallocSizeOf) override {
return mString.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
}
size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf) override {
return mString.SizeOfExcludingThisEvenIfShared(aMallocSizeOf);
}
private:
const nsCString mString;
};
class nsBorrowedSource final : public StreamBufferSource {
public:
explicit nsBorrowedSource(Span<const char> aBuffer) : mBuffer(aBuffer) {}
Span<const char> Data() override { return mBuffer; }
bool Owning() override { return false; }
size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf) override {
return 0;
}
private:
const Span<const char> mBuffer;
};
} // namespace mozilla
#endif // mozilla_StreamBufferSourceImpl_h

View File

@ -103,6 +103,7 @@ EXPORTS.mozilla += [
"StreamBufferSink.h",
"StreamBufferSinkImpl.h",
"StreamBufferSource.h",
"StreamBufferSourceImpl.h",
]
UNIFIED_SOURCES += [

View File

@ -24,7 +24,7 @@
#include "mozilla/ipc/InputStreamUtils.h"
#include "mozilla/Maybe.h"
#include "mozilla/ReentrantMonitor.h"
#include "mozilla/StreamBufferSource.h"
#include "mozilla/StreamBufferSourceImpl.h"
#include "nsIIPCSerializableInputStream.h"
#include "XPCOMModule.h"
@ -32,74 +32,13 @@ using namespace mozilla::ipc;
using mozilla::fallible;
using mozilla::MakeRefPtr;
using mozilla::MallocSizeOf;
using mozilla::nsBorrowedSource;
using mozilla::nsCStringSource;
using mozilla::nsTArraySource;
using mozilla::ReentrantMonitorAutoEnter;
using mozilla::Span;
using mozilla::StreamBufferSource;
//-----------------------------------------------------------------------------
// StreamBufferSource implementations
//-----------------------------------------------------------------------------
class nsTArraySource final : public StreamBufferSource {
public:
explicit nsTArraySource(nsTArray<uint8_t>&& aArray)
: mArray(std::move(aArray)) {}
Span<const char> Data() override {
return Span{reinterpret_cast<const char*>(mArray.Elements()),
mArray.Length()};
}
bool Owning() override { return true; }
size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf) override {
return mArray.ShallowSizeOfExcludingThis(aMallocSizeOf);
}
nsTArray<uint8_t> mArray;
};
class nsCStringSource final : public StreamBufferSource {
public:
nsCStringSource() = default;
Span<const char> Data() override { return mString; }
nsresult GetData(nsACString& aString) override {
if (!aString.Assign(mString, mozilla::fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
return NS_OK;
}
bool Owning() override { return true; }
size_t SizeOfExcludingThisIfUnshared(MallocSizeOf aMallocSizeOf) override {
return mString.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
}
size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf) override {
return mString.SizeOfExcludingThisEvenIfShared(aMallocSizeOf);
}
nsCString mString;
};
class nsBorrowedSource final : public StreamBufferSource {
public:
explicit nsBorrowedSource(Span<const char> aBuffer) : mBuffer(aBuffer) {}
Span<const char> Data() override { return mBuffer; }
bool Owning() override { return false; }
size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf) override {
return 0;
}
Span<const char> mBuffer;
};
//-----------------------------------------------------------------------------
// nsIStringInputStream implementation
//-----------------------------------------------------------------------------
@ -148,10 +87,11 @@ class nsStringInputStream final : public nsIStringInputStream,
};
nsresult nsStringInputStream::Init(nsCString&& aString) {
auto source = MakeRefPtr<nsCStringSource>();
if (!source->mString.Assign(std::move(aString), fallible)) {
nsCString string;
if (!string.Assign(std::move(aString), fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
auto source = MakeRefPtr<nsCStringSource>(std::move(string));
return SetDataSource(source);
}
@ -203,10 +143,11 @@ nsStringInputStream::GetData(nsACString& data) {
NS_IMETHODIMP
nsStringInputStream::SetData(const nsACString& aData) {
auto source = MakeRefPtr<nsCStringSource>();
if (!source->mString.Assign(aData, fallible)) {
nsCString string;
if (!string.Assign(aData, fallible)) {
return NS_ERROR_OUT_OF_MEMORY;
}
auto source = MakeRefPtr<nsCStringSource>(std::move(string));
return SetDataSource(source);
}
@ -226,10 +167,11 @@ nsStringInputStream::SetData(const char* aData, int32_t aDataLen) {
return NS_ERROR_INVALID_ARG;
}
auto source = MakeRefPtr<nsCStringSource>();
if (NS_WARN_IF(!source->mString.Assign(aData, aDataLen, fallible))) {
nsCString string;
if (NS_WARN_IF(!string.Assign(aData, aDataLen, fallible))) {
return NS_ERROR_OUT_OF_MEMORY;
}
auto source = MakeRefPtr<nsCStringSource>(std::move(string));
return SetDataSource(source);
}
@ -244,8 +186,9 @@ nsStringInputStream::AdoptData(char* aData, int32_t aDataLen) {
return NS_ERROR_INVALID_ARG;
}
auto source = MakeRefPtr<nsCStringSource>();
source->mString.Adopt(aData, aDataLen);
nsCString string;
string.Adopt(aData, aDataLen);
auto source = MakeRefPtr<nsCStringSource>(std::move(string));
return SetDataSource(source);
}
@ -476,8 +419,8 @@ void nsStringInputStream::Serialize(InputStreamParams& aParams,
// `ShareData`), create a new owning source so that it doesn't go away while
// async copying.
if (!mSource->Owning()) {
auto source = MakeRefPtr<nsCStringSource>();
source->mString.Assign(nsDependentCSubstring{mSource->Data()});
auto source =
MakeRefPtr<nsCStringSource>(nsDependentCSubstring(mSource->Data()));
mSource = source;
}