Chromium Code Reviews| 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/fileapi/file_writer_delegate.h" | 5 #include "webkit/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/file_util_proxy.h" | 9 #include "base/file_util_proxy.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/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/fileapi/file_stream_writer.h" | 15 #include "webkit/fileapi/file_stream_writer.h" |
| 16 #include "webkit/fileapi/file_system_context.h" | 16 #include "webkit/fileapi/file_system_context.h" |
| 17 | 17 |
| 18 namespace fileapi { | 18 namespace fileapi { |
| 19 | 19 |
| 20 static const int kReadBufSize = 32768; | 20 static const int kReadBufSize = 32768; |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 base::PlatformFileError NetErrorToPlatformFileError(int error) { | 24 base::PlatformFileError NetErrorToPlatformFileError(int error) { |
| 25 // TODO(kinuko): Move this static method to more convenient place. | 25 // TODO(kinuko): Move this static method to more convenient place. |
| 26 switch (error) { | 26 switch (error) { |
| 27 case net::OK: | |
| 28 return base::PLATFORM_FILE_OK; | |
| 27 case net::ERR_FILE_NO_SPACE: | 29 case net::ERR_FILE_NO_SPACE: |
| 28 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 30 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 29 case net::ERR_FILE_NOT_FOUND: | 31 case net::ERR_FILE_NOT_FOUND: |
| 30 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 32 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
| 31 case net::ERR_ACCESS_DENIED: | 33 case net::ERR_ACCESS_DENIED: |
| 32 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; | 34 return base::PLATFORM_FILE_ERROR_ACCESS_DENIED; |
| 33 default: | 35 default: |
| 34 return base::PLATFORM_FILE_ERROR_FAILED; | 36 return base::PLATFORM_FILE_ERROR_FAILED; |
| 35 } | 37 } |
| 36 } | 38 } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { | 188 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { |
| 187 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); | 189 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); |
| 188 static const int kMinProgressDelayMS = 200; | 190 static const int kMinProgressDelayMS = 200; |
| 189 base::Time currentTime = base::Time::Now(); | 191 base::Time currentTime = base::Time::Now(); |
| 190 if (done || last_progress_event_time_.is_null() || | 192 if (done || last_progress_event_time_.is_null() || |
| 191 (currentTime - last_progress_event_time_).InMilliseconds() > | 193 (currentTime - last_progress_event_time_).InMilliseconds() > |
| 192 kMinProgressDelayMS) { | 194 kMinProgressDelayMS) { |
| 193 bytes_written += bytes_written_backlog_; | 195 bytes_written += bytes_written_backlog_; |
| 194 last_progress_event_time_ = currentTime; | 196 last_progress_event_time_ = currentTime; |
| 195 bytes_written_backlog_ = 0; | 197 bytes_written_backlog_ = 0; |
| 196 write_callback_.Run( | 198 if (done) { |
| 197 base::PLATFORM_FILE_OK, bytes_written, done); | 199 // All write operation completed, be sure to flush the data. |
| 200 int result = file_stream_writer_->Flush( | |
| 201 base::Bind(&FileWriterDelegate::OnSuccessfulCompletion, | |
| 202 weak_factory_.GetWeakPtr(), | |
| 203 bytes_written)); | |
| 204 if (result != net::ERR_IO_PENDING) { | |
| 205 write_callback_.Run(NetErrorToPlatformFileError(result), | |
| 206 bytes_written, done); | |
| 207 } | |
| 208 } else { | |
| 209 write_callback_.Run(base::PLATFORM_FILE_OK, bytes_written, done); | |
| 210 } | |
|
kinuko
2012/09/26 08:34:08
Do we need to do this when error has happend after
kinaba
2012/09/28 11:15:25
Thanks. I rebased this CL on top of it.
I've also
| |
| 198 return; | 211 return; |
| 199 } | 212 } |
| 200 bytes_written_backlog_ += bytes_written; | 213 bytes_written_backlog_ += bytes_written; |
| 201 } | 214 } |
| 202 | 215 |
| 203 void FileWriterDelegate::OnWriteCancelled(int status) { | 216 void FileWriterDelegate::OnWriteCancelled(int status) { |
| 204 write_callback_.Run(base::PLATFORM_FILE_ERROR_ABORT, 0, true); | 217 write_callback_.Run(base::PLATFORM_FILE_ERROR_ABORT, 0, true); |
| 205 } | 218 } |
| 206 | 219 |
| 220 void FileWriterDelegate::OnSuccessfulCompletion(int bytes_written, int status) { | |
| 221 write_callback_.Run(NetErrorToPlatformFileError(status), bytes_written, true); | |
| 222 } | |
| 223 | |
| 207 } // namespace fileapi | 224 } // namespace fileapi |
| OLD | NEW |