2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/fileapi/Blob.h"
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "core/dom/DOMURL.h"
36 #include "core/dom/ExceptionCode.h"
37 #include "core/dom/ExecutionContext.h"
38 #include "core/fileapi/BlobPropertyBag.h"
39 #include "platform/blob/BlobRegistry.h"
40 #include "platform/blob/BlobURL.h"
46 class BlobURLRegistry final
: public URLRegistry
{
48 void registerURL(SecurityOrigin
*, const KURL
&, URLRegistrable
*) override
;
49 void unregisterURL(const KURL
&) override
;
51 static URLRegistry
& registry();
54 void BlobURLRegistry::registerURL(SecurityOrigin
* origin
, const KURL
& publicURL
, URLRegistrable
* registrableObject
)
56 ASSERT(®istrableObject
->registry() == this);
57 Blob
* blob
= static_cast<Blob
*>(registrableObject
);
58 BlobRegistry::registerPublicBlobURL(origin
, publicURL
, blob
->blobDataHandle());
61 void BlobURLRegistry::unregisterURL(const KURL
& publicURL
)
63 BlobRegistry::revokePublicBlobURL(publicURL
);
66 URLRegistry
& BlobURLRegistry::registry()
68 // This is called on multiple threads.
69 // (This code assumes it is safe to register or unregister URLs on
70 // BlobURLRegistry (that is implemented by the embedder) on
72 AtomicallyInitializedStaticReference(BlobURLRegistry
, instance
, new BlobURLRegistry());
78 Blob::Blob(PassRefPtr
<BlobDataHandle
> dataHandle
)
79 : m_blobDataHandle(dataHandle
)
80 , m_hasBeenClosed(false)
89 Blob
* Blob::create(const HeapVector
<ArrayBufferOrArrayBufferViewOrBlobOrString
>& blobParts
, const BlobPropertyBag
& options
, ExceptionState
& exceptionState
)
91 ASSERT(options
.hasType());
92 if (!options
.type().containsOnlyASCII()) {
93 exceptionState
.throwDOMException(SyntaxError
, "The 'type' property must consist of ASCII characters.");
97 ASSERT(options
.hasEndings());
98 bool normalizeLineEndingsToNative
= options
.endings() == "native";
100 OwnPtr
<BlobData
> blobData
= BlobData::create();
101 blobData
->setContentType(options
.type().lower());
103 populateBlobData(blobData
.get(), blobParts
, normalizeLineEndingsToNative
);
105 long long blobSize
= blobData
->length();
106 return new Blob(BlobDataHandle::create(blobData
.release(), blobSize
));
109 void Blob::clampSliceOffsets(long long size
, long long& start
, long long& end
)
113 // Convert the negative value that is used to select from the end.
115 start
= start
+ size
;
119 // Clamp the range if it exceeds the size limit.
127 } else if (end
< start
) {
129 } else if (end
> size
) {
134 Blob
* Blob::slice(long long start
, long long end
, const String
& contentType
, ExceptionState
& exceptionState
) const
136 if (hasBeenClosed()) {
137 exceptionState
.throwDOMException(InvalidStateError
, "Blob has been closed.");
141 long long size
= this->size();
142 clampSliceOffsets(size
, start
, end
);
144 long long length
= end
- start
;
145 OwnPtr
<BlobData
> blobData
= BlobData::create();
146 blobData
->setContentType(contentType
);
147 blobData
->appendBlob(m_blobDataHandle
, start
, length
);
148 return Blob::create(BlobDataHandle::create(blobData
.release(), length
));
151 void Blob::close(ExecutionContext
* executionContext
, ExceptionState
& exceptionState
)
153 if (hasBeenClosed()) {
154 exceptionState
.throwDOMException(InvalidStateError
, "Blob has been closed.");
158 // Dereferencing a Blob that has been closed should result in
159 // a network error. Revoke URLs registered against it through
161 DOMURL::revokeObjectUUID(executionContext
, uuid());
163 // A Blob enters a 'readability state' of closed, where it will report its
164 // size as zero. Blob and FileReader operations now throws on
165 // being passed a Blob in that state. Downstream uses of closed Blobs
166 // (e.g., XHR.send()) consider them as empty.
167 OwnPtr
<BlobData
> blobData
= BlobData::create();
168 blobData
->setContentType(type());
169 m_blobDataHandle
= BlobDataHandle::create(blobData
.release(), 0);
170 m_hasBeenClosed
= true;
173 void Blob::appendTo(BlobData
& blobData
) const
175 blobData
.appendBlob(m_blobDataHandle
, 0, m_blobDataHandle
->size());
178 URLRegistry
& Blob::registry() const
180 return BlobURLRegistry::registry();