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"
35 #include "ArrayBuffer.h"
37 #include "ExceptionCode.h"
39 #include "LineEnding.h"
40 #include "TextEncoding.h"
41 #include <wtf/PassRefPtr.h>
42 #include <wtf/Vector.h>
43 #include <wtf/text/AtomicString.h>
44 #include <wtf/text/CString.h>
48 WebKitBlobBuilder::WebKitBlobBuilder()
53 Vector<char>& WebKitBlobBuilder::getBuffer()
55 // If the last item is not a data item, create one. Otherwise, we simply append the new string to the last data item.
56 if (m_items.isEmpty() || m_items[m_items.size() - 1].type != BlobDataItem::Data)
57 m_items.append(BlobDataItem(RawData::create()));
59 return *m_items[m_items.size() - 1].data->mutableData();
62 void WebKitBlobBuilder::append(const String& text, const String& endingType, ExceptionCode& ec)
64 bool isEndingTypeTransparent = endingType == "transparent";
65 bool isEndingTypeNative = endingType == "native";
66 if (!endingType.isEmpty() && !isEndingTypeTransparent && !isEndingTypeNative) {
71 CString utf8Text = UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables);
73 Vector<char>& buffer = getBuffer();
74 size_t oldSize = buffer.size();
76 if (isEndingTypeNative)
77 normalizeLineEndingsToNative(utf8Text, buffer);
79 buffer.append(utf8Text.data(), utf8Text.length());
80 m_size += buffer.size() - oldSize;
83 void WebKitBlobBuilder::append(const String& text, ExceptionCode& ec)
85 append(text, String(), ec);
89 void WebKitBlobBuilder::append(ArrayBuffer* arrayBuffer)
93 Vector<char>& buffer = getBuffer();
94 size_t oldSize = buffer.size();
95 buffer.append(static_cast<const char*>(arrayBuffer->data()), arrayBuffer->byteLength());
96 m_size += buffer.size() - oldSize;
100 void WebKitBlobBuilder::append(Blob* blob)
104 if (blob->isFile()) {
105 // If the blob is file that is not snapshoted, capture the snapshot now.
106 // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous.
107 File* file = static_cast<File*>(blob);
108 long long snapshotSize;
109 double snapshotModificationTime;
110 file->captureSnapshot(snapshotSize, snapshotModificationTime);
112 m_size += snapshotSize;
113 m_items.append(BlobDataItem(file->path(), 0, snapshotSize, snapshotModificationTime));
115 long long blobSize = static_cast<long long>(blob->size());
117 m_items.append(BlobDataItem(blob->url(), 0, blobSize));
121 PassRefPtr<Blob> WebKitBlobBuilder::getBlob(const String& contentType)
123 OwnPtr<BlobData> blobData = BlobData::create();
124 blobData->setContentType(contentType);
125 blobData->swapItems(m_items);
127 RefPtr<Blob> blob = Blob::create(blobData.release(), m_size);
129 // After creating a blob from the current blob data, we do not need to keep the data around any more. Instead, we only
130 // need to keep a reference to the URL of the blob just created.
131 m_items.append(BlobDataItem(blob->url(), 0, m_size));
136 } // namespace WebCore