Fix the incorrect behavior touch sound on single tap event.
[framework/web/webkit-efl.git] / Source / WebCore / fileapi / FileReader.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 #if ENABLE(BLOB)
34
35 #include "FileReader.h"
36
37 #include "CrossThreadTask.h"
38 #include "ExceptionCode.h"
39 #include "File.h"
40 #include "Logging.h"
41 #include "ProgressEvent.h"
42 #include "ScriptExecutionContext.h"
43 #include <wtf/ArrayBuffer.h>
44 #include <wtf/CurrentTime.h>
45 #include <wtf/text/CString.h>
46
47 namespace WebCore {
48
49 static const double progressNotificationIntervalMS = 50;
50
51 PassRefPtr<FileReader> FileReader::create(ScriptExecutionContext* context)
52 {
53     RefPtr<FileReader> fileReader(adoptRef(new FileReader(context)));
54     fileReader->suspendIfNeeded();
55     return fileReader.release();
56 }
57
58 FileReader::FileReader(ScriptExecutionContext* context)
59     : ActiveDOMObject(context, this)
60     , m_state(EMPTY)
61     , m_aborting(false)
62     , m_readType(FileReaderLoader::ReadAsBinaryString)
63     , m_lastProgressNotificationTimeMS(0)
64 {
65 }
66
67 FileReader::~FileReader()
68 {
69     terminate();
70 }
71
72 const AtomicString& FileReader::interfaceName() const
73 {
74     return eventNames().interfaceForFileReader;
75 }
76
77 bool FileReader::canSuspend() const
78 {
79     // FIXME: It is not currently possible to suspend a FileReader, so pages with FileReader can not go into page cache.
80     return false;
81 }
82
83 void FileReader::stop()
84 {
85     terminate();
86 }
87
88 void FileReader::readAsArrayBuffer(Blob* blob, ExceptionCode& ec)
89 {
90     if (!blob)
91         return;
92
93     LOG(FileAPI, "FileReader: reading as array buffer: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? toFile(blob)->path().utf8().data() : "");
94
95     readInternal(blob, FileReaderLoader::ReadAsArrayBuffer, ec);
96 }
97
98 void FileReader::readAsBinaryString(Blob* blob, ExceptionCode& ec)
99 {
100     if (!blob)
101         return;
102
103     LOG(FileAPI, "FileReader: reading as binary: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? toFile(blob)->path().utf8().data() : "");
104
105     readInternal(blob, FileReaderLoader::ReadAsBinaryString, ec);
106 }
107
108 void FileReader::readAsText(Blob* blob, const String& encoding, ExceptionCode& ec)
109 {
110     if (!blob)
111         return;
112
113     LOG(FileAPI, "FileReader: reading as text: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? toFile(blob)->path().utf8().data() : "");
114
115     m_encoding = encoding;
116     readInternal(blob, FileReaderLoader::ReadAsText, ec);
117 }
118
119 void FileReader::readAsText(Blob* blob, ExceptionCode& ec)
120 {
121     readAsText(blob, String(), ec);
122 }
123
124 void FileReader::readAsDataURL(Blob* blob, ExceptionCode& ec)
125 {
126     if (!blob)
127         return;
128
129     LOG(FileAPI, "FileReader: reading as data URL: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? toFile(blob)->path().utf8().data() : "");
130
131     readInternal(blob, FileReaderLoader::ReadAsDataURL, ec);
132 }
133
134 void FileReader::readInternal(Blob* blob, FileReaderLoader::ReadType type, ExceptionCode& ec)
135 {
136     // If multiple concurrent read methods are called on the same FileReader, INVALID_STATE_ERR should be thrown when the state is LOADING.
137     if (m_state == LOADING) {
138         ec = INVALID_STATE_ERR;
139         return;
140     }
141
142     setPendingActivity(this);
143
144     m_blob = blob;
145     m_readType = type;
146     m_state = LOADING;
147     m_error = 0;
148
149     m_loader = adoptPtr(new FileReaderLoader(m_readType, this));
150     m_loader->setEncoding(m_encoding);
151     m_loader->setDataType(m_blob->type());
152     m_loader->start(scriptExecutionContext(), m_blob.get());
153 }
154
155 static void delayedAbort(ScriptExecutionContext*, FileReader* reader)
156 {
157     reader->doAbort();
158 }
159
160 void FileReader::abort()
161 {
162     LOG(FileAPI, "FileReader: aborting\n");
163
164     if (m_aborting)
165         return;
166     m_aborting = true;
167
168     // Schedule to have the abort done later since abort() might be called from the event handler and we do not want the resource loading code to be in the stack.
169     scriptExecutionContext()->postTask(
170         createCallbackTask(&delayedAbort, AllowAccessLater(this)));
171 }
172
173 void FileReader::doAbort()
174 {
175     ASSERT(m_state != DONE);
176
177     terminate();
178     m_aborting = false;
179
180     m_error = FileError::create(FileError::ABORT_ERR);
181
182     fireEvent(eventNames().errorEvent);
183     fireEvent(eventNames().abortEvent);
184     fireEvent(eventNames().loadendEvent);
185
186     // All possible events have fired and we're done, no more pending activity.
187     unsetPendingActivity(this);
188 }
189
190 void FileReader::terminate()
191 {
192     if (m_loader) {
193         m_loader->cancel();
194         m_loader = nullptr;
195     }
196     m_state = DONE;
197 }
198
199 void FileReader::didStartLoading()
200 {
201     fireEvent(eventNames().loadstartEvent);
202 }
203
204 void FileReader::didReceiveData()
205 {
206     // Fire the progress event at least every 50ms.
207     double now = currentTimeMS();
208     if (!m_lastProgressNotificationTimeMS)
209         m_lastProgressNotificationTimeMS = now;
210     else if (now - m_lastProgressNotificationTimeMS > progressNotificationIntervalMS) {
211         fireEvent(eventNames().progressEvent);
212         m_lastProgressNotificationTimeMS = now;
213     }
214 }
215
216 void FileReader::didFinishLoading()
217 {
218     ASSERT(m_state != DONE);
219     m_state = DONE;
220
221     fireEvent(eventNames().progressEvent);
222     fireEvent(eventNames().loadEvent);
223     fireEvent(eventNames().loadendEvent);
224     
225     // All possible events have fired and we're done, no more pending activity.
226     unsetPendingActivity(this);
227 }
228
229 void FileReader::didFail(int errorCode)
230 {
231     // If we're aborting, do not proceed with normal error handling since it is covered in aborting code.
232     if (m_aborting)
233         return;
234
235     ASSERT(m_state != DONE);
236     m_state = DONE;
237
238     m_error = FileError::create(static_cast<FileError::ErrorCode>(errorCode));
239     fireEvent(eventNames().errorEvent);
240     fireEvent(eventNames().loadendEvent);
241     
242     // All possible events have fired and we're done, no more pending activity.
243     unsetPendingActivity(this);
244 }
245
246 void FileReader::fireEvent(const AtomicString& type)
247 {
248     dispatchEvent(ProgressEvent::create(type, true, m_loader ? m_loader->bytesLoaded() : 0, m_loader ? m_loader->totalBytes() : 0));
249 }
250
251 PassRefPtr<ArrayBuffer> FileReader::arrayBufferResult() const
252 {
253     if (!m_loader || m_error)
254         return 0;
255     return m_loader->arrayBufferResult();
256 }
257
258 String FileReader::stringResult()
259 {
260     if (!m_loader || m_error)
261         return String();
262     return m_loader->stringResult();
263 }
264
265 } // namespace WebCore
266  
267 #endif // ENABLE(BLOB)