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