Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / xpcom / io / StreamBufferSourceImpl.h
blob0d04adcc248414a1925a45ef1651146ca7c42d37
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"
12 #include "nsTArray.h"
14 namespace mozilla {
16 class nsTArraySource final : public StreamBufferSource {
17 public:
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()),
23 mArray.Length()};
26 bool Owning() override { return true; }
28 size_t SizeOfExcludingThisEvenIfShared(MallocSizeOf aMallocSizeOf) override {
29 return mArray.ShallowSizeOfExcludingThis(aMallocSizeOf);
32 private:
33 const nsTArray<uint8_t> mArray;
36 class nsCStringSource final : public StreamBufferSource {
37 public:
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;
47 return NS_OK;
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);
60 private:
61 const nsCString mString;
64 class nsBorrowedSource final : public StreamBufferSource {
65 public:
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 {
73 return 0;
76 private:
77 const Span<const char> mBuffer;
80 } // namespace mozilla
82 #endif // mozilla_StreamBufferSourceImpl_h