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 FileWriter* FileWriter::create(ExecutionContext* context)
48 {
49 FileWriter* fileWriter = adoptRefCountedGarbageCollected(new FileWriter(context));
50 fileWriter->suspendIfNeeded();
51 return fileWriter;
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(m_queuedOperation == OperationNone);
236 ASSERT(m_readyState == WRITING);
237 m_blobBeingWritten.clear();
238 m_operationInProgress = OperationNone;
239 signalCompletion(static_cast<FileError::ErrorCode>(code));
240 unsetPendingActivity(this);
241 }
242
completeAbort()243 void FileWriter::completeAbort()
244 {
245 ASSERT(m_operationInProgress == OperationAbort);
246 m_operationInProgress = OperationNone;
247 Operation operation = m_queuedOperation;
248 m_queuedOperation = OperationNone;
249 doOperation(operation);
250 unsetPendingActivity(this);
251 }
252
doOperation(Operation operation)253 void FileWriter::doOperation(Operation operation)
254 {
255 switch (operation) {
256 case OperationWrite:
257 ASSERT(m_operationInProgress == OperationNone);
258 ASSERT(m_truncateLength == -1);
259 ASSERT(m_blobBeingWritten.get());
260 ASSERT(m_readyState == WRITING);
261 setPendingActivity(this);
262 writer()->write(position(), m_blobBeingWritten->uuid());
263 break;
264 case OperationTruncate:
265 ASSERT(m_operationInProgress == OperationNone);
266 ASSERT(m_truncateLength >= 0);
267 ASSERT(m_readyState == WRITING);
268 setPendingActivity(this);
269 writer()->truncate(m_truncateLength);
270 break;
271 case OperationNone:
272 ASSERT(m_operationInProgress == OperationNone);
273 ASSERT(m_truncateLength == -1);
274 ASSERT(!m_blobBeingWritten.get());
275 ASSERT(m_readyState == DONE);
276 break;
277 case OperationAbort:
278 if (m_operationInProgress == OperationWrite || m_operationInProgress == OperationTruncate)
279 writer()->cancel();
280 else if (m_operationInProgress != OperationAbort)
281 operation = OperationNone;
282 m_queuedOperation = OperationNone;
283 m_blobBeingWritten.clear();
284 m_truncateLength = -1;
285 break;
286 }
287 ASSERT(m_queuedOperation == OperationNone);
288 m_operationInProgress = operation;
289 }
290
signalCompletion(FileError::ErrorCode code)291 void FileWriter::signalCompletion(FileError::ErrorCode code)
292 {
293 m_readyState = DONE;
294 m_truncateLength = -1;
295 if (FileError::OK != code) {
296 m_error = FileError::create(code);
297 if (FileError::ABORT_ERR == code)
298 fireEvent(EventTypeNames::abort);
299 else
300 fireEvent(EventTypeNames::error);
301 } else
302 fireEvent(EventTypeNames::write);
303 fireEvent(EventTypeNames::writeend);
304 }
305
fireEvent(const AtomicString & type)306 void FileWriter::fireEvent(const AtomicString& type)
307 {
308 ++m_recursionDepth;
309 dispatchEvent(ProgressEvent::create(type, true, m_bytesWritten, m_bytesToWrite));
310 --m_recursionDepth;
311 ASSERT(m_recursionDepth >= 0);
312 }
313
setError(FileError::ErrorCode errorCode,ExceptionState & exceptionState)314 void FileWriter::setError(FileError::ErrorCode errorCode, ExceptionState& exceptionState)
315 {
316 ASSERT(errorCode);
317 FileError::throwDOMException(exceptionState, errorCode);
318 m_error = FileError::create(errorCode);
319 }
320
trace(Visitor * visitor)321 void FileWriter::trace(Visitor* visitor)
322 {
323 visitor->trace(m_error);
324 visitor->trace(m_blobBeingWritten);
325 FileWriterBase::trace(visitor);
326 EventTargetWithInlineData::trace(visitor);
327 }
328
329 } // namespace WebCore
330