Line data Source code
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 "nsCycleCollectionParticipant.h"
15 : #include "nsCOMPtr.h"
16 : #include "nsIDOMBlob.h"
17 : #include "nsIMutable.h"
18 : #include "nsIXMLHttpRequest.h"
19 : #include "nsWrapperCache.h"
20 : #include "nsWeakReference.h"
21 :
22 : class nsIInputStream;
23 :
24 : namespace mozilla {
25 : namespace dom {
26 :
27 : struct BlobPropertyBag;
28 : class File;
29 : class OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString;
30 :
31 : class Blob : public nsIDOMBlob
32 : , public nsIXHRSendable
33 : , public nsIMutable
34 : , public nsSupportsWeakReference
35 : , public nsWrapperCache
36 : {
37 : public:
38 : NS_DECL_NSIDOMBLOB
39 : NS_DECL_NSIXHRSENDABLE
40 : NS_DECL_NSIMUTABLE
41 :
42 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
43 3 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Blob, nsIDOMBlob)
44 :
45 : typedef OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString BlobPart;
46 :
47 : // This creates a Blob or a File based on the type of BlobImpl.
48 : static Blob*
49 : Create(nsISupports* aParent, BlobImpl* aImpl);
50 :
51 : static already_AddRefed<Blob>
52 : CreateStringBlob(nsISupports* aParent, const nsACString& aData,
53 : const nsAString& aContentType);
54 :
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>
59 : CreateMemoryBlob(nsISupports* aParent, void* aMemoryBuffer, uint64_t aLength,
60 : const nsAString& aContentType);
61 :
62 : static already_AddRefed<Blob>
63 : CreateTemporaryBlob(nsISupports* aParent, PRFileDesc* aFD,
64 : uint64_t aStartPos, uint64_t aLength,
65 : const nsAString& aContentType);
66 :
67 0 : BlobImpl* Impl() const
68 : {
69 0 : return mImpl;
70 : }
71 :
72 : bool IsFile() const;
73 :
74 : const nsTArray<RefPtr<BlobImpl>>* GetSubBlobImpls() const;
75 :
76 : // This method returns null if this Blob is not a File; it returns
77 : // the same object in case this Blob already implements the File interface;
78 : // otherwise it returns a new File object with the same BlobImpl.
79 : already_AddRefed<File> ToFile();
80 :
81 : // This method creates a new File object with the given name and the same
82 : // BlobImpl.
83 : already_AddRefed<File> ToFile(const nsAString& aName,
84 : ErrorResult& aRv) const;
85 :
86 : already_AddRefed<Blob>
87 : CreateSlice(uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
88 : ErrorResult& aRv);
89 :
90 : void
91 : GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv);
92 :
93 : int64_t
94 : GetFileId();
95 :
96 : // A utility function that enforces the spec constraints on the type of a
97 : // blob: no codepoints outside the ASCII range (otherwise type becomes empty)
98 : // and lowercase ASCII only. We can't just use our existing nsContentUtils
99 : // ASCII-related helpers because we need the "outside ASCII range" check, and
100 : // we can't use NS_IsAscii because its definition of "ASCII" (chars all <=
101 : // 0x7E) differs from the file API definition (which excludes control chars).
102 : static void
103 : MakeValidBlobType(nsAString& aType);
104 :
105 : // WebIDL methods
106 0 : nsISupports* GetParentObject() const
107 : {
108 0 : return mParent;
109 : }
110 :
111 : bool
112 : IsMemoryFile() const;
113 :
114 : // Blob constructor
115 : static already_AddRefed<Blob>
116 : Constructor(const GlobalObject& aGlobal,
117 : const Optional<Sequence<BlobPart>>& aData,
118 : const BlobPropertyBag& aBag,
119 : ErrorResult& aRv);
120 :
121 : virtual JSObject* WrapObject(JSContext* aCx,
122 : JS::Handle<JSObject*> aGivenProto) override;
123 :
124 : uint64_t GetSize(ErrorResult& aRv);
125 :
126 : void GetType(nsAString& aType);
127 :
128 : already_AddRefed<Blob> Slice(const Optional<int64_t>& aStart,
129 : const Optional<int64_t>& aEnd,
130 : const nsAString& aContentType,
131 : ErrorResult& aRv);
132 :
133 : protected:
134 : // File constructor should never be used directly. Use Blob::Create instead.
135 : Blob(nsISupports* aParent, BlobImpl* aImpl);
136 : virtual ~Blob();
137 :
138 0 : virtual bool HasFileInterface() const { return false; }
139 :
140 : // The member is the real backend implementation of this File/Blob.
141 : // It's thread-safe and not CC-able and it's the only element that is moved
142 : // between threads.
143 : // Note: we should not store any other state in this class!
144 : RefPtr<BlobImpl> mImpl;
145 :
146 : private:
147 : nsCOMPtr<nsISupports> mParent;
148 : };
149 :
150 : } // namespace dom
151 : } // namespace mozilla
152 :
153 : #endif // mozilla_dom_Blob_h
|