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 #include "modules/filesystem/FileWriter.h"
33
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"
41
42 namespace WebCore {
43
44 static const int kMaxRecursionDepth = 3;
45 static const double progressNotificationIntervalMS = 50;
46
create(ExecutionContext * context)47 PassRefPtr<FileWriter> FileWriter::create(ExecutionContext* context)
48 {
49 RefPtr<FileWriter> fileWriter(adoptRef(new FileWriter(context)));
50 fileWriter->suspendIfNeeded();
51 return fileWriter.release();
52 }
53
FileWriter(ExecutionContext * context)54 FileWriter::FileWriter(ExecutionContext* context)
55 : ActiveDOMObject(context)
56 , m_readyState(INIT)
57 , m_operationInProgress(OperationNone)
58 , m_queuedOperation(OperationNone)
59 , m_bytesWritten(0)
60 , m_bytesToWrite(0)
61 , m_truncateLength(-1)
62 , m_numAborts(0)
63 , m_recursionDepth(0)
64 , m_lastProgressNotificationTimeMS(0)
65 {
66 ScriptWrappable::init(this);
67 }
68
~FileWriter()69 FileWriter::~FileWriter()
70 {
71 ASSERT(!m_recursionDepth);
72 if (m_readyState == WRITING)
73 stop();
74 }
75
interfaceName() const76 const AtomicString& FileWriter::interfaceName() const
77 {
78 return EventTargetNames::FileWriter;
79 }
80
stop()81 void FileWriter::stop()
82 {
83 // Make sure we've actually got something to stop, and haven't already called abort().
84 if (!writer() || m_readyState != WRITING)
85 return;
86 doOperation(OperationAbort);
87 m_readyState = DONE;
88 }
89
write(Blob * data,ExceptionState & exceptionState)90 void FileWriter::write(Blob* data, ExceptionState& exceptionState)
91 {
92 ASSERT(writer());
93 ASSERT(data);
94 ASSERT(m_truncateLength == -1);
95 if (m_readyState == WRITING) {
96 setError(FileError::INVALID_STATE_ERR, exceptionState);
97 return;
98 }
99 if (!data) {
100 setError(FileError::TYPE_MISMATCH_ERR, exceptionState);
101 return;
102 }
103 if (m_recursionDepth > kMaxRecursionDepth) {
104 setError(FileError::SECURITY_ERR, exceptionState);
105 return;
106 }
107
108 m_blobBeingWritten = data;
109 m_readyState = WRITING;
110 m_bytesWritten = 0;
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;
117 } else
118 doOperation(OperationWrite);
119
120 fireEvent(EventTypeNames::writestart);
121 }
122
seek(long long position,ExceptionState & exceptionState)123 void FileWriter::seek(long long position, ExceptionState& exceptionState)
124 {
125 ASSERT(writer());
126 if (m_readyState == WRITING) {
127 setError(FileError::INVALID_STATE_ERR, exceptionState);
128 return;
129 }
130
131 ASSERT(m_truncateLength == -1);
132 ASSERT(m_queuedOperation == OperationNone);
133 seekInternal(position);
134 }
135
truncate(long long position,ExceptionState & exceptionState)136 void FileWriter::truncate(long long position, ExceptionState& exceptionState)
137 {
138 ASSERT(writer());
139 ASSERT(m_truncateLength == -1);
140 if (m_readyState == WRITING || position < 0) {
141 setError(FileError::INVALID_STATE_ERR, exceptionState);
142 return;
143 }
144 if (m_recursionDepth > kMaxRecursionDepth) {
145 setError(FileError::SECURITY_ERR, exceptionState);
146 return;
147 }
148
149 m_readyState = WRITING;
150 m_bytesWritten = 0;
151 m_bytesToWrite = 0;
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;
158 } else
159 doOperation(OperationTruncate);
160 fireEvent(EventTypeNames::writestart);
161 }
162
abort(ExceptionState & exceptionState)163 void FileWriter::abort(ExceptionState& exceptionState)
164 {
165 ASSERT(writer());
166 if (m_readyState != WRITING)
167 return;
168 ++m_numAborts;
169
170 doOperation(OperationAbort);
171 signalCompletion(FileError::ABORT_ERR);
172 }
173
didWrite(long long bytes,bool complete)174 void FileWriter::didWrite(long long bytes, bool complete)
175 {
176 if (m_operationInProgress == OperationAbort) {
177 completeAbort();
178 return;
179 }
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());
190 if (complete) {
191 m_blobBeingWritten.clear();
192 m_operationInProgress = OperationNone;
193 }
194
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);
202 }
203
204 if (complete) {
205 if (numAborts == m_numAborts)
206 signalCompletion(FileError::OK);
207 unsetPendingActivity(this);
208 }
209 }
210
didTruncate()211 void FileWriter::didTruncate()
212 {
213 if (m_operationInProgress == OperationAbort) {
214 completeAbort();
215 return;
216 }
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);
225 }
226
didFail(blink::WebFileError code)227 void FileWriter::didFail(blink::WebFileError code)
228 {
229 ASSERT(m_operationInProgress != OperationNone);
230 ASSERT(static_cast<FileError::ErrorCode>(code) != FileError::OK);
231 if (m_operationInProgress == OperationAbort) {
232 completeAbort();
233 return;
234 }
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);
242 }
243
completeAbort()244 void FileWriter::completeAbort()
245 {
246 ASSERT(m_operationInProgress == OperationAbort);
247 m_operationInProgress = OperationNone;
248 Operation operation = m_queuedOperation;
249 m_queuedOperation = OperationNone;
250 doOperation(operation);
251 unsetPendingActivity(this);
252 }
253
doOperation(Operation operation)254 void FileWriter::doOperation(Operation operation)
255 {
256 switch (operation) {
257 case OperationWrite:
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());
264 break;
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);
271 break;
272 case OperationNone:
273 ASSERT(m_operationInProgress == OperationNone);
274 ASSERT(m_truncateLength == -1);
275 ASSERT(!m_blobBeingWritten.get());
276 ASSERT(m_readyState == DONE);
277 break;
278 case OperationAbort:
279 if (m_operationInProgress == OperationWrite || m_operationInProgress == OperationTruncate)
280 writer()->cancel();
281 else if (m_operationInProgress != OperationAbort)
282 operation = OperationNone;
283 m_queuedOperation = OperationNone;
284 m_blobBeingWritten.clear();
285 m_truncateLength = -1;
286 break;
287 }
288 ASSERT(m_queuedOperation == OperationNone);
289 m_operationInProgress = operation;
290 }
291
signalCompletion(FileError::ErrorCode code)292 void FileWriter::signalCompletion(FileError::ErrorCode code)
293 {
294 m_readyState = DONE;
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);
300 else
301 fireEvent(EventTypeNames::error);
302 } else
303 fireEvent(EventTypeNames::write);
304 fireEvent(EventTypeNames::writeend);
305 }
306
fireEvent(const AtomicString & type)307 void FileWriter::fireEvent(const AtomicString& type)
308 {
309 ++m_recursionDepth;
310 dispatchEvent(ProgressEvent::create(type, true, m_bytesWritten, m_bytesToWrite));
311 --m_recursionDepth;
312 ASSERT(m_recursionDepth >= 0);
313 }
314
setError(FileError::ErrorCode errorCode,ExceptionState & exceptionState)315 void FileWriter::setError(FileError::ErrorCode errorCode, ExceptionState& exceptionState)
316 {
317 ASSERT(errorCode);
318 FileError::throwDOMException(exceptionState, errorCode);
319 m_error = FileError::create(errorCode);
320 }
321
322 } // namespace WebCore
323