1/* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31#include "config.h" 32 33#include "WebKitBlobBuilder.h" 34 35#include "ArrayBuffer.h" 36#include "Blob.h" 37#include "ExceptionCode.h" 38#include "File.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> 45 46namespace WebCore { 47 48WebKitBlobBuilder::WebKitBlobBuilder() 49 : m_size(0) 50{ 51} 52 53Vector<char>& WebKitBlobBuilder::getBuffer() 54{ 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())); 58 59 return *m_items[m_items.size() - 1].data->mutableData(); 60} 61 62void WebKitBlobBuilder::append(const String& text, const String& endingType, ExceptionCode& ec) 63{ 64 bool isEndingTypeTransparent = endingType == "transparent"; 65 bool isEndingTypeNative = endingType == "native"; 66 if (!endingType.isEmpty() && !isEndingTypeTransparent && !isEndingTypeNative) { 67 ec = SYNTAX_ERR; 68 return; 69 } 70 71 CString utf8Text = UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables); 72 73 Vector<char>& buffer = getBuffer(); 74 size_t oldSize = buffer.size(); 75 76 if (isEndingTypeNative) 77 normalizeLineEndingsToNative(utf8Text, buffer); 78 else 79 buffer.append(utf8Text.data(), utf8Text.length()); 80 m_size += buffer.size() - oldSize; 81} 82 83void WebKitBlobBuilder::append(const String& text, ExceptionCode& ec) 84{ 85 append(text, String(), ec); 86} 87 88#if ENABLE(BLOB) 89void WebKitBlobBuilder::append(ArrayBuffer* arrayBuffer) 90{ 91 if (!arrayBuffer) 92 return; 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; 97} 98#endif 99 100void WebKitBlobBuilder::append(Blob* blob) 101{ 102 if (!blob) 103 return; 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); 111 112 m_size += snapshotSize; 113 m_items.append(BlobDataItem(file->path(), 0, snapshotSize, snapshotModificationTime)); 114 } else { 115 long long blobSize = static_cast<long long>(blob->size()); 116 m_size += blobSize; 117 m_items.append(BlobDataItem(blob->url(), 0, blobSize)); 118 } 119} 120 121PassRefPtr<Blob> WebKitBlobBuilder::getBlob(const String& contentType) 122{ 123 OwnPtr<BlobData> blobData = BlobData::create(); 124 blobData->setContentType(contentType); 125 blobData->swapItems(m_items); 126 127 RefPtr<Blob> blob = Blob::create(blobData.release(), m_size); 128 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)); 132 133 return blob; 134} 135 136} // namespace WebCore 137