1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_StreamBufferSourceImpl_h
8 #define mozilla_StreamBufferSourceImpl_h
10 #include "mozilla/StreamBufferSource.h"
16 class nsTArraySource final
: public StreamBufferSource
{
18 explicit nsTArraySource(nsTArray
<uint8_t>&& aArray
)
19 : mArray(std::move(aArray
)) {}
21 Span
<const char> Data() override
{
22 return Span
{reinterpret_cast<const char*>(mArray
.Elements()),
26 bool Owning() override
{ return true; }
28 size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf
) override
{
29 return mArray
.ShallowSizeOfExcludingThis(aMallocSizeOf
);
33 const nsTArray
<uint8_t> mArray
;
36 class nsCStringSource final
: public StreamBufferSource
{
38 explicit nsCStringSource(nsACString
&& aString
)
39 : mString(std::move(aString
)) {}
41 Span
<const char> Data() override
{ return mString
; }
43 nsresult
GetData(nsACString
& aString
) override
{
44 if (!aString
.Assign(mString
, fallible
)) {
45 return NS_ERROR_OUT_OF_MEMORY
;
50 bool Owning() override
{ return true; }
52 size_t SizeOfExcludingThisIfUnshared(MallocSizeOf aMallocSizeOf
) override
{
53 return mString
.SizeOfExcludingThisIfUnshared(aMallocSizeOf
);
56 size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf
) override
{
57 return mString
.SizeOfExcludingThisEvenIfShared(aMallocSizeOf
);
61 const nsCString mString
;
64 class nsBorrowedSource final
: public StreamBufferSource
{
66 explicit nsBorrowedSource(Span
<const char> aBuffer
) : mBuffer(aBuffer
) {}
68 Span
<const char> Data() override
{ return mBuffer
; }
70 bool Owning() override
{ return false; }
72 size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf
) override
{
77 const Span
<const char> mBuffer
;
80 } // namespace mozilla
82 #endif // mozilla_StreamBufferSourceImpl_h