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.
33 #include "WebKitBlobBuilder.h"
36 #include "ExceptionCode.h"
38 #include "HistogramSupport.h"
39 #include "LineEnding.h"
40 #include "ScriptExecutionContext.h"
41 #include "TextEncoding.h"
42 #include <wtf/ArrayBuffer.h>
43 #include <wtf/ArrayBufferView.h>
44 #include <wtf/PassRefPtr.h>
45 #include <wtf/Vector.h>
46 #include <wtf/text/AtomicString.h>
47 #include <wtf/text/CString.h>
51 enum BlobConstructorArrayBufferOrView {
52 BlobConstructorArrayBuffer,
53 BlobConstructorArrayBufferView,
54 BlobConstructorArrayBufferOrViewMax,
58 PassRefPtr<WebKitBlobBuilder> WebKitBlobBuilder::create(ScriptExecutionContext* context)
60 String message("BlobBuilder is deprecated. Use \"Blob\" constructor instead.");
61 context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, message);
63 return adoptRef(new WebKitBlobBuilder());
66 WebKitBlobBuilder::WebKitBlobBuilder()
71 Vector<char>& WebKitBlobBuilder::getBuffer()
73 // If the last item is not a data item, create one. Otherwise, we simply append the new string to the last data item.
74 if (m_items.isEmpty() || m_items[m_items.size() - 1].type != BlobDataItem::Data)
75 m_items.append(BlobDataItem(RawData::create()));
77 return *m_items[m_items.size() - 1].data->mutableData();
80 void WebKitBlobBuilder::append(const String& text, const String& endingType, ExceptionCode& ec)
82 bool isEndingTypeTransparent = endingType == "transparent";
83 bool isEndingTypeNative = endingType == "native";
84 if (!endingType.isEmpty() && !isEndingTypeTransparent && !isEndingTypeNative) {
89 CString utf8Text = UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables);
91 Vector<char>& buffer = getBuffer();
92 size_t oldSize = buffer.size();
94 if (isEndingTypeNative)
95 normalizeLineEndingsToNative(utf8Text, buffer);
97 buffer.append(utf8Text.data(), utf8Text.length());
98 m_size += buffer.size() - oldSize;
101 void WebKitBlobBuilder::append(const String& text, ExceptionCode& ec)
103 append(text, String(), ec);
107 void WebKitBlobBuilder::append(ScriptExecutionContext* context, ArrayBuffer* arrayBuffer)
109 String consoleMessage("ArrayBuffer values are deprecated in Blob Constructor. Use ArrayBufferView instead.");
110 context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, consoleMessage);
112 HistogramSupport::histogramEnumeration("WebCore.Blob.constructor.ArrayBufferOrView", BlobConstructorArrayBuffer, BlobConstructorArrayBufferOrViewMax);
117 appendBytesData(arrayBuffer->data(), arrayBuffer->byteLength());
120 void WebKitBlobBuilder::append(ArrayBufferView* arrayBufferView)
122 HistogramSupport::histogramEnumeration("WebCore.Blob.constructor.ArrayBufferOrView", BlobConstructorArrayBufferView, BlobConstructorArrayBufferOrViewMax);
124 if (!arrayBufferView)
127 appendBytesData(arrayBufferView->baseAddress(), arrayBufferView->byteLength());
131 void WebKitBlobBuilder::append(Blob* blob)
135 if (blob->isFile()) {
136 File* file = toFile(blob);
137 // If the blob is file that is not snapshoted, capture the snapshot now.
138 // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
139 long long snapshotSize;
140 double snapshotModificationTime;
141 file->captureSnapshot(snapshotSize, snapshotModificationTime);
143 m_size += snapshotSize;
144 m_items.append(BlobDataItem(file->path(), 0, snapshotSize, snapshotModificationTime));
146 long long blobSize = static_cast<long long>(blob->size());
148 m_items.append(BlobDataItem(blob->url(), 0, blobSize));
153 void WebKitBlobBuilder::appendBytesData(const void* data, size_t length)
155 Vector<char>& buffer = getBuffer();
156 size_t oldSize = buffer.size();
157 buffer.append(static_cast<const char*>(data), length);
158 m_size += buffer.size() - oldSize;
161 PassRefPtr<Blob> WebKitBlobBuilder::getBlob(const String& contentType)
163 OwnPtr<BlobData> blobData = BlobData::create();
164 blobData->setContentType(contentType);
165 blobData->swapItems(m_items);
167 RefPtr<Blob> blob = Blob::create(blobData.release(), m_size);
169 // After creating a blob from the current blob data, we do not need to keep the data around any more. Instead, we only
170 // need to keep a reference to the URL of the blob just created.
171 m_items.append(BlobDataItem(blob->url(), 0, m_size));
176 } // namespace WebCore