Line data Source code
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 "StreamBlobImpl.h"
8 : #include "nsStringStream.h"
9 :
10 : namespace mozilla {
11 : namespace dom {
12 :
13 0 : NS_IMPL_ISUPPORTS_INHERITED(StreamBlobImpl, BlobImpl, nsIMemoryReporter)
14 :
15 : /* static */ already_AddRefed<StreamBlobImpl>
16 0 : StreamBlobImpl::Create(nsIInputStream* aInputStream,
17 : const nsAString& aContentType,
18 : uint64_t aLength)
19 : {
20 : RefPtr<StreamBlobImpl> blobImplStream =
21 0 : new StreamBlobImpl(aInputStream, aContentType, aLength);
22 0 : blobImplStream->MaybeRegisterMemoryReporter();
23 0 : return blobImplStream.forget();
24 : }
25 :
26 : /* static */ already_AddRefed<StreamBlobImpl>
27 0 : StreamBlobImpl::Create(nsIInputStream* aInputStream,
28 : const nsAString& aName,
29 : const nsAString& aContentType,
30 : int64_t aLastModifiedDate,
31 : uint64_t aLength)
32 : {
33 : RefPtr<StreamBlobImpl> blobImplStream =
34 : new StreamBlobImpl(aInputStream, aName, aContentType, aLastModifiedDate,
35 0 : aLength);
36 0 : blobImplStream->MaybeRegisterMemoryReporter();
37 0 : return blobImplStream.forget();
38 : }
39 :
40 0 : StreamBlobImpl::StreamBlobImpl(nsIInputStream* aInputStream,
41 : const nsAString& aContentType,
42 0 : uint64_t aLength)
43 : : BaseBlobImpl(aContentType, aLength)
44 : , mInputStream(aInputStream)
45 : , mIsDirectory(false)
46 0 : , mFileId(-1)
47 : {
48 0 : mImmutable = true;
49 0 : }
50 :
51 0 : StreamBlobImpl::StreamBlobImpl(StreamBlobImpl* aOther,
52 : const nsAString& aContentType,
53 0 : uint64_t aStart, uint64_t aLength)
54 0 : : BaseBlobImpl(aContentType, aOther->mStart + aStart, aLength)
55 0 : , mInputStream(new SlicedInputStream(aOther->mInputStream, aStart, aLength))
56 : , mIsDirectory(false)
57 0 : , mFileId(-1)
58 : {
59 0 : mImmutable = true;
60 0 : }
61 :
62 0 : StreamBlobImpl::StreamBlobImpl(nsIInputStream* aInputStream,
63 : const nsAString& aName,
64 : const nsAString& aContentType,
65 : int64_t aLastModifiedDate,
66 0 : uint64_t aLength)
67 : : BaseBlobImpl(aName, aContentType, aLength, aLastModifiedDate)
68 : , mInputStream(aInputStream)
69 : , mIsDirectory(false)
70 0 : , mFileId(-1)
71 : {
72 0 : mImmutable = true;
73 0 : }
74 :
75 0 : StreamBlobImpl::~StreamBlobImpl()
76 : {
77 0 : UnregisterWeakMemoryReporter(this);
78 0 : }
79 :
80 : void
81 0 : StreamBlobImpl::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
82 : {
83 0 : nsCOMPtr<nsIInputStream> clonedStream;
84 0 : nsCOMPtr<nsIInputStream> replacementStream;
85 :
86 0 : aRv = NS_CloneInputStream(mInputStream, getter_AddRefs(clonedStream),
87 0 : getter_AddRefs(replacementStream));
88 0 : if (NS_WARN_IF(aRv.Failed())) {
89 0 : return;
90 : }
91 :
92 0 : if (replacementStream) {
93 0 : mInputStream = replacementStream.forget();
94 : }
95 :
96 0 : clonedStream.forget(aStream);
97 : }
98 :
99 : already_AddRefed<BlobImpl>
100 0 : StreamBlobImpl::CreateSlice(uint64_t aStart, uint64_t aLength,
101 : const nsAString& aContentType, ErrorResult& aRv)
102 : {
103 0 : if (!aLength) {
104 0 : RefPtr<BlobImpl> impl = new EmptyBlobImpl(aContentType);
105 0 : return impl.forget();
106 : }
107 :
108 : RefPtr<BlobImpl> impl =
109 0 : new StreamBlobImpl(this, aContentType, aStart, aLength);
110 0 : return impl.forget();
111 : }
112 :
113 : void
114 0 : StreamBlobImpl::MaybeRegisterMemoryReporter()
115 : {
116 : // We report only stringInputStream.
117 : nsCOMPtr<nsIStringInputStream> stringInputStream =
118 0 : do_QueryInterface(mInputStream);
119 0 : if (!stringInputStream) {
120 0 : return;
121 : }
122 :
123 0 : RegisterWeakMemoryReporter(this);
124 : }
125 :
126 : NS_IMETHODIMP
127 0 : StreamBlobImpl::CollectReports(nsIHandleReportCallback* aHandleReport,
128 : nsISupports* aData, bool aAnonymize)
129 : {
130 : nsCOMPtr<nsIStringInputStream> stringInputStream =
131 0 : do_QueryInterface(mInputStream);
132 0 : if (!stringInputStream) {
133 0 : return NS_OK;
134 : }
135 :
136 0 : MOZ_COLLECT_REPORT(
137 : "explicit/dom/memory-file-data/stream", KIND_HEAP, UNITS_BYTES,
138 : stringInputStream->SizeOfIncludingThis(MallocSizeOf),
139 0 : "Memory used to back a File/Blob based on an input stream.");
140 :
141 0 : return NS_OK;
142 : }
143 :
144 : } // namespace dom
145 : } // namespace mozilla
|