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 "content/browser/download/download_file_impl.h" | 5 #include "content/browser/download/download_file_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 if (!update_timer_->IsRunning()) { | 81 if (!update_timer_->IsRunning()) { |
82 update_timer_->Start(FROM_HERE, | 82 update_timer_->Start(FROM_HERE, |
83 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), | 83 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), |
84 this, &DownloadFileImpl::SendUpdate); | 84 this, &DownloadFileImpl::SendUpdate); |
85 } | 85 } |
86 return content::ConvertNetErrorToInterruptReason( | 86 return content::ConvertNetErrorToInterruptReason( |
87 file_.AppendDataToFile(data, data_len), | 87 file_.AppendDataToFile(data, data_len), |
88 content::DOWNLOAD_INTERRUPT_FROM_DISK); | 88 content::DOWNLOAD_INTERRUPT_FROM_DISK); |
89 } | 89 } |
90 | 90 |
91 void DownloadFileImpl::Rename(const FilePath& full_path, | 91 content::DownloadInterruptReason DownloadFileImpl::Rename( |
92 bool overwrite_existing_file, | 92 const FilePath& full_path) { |
93 const RenameCompletionCallback& callback) { | 93 return content::ConvertNetErrorToInterruptReason( |
94 FilePath new_path(full_path); | 94 file_.Rename(full_path), |
95 if (!overwrite_existing_file) { | 95 content::DOWNLOAD_INTERRUPT_FROM_DISK); |
96 // Make the file unique if requested. | |
97 int uniquifier = | |
98 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); | |
99 if (uniquifier > 0) { | |
100 new_path = new_path.InsertBeforeExtensionASCII( | |
101 StringPrintf(" (%d)", uniquifier)); | |
102 } | |
103 } | |
104 | |
105 net::Error rename_error = file_.Rename(new_path); | |
106 content::DownloadInterruptReason reason( | |
107 content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
108 if (net::OK != rename_error) { | |
109 // Make sure our information is updated, since we're about to | |
110 // error out. | |
111 SendUpdate(); | |
112 | |
113 reason = | |
114 content::ConvertNetErrorToInterruptReason( | |
115 rename_error, | |
116 content::DOWNLOAD_INTERRUPT_FROM_DISK); | |
117 | |
118 new_path.clear(); | |
119 } | |
120 | |
121 BrowserThread::PostTask( | |
122 BrowserThread::UI, FROM_HERE, | |
123 base::Bind(callback, reason, new_path)); | |
124 } | 96 } |
125 | 97 |
126 void DownloadFileImpl::Detach() { | 98 void DownloadFileImpl::Detach() { |
127 file_.Detach(); | 99 file_.Detach(); |
128 } | 100 } |
129 | 101 |
130 void DownloadFileImpl::Cancel() { | 102 void DownloadFileImpl::Cancel() { |
131 file_.Cancel(); | 103 file_.Cancel(); |
132 } | 104 } |
133 | 105 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 183 |
212 switch (state) { | 184 switch (state) { |
213 case content::ByteStreamReader::STREAM_EMPTY: | 185 case content::ByteStreamReader::STREAM_EMPTY: |
214 break; | 186 break; |
215 case content::ByteStreamReader::STREAM_HAS_DATA: | 187 case content::ByteStreamReader::STREAM_HAS_DATA: |
216 { | 188 { |
217 ++num_buffers; | 189 ++num_buffers; |
218 base::TimeTicks write_start(base::TimeTicks::Now()); | 190 base::TimeTicks write_start(base::TimeTicks::Now()); |
219 reason = AppendDataToFile( | 191 reason = AppendDataToFile( |
220 incoming_data.get()->data(), incoming_data_size); | 192 incoming_data.get()->data(), incoming_data_size); |
221 // Note that if we're after a rename failure but before any | |
222 // cancel that our owner generates based on that rename failure, | |
223 // we'll get an ERR_UNEXPECTED from the above. Our consumers | |
224 // need to handle this situation. | |
225 disk_writes_time_ += (base::TimeTicks::Now() - write_start); | 193 disk_writes_time_ += (base::TimeTicks::Now() - write_start); |
226 bytes_seen_ += incoming_data_size; | 194 bytes_seen_ += incoming_data_size; |
227 total_incoming_data_size += incoming_data_size; | 195 total_incoming_data_size += incoming_data_size; |
228 } | 196 } |
229 break; | 197 break; |
230 case content::ByteStreamReader::STREAM_COMPLETE: | 198 case content::ByteStreamReader::STREAM_COMPLETE: |
231 { | 199 { |
232 reason = stream_reader_->GetStatus(); | 200 reason = stream_reader_->GetStatus(); |
233 SendUpdate(); | 201 SendUpdate(); |
234 base::TimeTicks close_start(base::TimeTicks::Now()); | 202 base::TimeTicks close_start(base::TimeTicks::Now()); |
(...skipping 28 matching lines...) Expand all Loading... |
263 | 231 |
264 download_stats::RecordContiguousWriteTime(now - start); | 232 download_stats::RecordContiguousWriteTime(now - start); |
265 | 233 |
266 // Take care of communication with our controller. | 234 // Take care of communication with our controller. |
267 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | 235 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
268 // Error case for both upstream source and file write. | 236 // Error case for both upstream source and file write. |
269 // Shut down processing and signal an error to our controller. | 237 // Shut down processing and signal an error to our controller. |
270 // Our controller will clean us up. | 238 // Our controller will clean us up. |
271 stream_reader_->RegisterCallback(base::Closure()); | 239 stream_reader_->RegisterCallback(base::Closure()); |
272 weak_factory_.InvalidateWeakPtrs(); | 240 weak_factory_.InvalidateWeakPtrs(); |
273 SendUpdate(); // Make info up to date before error. | |
274 BrowserThread::PostTask( | 241 BrowserThread::PostTask( |
275 BrowserThread::UI, FROM_HERE, | 242 BrowserThread::UI, FROM_HERE, |
276 base::Bind(&DownloadManager::OnDownloadInterrupted, | 243 base::Bind(&DownloadManager::OnDownloadInterrupted, |
277 download_manager_, id_.local(), reason)); | 244 download_manager_, id_.local(), |
| 245 BytesSoFar(), GetHashState(), reason)); |
278 } else if (state == content::ByteStreamReader::STREAM_COMPLETE) { | 246 } else if (state == content::ByteStreamReader::STREAM_COMPLETE) { |
279 // Signal successful completion and shut down processing. | 247 // Signal successful completion and shut down processing. |
280 stream_reader_->RegisterCallback(base::Closure()); | 248 stream_reader_->RegisterCallback(base::Closure()); |
281 weak_factory_.InvalidateWeakPtrs(); | 249 weak_factory_.InvalidateWeakPtrs(); |
282 std::string hash; | 250 std::string hash; |
283 if (!GetHash(&hash) || file_.IsEmptyHash(hash)) | 251 if (!GetHash(&hash) || file_.IsEmptyHash(hash)) |
284 hash.clear(); | 252 hash.clear(); |
285 BrowserThread::PostTask( | 253 BrowserThread::PostTask( |
286 BrowserThread::UI, FROM_HERE, | 254 BrowserThread::UI, FROM_HERE, |
287 base::Bind(&DownloadManager::OnResponseCompleted, | 255 base::Bind(&DownloadManager::OnResponseCompleted, |
288 download_manager_, id_.local(), | 256 download_manager_, id_.local(), |
289 BytesSoFar(), hash)); | 257 BytesSoFar(), hash)); |
290 } | 258 } |
291 if (bound_net_log_.IsLoggingAllEvents()) { | 259 if (bound_net_log_.IsLoggingAllEvents()) { |
292 bound_net_log_.AddEvent( | 260 bound_net_log_.AddEvent( |
293 net::NetLog::TYPE_DOWNLOAD_STREAM_DRAINED, | 261 net::NetLog::TYPE_DOWNLOAD_STREAM_DRAINED, |
294 base::Bind(&download_net_logs::FileStreamDrainedCallback, | 262 base::Bind(&download_net_logs::FileStreamDrainedCallback, |
295 total_incoming_data_size, num_buffers)); | 263 total_incoming_data_size, num_buffers)); |
296 } | 264 } |
297 } | 265 } |
298 | 266 |
299 void DownloadFileImpl::SendUpdate() { | 267 void DownloadFileImpl::SendUpdate() { |
300 BrowserThread::PostTask( | 268 BrowserThread::PostTask( |
301 BrowserThread::UI, FROM_HERE, | 269 BrowserThread::UI, FROM_HERE, |
302 base::Bind(&DownloadManager::UpdateDownload, | 270 base::Bind(&DownloadManager::UpdateDownload, |
303 download_manager_, id_.local(), | 271 download_manager_, id_.local(), |
304 BytesSoFar(), CurrentSpeed(), GetHashState())); | 272 BytesSoFar(), CurrentSpeed(), GetHashState())); |
305 } | 273 } |
OLD | NEW |