| 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 "storage/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 "storage/browser/fileapi/file_stream_writer.h" |
| 16 #include "webkit/browser/fileapi/file_system_context.h" | 16 #include "storage/browser/fileapi/file_system_context.h" |
| 17 #include "webkit/common/fileapi/file_system_util.h" | 17 #include "storage/common/fileapi/file_system_util.h" |
| 18 | 18 |
| 19 namespace fileapi { | 19 namespace storage { |
| 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 FlushPolicy flush_policy) |
| 26 : file_stream_writer_(file_stream_writer.Pass()), | 26 : file_stream_writer_(file_stream_writer.Pass()), |
| 27 writing_started_(false), | 27 writing_started_(false), |
| 28 flush_policy_(flush_policy), | 28 flush_policy_(flush_policy), |
| 29 bytes_written_backlog_(0), | 29 bytes_written_backlog_(0), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 43 request_->Start(); | 43 request_->Start(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void FileWriterDelegate::Cancel() { | 46 void FileWriterDelegate::Cancel() { |
| 47 if (request_) { | 47 if (request_) { |
| 48 // This halts any callbacks on this delegate. | 48 // This halts any callbacks on this delegate. |
| 49 request_->set_delegate(NULL); | 49 request_->set_delegate(NULL); |
| 50 request_->Cancel(); | 50 request_->Cancel(); |
| 51 } | 51 } |
| 52 | 52 |
| 53 const int status = file_stream_writer_->Cancel( | 53 const int status = file_stream_writer_->Cancel(base::Bind( |
| 54 base::Bind(&FileWriterDelegate::OnWriteCancelled, | 54 &FileWriterDelegate::OnWriteCancelled, weak_factory_.GetWeakPtr())); |
| 55 weak_factory_.GetWeakPtr())); | |
| 56 // Return true to finish immediately if we have no pending writes. | 55 // Return true to finish immediately if we have no pending writes. |
| 57 // Otherwise we'll do the final cleanup in the Cancel callback. | 56 // Otherwise we'll do the final cleanup in the Cancel callback. |
| 58 if (status != net::ERR_IO_PENDING) { | 57 if (status != net::ERR_IO_PENDING) { |
| 59 write_callback_.Run(base::File::FILE_ERROR_ABORT, 0, | 58 write_callback_.Run( |
| 60 GetCompletionStatusOnError()); | 59 base::File::FILE_ERROR_ABORT, 0, GetCompletionStatusOnError()); |
| 61 } | 60 } |
| 62 } | 61 } |
| 63 | 62 |
| 64 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, | 63 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, |
| 65 const GURL& new_url, | 64 const GURL& new_url, |
| 66 bool* defer_redirect) { | 65 bool* defer_redirect) { |
| 67 NOTREACHED(); | 66 NOTREACHED(); |
| 68 OnError(base::File::FILE_ERROR_SECURITY); | 67 OnError(base::File::FILE_ERROR_SECURITY); |
| 69 } | 68 } |
| 70 | 69 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 OnDataReceived(bytes_read); | 106 OnDataReceived(bytes_read); |
| 108 } | 107 } |
| 109 | 108 |
| 110 void FileWriterDelegate::Read() { | 109 void FileWriterDelegate::Read() { |
| 111 bytes_written_ = 0; | 110 bytes_written_ = 0; |
| 112 bytes_read_ = 0; | 111 bytes_read_ = 0; |
| 113 if (request_->Read(io_buffer_.get(), io_buffer_->size(), &bytes_read_)) { | 112 if (request_->Read(io_buffer_.get(), io_buffer_->size(), &bytes_read_)) { |
| 114 base::MessageLoop::current()->PostTask( | 113 base::MessageLoop::current()->PostTask( |
| 115 FROM_HERE, | 114 FROM_HERE, |
| 116 base::Bind(&FileWriterDelegate::OnDataReceived, | 115 base::Bind(&FileWriterDelegate::OnDataReceived, |
| 117 weak_factory_.GetWeakPtr(), bytes_read_)); | 116 weak_factory_.GetWeakPtr(), |
| 117 bytes_read_)); |
| 118 } else if (!request_->status().is_io_pending()) { | 118 } else if (!request_->status().is_io_pending()) { |
| 119 OnError(base::File::FILE_ERROR_FAILED); | 119 OnError(base::File::FILE_ERROR_FAILED); |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 void FileWriterDelegate::OnDataReceived(int bytes_read) { | 123 void FileWriterDelegate::OnDataReceived(int bytes_read) { |
| 124 bytes_read_ = bytes_read; | 124 bytes_read_ = bytes_read; |
| 125 if (!bytes_read_) { // We're done. | 125 if (!bytes_read_) { // We're done. |
| 126 OnProgress(0, true); | 126 OnProgress(0, true); |
| 127 } else { | 127 } else { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 138 int64 bytes_to_write = bytes_read_ - bytes_written_; | 138 int64 bytes_to_write = bytes_read_ - bytes_written_; |
| 139 int write_response = | 139 int write_response = |
| 140 file_stream_writer_->Write(cursor_.get(), | 140 file_stream_writer_->Write(cursor_.get(), |
| 141 static_cast<int>(bytes_to_write), | 141 static_cast<int>(bytes_to_write), |
| 142 base::Bind(&FileWriterDelegate::OnDataWritten, | 142 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 143 weak_factory_.GetWeakPtr())); | 143 weak_factory_.GetWeakPtr())); |
| 144 if (write_response > 0) { | 144 if (write_response > 0) { |
| 145 base::MessageLoop::current()->PostTask( | 145 base::MessageLoop::current()->PostTask( |
| 146 FROM_HERE, | 146 FROM_HERE, |
| 147 base::Bind(&FileWriterDelegate::OnDataWritten, | 147 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 148 weak_factory_.GetWeakPtr(), write_response)); | 148 weak_factory_.GetWeakPtr(), |
| 149 write_response)); |
| 149 } else if (net::ERR_IO_PENDING != write_response) { | 150 } else if (net::ERR_IO_PENDING != write_response) { |
| 150 OnError(NetErrorToFileError(write_response)); | 151 OnError(NetErrorToFileError(write_response)); |
| 151 } | 152 } |
| 152 } | 153 } |
| 153 | 154 |
| 154 void FileWriterDelegate::OnDataWritten(int write_response) { | 155 void FileWriterDelegate::OnDataWritten(int write_response) { |
| 155 if (write_response > 0) { | 156 if (write_response > 0) { |
| 156 OnProgress(write_response, false); | 157 OnProgress(write_response, false); |
| 157 cursor_->DidConsume(write_response); | 158 cursor_->DidConsume(write_response); |
| 158 bytes_written_ += write_response; | 159 bytes_written_ += write_response; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 187 static const int kMinProgressDelayMS = 200; | 188 static const int kMinProgressDelayMS = 200; |
| 188 base::Time currentTime = base::Time::Now(); | 189 base::Time currentTime = base::Time::Now(); |
| 189 if (done || last_progress_event_time_.is_null() || | 190 if (done || last_progress_event_time_.is_null() || |
| 190 (currentTime - last_progress_event_time_).InMilliseconds() > | 191 (currentTime - last_progress_event_time_).InMilliseconds() > |
| 191 kMinProgressDelayMS) { | 192 kMinProgressDelayMS) { |
| 192 bytes_written += bytes_written_backlog_; | 193 bytes_written += bytes_written_backlog_; |
| 193 last_progress_event_time_ = currentTime; | 194 last_progress_event_time_ = currentTime; |
| 194 bytes_written_backlog_ = 0; | 195 bytes_written_backlog_ = 0; |
| 195 | 196 |
| 196 if (done) { | 197 if (done) { |
| 197 MaybeFlushForCompletion(base::File::FILE_OK, bytes_written, | 198 MaybeFlushForCompletion( |
| 198 SUCCESS_COMPLETED); | 199 base::File::FILE_OK, bytes_written, SUCCESS_COMPLETED); |
| 199 } else { | 200 } else { |
| 200 write_callback_.Run(base::File::FILE_OK, bytes_written, | 201 write_callback_.Run( |
| 201 SUCCESS_IO_PENDING); | 202 base::File::FILE_OK, bytes_written, SUCCESS_IO_PENDING); |
| 202 } | 203 } |
| 203 return; | 204 return; |
| 204 } | 205 } |
| 205 bytes_written_backlog_ += bytes_written; | 206 bytes_written_backlog_ += bytes_written; |
| 206 } | 207 } |
| 207 | 208 |
| 208 void FileWriterDelegate::OnWriteCancelled(int status) { | 209 void FileWriterDelegate::OnWriteCancelled(int status) { |
| 209 write_callback_.Run(base::File::FILE_ERROR_ABORT, 0, | 210 write_callback_.Run( |
| 210 GetCompletionStatusOnError()); | 211 base::File::FILE_ERROR_ABORT, 0, GetCompletionStatusOnError()); |
| 211 } | 212 } |
| 212 | 213 |
| 213 void FileWriterDelegate::MaybeFlushForCompletion( | 214 void FileWriterDelegate::MaybeFlushForCompletion( |
| 214 base::File::Error error, | 215 base::File::Error error, |
| 215 int bytes_written, | 216 int bytes_written, |
| 216 WriteProgressStatus progress_status) { | 217 WriteProgressStatus progress_status) { |
| 217 if (flush_policy_ == NO_FLUSH_ON_COMPLETION) { | 218 if (flush_policy_ == NO_FLUSH_ON_COMPLETION) { |
| 218 write_callback_.Run(error, bytes_written, progress_status); | 219 write_callback_.Run(error, bytes_written, progress_status); |
| 219 return; | 220 return; |
| 220 } | 221 } |
| 221 DCHECK_EQ(FLUSH_ON_COMPLETION, flush_policy_); | 222 DCHECK_EQ(FLUSH_ON_COMPLETION, flush_policy_); |
| 222 | 223 |
| 223 int flush_error = file_stream_writer_->Flush( | 224 int flush_error = |
| 224 base::Bind(&FileWriterDelegate::OnFlushed, weak_factory_.GetWeakPtr(), | 225 file_stream_writer_->Flush(base::Bind(&FileWriterDelegate::OnFlushed, |
| 225 error, bytes_written, progress_status)); | 226 weak_factory_.GetWeakPtr(), |
| 227 error, |
| 228 bytes_written, |
| 229 progress_status)); |
| 226 if (flush_error != net::ERR_IO_PENDING) | 230 if (flush_error != net::ERR_IO_PENDING) |
| 227 OnFlushed(error, bytes_written, progress_status, flush_error); | 231 OnFlushed(error, bytes_written, progress_status, flush_error); |
| 228 } | 232 } |
| 229 | 233 |
| 230 void FileWriterDelegate::OnFlushed(base::File::Error error, | 234 void FileWriterDelegate::OnFlushed(base::File::Error error, |
| 231 int bytes_written, | 235 int bytes_written, |
| 232 WriteProgressStatus progress_status, | 236 WriteProgressStatus progress_status, |
| 233 int flush_error) { | 237 int flush_error) { |
| 234 if (error == base::File::FILE_OK && flush_error != net::OK) { | 238 if (error == base::File::FILE_OK && flush_error != net::OK) { |
| 235 // If the Flush introduced an error, overwrite the status. | 239 // If the Flush introduced an error, overwrite the status. |
| 236 // Otherwise, keep the original error status. | 240 // Otherwise, keep the original error status. |
| 237 error = NetErrorToFileError(flush_error); | 241 error = NetErrorToFileError(flush_error); |
| 238 progress_status = GetCompletionStatusOnError(); | 242 progress_status = GetCompletionStatusOnError(); |
| 239 } | 243 } |
| 240 write_callback_.Run(error, bytes_written, progress_status); | 244 write_callback_.Run(error, bytes_written, progress_status); |
| 241 } | 245 } |
| 242 | 246 |
| 243 } // namespace fileapi | 247 } // namespace storage |
| OLD | NEW |