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