Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: chrome/browser/download/save_package.cc

Issue 172074: Show or open downloaded items on the UI thread for Mac (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2009 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 "app/l10n_util.h" 7 #include "app/l10n_util.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 FilePath final_name = saved_main_directory_path_.Append(generated_name); 430 FilePath final_name = saved_main_directory_path_.Append(generated_name);
431 save_item->Rename(final_name); 431 save_item->Rename(final_name);
432 } else { 432 } else {
433 // It is the main HTML file, use the name chosen by the user. 433 // It is the main HTML file, use the name chosen by the user.
434 save_item->Rename(saved_main_file_path_); 434 save_item->Rename(saved_main_file_path_);
435 } 435 }
436 436
437 // If the save source is from file system, inform SaveFileManager to copy 437 // If the save source is from file system, inform SaveFileManager to copy
438 // corresponding file to the file path which this SaveItem specifies. 438 // corresponding file to the file path which this SaveItem specifies.
439 if (info->save_source == SaveFileCreateInfo::SAVE_FILE_FROM_FILE) { 439 if (info->save_source == SaveFileCreateInfo::SAVE_FILE_FROM_FILE) {
440 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, 440 file_manager_->file_loop()->PostTask(FROM_HERE,
441 NewRunnableMethod(file_manager_, 441 NewRunnableMethod(file_manager_,
442 &SaveFileManager::SaveLocalFile, 442 &SaveFileManager::SaveLocalFile,
443 save_item->url(), 443 save_item->url(),
444 save_item->save_id(), 444 save_item->save_id(),
445 tab_id())); 445 tab_id()));
446 return; 446 return;
447 } 447 }
448 448
449 // Check whether we begin to require serialized HTML data. 449 // Check whether we begin to require serialized HTML data.
450 if (save_type_ == SAVE_AS_COMPLETE_HTML && wait_state_ == HTML_DATA) { 450 if (save_type_ == SAVE_AS_COMPLETE_HTML && wait_state_ == HTML_DATA) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 // This vector contains the save ids of the save files which SaveFileManager 536 // This vector contains the save ids of the save files which SaveFileManager
537 // needs to remove from its save_file_map_. 537 // needs to remove from its save_file_map_.
538 SaveIDList save_ids; 538 SaveIDList save_ids;
539 for (SavedItemMap::iterator it = saved_success_items_.begin(); 539 for (SavedItemMap::iterator it = saved_success_items_.begin();
540 it != saved_success_items_.end(); ++it) 540 it != saved_success_items_.end(); ++it)
541 save_ids.push_back(it->first); 541 save_ids.push_back(it->first);
542 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); 542 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin();
543 it != saved_failed_items_.end(); ++it) 543 it != saved_failed_items_.end(); ++it)
544 save_ids.push_back(it->second->save_id()); 544 save_ids.push_back(it->second->save_id());
545 545
546 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, 546 file_manager_->file_loop()->PostTask(FROM_HERE,
547 NewRunnableMethod(file_manager_, 547 NewRunnableMethod(file_manager_,
548 &SaveFileManager::RemoveSavedFileFromFileMap, 548 &SaveFileManager::RemoveSavedFileFromFileMap,
549 save_ids)); 549 save_ids));
550 550
551 finished_ = true; 551 finished_ = true;
552 wait_state_ = FAILED; 552 wait_state_ = FAILED;
553 553
554 // Inform the DownloadItem we have canceled whole save page job. 554 // Inform the DownloadItem we have canceled whole save page job.
555 download_->Cancel(false); 555 download_->Cancel(false);
556 } 556 }
557 557
558 void SavePackage::CheckFinish() { 558 void SavePackage::CheckFinish() {
559 if (in_process_count() || finished_) 559 if (in_process_count() || finished_)
560 return; 560 return;
561 561
562 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && 562 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML &&
563 saved_success_items_.size() > 1) ? 563 saved_success_items_.size() > 1) ?
564 saved_main_directory_path_ : FilePath(); 564 saved_main_directory_path_ : FilePath();
565 565
566 // This vector contains the final names of all the successfully saved files 566 // This vector contains the final names of all the successfully saved files
567 // along with their save ids. It will be passed to SaveFileManager to do the 567 // along with their save ids. It will be passed to SaveFileManager to do the
568 // renaming job. 568 // renaming job.
569 FinalNameList final_names; 569 FinalNameList final_names;
570 for (SavedItemMap::iterator it = saved_success_items_.begin(); 570 for (SavedItemMap::iterator it = saved_success_items_.begin();
571 it != saved_success_items_.end(); ++it) 571 it != saved_success_items_.end(); ++it)
572 final_names.push_back(std::make_pair(it->first, 572 final_names.push_back(std::make_pair(it->first,
573 it->second->full_path())); 573 it->second->full_path()));
574 574
575 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, 575 file_manager_->file_loop()->PostTask(FROM_HERE,
576 NewRunnableMethod(file_manager_, 576 NewRunnableMethod(file_manager_,
577 &SaveFileManager::RenameAllFiles, 577 &SaveFileManager::RenameAllFiles,
578 final_names, 578 final_names,
579 dir, 579 dir,
580 tab_contents_->process()->pid(), 580 tab_contents_->process()->pid(),
581 tab_contents_->render_view_host()->routing_id())); 581 tab_contents_->render_view_host()->routing_id()));
582 } 582 }
583 583
584 // Successfully finished all items of this SavePackage. 584 // Successfully finished all items of this SavePackage.
585 void SavePackage::Finish() { 585 void SavePackage::Finish() {
586 // User may cancel the job when we're moving files to the final directory. 586 // User may cancel the job when we're moving files to the final directory.
587 if (canceled()) 587 if (canceled())
588 return; 588 return;
589 589
590 wait_state_ = SUCCESSFUL; 590 wait_state_ = SUCCESSFUL;
591 finished_ = true; 591 finished_ = true;
592 592
593 // This vector contains the save ids of the save files which SaveFileManager 593 // This vector contains the save ids of the save files which SaveFileManager
594 // needs to remove from its save_file_map_. 594 // needs to remove from its save_file_map_.
595 SaveIDList save_ids; 595 SaveIDList save_ids;
596 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); 596 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin();
597 it != saved_failed_items_.end(); ++it) 597 it != saved_failed_items_.end(); ++it)
598 save_ids.push_back(it->second->save_id()); 598 save_ids.push_back(it->second->save_id());
599 599
600 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, 600 file_manager_->file_loop()->PostTask(FROM_HERE,
601 NewRunnableMethod(file_manager_, 601 NewRunnableMethod(file_manager_,
602 &SaveFileManager::RemoveSavedFileFromFileMap, 602 &SaveFileManager::RemoveSavedFileFromFileMap,
603 save_ids)); 603 save_ids));
604 604
605 download_->Finished(all_save_items_count_); 605 download_->Finished(all_save_items_count_);
606 606
607 NotificationService::current()->Notify( 607 NotificationService::current()->Notify(
608 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, 608 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
609 Source<SavePackage>(this), 609 Source<SavePackage>(this),
610 Details<GURL>(&page_url_)); 610 Details<GURL>(&page_url_));
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 686
687 CheckFinish(); 687 CheckFinish();
688 } 688 }
689 689
690 void SavePackage::SaveCanceled(SaveItem* save_item) { 690 void SavePackage::SaveCanceled(SaveItem* save_item) {
691 // Call the RemoveSaveFile in UI thread. 691 // Call the RemoveSaveFile in UI thread.
692 file_manager_->RemoveSaveFile(save_item->save_id(), 692 file_manager_->RemoveSaveFile(save_item->save_id(),
693 save_item->url(), 693 save_item->url(),
694 this); 694 this);
695 if (save_item->save_id() != -1) 695 if (save_item->save_id() != -1)
696 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, 696 file_manager_->file_loop()->PostTask(FROM_HERE,
697 NewRunnableMethod(file_manager_, 697 NewRunnableMethod(file_manager_,
698 &SaveFileManager::CancelSave, 698 &SaveFileManager::CancelSave,
699 save_item->save_id())); 699 save_item->save_id()));
700 } 700 }
701 701
702 // Initiate a saving job of a specific URL. We send the request to 702 // Initiate a saving job of a specific URL. We send the request to
703 // SaveFileManager, which will dispatch it to different approach according to 703 // SaveFileManager, which will dispatch it to different approach according to
704 // the save source. Parameter process_all_remaining_items indicates whether 704 // the save source. Parameter process_all_remaining_items indicates whether
705 // we need to save all remaining items. 705 // we need to save all remaining items.
706 void SavePackage::SaveNextFile(bool process_all_remaining_items) { 706 void SavePackage::SaveNextFile(bool process_all_remaining_items) {
(...skipping 21 matching lines...) Expand all
728 this); 728 this);
729 } while (process_all_remaining_items && waiting_item_queue_.size()); 729 } while (process_all_remaining_items && waiting_item_queue_.size());
730 } 730 }
731 731
732 732
733 // Open download page in windows explorer on file thread, to avoid blocking the 733 // Open download page in windows explorer on file thread, to avoid blocking the
734 // user interface. 734 // user interface.
735 void SavePackage::ShowDownloadInShell() { 735 void SavePackage::ShowDownloadInShell() {
736 DCHECK(file_manager_); 736 DCHECK(file_manager_);
737 DCHECK(finished_ && !canceled() && !saved_main_file_path_.empty()); 737 DCHECK(finished_ && !canceled() && !saved_main_file_path_.empty());
738 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, 738 DCHECK(MessageLoop::current() == file_manager_->ui_loop());
739 #if defined(OS_MACOSX)
740 // Mac OS X requires opening downloads on the UI thread.
741 platform_util::ShowItemInFolder(saved_main_file_path_);
742 #else
743 file_manager_->file_loop()->PostTask(FROM_HERE,
739 NewRunnableMethod(file_manager_, 744 NewRunnableMethod(file_manager_,
740 &SaveFileManager::OnShowSavedFileInShell, 745 &SaveFileManager::OnShowSavedFileInShell,
741 saved_main_file_path_)); 746 saved_main_file_path_));
747 #endif
742 } 748 }
743 749
744 // Calculate the percentage of whole save page job. 750 // Calculate the percentage of whole save page job.
745 int SavePackage::PercentComplete() { 751 int SavePackage::PercentComplete() {
746 if (!all_save_items_count_) 752 if (!all_save_items_count_)
747 return 0; 753 return 0;
748 else if (!in_process_count()) 754 else if (!in_process_count())
749 return 100; 755 return 100;
750 else 756 else
751 return completed_count() / all_save_items_count_; 757 return completed_count() / all_save_items_count_;
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 // Check current state. 854 // Check current state.
849 if (wait_state_ != HTML_DATA) 855 if (wait_state_ != HTML_DATA)
850 return; 856 return;
851 857
852 int id = tab_id(); 858 int id = tab_id();
853 // If the all frames are finished saving, we need to close the 859 // If the all frames are finished saving, we need to close the
854 // remaining SaveItems. 860 // remaining SaveItems.
855 if (flag == webkit_glue::DomSerializerDelegate::ALL_FRAMES_ARE_FINISHED) { 861 if (flag == webkit_glue::DomSerializerDelegate::ALL_FRAMES_ARE_FINISHED) {
856 for (SaveUrlItemMap::iterator it = in_progress_items_.begin(); 862 for (SaveUrlItemMap::iterator it = in_progress_items_.begin();
857 it != in_progress_items_.end(); ++it) { 863 it != in_progress_items_.end(); ++it) {
858 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, 864 file_manager_->file_loop()->PostTask(FROM_HERE,
859 NewRunnableMethod(file_manager_, 865 NewRunnableMethod(file_manager_,
860 &SaveFileManager::SaveFinished, 866 &SaveFileManager::SaveFinished,
861 it->second->save_id(), 867 it->second->save_id(),
862 it->second->url(), 868 it->second->url(),
863 id, 869 id,
864 true)); 870 true));
865 } 871 }
866 return; 872 return;
867 } 873 }
868 874
869 SaveUrlItemMap::iterator it = in_progress_items_.find(frame_url.spec()); 875 SaveUrlItemMap::iterator it = in_progress_items_.find(frame_url.spec());
870 if (it == in_progress_items_.end()) 876 if (it == in_progress_items_.end())
871 return; 877 return;
872 SaveItem* save_item = it->second; 878 SaveItem* save_item = it->second;
873 DCHECK(save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM); 879 DCHECK(save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM);
874 880
875 if (!data.empty()) { 881 if (!data.empty()) {
876 // Prepare buffer for saving HTML data. 882 // Prepare buffer for saving HTML data.
877 net::IOBuffer* new_data = new net::IOBuffer(data.size()); 883 net::IOBuffer* new_data = new net::IOBuffer(data.size());
878 new_data->AddRef(); // We'll pass the buffer to SaveFileManager. 884 new_data->AddRef(); // We'll pass the buffer to SaveFileManager.
879 memcpy(new_data->data(), data.data(), data.size()); 885 memcpy(new_data->data(), data.data(), data.size());
880 886
881 // Call write file functionality in file thread. 887 // Call write file functionality in file thread.
882 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, 888 file_manager_->file_loop()->PostTask(FROM_HERE,
883 NewRunnableMethod(file_manager_, 889 NewRunnableMethod(file_manager_,
884 &SaveFileManager::UpdateSaveProgress, 890 &SaveFileManager::UpdateSaveProgress,
885 save_item->save_id(), 891 save_item->save_id(),
886 new_data, 892 new_data,
887 static_cast<int>(data.size()))); 893 static_cast<int>(data.size())));
888 } 894 }
889 895
890 // Current frame is completed saving, call finish in file thread. 896 // Current frame is completed saving, call finish in file thread.
891 if (flag == webkit_glue::DomSerializerDelegate::CURRENT_FRAME_IS_FINISHED) { 897 if (flag == webkit_glue::DomSerializerDelegate::CURRENT_FRAME_IS_FINISHED) {
892 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, 898 file_manager_->file_loop()->PostTask(FROM_HERE,
893 NewRunnableMethod(file_manager_, 899 NewRunnableMethod(file_manager_,
894 &SaveFileManager::SaveFinished, 900 &SaveFileManager::SaveFinished,
895 save_item->save_id(), 901 save_item->save_id(),
896 save_item->url(), 902 save_item->url(),
897 id, 903 id,
898 true)); 904 true));
899 } 905 }
900 } 906 }
901 907
902 // Ask for all savable resource links from backend, include main frame and 908 // Ask for all savable resource links from backend, include main frame and
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
1200 int index, void* params) { 1206 int index, void* params) {
1201 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); 1207 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params);
1202 ContinueSave(save_params, path, index); 1208 ContinueSave(save_params, path, index);
1203 delete save_params; 1209 delete save_params;
1204 } 1210 }
1205 1211
1206 void SavePackage::FileSelectionCanceled(void* params) { 1212 void SavePackage::FileSelectionCanceled(void* params) {
1207 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params); 1213 SavePackageParam* save_params = reinterpret_cast<SavePackageParam*>(params);
1208 delete save_params; 1214 delete save_params;
1209 } 1215 }
OLDNEW
« no previous file with comments | « chrome/browser/download/save_file_manager.cc ('k') | chrome/browser/renderer_host/save_file_resource_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698