| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_manager.h" | 5 #include "content/browser/download/download_file_manager.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 112 |
| 113 void DownloadFileManager::UpdateInProgressDownloads() { | 113 void DownloadFileManager::UpdateInProgressDownloads() { |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 115 for (DownloadFileMap::iterator i = downloads_.begin(); | 115 for (DownloadFileMap::iterator i = downloads_.begin(); |
| 116 i != downloads_.end(); ++i) { | 116 i != downloads_.end(); ++i) { |
| 117 DownloadId global_id = i->first; | 117 DownloadId global_id = i->first; |
| 118 DownloadFile* download_file = i->second; | 118 DownloadFile* download_file = i->second; |
| 119 DownloadManager* manager = download_file->GetDownloadManager(); | 119 DownloadManager* manager = download_file->GetDownloadManager(); |
| 120 if (manager) { | 120 if (manager) { |
| 121 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 121 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 122 base::Bind(&DownloadManager::UpdateDownload, manager, | 122 base::Bind(&DownloadManager::UpdateDownload, |
| 123 global_id.local(), download_file->BytesSoFar())); | 123 manager, |
| 124 global_id.local(), |
| 125 download_file->BytesSoFar(), |
| 126 download_file->GetSha256HashState())); |
| 124 } | 127 } |
| 125 } | 128 } |
| 126 } | 129 } |
| 127 | 130 |
| 128 void DownloadFileManager::StartDownload( | 131 void DownloadFileManager::StartDownload( |
| 129 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { | 132 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { |
| 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 131 DCHECK(info); | 134 DCHECK(info); |
| 132 | 135 |
| 133 DownloadManager* manager = request_handle.GetDownloadManager(); | 136 DownloadManager* manager = request_handle.GetDownloadManager(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 const int data_len = (*contents)[i].second; | 171 const int data_len = (*contents)[i].second; |
| 169 if (!had_error && download_file) { | 172 if (!had_error && download_file) { |
| 170 net::Error write_result = | 173 net::Error write_result = |
| 171 download_file->AppendDataToFile(data->data(), data_len); | 174 download_file->AppendDataToFile(data->data(), data_len); |
| 172 if (write_result != net::OK) { | 175 if (write_result != net::OK) { |
| 173 // Write failed: interrupt the download. | 176 // Write failed: interrupt the download. |
| 174 DownloadManager* download_manager = download_file->GetDownloadManager(); | 177 DownloadManager* download_manager = download_file->GetDownloadManager(); |
| 175 had_error = true; | 178 had_error = true; |
| 176 | 179 |
| 177 int64 bytes_downloaded = download_file->BytesSoFar(); | 180 int64 bytes_downloaded = download_file->BytesSoFar(); |
| 181 std::string hash_state(download_file->GetSha256HashState()); |
| 182 |
| 178 // Calling this here in case we get more data, to avoid | 183 // Calling this here in case we get more data, to avoid |
| 179 // processing data after an error. That could lead to | 184 // processing data after an error. That could lead to |
| 180 // files that are corrupted if the later processing succeeded. | 185 // files that are corrupted if the later processing succeeded. |
| 181 CancelDownload(global_id); | 186 CancelDownload(global_id); |
| 182 download_file = NULL; // Was deleted in |CancelDownload|. | 187 download_file = NULL; // Was deleted in |CancelDownload|. |
| 183 | 188 |
| 184 if (download_manager) { | 189 if (download_manager) { |
| 185 BrowserThread::PostTask( | 190 BrowserThread::PostTask( |
| 186 BrowserThread::UI, FROM_HERE, | 191 BrowserThread::UI, FROM_HERE, |
| 187 base::Bind(&DownloadManager::OnDownloadInterrupted, | 192 base::Bind(&DownloadManager::OnDownloadInterrupted, |
| 188 download_manager, global_id.local(), bytes_downloaded, | 193 download_manager, |
| 194 global_id.local(), |
| 195 bytes_downloaded, |
| 196 hash_state, |
| 189 ConvertNetErrorToInterruptReason( | 197 ConvertNetErrorToInterruptReason( |
| 190 write_result, DOWNLOAD_INTERRUPT_FROM_DISK))); | 198 write_result, |
| 199 DOWNLOAD_INTERRUPT_FROM_DISK))); |
| 191 } | 200 } |
| 192 } | 201 } |
| 193 } | 202 } |
| 194 data->Release(); | 203 data->Release(); |
| 195 } | 204 } |
| 196 } | 205 } |
| 197 | 206 |
| 198 void DownloadFileManager::OnResponseCompleted( | 207 void DownloadFileManager::OnResponseCompleted( |
| 199 DownloadId global_id, | 208 DownloadId global_id, |
| 200 InterruptReason reason, | 209 InterruptReason reason, |
| 201 const std::string& security_info) { | 210 const std::string& security_info) { |
| 202 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id | 211 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id |
| 203 << " reason = " << InterruptReasonDebugString(reason) | 212 << " reason = " << InterruptReasonDebugString(reason) |
| 204 << " security_info = \"" << security_info << "\""; | 213 << " security_info = \"" << security_info << "\""; |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 206 DownloadFile* download_file = GetDownloadFile(global_id); | 215 DownloadFile* download_file = GetDownloadFile(global_id); |
| 207 if (!download_file) | 216 if (!download_file) |
| 208 return; | 217 return; |
| 209 | 218 |
| 210 download_file->Finish(); | 219 download_file->Finish(); |
| 211 | 220 |
| 212 DownloadManager* download_manager = download_file->GetDownloadManager(); | 221 DownloadManager* download_manager = download_file->GetDownloadManager(); |
| 213 if (!download_manager) { | 222 if (!download_manager) { |
| 214 CancelDownload(global_id); | 223 CancelDownload(global_id); |
| 215 return; | 224 return; |
| 216 } | 225 } |
| 217 | 226 |
| 218 std::string hash; | 227 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 219 if (!download_file->GetSha256Hash(&hash) || BaseFile::IsEmptySha256Hash(hash)) | 228 std::string hash; |
| 220 hash.clear(); | 229 if (!download_file->GetSha256Hash(&hash) || |
| 230 BaseFile::IsEmptySha256Hash(hash)) { |
| 231 hash.clear(); |
| 232 } |
| 221 | 233 |
| 222 if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) { | |
| 223 BrowserThread::PostTask( | 234 BrowserThread::PostTask( |
| 224 BrowserThread::UI, FROM_HERE, | 235 BrowserThread::UI, FROM_HERE, |
| 225 base::Bind(&DownloadManager::OnResponseCompleted, | 236 base::Bind(&DownloadManager::OnResponseCompleted, |
| 226 download_manager, global_id.local(), | 237 download_manager, global_id.local(), |
| 227 download_file->BytesSoFar(), hash)); | 238 download_file->BytesSoFar(), hash)); |
| 228 } else { | 239 } else { |
| 229 BrowserThread::PostTask( | 240 BrowserThread::PostTask( |
| 230 BrowserThread::UI, FROM_HERE, | 241 BrowserThread::UI, FROM_HERE, |
| 231 base::Bind(&DownloadManager::OnDownloadInterrupted, | 242 base::Bind(&DownloadManager::OnDownloadInterrupted, |
| 232 download_manager, global_id.local(), | 243 download_manager, |
| 233 download_file->BytesSoFar(), reason)); | 244 global_id.local(), |
| 245 download_file->BytesSoFar(), |
| 246 download_file->GetSha256HashState(), |
| 247 reason)); |
| 234 } | 248 } |
| 235 // We need to keep the download around until the UI thread has finalized | 249 // We need to keep the download around until the UI thread has finalized |
| 236 // the name. | 250 // the name. |
| 237 } | 251 } |
| 238 | 252 |
| 239 // This method will be sent via a user action, or shutdown on the UI thread, and | 253 // This method will be sent via a user action, or shutdown on the UI thread, and |
| 240 // run on the download thread. Since this message has been sent from the UI | 254 // run on the download thread. Since this message has been sent from the UI |
| 241 // thread, the download may have already completed and won't exist in our map. | 255 // thread, the download may have already completed and won't exist in our map. |
| 242 void DownloadFileManager::CancelDownload(DownloadId global_id) { | 256 void DownloadFileManager::CancelDownload(DownloadId global_id) { |
| 243 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id; | 257 VLOG(20) << __FUNCTION__ << "()" << " id = " << global_id; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 // Without a download manager, we can't cancel the request normally, so we | 414 // Without a download manager, we can't cancel the request normally, so we |
| 401 // need to do it here. The normal path will also update the download | 415 // need to do it here. The normal path will also update the download |
| 402 // history before canceling the request. | 416 // history before canceling the request. |
| 403 download_file->CancelDownloadRequest(); | 417 download_file->CancelDownloadRequest(); |
| 404 return; | 418 return; |
| 405 } | 419 } |
| 406 | 420 |
| 407 BrowserThread::PostTask( | 421 BrowserThread::PostTask( |
| 408 BrowserThread::UI, FROM_HERE, | 422 BrowserThread::UI, FROM_HERE, |
| 409 base::Bind(&DownloadManager::OnDownloadInterrupted, | 423 base::Bind(&DownloadManager::OnDownloadInterrupted, |
| 410 download_manager, global_id.local(), | 424 download_manager, |
| 425 global_id.local(), |
| 411 download_file->BytesSoFar(), | 426 download_file->BytesSoFar(), |
| 427 download_file->GetSha256HashState(), |
| 412 ConvertNetErrorToInterruptReason( | 428 ConvertNetErrorToInterruptReason( |
| 413 rename_error, DOWNLOAD_INTERRUPT_FROM_DISK))); | 429 rename_error, |
| 430 DOWNLOAD_INTERRUPT_FROM_DISK))); |
| 414 } | 431 } |
| 415 | 432 |
| 416 void DownloadFileManager::EraseDownload(DownloadId global_id) { | 433 void DownloadFileManager::EraseDownload(DownloadId global_id) { |
| 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 418 | 435 |
| 419 if (!ContainsKey(downloads_, global_id)) | 436 if (!ContainsKey(downloads_, global_id)) |
| 420 return; | 437 return; |
| 421 | 438 |
| 422 DownloadFile* download_file = downloads_[global_id]; | 439 DownloadFile* download_file = downloads_[global_id]; |
| 423 | 440 |
| 424 VLOG(20) << " " << __FUNCTION__ << "()" | 441 VLOG(20) << " " << __FUNCTION__ << "()" |
| 425 << " id = " << global_id | 442 << " id = " << global_id |
| 426 << " download_file = " << download_file->DebugString(); | 443 << " download_file = " << download_file->DebugString(); |
| 427 | 444 |
| 428 downloads_.erase(global_id); | 445 downloads_.erase(global_id); |
| 429 | 446 |
| 430 delete download_file; | 447 delete download_file; |
| 431 | 448 |
| 432 if (downloads_.empty()) | 449 if (downloads_.empty()) |
| 433 StopUpdateTimer(); | 450 StopUpdateTimer(); |
| 434 } | 451 } |
| OLD | NEW |