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.
35 #include "FileReader.h"
37 #include "CrossThreadTask.h"
38 #include "ExceptionCode.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>
49 static const double progressNotificationIntervalMS = 50;
51 PassRefPtr<FileReader> FileReader::create(ScriptExecutionContext* context)
53 RefPtr<FileReader> fileReader(adoptRef(new FileReader(context)));
54 fileReader->suspendIfNeeded();
55 return fileReader.release();
58 FileReader::FileReader(ScriptExecutionContext* context)
59 : ActiveDOMObject(context, this)
62 , m_readType(FileReaderLoader::ReadAsBinaryString)
63 , m_lastProgressNotificationTimeMS(0)
67 FileReader::~FileReader()
72 const AtomicString& FileReader::interfaceName() const
74 return eventNames().interfaceForFileReader;
77 bool FileReader::canSuspend() const
79 // FIXME: It is not currently possible to suspend a FileReader, so pages with FileReader can not go into page cache.
83 void FileReader::stop()
88 void FileReader::readAsArrayBuffer(Blob* blob, ExceptionCode& ec)
93 LOG(FileAPI, "FileReader: reading as array buffer: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? toFile(blob)->path().utf8().data() : "");
95 readInternal(blob, FileReaderLoader::ReadAsArrayBuffer, ec);
98 void FileReader::readAsBinaryString(Blob* blob, ExceptionCode& ec)
103 LOG(FileAPI, "FileReader: reading as binary: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? toFile(blob)->path().utf8().data() : "");
105 readInternal(blob, FileReaderLoader::ReadAsBinaryString, ec);
108 void FileReader::readAsText(Blob* blob, const String& encoding, ExceptionCode& ec)
113 LOG(FileAPI, "FileReader: reading as text: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? toFile(blob)->path().utf8().data() : "");
115 m_encoding = encoding;
116 readInternal(blob, FileReaderLoader::ReadAsText, ec);
119 void FileReader::readAsText(Blob* blob, ExceptionCode& ec)
121 readAsText(blob, String(), ec);
124 void FileReader::readAsDataURL(Blob* blob, ExceptionCode& ec)
129 LOG(FileAPI, "FileReader: reading as data URL: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? toFile(blob)->path().utf8().data() : "");
131 readInternal(blob, FileReaderLoader::ReadAsDataURL, ec);
134 void FileReader::readInternal(Blob* blob, FileReaderLoader::ReadType type, ExceptionCode& ec)
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;
142 setPendingActivity(this);
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());
155 static void delayedAbort(ScriptExecutionContext*, FileReader* reader)
160 void FileReader::abort()
162 LOG(FileAPI, "FileReader: aborting\n");
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)));
173 void FileReader::doAbort()
175 ASSERT(m_state != DONE);
180 m_error = FileError::create(FileError::ABORT_ERR);
182 fireEvent(eventNames().errorEvent);
183 fireEvent(eventNames().abortEvent);
184 fireEvent(eventNames().loadendEvent);
186 // All possible events have fired and we're done, no more pending activity.
187 unsetPendingActivity(this);
190 void FileReader::terminate()
199 void FileReader::didStartLoading()
201 fireEvent(eventNames().loadstartEvent);
204 void FileReader::didReceiveData()
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;
216 void FileReader::didFinishLoading()
218 ASSERT(m_state != DONE);
221 fireEvent(eventNames().progressEvent);
222 fireEvent(eventNames().loadEvent);
223 fireEvent(eventNames().loadendEvent);
225 // All possible events have fired and we're done, no more pending activity.
226 unsetPendingActivity(this);
229 void FileReader::didFail(int errorCode)
231 // If we're aborting, do not proceed with normal error handling since it is covered in aborting code.
235 ASSERT(m_state != DONE);
238 m_error = FileError::create(static_cast<FileError::ErrorCode>(errorCode));
239 fireEvent(eventNames().errorEvent);
240 fireEvent(eventNames().loadendEvent);
242 // All possible events have fired and we're done, no more pending activity.
243 unsetPendingActivity(this);
246 void FileReader::fireEvent(const AtomicString& type)
248 dispatchEvent(ProgressEvent::create(type, true, m_loader ? m_loader->bytesLoaded() : 0, m_loader ? m_loader->totalBytes() : 0));
251 PassRefPtr<ArrayBuffer> FileReader::arrayBufferResult() const
253 if (!m_loader || m_error)
255 return m_loader->arrayBufferResult();
258 String FileReader::stringResult()
260 if (!m_loader || m_error)
262 return m_loader->stringResult();
265 } // namespace WebCore
267 #endif // ENABLE(BLOB)