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/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/sequenced_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 return OK; | 33 return OK; |
34 } | 34 } |
35 | 35 |
36 int URLFetcherStringWriter::Write(IOBuffer* buffer, | 36 int URLFetcherStringWriter::Write(IOBuffer* buffer, |
37 int num_bytes, | 37 int num_bytes, |
38 const CompletionCallback& callback) { | 38 const CompletionCallback& callback) { |
39 data_.append(buffer->data(), num_bytes); | 39 data_.append(buffer->data(), num_bytes); |
40 return num_bytes; | 40 return num_bytes; |
41 } | 41 } |
42 | 42 |
43 int URLFetcherStringWriter::Finish(const CompletionCallback& callback) { | 43 int URLFetcherStringWriter::Finish(int net_error, |
| 44 const CompletionCallback& callback) { |
44 // Do nothing. | 45 // Do nothing. |
45 return OK; | 46 return OK; |
46 } | 47 } |
47 | 48 |
48 URLFetcherStringWriter* URLFetcherStringWriter::AsStringWriter() { | 49 URLFetcherStringWriter* URLFetcherStringWriter::AsStringWriter() { |
49 return this; | 50 return this; |
50 } | 51 } |
51 | 52 |
52 URLFetcherFileWriter::URLFetcherFileWriter( | 53 URLFetcherFileWriter::URLFetcherFileWriter( |
53 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 54 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 int result = file_stream_->Write(buffer, num_bytes, | 100 int result = file_stream_->Write(buffer, num_bytes, |
100 base::Bind(&URLFetcherFileWriter::DidWrite, | 101 base::Bind(&URLFetcherFileWriter::DidWrite, |
101 weak_factory_.GetWeakPtr(), | 102 weak_factory_.GetWeakPtr(), |
102 callback)); | 103 callback)); |
103 if (result < 0 && result != ERR_IO_PENDING) | 104 if (result < 0 && result != ERR_IO_PENDING) |
104 CloseAndDeleteFile(); | 105 CloseAndDeleteFile(); |
105 | 106 |
106 return result; | 107 return result; |
107 } | 108 } |
108 | 109 |
109 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { | 110 int URLFetcherFileWriter::Finish(int net_error, |
| 111 const CompletionCallback& callback) { |
| 112 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 113 if (net_error < 0) { |
| 114 // If an error occurred, simply delete the file after any pending operation |
| 115 // is done. Do not call file_stream_->Close() because there might |
| 116 // be an operation pending. See crbug.com/487732. |
| 117 CloseAndDeleteFile(); |
| 118 return OK; |
| 119 } |
110 // If the file_stream_ still exists at this point, close it. | 120 // If the file_stream_ still exists at this point, close it. |
111 if (file_stream_) { | 121 if (file_stream_) { |
112 int result = file_stream_->Close(base::Bind( | 122 int result = file_stream_->Close(base::Bind( |
113 &URLFetcherFileWriter::CloseComplete, | 123 &URLFetcherFileWriter::CloseComplete, |
114 weak_factory_.GetWeakPtr(), callback)); | 124 weak_factory_.GetWeakPtr(), callback)); |
115 if (result != ERR_IO_PENDING) | 125 if (result != ERR_IO_PENDING) |
116 file_stream_.reset(); | 126 file_stream_.reset(); |
117 return result; | 127 return result; |
118 } | 128 } |
119 return OK; | 129 return OK; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 } | 192 } |
183 | 193 |
184 void URLFetcherFileWriter::CloseComplete(const CompletionCallback& callback, | 194 void URLFetcherFileWriter::CloseComplete(const CompletionCallback& callback, |
185 int result) { | 195 int result) { |
186 // Destroy |file_stream_| whether or not the close succeeded. | 196 // Destroy |file_stream_| whether or not the close succeeded. |
187 file_stream_.reset(); | 197 file_stream_.reset(); |
188 callback.Run(result); | 198 callback.Run(result); |
189 } | 199 } |
190 | 200 |
191 } // namespace net | 201 } // namespace net |
OLD | NEW |