| 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 "storage/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" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 | 38 |
| 39 void FileWriterDelegate::Start(scoped_ptr<net::URLRequest> request, | 39 void FileWriterDelegate::Start(scoped_ptr<net::URLRequest> request, |
| 40 const DelegateWriteCallback& write_callback) { | 40 const DelegateWriteCallback& write_callback) { |
| 41 write_callback_ = write_callback; | 41 write_callback_ = write_callback; |
| 42 request_ = request.Pass(); | 42 request_ = request.Pass(); |
| 43 request_->Start(); | 43 request_->Start(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void FileWriterDelegate::Cancel() { | 46 void FileWriterDelegate::Cancel() { |
| 47 if (request_) { | 47 // Destroy the request to prevent it from invoking any callbacks. |
| 48 // This halts any callbacks on this delegate. | 48 request_.reset(); |
| 49 request_->set_delegate(NULL); | |
| 50 request_->Cancel(); | |
| 51 } | |
| 52 | 49 |
| 53 const int status = file_stream_writer_->Cancel( | 50 const int status = file_stream_writer_->Cancel( |
| 54 base::Bind(&FileWriterDelegate::OnWriteCancelled, | 51 base::Bind(&FileWriterDelegate::OnWriteCancelled, |
| 55 weak_factory_.GetWeakPtr())); | 52 weak_factory_.GetWeakPtr())); |
| 56 // Return true to finish immediately if we have no pending writes. | 53 // Return true to finish immediately if we have no pending writes. |
| 57 // Otherwise we'll do the final cleanup in the Cancel callback. | 54 // Otherwise we'll do the final cleanup in the Cancel callback. |
| 58 if (status != net::ERR_IO_PENDING) { | 55 if (status != net::ERR_IO_PENDING) { |
| 59 write_callback_.Run(base::File::FILE_ERROR_ABORT, 0, | 56 write_callback_.Run(base::File::FILE_ERROR_ABORT, 0, |
| 60 GetCompletionStatusOnError()); | 57 GetCompletionStatusOnError()); |
| 61 } | 58 } |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 OnError(NetErrorToFileError(write_response)); | 162 OnError(NetErrorToFileError(write_response)); |
| 166 } | 163 } |
| 167 } | 164 } |
| 168 | 165 |
| 169 FileWriterDelegate::WriteProgressStatus | 166 FileWriterDelegate::WriteProgressStatus |
| 170 FileWriterDelegate::GetCompletionStatusOnError() const { | 167 FileWriterDelegate::GetCompletionStatusOnError() const { |
| 171 return writing_started_ ? ERROR_WRITE_STARTED : ERROR_WRITE_NOT_STARTED; | 168 return writing_started_ ? ERROR_WRITE_STARTED : ERROR_WRITE_NOT_STARTED; |
| 172 } | 169 } |
| 173 | 170 |
| 174 void FileWriterDelegate::OnError(base::File::Error error) { | 171 void FileWriterDelegate::OnError(base::File::Error error) { |
| 175 if (request_) { | 172 // Destroy the request to prevent it from invoking any callbacks. |
| 176 request_->set_delegate(NULL); | 173 request_.reset(); |
| 177 request_->Cancel(); | |
| 178 } | |
| 179 | 174 |
| 180 if (writing_started_) | 175 if (writing_started_) |
| 181 MaybeFlushForCompletion(error, 0, ERROR_WRITE_STARTED); | 176 MaybeFlushForCompletion(error, 0, ERROR_WRITE_STARTED); |
| 182 else | 177 else |
| 183 write_callback_.Run(error, 0, ERROR_WRITE_NOT_STARTED); | 178 write_callback_.Run(error, 0, ERROR_WRITE_NOT_STARTED); |
| 184 } | 179 } |
| 185 | 180 |
| 186 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { | 181 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { |
| 187 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); | 182 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); |
| 188 static const int kMinProgressDelayMS = 200; | 183 static const int kMinProgressDelayMS = 200; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 if (error == base::File::FILE_OK && flush_error != net::OK) { | 231 if (error == base::File::FILE_OK && flush_error != net::OK) { |
| 237 // If the Flush introduced an error, overwrite the status. | 232 // If the Flush introduced an error, overwrite the status. |
| 238 // Otherwise, keep the original error status. | 233 // Otherwise, keep the original error status. |
| 239 error = NetErrorToFileError(flush_error); | 234 error = NetErrorToFileError(flush_error); |
| 240 progress_status = GetCompletionStatusOnError(); | 235 progress_status = GetCompletionStatusOnError(); |
| 241 } | 236 } |
| 242 write_callback_.Run(error, bytes_written, progress_status); | 237 write_callback_.Run(error, bytes_written, progress_status); |
| 243 } | 238 } |
| 244 | 239 |
| 245 } // namespace storage | 240 } // namespace storage |
| OLD | NEW |