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