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, |
54 const base::FilePath& file_path) | 55 const base::FilePath& file_path) |
55 : file_task_runner_(file_task_runner), | 56 : file_task_runner_(file_task_runner), |
56 file_path_(file_path), | 57 file_path_(file_path), |
57 owns_file_(false), | 58 owns_file_(false), |
58 weak_factory_(this) { | 59 weak_factory_(this) { |
59 DCHECK(file_task_runner_.get()); | 60 DCHECK(file_task_runner_.get()); |
60 } | 61 } |
61 | 62 |
62 URLFetcherFileWriter::~URLFetcherFileWriter() { | 63 URLFetcherFileWriter::~URLFetcherFileWriter() { |
63 CloseAndDeleteFile(); | 64 CloseAndDeleteFile(); |
64 } | 65 } |
65 | 66 |
66 int URLFetcherFileWriter::Initialize(const CompletionCallback& callback) { | 67 int URLFetcherFileWriter::Initialize(const CompletionCallback& callback) { |
67 file_stream_.reset(new FileStream(file_task_runner_)); | 68 file_stream_.reset(new FileStream(file_task_runner_)); |
68 | 69 |
69 int result = ERR_IO_PENDING; | 70 int result = ERR_IO_PENDING; |
| 71 owns_file_ = true; |
70 if (file_path_.empty()) { | 72 if (file_path_.empty()) { |
71 base::FilePath* temp_file_path = new base::FilePath; | 73 base::FilePath* temp_file_path = new base::FilePath; |
72 base::PostTaskAndReplyWithResult( | 74 base::PostTaskAndReplyWithResult( |
73 file_task_runner_.get(), | 75 file_task_runner_.get(), |
74 FROM_HERE, | 76 FROM_HERE, |
75 base::Bind(&base::CreateTemporaryFile, temp_file_path), | 77 base::Bind(&base::CreateTemporaryFile, temp_file_path), |
76 base::Bind(&URLFetcherFileWriter::DidCreateTempFile, | 78 base::Bind(&URLFetcherFileWriter::DidCreateTempFile, |
77 weak_factory_.GetWeakPtr(), | 79 weak_factory_.GetWeakPtr(), |
78 callback, | 80 callback, |
79 base::Owned(temp_file_path))); | 81 base::Owned(temp_file_path))); |
(...skipping 19 matching lines...) Expand all Loading... |
99 int result = file_stream_->Write(buffer, num_bytes, | 101 int result = file_stream_->Write(buffer, num_bytes, |
100 base::Bind(&URLFetcherFileWriter::DidWrite, | 102 base::Bind(&URLFetcherFileWriter::DidWrite, |
101 weak_factory_.GetWeakPtr(), | 103 weak_factory_.GetWeakPtr(), |
102 callback)); | 104 callback)); |
103 if (result < 0 && result != ERR_IO_PENDING) | 105 if (result < 0 && result != ERR_IO_PENDING) |
104 CloseAndDeleteFile(); | 106 CloseAndDeleteFile(); |
105 | 107 |
106 return result; | 108 return result; |
107 } | 109 } |
108 | 110 |
109 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { | 111 int URLFetcherFileWriter::Finish(int net_error, |
| 112 const CompletionCallback& callback) { |
| 113 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 114 if (net_error < 0) { |
| 115 // If an error occurred, simply delete the file after any pending operation |
| 116 // is done. Do not call file_stream_->Close() because there might |
| 117 // be an operation pending. See crbug.com/487732. |
| 118 CloseAndDeleteFile(); |
| 119 return OK; |
| 120 } |
110 // If the file_stream_ still exists at this point, close it. | 121 // If the file_stream_ still exists at this point, close it. |
111 if (file_stream_) { | 122 if (file_stream_) { |
112 int result = file_stream_->Close(base::Bind( | 123 int result = file_stream_->Close(base::Bind( |
113 &URLFetcherFileWriter::CloseComplete, | 124 &URLFetcherFileWriter::CloseComplete, |
114 weak_factory_.GetWeakPtr(), callback)); | 125 weak_factory_.GetWeakPtr(), callback)); |
115 if (result != ERR_IO_PENDING) | 126 if (result != ERR_IO_PENDING) |
116 file_stream_.reset(); | 127 file_stream_.reset(); |
117 return result; | 128 return result; |
118 } | 129 } |
119 return OK; | 130 return OK; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 } | 163 } |
153 | 164 |
154 void URLFetcherFileWriter::DidCreateTempFile(const CompletionCallback& callback, | 165 void URLFetcherFileWriter::DidCreateTempFile(const CompletionCallback& callback, |
155 base::FilePath* temp_file_path, | 166 base::FilePath* temp_file_path, |
156 bool success) { | 167 bool success) { |
157 if (!success) { | 168 if (!success) { |
158 callback.Run(ERR_FILE_NOT_FOUND); | 169 callback.Run(ERR_FILE_NOT_FOUND); |
159 return; | 170 return; |
160 } | 171 } |
161 file_path_ = *temp_file_path; | 172 file_path_ = *temp_file_path; |
162 owns_file_ = true; | |
163 const int result = file_stream_->Open( | 173 const int result = file_stream_->Open( |
164 file_path_, | 174 file_path_, |
165 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | | 175 base::File::FLAG_WRITE | base::File::FLAG_ASYNC | |
166 base::File::FLAG_OPEN, | 176 base::File::FLAG_OPEN, |
167 base::Bind(&URLFetcherFileWriter::DidOpenFile, | 177 base::Bind(&URLFetcherFileWriter::DidOpenFile, |
168 weak_factory_.GetWeakPtr(), | 178 weak_factory_.GetWeakPtr(), |
169 callback)); | 179 callback)); |
170 if (result != ERR_IO_PENDING) | 180 if (result != ERR_IO_PENDING) |
171 DidOpenFile(callback, result); | 181 DidOpenFile(callback, result); |
172 } | 182 } |
173 | 183 |
174 void URLFetcherFileWriter::DidOpenFile(const CompletionCallback& callback, | 184 void URLFetcherFileWriter::DidOpenFile(const CompletionCallback& callback, |
175 int result) { | 185 int result) { |
176 if (result == OK) | 186 if (result < OK) |
177 owns_file_ = true; | |
178 else | |
179 CloseAndDeleteFile(); | 187 CloseAndDeleteFile(); |
180 | 188 |
181 callback.Run(result); | 189 callback.Run(result); |
182 } | 190 } |
183 | 191 |
184 void URLFetcherFileWriter::CloseComplete(const CompletionCallback& callback, | 192 void URLFetcherFileWriter::CloseComplete(const CompletionCallback& callback, |
185 int result) { | 193 int result) { |
186 // Destroy |file_stream_| whether or not the close succeeded. | 194 // Destroy |file_stream_| whether or not the close succeeded. |
187 file_stream_.reset(); | 195 file_stream_.reset(); |
188 callback.Run(result); | 196 callback.Run(result); |
189 } | 197 } |
190 | 198 |
191 } // namespace net | 199 } // namespace net |
OLD | NEW |