| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "net/url_request/url_fetcher_response_writer.h" | 5 #include "net/url_request/url_fetcher_response_writer.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/task_runner.h" | 9 #include "base/task_runner.h" |
| 10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 } | 84 } |
| 85 return result; | 85 return result; |
| 86 } | 86 } |
| 87 | 87 |
| 88 int URLFetcherFileWriter::Write(IOBuffer* buffer, | 88 int URLFetcherFileWriter::Write(IOBuffer* buffer, |
| 89 int num_bytes, | 89 int num_bytes, |
| 90 const CompletionCallback& callback) { | 90 const CompletionCallback& callback) { |
| 91 DCHECK(file_stream_); | 91 DCHECK(file_stream_); |
| 92 DCHECK(owns_file_); | 92 DCHECK(owns_file_); |
| 93 | 93 |
| 94 ContinueWrite(new DrainableIOBuffer(buffer, num_bytes), callback, OK); | 94 int result = file_stream_->Write(buffer, num_bytes, |
| 95 return ERR_IO_PENDING; | 95 base::Bind(&URLFetcherFileWriter::DidWrite, |
| 96 weak_factory_.GetWeakPtr(), |
| 97 callback)); |
| 98 if (result < 0 && result != ERR_IO_PENDING) { |
| 99 error_code_ = result; |
| 100 CloseAndDeleteFile(); |
| 101 } |
| 102 return result; |
| 96 } | 103 } |
| 97 | 104 |
| 98 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { | 105 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { |
| 99 file_stream_.reset(); | 106 file_stream_.reset(); |
| 100 return OK; | 107 return OK; |
| 101 } | 108 } |
| 102 | 109 |
| 103 void URLFetcherFileWriter::ContinueWrite( | 110 void URLFetcherFileWriter::DidWrite(const CompletionCallback& callback, |
| 104 scoped_refptr<DrainableIOBuffer> buffer, | 111 int result) { |
| 105 const CompletionCallback& callback, | |
| 106 int result) { | |
| 107 // |file_stream_| should be alive when write is in progress. | |
| 108 DCHECK(file_stream_); | |
| 109 | |
| 110 if (result < 0) { | 112 if (result < 0) { |
| 111 error_code_ = result; | 113 error_code_ = result; |
| 112 CloseAndDeleteFile(); | 114 CloseAndDeleteFile(); |
| 113 callback.Run(result); | |
| 114 return; | |
| 115 } | 115 } |
| 116 | 116 callback.Run(result); |
| 117 total_bytes_written_ += result; | |
| 118 buffer->DidConsume(result); | |
| 119 | |
| 120 if (buffer->BytesRemaining() > 0) { | |
| 121 file_stream_->Write(buffer, buffer->BytesRemaining(), | |
| 122 base::Bind(&URLFetcherFileWriter::ContinueWrite, | |
| 123 weak_factory_.GetWeakPtr(), | |
| 124 buffer, | |
| 125 callback)); | |
| 126 } else { | |
| 127 // Finished writing buffer to the file. | |
| 128 callback.Run(buffer->size()); | |
| 129 } | |
| 130 } | 117 } |
| 131 | 118 |
| 132 void URLFetcherFileWriter::DisownFile() { | 119 void URLFetcherFileWriter::DisownFile() { |
| 133 // Disowning is done by the delegate's OnURLFetchComplete method. | 120 // Disowning is done by the delegate's OnURLFetchComplete method. |
| 134 // The file should be closed by the time that method is called. | 121 // The file should be closed by the time that method is called. |
| 135 DCHECK(!file_stream_); | 122 DCHECK(!file_stream_); |
| 136 | 123 |
| 137 owns_file_ = false; | 124 owns_file_ = false; |
| 138 } | 125 } |
| 139 | 126 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 total_bytes_written_ = 0; | 163 total_bytes_written_ = 0; |
| 177 owns_file_ = true; | 164 owns_file_ = true; |
| 178 } else { | 165 } else { |
| 179 error_code_ = result; | 166 error_code_ = result; |
| 180 CloseAndDeleteFile(); | 167 CloseAndDeleteFile(); |
| 181 } | 168 } |
| 182 callback.Run(result); | 169 callback.Run(result); |
| 183 } | 170 } |
| 184 | 171 |
| 185 } // namespace net | 172 } // namespace net |
| OLD | NEW |