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/. */
7 #ifndef mozilla_dom_Blob_h
8 #define mozilla_dom_Blob_h
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"
17 #include "nsWrapperCache.h"
18 #include "nsWeakReference.h"
20 class nsIGlobalObject
;
26 struct BlobPropertyBag
;
28 class OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString
;
31 #define NS_DOM_BLOB_IID \
33 0x648c2a83, 0xbdb1, 0x4a7d, { \
34 0xb5, 0x0a, 0xca, 0xcd, 0x92, 0x87, 0x45, 0xc2 \
38 class Blob
: public nsSupportsWeakReference
, public nsWrapperCache
{
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
)
44 typedef OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString BlobPart
;
46 // This creates a Blob or a File based on the type of BlobImpl.
47 static Blob
* Create(nsIGlobalObject
* aGlobal
, BlobImpl
* aImpl
);
49 static already_AddRefed
<Blob
> CreateStringBlob(nsIGlobalObject
* aGlobal
,
50 const nsACString
& aData
,
51 const nsAString
& aContentType
);
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
,
59 const nsAString
& aContentType
);
61 BlobImpl
* Impl() const { return mImpl
; }
65 const nsTArray
<RefPtr
<BlobImpl
>>* GetSubBlobImpls() const;
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();
72 // This method creates a new File object with the given name and the same
74 already_AddRefed
<File
> ToFile(const nsAString
& aName
, ErrorResult
& aRv
) const;
76 already_AddRefed
<Blob
> CreateSlice(uint64_t aStart
, uint64_t aLength
,
77 const nsAString
& aContentType
,
80 void CreateInputStream(nsIInputStream
** aStream
, ErrorResult
& aRv
);
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
);
93 nsIGlobalObject
* GetParentObject() const { return mGlobal
; }
95 bool IsMemoryFile() const;
98 static already_AddRefed
<Blob
> Constructor(
99 const GlobalObject
& aGlobal
, const Optional
<Sequence
<BlobPart
>>& aData
,
100 const BlobPropertyBag
& aBag
, ErrorResult
& aRv
);
102 virtual JSObject
* WrapObject(JSContext
* aCx
,
103 JS::Handle
<JSObject
*> aGivenProto
) override
;
105 uint64_t GetSize(ErrorResult
& aRv
);
107 void GetType(nsAString
& aType
);
109 void GetBlobImplType(nsAString
& aBlobImplType
);
111 already_AddRefed
<Blob
> Slice(const Optional
<int64_t>& aStart
,
112 const Optional
<int64_t>& aEnd
,
113 const Optional
<nsAString
>& aContentType
,
116 size_t GetAllocationSize() const;
118 nsresult
GetSendInfo(nsIInputStream
** aBody
, uint64_t* aContentLength
,
119 nsACString
& aContentType
, nsACString
& aCharset
) const;
121 void Stream(JSContext
* aCx
, JS::MutableHandle
<JSObject
*> aStream
,
123 already_AddRefed
<Promise
> Text(ErrorResult
& aRv
);
124 already_AddRefed
<Promise
> ArrayBuffer(ErrorResult
& aRv
);
127 // File constructor should never be used directly. Use Blob::Create instead.
128 Blob(nsIGlobalObject
* aGlobal
, BlobImpl
* aImpl
);
131 virtual bool HasFileInterface() const { return false; }
133 already_AddRefed
<Promise
> ConsumeBody(BodyConsumer::ConsumeType aConsumeType
,
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
139 // Note: we should not store any other state in this class!
140 RefPtr
<BlobImpl
> mImpl
;
143 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
146 NS_DEFINE_STATIC_IID_ACCESSOR(Blob
, NS_DOM_BLOB_IID
)
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
);
153 } // namespace mozilla
155 inline nsISupports
* ToSupports(mozilla::dom::Blob
* aBlob
) {
156 return static_cast<nsISupportsWeakReference
*>(aBlob
);
159 #endif // mozilla_dom_Blob_h