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 "chrome/browser/download/save_package.h" | 5 #include "chrome/browser/download/save_package.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/i18n/file_util_icu.h" | 11 #include "base/i18n/file_util_icu.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/stl_util-inl.h" | 14 #include "base/stl_util-inl.h" |
15 #include "base/string_piece.h" | 15 #include "base/string_piece.h" |
16 #include "base/string_split.h" | 16 #include "base/string_split.h" |
17 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "base/task.h" | 19 #include "base/task.h" |
20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
21 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
22 #include "chrome/browser/browser_thread.h" | 22 #include "chrome/browser/browser_thread.h" |
23 #include "chrome/browser/download/download_history.h" | |
23 #include "chrome/browser/download/download_item.h" | 24 #include "chrome/browser/download/download_item.h" |
24 #include "chrome/browser/download/download_item_model.h" | 25 #include "chrome/browser/download/download_item_model.h" |
25 #include "chrome/browser/download/download_manager.h" | 26 #include "chrome/browser/download/download_manager.h" |
26 #include "chrome/browser/download/download_shelf.h" | 27 #include "chrome/browser/download/download_shelf.h" |
27 #include "chrome/browser/download/download_util.h" | 28 #include "chrome/browser/download/download_util.h" |
28 #include "chrome/browser/download/save_file.h" | 29 #include "chrome/browser/download/save_file.h" |
29 #include "chrome/browser/download/save_file_manager.h" | 30 #include "chrome/browser/download/save_file_manager.h" |
30 #include "chrome/browser/download/save_item.h" | 31 #include "chrome/browser/download/save_item.h" |
31 #include "chrome/browser/net/url_fixer_upper.h" | 32 #include "chrome/browser/net/url_fixer_upper.h" |
32 #include "chrome/browser/platform_util.h" | 33 #include "chrome/browser/platform_util.h" |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 | 341 |
341 // Initialize the request context and resource dispatcher. | 342 // Initialize the request context and resource dispatcher. |
342 Profile* profile = tab_contents_->profile(); | 343 Profile* profile = tab_contents_->profile(); |
343 if (!profile) { | 344 if (!profile) { |
344 NOTREACHED(); | 345 NOTREACHED(); |
345 return false; | 346 return false; |
346 } | 347 } |
347 | 348 |
348 request_context_getter_ = profile->GetRequestContext(); | 349 request_context_getter_ = profile->GetRequestContext(); |
349 | 350 |
350 // Create the fake DownloadItem and display the view. | 351 // Create the fake DownloadItem and add it to the download history. |
351 DownloadManager* download_manager = | 352 CreateDownloadItem(saved_main_file_path_, |
352 tab_contents_->profile()->GetDownloadManager(); | 353 page_url_, |
353 download_ = new DownloadItem(download_manager, | 354 profile->IsOffTheRecord()); |
354 saved_main_file_path_, | |
355 page_url_, | |
356 profile->IsOffTheRecord()); | |
357 | |
358 // Transfer the ownership to the download manager. We need the DownloadItem | |
359 // to be alive as long as the Profile is alive. | |
360 download_manager->SavePageAsDownloadStarted(download_); | |
361 | 355 |
362 tab_contents_->OnStartDownload(download_); | 356 tab_contents_->OnStartDownload(download_); |
363 | 357 |
364 // Check save type and process the save page job. | 358 // Check save type and process the save page job. |
365 if (save_type_ == SAVE_AS_COMPLETE_HTML) { | 359 if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
366 // Get directory | 360 // Get directory |
367 DCHECK(!saved_main_directory_path_.empty()); | 361 DCHECK(!saved_main_directory_path_.empty()); |
368 GetAllSavableResourceLinksForCurrentPage(); | 362 GetAllSavableResourceLinksForCurrentPage(); |
369 } else { | 363 } else { |
370 wait_state_ = NET_FILES; | 364 wait_state_ = NET_FILES; |
371 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? | 365 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? |
372 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 366 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
373 SaveFileCreateInfo::SAVE_FILE_FROM_NET; | 367 SaveFileCreateInfo::SAVE_FILE_FROM_NET; |
374 SaveItem* save_item = new SaveItem(page_url_, | 368 SaveItem* save_item = new SaveItem(page_url_, |
375 GURL(), | 369 GURL(), |
376 this, | 370 this, |
377 save_source); | 371 save_source); |
378 // Add this item to waiting list. | 372 // Add this item to waiting list. |
379 waiting_item_queue_.push(save_item); | 373 waiting_item_queue_.push(save_item); |
380 all_save_items_count_ = 1; | 374 all_save_items_count_ = 1; |
381 download_->set_total_bytes(1); | 375 download_->set_total_bytes(1); |
382 | 376 |
383 DoSavingProcess(); | 377 DoSavingProcess(); |
384 } | 378 } |
385 | 379 |
386 return true; | 380 return true; |
387 } | 381 } |
388 | 382 |
383 void SavePackage::OnDownloadEntryAdded(DownloadCreateInfo info, | |
384 int64 db_handle) { | |
385 DownloadManager* download_manager = | |
386 tab_contents_->profile()->GetDownloadManager(); | |
387 | |
388 download_manager->AddDownloadItemToHistory(download_, db_handle); | |
389 } | |
390 | |
391 void SavePackage::CreateDownloadItem(const FilePath& path, | |
392 const GURL& url, | |
393 bool is_otr) { | |
394 DownloadManager* download_manager = | |
395 tab_contents_->profile()->GetDownloadManager(); | |
396 | |
397 DownloadItem* item = new DownloadItem(download_manager, path, url, is_otr); | |
Paweł Hajdan Jr.
2011/02/11 19:17:49
nit: If it's going to be stored on |item_| anyway,
magnus
2011/02/13 03:32:41
Done.
| |
398 download_ = item; | |
399 // Transfer the ownership to the download manager. We need the DownloadItem | |
400 // to be alive as long as the Profile is alive. | |
401 download_manager->SavePageAsDownloadStarted(item); | |
402 // Copy over the fields used by the history service | |
403 DownloadCreateInfo info(item->full_path(), | |
404 item->url(), | |
405 item->start_time(), | |
406 0, 0, | |
407 item->state(), | |
408 item->id(), | |
409 false); | |
410 DownloadHistory* download_history = download_manager->download_history(); | |
411 // Add entry to the history service | |
Paweł Hajdan Jr.
2011/02/11 19:17:49
nit: Dot at the end. Also, please add a blank line
magnus
2011/02/13 03:32:41
Done.
| |
412 download_history->AddEntry(info, download_, | |
413 NewCallback(this, &SavePackage::OnDownloadEntryAdded)); | |
414 } | |
415 | |
389 // Generate name for saving resource. | 416 // Generate name for saving resource. |
390 bool SavePackage::GenerateFileName(const std::string& disposition, | 417 bool SavePackage::GenerateFileName(const std::string& disposition, |
391 const GURL& url, | 418 const GURL& url, |
392 bool need_html_ext, | 419 bool need_html_ext, |
393 FilePath::StringType* generated_name) { | 420 FilePath::StringType* generated_name) { |
394 // TODO(jungshik): Figure out the referrer charset when having one | 421 // TODO(jungshik): Figure out the referrer charset when having one |
395 // makes sense and pass it to GetSuggestedFilename. | 422 // makes sense and pass it to GetSuggestedFilename. |
396 string16 suggested_name = | 423 string16 suggested_name = |
397 net::GetSuggestedFilename(url, disposition, "", | 424 net::GetSuggestedFilename(url, disposition, "", |
398 ASCIIToUTF16(kDefaultSaveName)); | 425 ASCIIToUTF16(kDefaultSaveName)); |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
656 BrowserThread::PostTask( | 683 BrowserThread::PostTask( |
657 BrowserThread::FILE, FROM_HERE, | 684 BrowserThread::FILE, FROM_HERE, |
658 NewRunnableMethod(file_manager_, | 685 NewRunnableMethod(file_manager_, |
659 &SaveFileManager::RemoveSavedFileFromFileMap, | 686 &SaveFileManager::RemoveSavedFileFromFileMap, |
660 save_ids)); | 687 save_ids)); |
661 | 688 |
662 finished_ = true; | 689 finished_ = true; |
663 wait_state_ = FAILED; | 690 wait_state_ = FAILED; |
664 | 691 |
665 // Inform the DownloadItem we have canceled whole save page job. | 692 // Inform the DownloadItem we have canceled whole save page job. |
693 DownloadManager* download_manager = | |
694 tab_contents_->profile()->GetDownloadManager(); | |
666 download_->Cancel(false); | 695 download_->Cancel(false); |
696 // Remove DownloadItem from history. | |
697 download_manager->download_history()->RemoveEntry(download_); | |
Randy Smith (Not in Mondays)
2011/02/11 19:10:07
I'm ok with the difference in behavior, but this i
magnus
2011/02/13 03:32:41
Changed it to keep the download in the history.
| |
667 } | 698 } |
668 | 699 |
669 void SavePackage::CheckFinish() { | 700 void SavePackage::CheckFinish() { |
670 if (in_process_count() || finished_) | 701 if (in_process_count() || finished_) |
671 return; | 702 return; |
672 | 703 |
673 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && | 704 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && |
674 saved_success_items_.size() > 1) ? | 705 saved_success_items_.size() > 1) ? |
675 saved_main_directory_path_ : FilePath(); | 706 saved_main_directory_path_ : FilePath(); |
676 | 707 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
709 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); | 740 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); |
710 it != saved_failed_items_.end(); ++it) | 741 it != saved_failed_items_.end(); ++it) |
711 save_ids.push_back(it->second->save_id()); | 742 save_ids.push_back(it->second->save_id()); |
712 | 743 |
713 BrowserThread::PostTask( | 744 BrowserThread::PostTask( |
714 BrowserThread::FILE, FROM_HERE, | 745 BrowserThread::FILE, FROM_HERE, |
715 NewRunnableMethod(file_manager_, | 746 NewRunnableMethod(file_manager_, |
716 &SaveFileManager::RemoveSavedFileFromFileMap, | 747 &SaveFileManager::RemoveSavedFileFromFileMap, |
717 save_ids)); | 748 save_ids)); |
718 | 749 |
719 download_->OnAllDataSaved(all_save_items_count_); | 750 DownloadItem* item = download_; |
Randy Smith (Not in Mondays)
2011/02/11 19:10:07
What's the value of the local variable rather than
magnus
2011/02/13 03:32:41
Removed local variable.
| |
720 download_->MarkAsComplete(); | 751 item->OnAllDataSaved(all_save_items_count_); |
752 item->MarkAsComplete(); | |
721 // Notify download observers that we are complete (the call | 753 // Notify download observers that we are complete (the call |
722 // to OnReadyToFinish() set the state to complete but did not notify). | 754 // to OnReadyToFinish() set the state to complete but did not notify). |
723 download_->UpdateObservers(); | 755 item->UpdateObservers(); |
756 // Update the download history | |
Paweł Hajdan Jr.
2011/02/11 19:17:49
nit: Dot at the end.
magnus
2011/02/13 03:32:41
Done.
| |
757 DownloadManager* download_manager = | |
758 tab_contents_->profile()->GetDownloadManager(); | |
759 download_manager->download_history()->UpdateEntry(item); | |
724 | 760 |
725 NotificationService::current()->Notify( | 761 NotificationService::current()->Notify( |
726 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 762 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
727 Source<SavePackage>(this), | 763 Source<SavePackage>(this), |
728 Details<GURL>(&page_url_)); | 764 Details<GURL>(&page_url_)); |
729 } | 765 } |
730 | 766 |
731 // Called for updating end state. | 767 // Called for updating end state. |
732 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 768 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
733 // Because we might have canceled this saving job before, | 769 // Because we might have canceled this saving job before, |
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1431 } | 1467 } |
1432 | 1468 |
1433 // SelectFileDialog::Listener interface. | 1469 // SelectFileDialog::Listener interface. |
1434 void SavePackage::FileSelected(const FilePath& path, | 1470 void SavePackage::FileSelected(const FilePath& path, |
1435 int index, void* params) { | 1471 int index, void* params) { |
1436 ContinueSave(path, index); | 1472 ContinueSave(path, index); |
1437 } | 1473 } |
1438 | 1474 |
1439 void SavePackage::FileSelectionCanceled(void* params) { | 1475 void SavePackage::FileSelectionCanceled(void* params) { |
1440 } | 1476 } |
OLD | NEW |