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 "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 27 matching lines...) Expand all Loading... | |
| 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 if (request_) { |
| 48 // This halts any callbacks on this delegate. | 48 // Destroy the request to prevent it from invoking any callbacks. |
| 49 request_->set_delegate(NULL); | 49 request_.reset(); |
| 50 request_->Cancel(); | |
| 51 } | 50 } |
| 52 | 51 |
| 53 const int status = file_stream_writer_->Cancel( | 52 const int status = file_stream_writer_->Cancel( |
| 54 base::Bind(&FileWriterDelegate::OnWriteCancelled, | 53 base::Bind(&FileWriterDelegate::OnWriteCancelled, |
| 55 weak_factory_.GetWeakPtr())); | 54 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(base::File::FILE_ERROR_ABORT, 0, |
| 60 GetCompletionStatusOnError()); | 59 GetCompletionStatusOnError()); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 OnError(NetErrorToFileError(write_response)); | 150 OnError(NetErrorToFileError(write_response)); |
| 152 } | 151 } |
| 153 } | 152 } |
| 154 | 153 |
| 155 void FileWriterDelegate::OnDataWritten(int write_response) { | 154 void FileWriterDelegate::OnDataWritten(int write_response) { |
| 156 if (write_response > 0) { | 155 if (write_response > 0) { |
| 157 OnProgress(write_response, false); | 156 OnProgress(write_response, false); |
| 158 cursor_->DidConsume(write_response); | 157 cursor_->DidConsume(write_response); |
| 159 bytes_written_ += write_response; | 158 bytes_written_ += write_response; |
| 160 if (bytes_written_ == bytes_read_) | 159 if (bytes_written_ == bytes_read_) |
| 161 Read(); | 160 Read(); |
|
michaeln
2015/12/14 20:27:26
Not sure? I think you may need a null check in Rea
mmenke
2015/12/14 20:38:23
I don't think so. From FileStreamWriter::Cancel:
michaeln
2015/12/14 20:58:30
What about the other .reset() callsite in OnError?
| |
| 162 else | 161 else |
| 163 Write(); | 162 Write(); |
| 164 } else { | 163 } else { |
| 165 OnError(NetErrorToFileError(write_response)); | 164 OnError(NetErrorToFileError(write_response)); |
| 166 } | 165 } |
| 167 } | 166 } |
| 168 | 167 |
| 169 FileWriterDelegate::WriteProgressStatus | 168 FileWriterDelegate::WriteProgressStatus |
| 170 FileWriterDelegate::GetCompletionStatusOnError() const { | 169 FileWriterDelegate::GetCompletionStatusOnError() const { |
| 171 return writing_started_ ? ERROR_WRITE_STARTED : ERROR_WRITE_NOT_STARTED; | 170 return writing_started_ ? ERROR_WRITE_STARTED : ERROR_WRITE_NOT_STARTED; |
| 172 } | 171 } |
| 173 | 172 |
| 174 void FileWriterDelegate::OnError(base::File::Error error) { | 173 void FileWriterDelegate::OnError(base::File::Error error) { |
| 175 if (request_) { | 174 if (request_) { |
| 176 request_->set_delegate(NULL); | 175 // Destroy the request to prevent it from invoking any callbacks. |
| 177 request_->Cancel(); | 176 request_.reset(); |
| 178 } | 177 } |
| 179 | 178 |
| 180 if (writing_started_) | 179 if (writing_started_) |
| 181 MaybeFlushForCompletion(error, 0, ERROR_WRITE_STARTED); | 180 MaybeFlushForCompletion(error, 0, ERROR_WRITE_STARTED); |
| 182 else | 181 else |
| 183 write_callback_.Run(error, 0, ERROR_WRITE_NOT_STARTED); | 182 write_callback_.Run(error, 0, ERROR_WRITE_NOT_STARTED); |
| 184 } | 183 } |
| 185 | 184 |
| 186 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { | 185 void FileWriterDelegate::OnProgress(int bytes_written, bool done) { |
| 187 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); | 186 DCHECK(bytes_written + bytes_written_backlog_ >= bytes_written_backlog_); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 if (error == base::File::FILE_OK && flush_error != net::OK) { | 235 if (error == base::File::FILE_OK && flush_error != net::OK) { |
| 237 // If the Flush introduced an error, overwrite the status. | 236 // If the Flush introduced an error, overwrite the status. |
| 238 // Otherwise, keep the original error status. | 237 // Otherwise, keep the original error status. |
| 239 error = NetErrorToFileError(flush_error); | 238 error = NetErrorToFileError(flush_error); |
| 240 progress_status = GetCompletionStatusOnError(); | 239 progress_status = GetCompletionStatusOnError(); |
| 241 } | 240 } |
| 242 write_callback_.Run(error, bytes_written, progress_status); | 241 write_callback_.Run(error, bytes_written, progress_status); |
| 243 } | 242 } |
| 244 | 243 |
| 245 } // namespace storage | 244 } // namespace storage |
| OLD | NEW |