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 #ifndef mozilla_dom_StreamBlobImpl_h
8 #define mozilla_dom_StreamBlobImpl_h
9 
10 #include "BaseBlobImpl.h"
11 #include "nsIMemoryReporter.h"
12 
13 namespace mozilla {
14 namespace dom {
15 
16 class StreamBlobImpl final : public BaseBlobImpl, public nsIMemoryReporter {
17   MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
18 
19  public:
20   NS_DECL_ISUPPORTS_INHERITED
21   NS_DECL_NSIMEMORYREPORTER
22 
23   // Blob constructor.
24   static already_AddRefed<StreamBlobImpl> Create(
25       already_AddRefed<nsIInputStream> aInputStream,
26       const nsAString& aContentType, uint64_t aLength,
27       const nsAString& aBlobImplType);
28 
29   // File constructor.
30   static already_AddRefed<StreamBlobImpl> Create(
31       already_AddRefed<nsIInputStream> aInputStream, const nsAString& aName,
32       const nsAString& aContentType, int64_t aLastModifiedDate,
33       uint64_t aLength, const nsAString& aBlobImplType);
34 
35   void CreateInputStream(nsIInputStream** aStream, ErrorResult& aRv) override;
36 
37   already_AddRefed<BlobImpl> CreateSlice(uint64_t aStart, uint64_t aLength,
38                                          const nsAString& aContentType,
39                                          ErrorResult& aRv) override;
40 
IsMemoryFile()41   bool IsMemoryFile() const override { return true; }
42 
GetFileId()43   int64_t GetFileId() override { return mFileId; }
44 
SetFileId(int64_t aFileId)45   void SetFileId(int64_t aFileId) { mFileId = aFileId; }
46 
SetFullPath(const nsAString & aFullPath)47   void SetFullPath(const nsAString& aFullPath) { mFullPath = aFullPath; }
48 
GetMozFullPathInternal(nsAString & aFullPath,ErrorResult & aRv)49   void GetMozFullPathInternal(nsAString& aFullPath, ErrorResult& aRv) override {
50     aFullPath = mFullPath;
51   }
52 
SetIsDirectory(bool aIsDirectory)53   void SetIsDirectory(bool aIsDirectory) { mIsDirectory = aIsDirectory; }
54 
IsDirectory()55   bool IsDirectory() const override { return mIsDirectory; }
56 
57   size_t GetAllocationSize() const override;
58 
GetAllocationSize(FallibleTArray<BlobImpl * > & aVisitedBlobImpls)59   size_t GetAllocationSize(
60       FallibleTArray<BlobImpl*>& aVisitedBlobImpls) const override {
61     return GetAllocationSize();
62   }
63 
64   void GetBlobImplType(nsAString& aBlobImplType) const override;
65 
66  private:
67   // Blob constructor.
68   StreamBlobImpl(already_AddRefed<nsIInputStream> aInputStream,
69                  const nsAString& aContentType, uint64_t aLength,
70                  const nsAString& aBlobImplType);
71 
72   // File constructor.
73   StreamBlobImpl(already_AddRefed<nsIInputStream> aInputStream,
74                  const nsAString& aName, const nsAString& aContentType,
75                  int64_t aLastModifiedDate, uint64_t aLength,
76                  const nsAString& aBlobImplType);
77 
78   ~StreamBlobImpl();
79 
80   void MaybeRegisterMemoryReporter();
81 
82   nsCOMPtr<nsIInputStream> mInputStream;
83 
84   nsString mBlobImplType;
85 
86   nsString mFullPath;
87   bool mIsDirectory;
88   int64_t mFileId;
89 };
90 
91 }  // namespace dom
92 }  // namespace mozilla
93 
94 #endif  // mozilla_dom_StreamBlobImpl_h
95