1 | /* |
2 | * Copyright (C) 2010 Google Inc. All rights reserved. |
3 | * Copyright (C) 2012-2019 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
7 | * met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following disclaimer |
13 | * in the documentation and/or other materials provided with the |
14 | * distribution. |
15 | * * Neither the name of Google Inc. nor the names of its |
16 | * contributors may be used to endorse or promote products derived from |
17 | * this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #pragma once |
33 | |
34 | #include "BlobPropertyBag.h" |
35 | #include "ScriptWrappable.h" |
36 | #include <wtf/IsoMalloc.h> |
37 | #include <wtf/URL.h> |
38 | #include "URLRegistry.h" |
39 | #include <wtf/Variant.h> |
40 | |
41 | namespace JSC { |
42 | class ArrayBufferView; |
43 | class ArrayBuffer; |
44 | } |
45 | |
46 | namespace WebCore { |
47 | |
48 | class Blob; |
49 | class ScriptExecutionContext; |
50 | class SharedBuffer; |
51 | |
52 | using BlobPartVariant = Variant<RefPtr<JSC::ArrayBufferView>, RefPtr<JSC::ArrayBuffer>, RefPtr<Blob>, String>; |
53 | |
54 | class Blob : public ScriptWrappable, public URLRegistrable, public RefCounted<Blob> { |
55 | WTF_MAKE_ISO_ALLOCATED_EXPORT(Blob, WEBCORE_EXPORT); |
56 | public: |
57 | static Ref<Blob> create() |
58 | { |
59 | return adoptRef(*new Blob); |
60 | } |
61 | |
62 | static Ref<Blob> create(Vector<BlobPartVariant>&& blobPartVariants, const BlobPropertyBag& propertyBag) |
63 | { |
64 | return adoptRef(*new Blob(WTFMove(blobPartVariants), propertyBag)); |
65 | } |
66 | |
67 | static Ref<Blob> create(const SharedBuffer& buffer, const String& contentType) |
68 | { |
69 | return adoptRef(*new Blob(buffer, contentType)); |
70 | } |
71 | |
72 | static Ref<Blob> create(Vector<uint8_t>&& data, const String& contentType) |
73 | { |
74 | return adoptRef(*new Blob(WTFMove(data), contentType)); |
75 | } |
76 | |
77 | static Ref<Blob> deserialize(const URL& srcURL, const String& type, long long size, const String& fileBackedPath) |
78 | { |
79 | ASSERT(Blob::isNormalizedContentType(type)); |
80 | return adoptRef(*new Blob(deserializationContructor, srcURL, type, size, fileBackedPath)); |
81 | } |
82 | |
83 | virtual ~Blob(); |
84 | |
85 | const URL& url() const { return m_internalURL; } |
86 | const String& type() const { return m_type; } |
87 | |
88 | WEBCORE_EXPORT unsigned long long size() const; |
89 | virtual bool isFile() const { return false; } |
90 | |
91 | // The checks described in the File API spec. |
92 | static bool isValidContentType(const String&); |
93 | // The normalization procedure described in the File API spec. |
94 | static String normalizedContentType(const String&); |
95 | #if !ASSERT_DISABLED |
96 | static bool isNormalizedContentType(const String&); |
97 | static bool isNormalizedContentType(const CString&); |
98 | #endif |
99 | |
100 | // URLRegistrable |
101 | URLRegistry& registry() const override; |
102 | |
103 | Ref<Blob> slice(long long start = 0, long long end = std::numeric_limits<long long>::max(), const String& contentType = String()) const |
104 | { |
105 | return adoptRef(*new Blob(m_internalURL, start, end, contentType)); |
106 | } |
107 | |
108 | protected: |
109 | WEBCORE_EXPORT Blob(); |
110 | Blob(Vector<BlobPartVariant>&&, const BlobPropertyBag&); |
111 | Blob(const SharedBuffer&, const String& contentType); |
112 | Blob(Vector<uint8_t>&&, const String& contentType); |
113 | |
114 | enum ReferencingExistingBlobConstructor { referencingExistingBlobConstructor }; |
115 | Blob(ReferencingExistingBlobConstructor, const Blob&); |
116 | |
117 | enum UninitializedContructor { uninitializedContructor }; |
118 | Blob(UninitializedContructor); |
119 | |
120 | enum DeserializationContructor { deserializationContructor }; |
121 | Blob(DeserializationContructor, const URL& srcURL, const String& type, long long size, const String& fileBackedPath); |
122 | |
123 | // For slicing. |
124 | Blob(const URL& srcURL, long long start, long long end, const String& contentType); |
125 | |
126 | // This is an internal URL referring to the blob data associated with this object. It serves |
127 | // as an identifier for this blob. The internal URL is never used to source the blob's content |
128 | // into an HTML or for FileRead'ing, public blob URLs must be used for those purposes. |
129 | URL m_internalURL; |
130 | |
131 | String m_type; |
132 | mutable long long m_size; |
133 | }; |
134 | |
135 | } // namespace WebCore |
136 | |