OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/browser/fileapi/file_writer_delegate.h" | 5 #include "webkit/browser/fileapi/file_writer_delegate.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file_util_proxy.h" | 9 #include "base/files/file_util_proxy.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "webkit/browser/fileapi/file_stream_writer.h" | 15 #include "webkit/browser/fileapi/file_stream_writer.h" |
16 #include "webkit/browser/fileapi/file_system_context.h" | 16 #include "webkit/browser/fileapi/file_system_context.h" |
17 #include "webkit/common/fileapi/file_system_util.h" | 17 #include "webkit/common/fileapi/file_system_util.h" |
18 | 18 |
19 namespace fileapi { | 19 namespace fileapi { |
20 | 20 |
21 static const int kReadBufSize = 32768; | 21 static const int kReadBufSize = 32768; |
22 | 22 |
23 FileWriterDelegate::FileWriterDelegate( | 23 FileWriterDelegate::FileWriterDelegate( |
24 scoped_ptr<FileStreamWriter> file_stream_writer) | 24 scoped_ptr<FileStreamWriter> file_stream_writer, |
| 25 FlushPolicy flush_policy) |
25 : file_stream_writer_(file_stream_writer.Pass()), | 26 : file_stream_writer_(file_stream_writer.Pass()), |
26 writing_started_(false), | 27 writing_started_(false), |
| 28 flush_policy_(flush_policy), |
27 bytes_written_backlog_(0), | 29 bytes_written_backlog_(0), |
28 bytes_written_(0), | 30 bytes_written_(0), |
29 bytes_read_(0), | 31 bytes_read_(0), |
30 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), | 32 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), |
31 weak_factory_(this) { | 33 weak_factory_(this) { |
32 } | 34 } |
33 | 35 |
34 FileWriterDelegate::~FileWriterDelegate() { | 36 FileWriterDelegate::~FileWriterDelegate() { |
35 } | 37 } |
36 | 38 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 return writing_started_ ? ERROR_WRITE_STARTED : ERROR_WRITE_NOT_STARTED; | 170 return writing_started_ ? ERROR_WRITE_STARTED : ERROR_WRITE_NOT_STARTED; |
169 } | 171 } |
170 | 172 |
171 void FileWriterDelegate::OnError(base::File::Error error) { | 173 void FileWriterDelegate::OnError(base::File::Error error) { |
172 if (request_) { | 174 if (request_) { |
173 request_->set_delegate(NULL); | 175 request_->set_delegate(NULL); |
174 request_->Cancel(); | 176 request_->Cancel(); |
175 } | 177 } |
176 | 178 |
177 if (writing_started_) | 179 if (writing_started_) |
178 FlushForCompletion(error, 0, ERROR_WRITE_STARTED); | 180 MaybeFlushForCompletion(error, 0, ERROR_WRITE_STARTED); |
179 else | 181 else |
180 write_callback_.Run(error, 0, ERROR_WRITE_NOT_STARTED); | 182 write_callback_.Run(error, 0, ERROR_WRITE_NOT_STARTED); |
181 } | 183 } |
182 | 184 |
183 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { | 185 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { |
184 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); | 186 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); |
185 static const int kMinProgressDelayMS = 200; | 187 static const int kMinProgressDelayMS = 200; |
186 base::Time currentTime = base::Time::Now(); | 188 base::Time currentTime = base::Time::Now(); |
187 if (done || last_progress_event_time_.is_null() || | 189 if (done || last_progress_event_time_.is_null() || |
188 (currentTime - last_progress_event_time_).InMilliseconds() > | 190 (currentTime - last_progress_event_time_).InMilliseconds() > |
189 kMinProgressDelayMS) { | 191 kMinProgressDelayMS) { |
190 bytes_written += bytes_written_backlog_; | 192 bytes_written += bytes_written_backlog_; |
191 last_progress_event_time_ = currentTime; | 193 last_progress_event_time_ = currentTime; |
192 bytes_written_backlog_ = 0; | 194 bytes_written_backlog_ = 0; |
193 | 195 |
194 if (done) { | 196 if (done) { |
195 FlushForCompletion(base::File::FILE_OK, bytes_written, | 197 MaybeFlushForCompletion(base::File::FILE_OK, bytes_written, |
196 SUCCESS_COMPLETED); | 198 SUCCESS_COMPLETED); |
197 } else { | 199 } else { |
198 write_callback_.Run(base::File::FILE_OK, bytes_written, | 200 write_callback_.Run(base::File::FILE_OK, bytes_written, |
199 SUCCESS_IO_PENDING); | 201 SUCCESS_IO_PENDING); |
200 } | 202 } |
201 return; | 203 return; |
202 } | 204 } |
203 bytes_written_backlog_ += bytes_written; | 205 bytes_written_backlog_ += bytes_written; |
204 } | 206 } |
205 | 207 |
206 void FileWriterDelegate::OnWriteCancelled(int status) { | 208 void FileWriterDelegate::OnWriteCancelled(int status) { |
207 write_callback_.Run(base::File::FILE_ERROR_ABORT, 0, | 209 write_callback_.Run(base::File::FILE_ERROR_ABORT, 0, |
208 GetCompletionStatusOnError()); | 210 GetCompletionStatusOnError()); |
209 } | 211 } |
210 | 212 |
211 void FileWriterDelegate::FlushForCompletion( | 213 void FileWriterDelegate::MaybeFlushForCompletion( |
212 base::File::Error error, | 214 base::File::Error error, |
213 int bytes_written, | 215 int bytes_written, |
214 WriteProgressStatus progress_status) { | 216 WriteProgressStatus progress_status) { |
| 217 if (flush_policy_ == NO_FLUSH_ON_COMPLETION) { |
| 218 write_callback_.Run(error, bytes_written, progress_status); |
| 219 return; |
| 220 } |
| 221 DCHECK_EQ(FLUSH_ON_COMPLETION, flush_policy_); |
| 222 |
215 int flush_error = file_stream_writer_->Flush( | 223 int flush_error = file_stream_writer_->Flush( |
216 base::Bind(&FileWriterDelegate::OnFlushed, weak_factory_.GetWeakPtr(), | 224 base::Bind(&FileWriterDelegate::OnFlushed, weak_factory_.GetWeakPtr(), |
217 error, bytes_written, progress_status)); | 225 error, bytes_written, progress_status)); |
218 if (flush_error != net::ERR_IO_PENDING) | 226 if (flush_error != net::ERR_IO_PENDING) |
219 OnFlushed(error, bytes_written, progress_status, flush_error); | 227 OnFlushed(error, bytes_written, progress_status, flush_error); |
220 } | 228 } |
221 | 229 |
222 void FileWriterDelegate::OnFlushed(base::File::Error error, | 230 void FileWriterDelegate::OnFlushed(base::File::Error error, |
223 int bytes_written, | 231 int bytes_written, |
224 WriteProgressStatus progress_status, | 232 WriteProgressStatus progress_status, |
225 int flush_error) { | 233 int flush_error) { |
226 if (error == base::File::FILE_OK && flush_error != net::OK) { | 234 if (error == base::File::FILE_OK && flush_error != net::OK) { |
227 // If the Flush introduced an error, overwrite the status. | 235 // If the Flush introduced an error, overwrite the status. |
228 // Otherwise, keep the original error status. | 236 // Otherwise, keep the original error status. |
229 error = NetErrorToFileError(flush_error); | 237 error = NetErrorToFileError(flush_error); |
230 progress_status = GetCompletionStatusOnError(); | 238 progress_status = GetCompletionStatusOnError(); |
231 } | 239 } |
232 write_callback_.Run(error, bytes_written, progress_status); | 240 write_callback_.Run(error, bytes_written, progress_status); |
233 } | 241 } |
234 | 242 |
235 } // namespace fileapi | 243 } // namespace fileapi |
OLD | NEW |