| 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.h" | 10 #include "base/message_loop.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 FileWriterDelegate::FileWriterDelegate( | 42 FileWriterDelegate::FileWriterDelegate( |
| 43 const DelegateWriteCallback& write_callback, | 43 const DelegateWriteCallback& write_callback, |
| 44 scoped_ptr<FileStreamWriter> file_stream_writer) | 44 scoped_ptr<FileStreamWriter> file_stream_writer) |
| 45 : write_callback_(write_callback), | 45 : write_callback_(write_callback), |
| 46 file_stream_writer_(file_stream_writer.Pass()), | 46 file_stream_writer_(file_stream_writer.Pass()), |
| 47 writing_started_(false), | 47 writing_started_(false), |
| 48 bytes_written_backlog_(0), | 48 bytes_written_backlog_(0), |
| 49 bytes_written_(0), | 49 bytes_written_(0), |
| 50 bytes_read_(0), | 50 bytes_read_(0), |
| 51 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), | 51 io_buffer_(new net::IOBufferWithSize(kReadBufSize)) { |
| 52 weak_factory_(this) { | |
| 53 } | 52 } |
| 54 | 53 |
| 55 FileWriterDelegate::~FileWriterDelegate() { | 54 FileWriterDelegate::~FileWriterDelegate() { |
| 56 } | 55 } |
| 57 | 56 |
| 58 void FileWriterDelegate::Start(scoped_ptr<net::URLRequest> request) { | 57 void FileWriterDelegate::Start(scoped_ptr<net::URLRequest> request) { |
| 59 request_ = request.Pass(); | 58 request_ = request.Pass(); |
| 60 request_->Start(); | 59 request_->Start(); |
| 61 } | 60 } |
| 62 | 61 |
| 63 bool FileWriterDelegate::Cancel() { | 62 void FileWriterDelegate::Cancel() { |
| 64 if (request_) { | 63 if (request_) { |
| 65 // This halts any callbacks on this delegate. | 64 // This halts any callbacks on this delegate. |
| 66 request_->set_delegate(NULL); | 65 request_->set_delegate(NULL); |
| 67 request_->Cancel(); | 66 request_->Cancel(); |
| 68 } | 67 } |
| 69 | 68 |
| 70 const int status = file_stream_writer_->Cancel( | 69 const int status = file_stream_writer_->Cancel( |
| 71 base::Bind(&FileWriterDelegate::OnWriteCancelled, | 70 base::Bind(&FileWriterDelegate::OnWriteCancelled, AsWeakPtr())); |
| 72 weak_factory_.GetWeakPtr())); | |
| 73 // Return true to finish immediately if we have no pending writes. | 71 // Return true to finish immediately if we have no pending writes. |
| 74 // Otherwise we'll do the final cleanup in the Cancel callback. | 72 // Otherwise we'll do the final cleanup in the Cancel callback. |
| 75 return (status != net::ERR_IO_PENDING); | 73 if (status != net::ERR_IO_PENDING) { |
| 74 write_callback_.Run(base::PLATFORM_FILE_ERROR_ABORT, 0, |
| 75 GetCompletionStatusOnError()); |
| 76 } |
| 76 } | 77 } |
| 77 | 78 |
| 78 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, | 79 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, |
| 79 const GURL& new_url, | 80 const GURL& new_url, |
| 80 bool* defer_redirect) { | 81 bool* defer_redirect) { |
| 81 NOTREACHED(); | 82 NOTREACHED(); |
| 82 OnError(base::PLATFORM_FILE_ERROR_SECURITY); | 83 OnError(base::PLATFORM_FILE_ERROR_SECURITY); |
| 83 } | 84 } |
| 84 | 85 |
| 85 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request, | 86 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 OnDataReceived(bytes_read); | 122 OnDataReceived(bytes_read); |
| 122 } | 123 } |
| 123 | 124 |
| 124 void FileWriterDelegate::Read() { | 125 void FileWriterDelegate::Read() { |
| 125 bytes_written_ = 0; | 126 bytes_written_ = 0; |
| 126 bytes_read_ = 0; | 127 bytes_read_ = 0; |
| 127 if (request_->Read(io_buffer_.get(), io_buffer_->size(), &bytes_read_)) { | 128 if (request_->Read(io_buffer_.get(), io_buffer_->size(), &bytes_read_)) { |
| 128 base::MessageLoop::current()->PostTask( | 129 base::MessageLoop::current()->PostTask( |
| 129 FROM_HERE, | 130 FROM_HERE, |
| 130 base::Bind(&FileWriterDelegate::OnDataReceived, | 131 base::Bind(&FileWriterDelegate::OnDataReceived, |
| 131 weak_factory_.GetWeakPtr(), | 132 AsWeakPtr(), bytes_read_)); |
| 132 bytes_read_)); | |
| 133 } else if (!request_->status().is_io_pending()) { | 133 } else if (!request_->status().is_io_pending()) { |
| 134 OnError(base::PLATFORM_FILE_ERROR_FAILED); | 134 OnError(base::PLATFORM_FILE_ERROR_FAILED); |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 | 137 |
| 138 void FileWriterDelegate::OnDataReceived(int bytes_read) { | 138 void FileWriterDelegate::OnDataReceived(int bytes_read) { |
| 139 bytes_read_ = bytes_read; | 139 bytes_read_ = bytes_read; |
| 140 if (!bytes_read_) { // We're done. | 140 if (!bytes_read_) { // We're done. |
| 141 OnProgress(0, true); | 141 OnProgress(0, true); |
| 142 } else { | 142 } else { |
| 143 // This could easily be optimized to rotate between a pool of buffers, so | 143 // This could easily be optimized to rotate between a pool of buffers, so |
| 144 // that we could read and write at the same time. It's not yet clear that | 144 // that we could read and write at the same time. It's not yet clear that |
| 145 // it's necessary. | 145 // it's necessary. |
| 146 cursor_ = new net::DrainableIOBuffer(io_buffer_.get(), bytes_read_); | 146 cursor_ = new net::DrainableIOBuffer(io_buffer_.get(), bytes_read_); |
| 147 Write(); | 147 Write(); |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 | 150 |
| 151 void FileWriterDelegate::Write() { | 151 void FileWriterDelegate::Write() { |
| 152 writing_started_ = true; | 152 writing_started_ = true; |
| 153 int64 bytes_to_write = bytes_read_ - bytes_written_; | 153 int64 bytes_to_write = bytes_read_ - bytes_written_; |
| 154 int write_response = | 154 int write_response = |
| 155 file_stream_writer_->Write(cursor_.get(), | 155 file_stream_writer_->Write(cursor_.get(), |
| 156 static_cast<int>(bytes_to_write), | 156 static_cast<int>(bytes_to_write), |
| 157 base::Bind(&FileWriterDelegate::OnDataWritten, | 157 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 158 weak_factory_.GetWeakPtr())); | 158 AsWeakPtr())); |
| 159 if (write_response > 0) | 159 if (write_response > 0) { |
| 160 base::MessageLoop::current()->PostTask( | 160 base::MessageLoop::current()->PostTask( |
| 161 FROM_HERE, | 161 FROM_HERE, |
| 162 base::Bind(&FileWriterDelegate::OnDataWritten, | 162 base::Bind(&FileWriterDelegate::OnDataWritten, |
| 163 weak_factory_.GetWeakPtr(), | 163 AsWeakPtr(), write_response)); |
| 164 write_response)); | 164 } else if (net::ERR_IO_PENDING != write_response) { |
| 165 else if (net::ERR_IO_PENDING != write_response) | |
| 166 OnError(NetErrorToPlatformFileError(write_response)); | 165 OnError(NetErrorToPlatformFileError(write_response)); |
| 166 } |
| 167 } | 167 } |
| 168 | 168 |
| 169 void FileWriterDelegate::OnDataWritten(int write_response) { | 169 void FileWriterDelegate::OnDataWritten(int write_response) { |
| 170 if (write_response > 0) { | 170 if (write_response > 0) { |
| 171 OnProgress(write_response, false); | 171 OnProgress(write_response, false); |
| 172 cursor_->DidConsume(write_response); | 172 cursor_->DidConsume(write_response); |
| 173 bytes_written_ += write_response; | 173 bytes_written_ += write_response; |
| 174 if (bytes_written_ == bytes_read_) | 174 if (bytes_written_ == bytes_read_) |
| 175 Read(); | 175 Read(); |
| 176 else | 176 else |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 void FileWriterDelegate::OnWriteCancelled(int status) { | 223 void FileWriterDelegate::OnWriteCancelled(int status) { |
| 224 write_callback_.Run(base::PLATFORM_FILE_ERROR_ABORT, 0, | 224 write_callback_.Run(base::PLATFORM_FILE_ERROR_ABORT, 0, |
| 225 GetCompletionStatusOnError()); | 225 GetCompletionStatusOnError()); |
| 226 } | 226 } |
| 227 | 227 |
| 228 void FileWriterDelegate::FlushForCompletion( | 228 void FileWriterDelegate::FlushForCompletion( |
| 229 base::PlatformFileError error, | 229 base::PlatformFileError error, |
| 230 int bytes_written, | 230 int bytes_written, |
| 231 WriteProgressStatus progress_status) { | 231 WriteProgressStatus progress_status) { |
| 232 int flush_error = file_stream_writer_->Flush( | 232 int flush_error = file_stream_writer_->Flush( |
| 233 base::Bind(&FileWriterDelegate::OnFlushed, | 233 base::Bind(&FileWriterDelegate::OnFlushed, AsWeakPtr(), |
| 234 weak_factory_.GetWeakPtr(), | |
| 235 error, bytes_written, progress_status)); | 234 error, bytes_written, progress_status)); |
| 236 if (flush_error != net::ERR_IO_PENDING) | 235 if (flush_error != net::ERR_IO_PENDING) |
| 237 OnFlushed(error, bytes_written, progress_status, flush_error); | 236 OnFlushed(error, bytes_written, progress_status, flush_error); |
| 238 } | 237 } |
| 239 | 238 |
| 240 void FileWriterDelegate::OnFlushed(base::PlatformFileError error, | 239 void FileWriterDelegate::OnFlushed(base::PlatformFileError error, |
| 241 int bytes_written, | 240 int bytes_written, |
| 242 WriteProgressStatus progress_status, | 241 WriteProgressStatus progress_status, |
| 243 int flush_error) { | 242 int flush_error) { |
| 244 if (error == base::PLATFORM_FILE_OK && flush_error != net::OK) { | 243 if (error == base::PLATFORM_FILE_OK && flush_error != net::OK) { |
| 245 // If the Flush introduced an error, overwrite the status. | 244 // If the Flush introduced an error, overwrite the status. |
| 246 // Otherwise, keep the original error status. | 245 // Otherwise, keep the original error status. |
| 247 error = NetErrorToPlatformFileError(flush_error); | 246 error = NetErrorToPlatformFileError(flush_error); |
| 248 progress_status = GetCompletionStatusOnError(); | 247 progress_status = GetCompletionStatusOnError(); |
| 249 } | 248 } |
| 250 write_callback_.Run(error, bytes_written, progress_status); | 249 write_callback_.Run(error, bytes_written, progress_status); |
| 251 } | 250 } |
| 252 | 251 |
| 253 } // namespace fileapi | 252 } // namespace fileapi |
| OLD | NEW |