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
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "StringBlobImpl.h"
8 #include "nsStringStream.h"
9 
10 namespace mozilla::dom {
11 
NS_IMPL_ISUPPORTS_INHERITED(StringBlobImpl,BlobImpl,nsIMemoryReporter)12 NS_IMPL_ISUPPORTS_INHERITED(StringBlobImpl, BlobImpl, nsIMemoryReporter)
13 
14 /* static */
15 already_AddRefed<StringBlobImpl> StringBlobImpl::Create(
16     const nsACString& aData, const nsAString& aContentType) {
17   RefPtr<StringBlobImpl> blobImpl = new StringBlobImpl(aData, aContentType);
18   RegisterWeakMemoryReporter(blobImpl);
19   return blobImpl.forget();
20 }
21 
StringBlobImpl(const nsACString & aData,const nsAString & aContentType)22 StringBlobImpl::StringBlobImpl(const nsACString& aData,
23                                const nsAString& aContentType)
24     : BaseBlobImpl(aContentType, aData.Length()), mData(aData) {}
25 
~StringBlobImpl()26 StringBlobImpl::~StringBlobImpl() { UnregisterWeakMemoryReporter(this); }
27 
CreateSlice(uint64_t aStart,uint64_t aLength,const nsAString & aContentType,ErrorResult & aRv)28 already_AddRefed<BlobImpl> StringBlobImpl::CreateSlice(
29     uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
30     ErrorResult& aRv) {
31   RefPtr<BlobImpl> impl =
32       new StringBlobImpl(Substring(mData, aStart, aLength), aContentType);
33   return impl.forget();
34 }
35 
CreateInputStream(nsIInputStream ** aStream,ErrorResult & aRv)36 void StringBlobImpl::CreateInputStream(nsIInputStream** aStream,
37                                        ErrorResult& aRv) {
38   aRv = NS_NewCStringInputStream(aStream, mData);
39 }
40 
41 NS_IMETHODIMP
CollectReports(nsIHandleReportCallback * aHandleReport,nsISupports * aData,bool aAnonymize)42 StringBlobImpl::CollectReports(nsIHandleReportCallback* aHandleReport,
43                                nsISupports* aData, bool aAnonymize) {
44   MOZ_COLLECT_REPORT("explicit/dom/memory-file-data/string", KIND_HEAP,
45                      UNITS_BYTES,
46                      mData.SizeOfExcludingThisIfUnshared(MallocSizeOf),
47                      "Memory used to back a File/Blob based on a string.");
48   return NS_OK;
49 }
50 
51 }  // namespace mozilla::dom
52