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