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.
32 #include "modules/filesystem/FileWriter.h"
34 #include "bindings/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "core/events/ProgressEvent.h"
37 #include "core/fileapi/Blob.h"
38 #include "public/platform/WebFileWriter.h"
39 #include "public/platform/WebURL.h"
40 #include "wtf/CurrentTime.h"
44 static const int kMaxRecursionDepth = 3;
45 static const double progressNotificationIntervalMS = 50;
47 PassRefPtrWillBeRawPtr<FileWriter> FileWriter::create(ExecutionContext* context)
49 RefPtrWillBeRawPtr<FileWriter> fileWriter(adoptRefWillBeRefCountedGarbageCollected(new FileWriter(context)));
50 fileWriter->suspendIfNeeded();
51 return fileWriter.release();
54 FileWriter::FileWriter(ExecutionContext* context)
55 : ActiveDOMObject(context)
57 , m_operationInProgress(OperationNone)
58 , m_queuedOperation(OperationNone)
61 , m_truncateLength(-1)
64 , m_lastProgressNotificationTimeMS(0)
66 ScriptWrappable::init(this);
69 FileWriter::~FileWriter()
71 ASSERT(!m_recursionDepth);
72 if (m_readyState == WRITING)
76 const AtomicString& FileWriter::interfaceName() const
78 return EventTargetNames::FileWriter;
81 void FileWriter::stop()
83 // Make sure we've actually got something to stop, and haven't already called abort().
84 if (!writer() || m_readyState != WRITING)
86 doOperation(OperationAbort);
90 void FileWriter::write(Blob* data, ExceptionState& exceptionState)
94 ASSERT(m_truncateLength == -1);
95 if (m_readyState == WRITING) {
96 setError(FileError::INVALID_STATE_ERR, exceptionState);
100 setError(FileError::TYPE_MISMATCH_ERR, exceptionState);
103 if (m_recursionDepth > kMaxRecursionDepth) {
104 setError(FileError::SECURITY_ERR, exceptionState);
108 m_blobBeingWritten = data;
109 m_readyState = WRITING;
111 m_bytesToWrite = data->size();
112 ASSERT(m_queuedOperation == OperationNone);
113 if (m_operationInProgress != OperationNone) {
114 // We must be waiting for an abort to complete, since m_readyState wasn't WRITING.
115 ASSERT(m_operationInProgress == OperationAbort);
116 m_queuedOperation = OperationWrite;
118 doOperation(OperationWrite);
120 fireEvent(EventTypeNames::writestart);
123 void FileWriter::seek(long long position, ExceptionState& exceptionState)
126 if (m_readyState == WRITING) {
127 setError(FileError::INVALID_STATE_ERR, exceptionState);
131 ASSERT(m_truncateLength == -1);
132 ASSERT(m_queuedOperation == OperationNone);
133 seekInternal(position);
136 void FileWriter::truncate(long long position, ExceptionState& exceptionState)
139 ASSERT(m_truncateLength == -1);
140 if (m_readyState == WRITING || position < 0) {
141 setError(FileError::INVALID_STATE_ERR, exceptionState);
144 if (m_recursionDepth > kMaxRecursionDepth) {
145 setError(FileError::SECURITY_ERR, exceptionState);
149 m_readyState = WRITING;
152 m_truncateLength = position;
153 ASSERT(m_queuedOperation == OperationNone);
154 if (m_operationInProgress != OperationNone) {
155 // We must be waiting for an abort to complete, since m_readyState wasn't WRITING.
156 ASSERT(m_operationInProgress == OperationAbort);
157 m_queuedOperation = OperationTruncate;
159 doOperation(OperationTruncate);
160 fireEvent(EventTypeNames::writestart);
163 void FileWriter::abort(ExceptionState& exceptionState)
166 if (m_readyState != WRITING)
170 doOperation(OperationAbort);
171 signalCompletion(FileError::ABORT_ERR);
174 void FileWriter::didWrite(long long bytes, bool complete)
176 if (m_operationInProgress == OperationAbort) {
180 ASSERT(m_readyState == WRITING);
181 ASSERT(m_truncateLength == -1);
182 ASSERT(m_operationInProgress == OperationWrite);
183 ASSERT(!m_bytesToWrite || bytes + m_bytesWritten > 0);
184 ASSERT(bytes + m_bytesWritten <= m_bytesToWrite);
185 m_bytesWritten += bytes;
186 ASSERT((m_bytesWritten == m_bytesToWrite) || !complete);
187 setPosition(position() + bytes);
188 if (position() > length())
189 setLength(position());
191 m_blobBeingWritten.clear();
192 m_operationInProgress = OperationNone;
195 int numAborts = m_numAborts;
196 // We could get an abort in the handler for this event. If we do, it's
197 // already handled the cleanup and signalCompletion call.
198 double now = currentTimeMS();
199 if (complete || !m_lastProgressNotificationTimeMS || (now - m_lastProgressNotificationTimeMS > progressNotificationIntervalMS)) {
200 m_lastProgressNotificationTimeMS = now;
201 fireEvent(EventTypeNames::progress);
205 if (numAborts == m_numAborts)
206 signalCompletion(FileError::OK);
207 unsetPendingActivity(this);
211 void FileWriter::didTruncate()
213 if (m_operationInProgress == OperationAbort) {
217 ASSERT(m_operationInProgress == OperationTruncate);
218 ASSERT(m_truncateLength >= 0);
219 setLength(m_truncateLength);
220 if (position() > length())
221 setPosition(length());
222 m_operationInProgress = OperationNone;
223 signalCompletion(FileError::OK);
224 unsetPendingActivity(this);
227 void FileWriter::didFail(blink::WebFileError code)
229 ASSERT(m_operationInProgress != OperationNone);
230 ASSERT(static_cast<FileError::ErrorCode>(code) != FileError::OK);
231 if (m_operationInProgress == OperationAbort) {
235 ASSERT(static_cast<FileError::ErrorCode>(code) != FileError::ABORT_ERR);
236 ASSERT(m_queuedOperation == OperationNone);
237 ASSERT(m_readyState == WRITING);
238 m_blobBeingWritten.clear();
239 m_operationInProgress = OperationNone;
240 signalCompletion(static_cast<FileError::ErrorCode>(code));
241 unsetPendingActivity(this);
244 void FileWriter::completeAbort()
246 ASSERT(m_operationInProgress == OperationAbort);
247 m_operationInProgress = OperationNone;
248 Operation operation = m_queuedOperation;
249 m_queuedOperation = OperationNone;
250 doOperation(operation);
251 unsetPendingActivity(this);
254 void FileWriter::doOperation(Operation operation)
258 ASSERT(m_operationInProgress == OperationNone);
259 ASSERT(m_truncateLength == -1);
260 ASSERT(m_blobBeingWritten.get());
261 ASSERT(m_readyState == WRITING);
262 setPendingActivity(this);
263 writer()->write(position(), m_blobBeingWritten->uuid());
265 case OperationTruncate:
266 ASSERT(m_operationInProgress == OperationNone);
267 ASSERT(m_truncateLength >= 0);
268 ASSERT(m_readyState == WRITING);
269 setPendingActivity(this);
270 writer()->truncate(m_truncateLength);
273 ASSERT(m_operationInProgress == OperationNone);
274 ASSERT(m_truncateLength == -1);
275 ASSERT(!m_blobBeingWritten.get());
276 ASSERT(m_readyState == DONE);
279 if (m_operationInProgress == OperationWrite || m_operationInProgress == OperationTruncate)
281 else if (m_operationInProgress != OperationAbort)
282 operation = OperationNone;
283 m_queuedOperation = OperationNone;
284 m_blobBeingWritten.clear();
285 m_truncateLength = -1;
288 ASSERT(m_queuedOperation == OperationNone);
289 m_operationInProgress = operation;
292 void FileWriter::signalCompletion(FileError::ErrorCode code)
295 m_truncateLength = -1;
296 if (FileError::OK != code) {
297 m_error = FileError::create(code);
298 if (FileError::ABORT_ERR == code)
299 fireEvent(EventTypeNames::abort);
301 fireEvent(EventTypeNames::error);
303 fireEvent(EventTypeNames::write);
304 fireEvent(EventTypeNames::writeend);
307 void FileWriter::fireEvent(const AtomicString& type)
310 dispatchEvent(ProgressEvent::create(type, true, m_bytesWritten, m_bytesToWrite));
312 ASSERT(m_recursionDepth >= 0);
315 void FileWriter::setError(FileError::ErrorCode errorCode, ExceptionState& exceptionState)
318 FileError::throwDOMException(exceptionState, errorCode);
319 m_error = FileError::create(errorCode);
322 void FileWriter::trace(Visitor* visitor)
324 visitor->trace(m_error);
325 visitor->trace(m_blobBeingWritten);
326 FileWriterBase::trace(visitor);
329 } // namespace WebCore