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_Blob_h
8 #define mozilla_dom_Blob_h
9 
10 #include "mozilla/Attributes.h"
11 #include "mozilla/ErrorResult.h"
12 #include "mozilla/dom/BindingDeclarations.h"
13 #include "mozilla/dom/BlobImpl.h"
14 #include "mozilla/dom/BodyConsumer.h"
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsCOMPtr.h"
17 #include "nsWrapperCache.h"
18 #include "nsWeakReference.h"
19 
20 class nsIGlobalObject;
21 class nsIInputStream;
22 
23 namespace mozilla {
24 namespace dom {
25 
26 struct BlobPropertyBag;
27 class File;
28 class OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString;
29 class Promise;
30 
31 #define NS_DOM_BLOB_IID                              \
32   {                                                  \
33     0x648c2a83, 0xbdb1, 0x4a7d, {                    \
34       0xb5, 0x0a, 0xca, 0xcd, 0x92, 0x87, 0x45, 0xc2 \
35     }                                                \
36   }
37 
38 class Blob : public nsSupportsWeakReference, public nsWrapperCache {
39  public:
40   NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL
41   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Blob)
42   NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_BLOB_IID)
43 
44   typedef OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString BlobPart;
45 
46   // This creates a Blob or a File based on the type of BlobImpl.
47   static Blob* Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl);
48 
49   static already_AddRefed<Blob> CreateStringBlob(nsIGlobalObject* aGlobal,
50                                                  const nsACString& aData,
51                                                  const nsAString& aContentType);
52 
53   // The returned Blob takes ownership of aMemoryBuffer. aMemoryBuffer will be
54   // freed by free so it must be allocated by malloc or something
55   // compatible with it.
56   static already_AddRefed<Blob> CreateMemoryBlob(nsIGlobalObject* aGlobal,
57                                                  void* aMemoryBuffer,
58                                                  uint64_t aLength,
59                                                  const nsAString& aContentType);
60 
Impl()61   BlobImpl* Impl() const { return mImpl; }
62 
63   bool IsFile() const;
64 
65   const nsTArray<RefPtr<BlobImpl>>* GetSubBlobImpls() const;
66 
67   // This method returns null if this Blob is not a File; it returns
68   // the same object in case this Blob already implements the File interface;
69   // otherwise it returns a new File object with the same BlobImpl.
70   already_AddRefed<File> ToFile();
71 
72   // This method creates a new File object with the given name and the same
73   // BlobImpl.
74   already_AddRefed<File> ToFile(const nsAString& aName, ErrorResult& aRv) const;
75 
76   already_AddRefed<Blob> CreateSlice(uint64_t aStart, uint64_t aLength,
77                                      const nsAString& aContentType,
78                                      ErrorResult& aRv);
79 
80   void CreateInputStream(nsIInputStream** aStream, ErrorResult& aRv);
81 
82   int64_t GetFileId();
83 
84   // A utility function that enforces the spec constraints on the type of a
85   // blob: no codepoints outside the ASCII range (otherwise type becomes empty)
86   // and lowercase ASCII only.  We can't just use our existing nsContentUtils
87   // ASCII-related helpers because we need the "outside ASCII range" check, and
88   // we can't use NS_IsAscii because its definition of "ASCII" (chars all <=
89   // 0x7E) differs from the file API definition (which excludes control chars).
90   static void MakeValidBlobType(nsAString& aType);
91 
92   // WebIDL methods
GetParentObject()93   nsIGlobalObject* GetParentObject() const { return mGlobal; }
94 
95   bool IsMemoryFile() const;
96 
97   // Blob constructor
98   static already_AddRefed<Blob> Constructor(
99       const GlobalObject& aGlobal, const Optional<Sequence<BlobPart>>& aData,
100       const BlobPropertyBag& aBag, ErrorResult& aRv);
101 
102   virtual JSObject* WrapObject(JSContext* aCx,
103                                JS::Handle<JSObject*> aGivenProto) override;
104 
105   uint64_t GetSize(ErrorResult& aRv);
106 
107   void GetType(nsAString& aType);
108 
109   void GetBlobImplType(nsAString& aBlobImplType);
110 
111   already_AddRefed<Blob> Slice(const Optional<int64_t>& aStart,
112                                const Optional<int64_t>& aEnd,
113                                const Optional<nsAString>& aContentType,
114                                ErrorResult& aRv);
115 
116   size_t GetAllocationSize() const;
117 
118   nsresult GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
119                        nsACString& aContentType, nsACString& aCharset) const;
120 
121   void Stream(JSContext* aCx, JS::MutableHandle<JSObject*> aStream,
122               ErrorResult& aRv);
123   already_AddRefed<Promise> Text(ErrorResult& aRv);
124   already_AddRefed<Promise> ArrayBuffer(ErrorResult& aRv);
125 
126  protected:
127   // File constructor should never be used directly. Use Blob::Create instead.
128   Blob(nsIGlobalObject* aGlobal, BlobImpl* aImpl);
129   virtual ~Blob();
130 
HasFileInterface()131   virtual bool HasFileInterface() const { return false; }
132 
133   already_AddRefed<Promise> ConsumeBody(BodyConsumer::ConsumeType aConsumeType,
134                                         ErrorResult& aRv);
135 
136   // The member is the real backend implementation of this File/Blob.
137   // It's thread-safe and not CC-able and it's the only element that is moved
138   // between threads.
139   // Note: we should not store any other state in this class!
140   RefPtr<BlobImpl> mImpl;
141 
142  private:
143   nsCOMPtr<nsIGlobalObject> mGlobal;
144 };
145 
146 NS_DEFINE_STATIC_IID_ACCESSOR(Blob, NS_DOM_BLOB_IID)
147 
148 // Override BindingJSObjectMallocBytes for blobs to tell the JS GC how much
149 // memory is held live by the binding object.
150 size_t BindingJSObjectMallocBytes(Blob* aBlob);
151 
152 }  // namespace dom
153 }  // namespace mozilla
154 
ToSupports(mozilla::dom::Blob * aBlob)155 inline nsISupports* ToSupports(mozilla::dom::Blob* aBlob) {
156   return static_cast<nsISupportsWeakReference*>(aBlob);
157 }
158 
159 #endif  // mozilla_dom_Blob_h
160