initial import
[vuplus_webkit] / Source / WebCore / fileapi / FileWriter.cpp
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
33 #if ENABLE(FILE_SYSTEM)
34
35 #include "FileWriter.h"
36
37 #include "AsyncFileWriter.h"
38 #include "Blob.h"
39 #include "ExceptionCode.h"
40 #include "FileError.h"
41 #include "FileException.h"
42 #include "ProgressEvent.h"
43
44 namespace WebCore {
45
46 FileWriter::FileWriter(ScriptExecutionContext* context)
47     : ActiveDOMObject(context, this)
48     , m_readyState(INIT)
49     , m_startedWriting(false)
50     , m_bytesWritten(0)
51     , m_bytesToWrite(0)
52     , m_truncateLength(-1)
53 {
54 }
55
56 FileWriter::~FileWriter()
57 {
58     if (m_readyState == WRITING)
59         stop();
60 }
61
62 bool FileWriter::hasPendingActivity() const
63 {
64     return m_readyState == WRITING || ActiveDOMObject::hasPendingActivity();
65 }
66
67 bool FileWriter::canSuspend() const
68 {
69     // FIXME: It is not currently possible to suspend a FileWriter, so pages with FileWriter can not go into page cache.
70     return false;
71 }
72
73 void FileWriter::stop()
74 {
75     if (writer() && m_readyState == WRITING)
76         writer()->abort();
77     m_blobBeingWritten.clear();
78     m_readyState = DONE;
79 }
80
81 void FileWriter::write(Blob* data, ExceptionCode& ec)
82 {
83     ASSERT(writer());
84     if (m_readyState == WRITING) {
85         setError(FileError::INVALID_STATE_ERR, ec);
86         return;
87     }
88     if (!data) {
89         setError(FileError::TYPE_MISMATCH_ERR, ec);
90         return;
91     }
92
93     m_blobBeingWritten = data;
94     m_readyState = WRITING;
95     m_startedWriting = false;
96     m_bytesWritten = 0;
97     m_bytesToWrite = data->size();
98     writer()->write(position(), data);
99 }
100
101 void FileWriter::seek(long long position, ExceptionCode& ec)
102 {
103     ASSERT(writer());
104     if (m_readyState == WRITING) {
105         setError(FileError::INVALID_STATE_ERR, ec);
106         return;
107     }
108
109     m_bytesWritten = 0;
110     m_bytesToWrite = 0;
111     seekInternal(position);
112 }
113
114 void FileWriter::truncate(long long position, ExceptionCode& ec)
115 {
116     ASSERT(writer());
117     if (m_readyState == WRITING || position < 0) {
118         setError(FileError::INVALID_STATE_ERR, ec);
119         return;
120     }
121     m_readyState = WRITING;
122     m_bytesWritten = 0;
123     m_bytesToWrite = 0;
124     m_truncateLength = position;
125     writer()->truncate(position);
126 }
127
128 void FileWriter::abort(ExceptionCode& ec)
129 {
130     ASSERT(writer());
131     if (m_readyState != WRITING) {
132         setError(FileError::INVALID_STATE_ERR, ec);
133         return;
134     }
135
136     writer()->abort();
137 }
138
139 void FileWriter::didWrite(long long bytes, bool complete)
140 {
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;
146     }
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);
153     if (complete) {
154         m_blobBeingWritten.clear();
155         scriptExecutionContext()->postTask(FileWriterCompletionEventTask::create(this, FileError::OK));
156     }
157 }
158
159 void FileWriter::didTruncate()
160 {
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));
168 }
169
170 void FileWriter::didFail(FileError::ErrorCode code)
171 {
172     ASSERT(code != FileError::OK);
173     m_blobBeingWritten.clear();
174     scriptExecutionContext()->postTask(FileWriterCompletionEventTask::create(this, code));
175 }
176
177 void FileWriter::signalCompletion(FileError::ErrorCode code)
178 {
179     m_readyState = DONE;
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);
185     } else
186         fireEvent(eventNames().writeEvent);
187     fireEvent(eventNames().writeendEvent);
188 }
189
190 void FileWriter::fireEvent(const AtomicString& type)
191 {
192     dispatchEvent(ProgressEvent::create(type, true, m_bytesWritten, m_bytesToWrite));
193 }
194
195 void FileWriter::setError(FileError::ErrorCode errorCode, ExceptionCode& ec)
196 {
197     ec = FileException::ErrorCodeToExceptionCode(errorCode);
198     m_error = FileError::create(errorCode);
199 }
200
201 } // namespace WebCore
202
203 #endif // ENABLE(FILE_SYSTEM)