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