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 #if ENABLE(FILE_SYSTEM)
35 #include "FileWriter.h"
37 #include "AsyncFileWriter.h"
39 #include "ExceptionCode.h"
40 #include "FileError.h"
41 #include "FileException.h"
42 #include "ProgressEvent.h"
46 FileWriter::FileWriter(ScriptExecutionContext* context)
47 : ActiveDOMObject(context, this)
49 , m_startedWriting(false)
52 , m_truncateLength(-1)
56 FileWriter::~FileWriter()
58 if (m_readyState == WRITING)
62 bool FileWriter::hasPendingActivity() const
64 return m_readyState == WRITING || ActiveDOMObject::hasPendingActivity();
67 bool FileWriter::canSuspend() const
69 // FIXME: It is not currently possible to suspend a FileWriter, so pages with FileWriter can not go into page cache.
73 void FileWriter::stop()
75 if (writer() && m_readyState == WRITING)
77 m_blobBeingWritten.clear();
81 void FileWriter::write(Blob* data, ExceptionCode& ec)
84 if (m_readyState == WRITING) {
85 setError(FileError::INVALID_STATE_ERR, ec);
89 setError(FileError::TYPE_MISMATCH_ERR, ec);
93 m_blobBeingWritten = data;
94 m_readyState = WRITING;
95 m_startedWriting = false;
97 m_bytesToWrite = data->size();
98 writer()->write(position(), data);
101 void FileWriter::seek(long long position, ExceptionCode& ec)
104 if (m_readyState == WRITING) {
105 setError(FileError::INVALID_STATE_ERR, ec);
111 seekInternal(position);
114 void FileWriter::truncate(long long position, ExceptionCode& ec)
117 if (m_readyState == WRITING || position < 0) {
118 setError(FileError::INVALID_STATE_ERR, ec);
121 m_readyState = WRITING;
124 m_truncateLength = position;
125 writer()->truncate(position);
128 void FileWriter::abort(ExceptionCode& ec)
131 if (m_readyState != WRITING) {
132 setError(FileError::INVALID_STATE_ERR, ec);
139 void FileWriter::didWrite(long long bytes, bool complete)
141 ASSERT(bytes + m_bytesWritten > 0);
142 ASSERT(bytes + m_bytesWritten <= m_bytesToWrite);
143 if (!m_startedWriting) {
144 fireEvent(eventNames().writestartEvent);
145 m_startedWriting = true;
147 m_bytesWritten += bytes;
148 ASSERT((m_bytesWritten == m_bytesToWrite) || !complete);
149 setPosition(position() + bytes);
150 if (position() > length())
151 setLength(position());
152 fireEvent(eventNames().progressEvent);
154 m_blobBeingWritten.clear();
155 scriptExecutionContext()->postTask(FileWriterCompletionEventTask::create(this, FileError::OK));
159 void FileWriter::didTruncate()
161 ASSERT(m_truncateLength >= 0);
162 fireEvent(eventNames().writestartEvent);
163 setLength(m_truncateLength);
164 if (position() > length())
165 setPosition(length());
166 m_truncateLength = -1;
167 scriptExecutionContext()->postTask(FileWriterCompletionEventTask::create(this, FileError::OK));
170 void FileWriter::didFail(FileError::ErrorCode code)
172 ASSERT(code != FileError::OK);
173 m_blobBeingWritten.clear();
174 scriptExecutionContext()->postTask(FileWriterCompletionEventTask::create(this, code));
177 void FileWriter::signalCompletion(FileError::ErrorCode code)
180 if (FileError::OK != code) {
181 m_error = FileError::create(code);
182 fireEvent(eventNames().errorEvent);
183 if (FileError::ABORT_ERR == code)
184 fireEvent(eventNames().abortEvent);
186 fireEvent(eventNames().writeEvent);
187 fireEvent(eventNames().writeendEvent);
190 void FileWriter::fireEvent(const AtomicString& type)
192 dispatchEvent(ProgressEvent::create(type, true, m_bytesWritten, m_bytesToWrite));
195 void FileWriter::setError(FileError::ErrorCode errorCode, ExceptionCode& ec)
197 ec = FileException::ErrorCodeToExceptionCode(errorCode);
198 m_error = FileError::create(errorCode);
201 } // namespace WebCore
203 #endif // ENABLE(FILE_SYSTEM)