Fix the incorrect behavior touch sound on single tap event.
[framework/web/webkit-efl.git] / Source / WebCore / fileapi / WebKitBlobBuilder.cpp
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 "Blob.h"
36 #include "ExceptionCode.h"
37 #include "File.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>
48
49 namespace WebCore {
50
51 enum BlobConstructorArrayBufferOrView {
52     BlobConstructorArrayBuffer,
53     BlobConstructorArrayBufferView,
54     BlobConstructorArrayBufferOrViewMax,
55 };
56
57 // static
58 PassRefPtr<WebKitBlobBuilder> WebKitBlobBuilder::create(ScriptExecutionContext* context)
59 {
60     String message("BlobBuilder is deprecated. Use \"Blob\" constructor instead.");
61     context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, message);
62
63     return adoptRef(new WebKitBlobBuilder());
64 }
65
66 WebKitBlobBuilder::WebKitBlobBuilder()
67     : m_size(0)
68 {
69 }
70
71 Vector<char>& WebKitBlobBuilder::getBuffer()
72 {
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()));
76
77     return *m_items[m_items.size() - 1].data->mutableData();
78 }
79
80 void WebKitBlobBuilder::append(const String& text, const String& endingType, ExceptionCode& ec)
81 {
82     bool isEndingTypeTransparent = endingType == "transparent";
83     bool isEndingTypeNative = endingType == "native";
84     if (!endingType.isEmpty() && !isEndingTypeTransparent && !isEndingTypeNative) {
85         ec = SYNTAX_ERR;
86         return;
87     }
88
89     CString utf8Text = UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables);
90
91     Vector<char>& buffer = getBuffer();
92     size_t oldSize = buffer.size();
93
94     if (isEndingTypeNative)
95         normalizeLineEndingsToNative(utf8Text, buffer);
96     else
97         buffer.append(utf8Text.data(), utf8Text.length());
98     m_size += buffer.size() - oldSize;
99 }
100
101 void WebKitBlobBuilder::append(const String& text, ExceptionCode& ec)
102 {
103     append(text, String(), ec);
104 }
105
106 #if ENABLE(BLOB)
107 void WebKitBlobBuilder::append(ScriptExecutionContext* context, ArrayBuffer* arrayBuffer)
108 {
109     String consoleMessage("ArrayBuffer values are deprecated in Blob Constructor. Use ArrayBufferView instead.");
110     context->addConsoleMessage(JSMessageSource, LogMessageType, WarningMessageLevel, consoleMessage);
111
112     HistogramSupport::histogramEnumeration("WebCore.Blob.constructor.ArrayBufferOrView", BlobConstructorArrayBuffer, BlobConstructorArrayBufferOrViewMax);
113
114     if (!arrayBuffer)
115         return;
116
117     appendBytesData(arrayBuffer->data(), arrayBuffer->byteLength());
118 }
119
120 void WebKitBlobBuilder::append(ArrayBufferView* arrayBufferView)
121 {
122     HistogramSupport::histogramEnumeration("WebCore.Blob.constructor.ArrayBufferOrView", BlobConstructorArrayBufferView, BlobConstructorArrayBufferOrViewMax);
123
124     if (!arrayBufferView)
125         return;
126
127     appendBytesData(arrayBufferView->baseAddress(), arrayBufferView->byteLength());
128 }
129 #endif
130
131 void WebKitBlobBuilder::append(Blob* blob)
132 {
133     if (!blob)
134         return;
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);
142
143         m_size += snapshotSize;
144         m_items.append(BlobDataItem(file->path(), 0, snapshotSize, snapshotModificationTime));
145     } else {
146         long long blobSize = static_cast<long long>(blob->size());
147         m_size += blobSize;
148         m_items.append(BlobDataItem(blob->url(), 0, blobSize));
149     }
150 }
151
152
153 void WebKitBlobBuilder::appendBytesData(const void* data, size_t length)
154 {
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;
159 }
160
161 PassRefPtr<Blob> WebKitBlobBuilder::getBlob(const String& contentType)
162 {
163     OwnPtr<BlobData> blobData = BlobData::create();
164     blobData->setContentType(contentType);
165     blobData->swapItems(m_items);
166
167     RefPtr<Blob> blob = Blob::create(blobData.release(), m_size);
168
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));
172
173     return blob;
174 }
175
176 } // namespace WebCore