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/dom/BodyConsumer.h"
11 #include "nsCycleCollectionParticipant.h"
13 #include "nsWrapperCache.h"
14 #include "nsWeakReference.h"
16 class nsIGlobalObject
;
24 struct BlobPropertyBag
;
28 class OwningArrayBufferViewOrArrayBufferOrBlobOrUTF8String
;
33 #define NS_DOM_BLOB_IID \
35 0x648c2a83, 0xbdb1, 0x4a7d, { \
36 0xb5, 0x0a, 0xca, 0xcd, 0x92, 0x87, 0x45, 0xc2 \
40 class Blob
: public nsSupportsWeakReference
, public nsWrapperCache
{
42 NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL
43 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Blob
)
44 NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_BLOB_IID
)
46 using BlobPart
= OwningArrayBufferViewOrArrayBufferOrBlobOrUTF8String
;
48 // This creates a Blob or a File based on the type of BlobImpl.
49 static Blob
* Create(nsIGlobalObject
* aGlobal
, BlobImpl
* aImpl
);
51 static already_AddRefed
<Blob
> CreateStringBlob(nsIGlobalObject
* aGlobal
,
52 const nsACString
& aData
,
53 const nsAString
& aContentType
);
55 // The returned Blob takes ownership of aMemoryBuffer. aMemoryBuffer will be
56 // freed by free so it must be allocated by malloc or something
57 // compatible with it.
58 static already_AddRefed
<Blob
> CreateMemoryBlob(nsIGlobalObject
* aGlobal
,
61 const nsAString
& aContentType
);
63 BlobImpl
* Impl() const { return mImpl
; }
67 const nsTArray
<RefPtr
<BlobImpl
>>* GetSubBlobImpls() const;
69 // This method returns null if this Blob is not a File; it returns
70 // the same object in case this Blob already implements the File interface;
71 // otherwise it returns a new File object with the same BlobImpl.
72 already_AddRefed
<File
> ToFile();
74 // This method creates a new File object with the given name and the same
76 already_AddRefed
<File
> ToFile(const nsAString
& aName
, ErrorResult
& aRv
) const;
78 already_AddRefed
<Blob
> CreateSlice(uint64_t aStart
, uint64_t aLength
,
79 const nsAString
& aContentType
,
80 ErrorResult
& aRv
) const;
82 void CreateInputStream(nsIInputStream
** aStream
, ErrorResult
& aRv
) const;
84 int64_t GetFileId() const;
86 // A utility function that enforces the spec constraints on the type of a
87 // blob: no codepoints outside the ASCII range (otherwise type becomes empty)
88 // and lowercase ASCII only. We can't just use our existing nsContentUtils
89 // ASCII-related helpers because we need the "outside ASCII range" check, and
90 // we can't use NS_IsAscii because its definition of "ASCII" (chars all <=
91 // 0x7E) differs from the file API definition (which excludes control chars).
92 static void MakeValidBlobType(nsAString
& aType
);
95 nsIGlobalObject
* GetParentObject() const { return mGlobal
; }
97 bool IsMemoryFile() const;
100 static already_AddRefed
<Blob
> Constructor(
101 const GlobalObject
& aGlobal
, const Optional
<Sequence
<BlobPart
>>& aData
,
102 const BlobPropertyBag
& aBag
, ErrorResult
& aRv
);
104 JSObject
* WrapObject(JSContext
* aCx
,
105 JS::Handle
<JSObject
*> aGivenProto
) override
;
107 uint64_t GetSize(ErrorResult
& aRv
);
109 void GetType(nsAString
& aType
);
111 void GetBlobImplType(nsAString
& aBlobImplType
);
113 already_AddRefed
<Blob
> Slice(const Optional
<int64_t>& aStart
,
114 const Optional
<int64_t>& aEnd
,
115 const Optional
<nsAString
>& aContentType
,
118 size_t GetAllocationSize() const;
120 nsresult
GetSendInfo(nsIInputStream
** aBody
, uint64_t* aContentLength
,
121 nsACString
& aContentType
, nsACString
& aCharset
) const;
123 already_AddRefed
<ReadableStream
> Stream(JSContext
* aCx
,
124 ErrorResult
& aRv
) const;
125 already_AddRefed
<Promise
> Text(ErrorResult
& aRv
) const;
126 already_AddRefed
<Promise
> ArrayBuffer(ErrorResult
& aRv
) const;
129 // File constructor should never be used directly. Use Blob::Create instead.
130 Blob(nsIGlobalObject
* aGlobal
, BlobImpl
* aImpl
);
133 virtual bool HasFileInterface() const { return false; }
135 already_AddRefed
<Promise
> ConsumeBody(BodyConsumer::ConsumeType aConsumeType
,
136 ErrorResult
& aRv
) const;
138 // The member is the real backend implementation of this File/Blob.
139 // It's thread-safe and not CC-able and it's the only element that is moved
141 // Note: we should not store any other state in this class!
142 RefPtr
<BlobImpl
> mImpl
;
145 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
148 NS_DEFINE_STATIC_IID_ACCESSOR(Blob
, NS_DOM_BLOB_IID
)
150 // Override BindingJSObjectMallocBytes for blobs to tell the JS GC how much
151 // memory is held live by the binding object.
152 size_t BindingJSObjectMallocBytes(Blob
* aBlob
);
155 } // namespace mozilla
157 inline nsISupports
* ToSupports(mozilla::dom::Blob
* aBlob
) {
158 return static_cast<nsISupportsWeakReference
*>(aBlob
);
161 #endif // mozilla_dom_Blob_h