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_BaseBlobImpl_h
8 #define mozilla_dom_BaseBlobImpl_h
9 
10 #include "mozilla/dom/BlobImpl.h"
11 #include "mozilla/ErrorResult.h"
12 
13 namespace mozilla {
14 namespace dom {
15 
16 class FileBlobImpl;
17 
18 class BaseBlobImpl : public BlobImpl {
19   friend class FileBlobImpl;
20 
21  public:
22   // File constructor.
BaseBlobImpl(const nsAString & aName,const nsAString & aContentType,uint64_t aLength,int64_t aLastModifiedDate)23   BaseBlobImpl(const nsAString& aName, const nsAString& aContentType,
24                uint64_t aLength, int64_t aLastModifiedDate)
25       : mIsFile(true),
26         mContentType(aContentType),
27         mName(aName),
28         mStart(0),
29         mLength(aLength),
30         mSerialNumber(NextSerialNumber()),
31         mLastModificationDate(aLastModifiedDate) {
32     // Ensure non-null mContentType by default
33     mContentType.SetIsVoid(false);
34   }
35 
36   // Blob constructor without starting point.
BaseBlobImpl(const nsAString & aContentType,uint64_t aLength)37   BaseBlobImpl(const nsAString& aContentType, uint64_t aLength)
38       : mIsFile(false),
39         mContentType(aContentType),
40         mStart(0),
41         mLength(aLength),
42         mSerialNumber(NextSerialNumber()),
43         mLastModificationDate(0) {
44     // Ensure non-null mContentType by default
45     mContentType.SetIsVoid(false);
46   }
47 
48   // Blob constructor with starting point.
BaseBlobImpl(const nsAString & aContentType,uint64_t aStart,uint64_t aLength)49   BaseBlobImpl(const nsAString& aContentType, uint64_t aStart, uint64_t aLength)
50       : mIsFile(false),
51         mContentType(aContentType),
52         mStart(aStart),
53         mLength(aLength),
54         mSerialNumber(NextSerialNumber()),
55         mLastModificationDate(0) {
56     // Ensure non-null mContentType by default
57     mContentType.SetIsVoid(false);
58   }
59 
60   virtual void GetName(nsAString& aName) const override;
61 
62   virtual void GetDOMPath(nsAString& aName) const override;
63 
64   virtual void SetDOMPath(const nsAString& aName) override;
65 
66   virtual int64_t GetLastModified(ErrorResult& aRv) override;
67 
68   virtual void GetMozFullPath(nsAString& aName,
69                               SystemCallerGuarantee /* unused */,
70                               ErrorResult& aRv) override;
71 
72   virtual void GetMozFullPathInternal(nsAString& aFileName,
73                                       ErrorResult& aRv) override;
74 
GetSize(ErrorResult & aRv)75   virtual uint64_t GetSize(ErrorResult& aRv) override { return mLength; }
76 
77   virtual void GetType(nsAString& aType) override;
78 
GetAllocationSize()79   size_t GetAllocationSize() const override { return 0; }
80 
GetAllocationSize(FallibleTArray<BlobImpl * > & aVisitedBlobImpls)81   size_t GetAllocationSize(
82       FallibleTArray<BlobImpl*>& aVisitedBlobImpls) const override {
83     return GetAllocationSize();
84   }
85 
GetSerialNumber()86   virtual uint64_t GetSerialNumber() const override { return mSerialNumber; }
87 
CreateSlice(uint64_t aStart,uint64_t aLength,const nsAString & aContentType,ErrorResult & aRv)88   virtual already_AddRefed<BlobImpl> CreateSlice(uint64_t aStart,
89                                                  uint64_t aLength,
90                                                  const nsAString& aContentType,
91                                                  ErrorResult& aRv) override {
92     return nullptr;
93   }
94 
GetSubBlobImpls()95   virtual const nsTArray<RefPtr<BlobImpl>>* GetSubBlobImpls() const override {
96     return nullptr;
97   }
98 
CreateInputStream(nsIInputStream ** aStream,ErrorResult & aRv)99   virtual void CreateInputStream(nsIInputStream** aStream,
100                                  ErrorResult& aRv) override {
101     aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
102   }
103 
104   virtual int64_t GetFileId() override;
105 
SetLazyData(const nsAString & aName,const nsAString & aContentType,uint64_t aLength,int64_t aLastModifiedDate)106   virtual void SetLazyData(const nsAString& aName,
107                            const nsAString& aContentType, uint64_t aLength,
108                            int64_t aLastModifiedDate) override {
109     mName = aName;
110     mContentType = aContentType;
111     mLength = aLength;
112     SetLastModificationDatePrecisely(aLastModifiedDate);
113     mIsFile = !aName.IsVoid();
114   }
115 
IsMemoryFile()116   virtual bool IsMemoryFile() const override { return false; }
117 
IsFile()118   virtual bool IsFile() const override { return mIsFile; }
119 
GetBlobImplType(nsAString & aBlobImplType)120   virtual void GetBlobImplType(nsAString& aBlobImplType) const override {
121     aBlobImplType = u"BaseBlobImpl"_ns;
122   }
123 
124  protected:
125   virtual ~BaseBlobImpl() = default;
126 
127   /**
128    * Returns a new, effectively-unique serial number. This should be used
129    * by implementations to obtain a serial number for GetSerialNumber().
130    * The implementation is thread safe.
131    */
132   static uint64_t NextSerialNumber();
133 
134   void SetLastModificationDate(bool aCrossOriginIsolated, int64_t aDate);
135   void SetLastModificationDatePrecisely(int64_t aDate);
136 
137 #ifdef DEBUG
IsLastModificationDateUnset()138   bool IsLastModificationDateUnset() const {
139     return mLastModificationDate == INT64_MAX;
140   }
141 #endif
142 
143   const nsString mBlobImplType;
144 
145   bool mIsFile;
146 
147   nsString mContentType;
148   nsString mName;
149   nsString mPath;  // The path relative to a directory chosen by the user
150 
151   uint64_t mStart;
152   uint64_t mLength;
153 
154   const uint64_t mSerialNumber;
155 
156  private:
157   int64_t mLastModificationDate;
158 };
159 
160 }  // namespace dom
161 }  // namespace mozilla
162 
163 #endif  // mozilla_dom_BaseBlobImpl_h
164