| 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 download_ = new DownloadItem(download_manager, path, url, is_otr); |
| 398 |
| 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(download_); |
| 402 |
| 403 // Copy over the fields used by the history service. |
| 404 DownloadCreateInfo info(download_->full_path(), |
| 405 download_->url(), |
| 406 download_->start_time(), |
| 407 0, 0, |
| 408 download_->state(), |
| 409 download_->id(), |
| 410 false); |
| 411 |
| 412 // Add entry to the history service. |
| 413 DownloadHistory* download_history = download_manager->download_history(); |
| 414 download_history->AddEntry(info, download_, |
| 415 NewCallback(this, &SavePackage::OnDownloadEntryAdded)); |
| 416 } |
| 417 |
| 389 // Generate name for saving resource. | 418 // Generate name for saving resource. |
| 390 bool SavePackage::GenerateFileName(const std::string& disposition, | 419 bool SavePackage::GenerateFileName(const std::string& disposition, |
| 391 const GURL& url, | 420 const GURL& url, |
| 392 bool need_html_ext, | 421 bool need_html_ext, |
| 393 FilePath::StringType* generated_name) { | 422 FilePath::StringType* generated_name) { |
| 394 // TODO(jungshik): Figure out the referrer charset when having one | 423 // TODO(jungshik): Figure out the referrer charset when having one |
| 395 // makes sense and pass it to GetSuggestedFilename. | 424 // makes sense and pass it to GetSuggestedFilename. |
| 396 string16 suggested_name = | 425 string16 suggested_name = |
| 397 net::GetSuggestedFilename(url, disposition, "", | 426 net::GetSuggestedFilename(url, disposition, "", |
| 398 ASCIIToUTF16(kDefaultSaveName)); | 427 ASCIIToUTF16(kDefaultSaveName)); |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 BrowserThread::FILE, FROM_HERE, | 686 BrowserThread::FILE, FROM_HERE, |
| 658 NewRunnableMethod(file_manager_, | 687 NewRunnableMethod(file_manager_, |
| 659 &SaveFileManager::RemoveSavedFileFromFileMap, | 688 &SaveFileManager::RemoveSavedFileFromFileMap, |
| 660 save_ids)); | 689 save_ids)); |
| 661 | 690 |
| 662 finished_ = true; | 691 finished_ = true; |
| 663 wait_state_ = FAILED; | 692 wait_state_ = FAILED; |
| 664 | 693 |
| 665 // Inform the DownloadItem we have canceled whole save page job. | 694 // Inform the DownloadItem we have canceled whole save page job. |
| 666 download_->Cancel(false); | 695 download_->Cancel(false); |
| 696 DownloadManager* download_manager = |
| 697 tab_contents_->profile()->GetDownloadManager(); |
| 698 download_manager->download_history()->UpdateEntry(download_); |
| 667 } | 699 } |
| 668 | 700 |
| 669 void SavePackage::CheckFinish() { | 701 void SavePackage::CheckFinish() { |
| 670 if (in_process_count() || finished_) | 702 if (in_process_count() || finished_) |
| 671 return; | 703 return; |
| 672 | 704 |
| 673 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && | 705 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && |
| 674 saved_success_items_.size() > 1) ? | 706 saved_success_items_.size() > 1) ? |
| 675 saved_main_directory_path_ : FilePath(); | 707 saved_main_directory_path_ : FilePath(); |
| 676 | 708 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 save_ids.push_back(it->second->save_id()); | 743 save_ids.push_back(it->second->save_id()); |
| 712 | 744 |
| 713 BrowserThread::PostTask( | 745 BrowserThread::PostTask( |
| 714 BrowserThread::FILE, FROM_HERE, | 746 BrowserThread::FILE, FROM_HERE, |
| 715 NewRunnableMethod(file_manager_, | 747 NewRunnableMethod(file_manager_, |
| 716 &SaveFileManager::RemoveSavedFileFromFileMap, | 748 &SaveFileManager::RemoveSavedFileFromFileMap, |
| 717 save_ids)); | 749 save_ids)); |
| 718 | 750 |
| 719 download_->OnAllDataSaved(all_save_items_count_); | 751 download_->OnAllDataSaved(all_save_items_count_); |
| 720 download_->MarkAsComplete(); | 752 download_->MarkAsComplete(); |
| 753 |
| 721 // Notify download observers that we are complete (the call | 754 // Notify download observers that we are complete (the call |
| 722 // to OnReadyToFinish() set the state to complete but did not notify). | 755 // to OnReadyToFinish() set the state to complete but did not notify). |
| 723 download_->UpdateObservers(); | 756 download_->UpdateObservers(); |
| 724 | 757 |
| 758 // Update the download history. |
| 759 DownloadManager* download_manager = |
| 760 tab_contents_->profile()->GetDownloadManager(); |
| 761 download_manager->download_history()->UpdateEntry(download_); |
| 762 |
| 725 NotificationService::current()->Notify( | 763 NotificationService::current()->Notify( |
| 726 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 764 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 727 Source<SavePackage>(this), | 765 Source<SavePackage>(this), |
| 728 Details<GURL>(&page_url_)); | 766 Details<GURL>(&page_url_)); |
| 729 } | 767 } |
| 730 | 768 |
| 731 // Called for updating end state. | 769 // Called for updating end state. |
| 732 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 770 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
| 733 // Because we might have canceled this saving job before, | 771 // Because we might have canceled this saving job before, |
| 734 // so we might not find corresponding SaveItem. Just ignore it. | 772 // so we might not find corresponding SaveItem. Just ignore it. |
| (...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1431 } | 1469 } |
| 1432 | 1470 |
| 1433 // SelectFileDialog::Listener interface. | 1471 // SelectFileDialog::Listener interface. |
| 1434 void SavePackage::FileSelected(const FilePath& path, | 1472 void SavePackage::FileSelected(const FilePath& path, |
| 1435 int index, void* params) { | 1473 int index, void* params) { |
| 1436 ContinueSave(path, index); | 1474 ContinueSave(path, index); |
| 1437 } | 1475 } |
| 1438 | 1476 |
| 1439 void SavePackage::FileSelectionCanceled(void* params) { | 1477 void SavePackage::FileSelectionCanceled(void* params) { |
| 1440 } | 1478 } |
| OLD | NEW |