OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/save_package.h" | 5 #include "content/browser/download/save_package.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/i18n/file_util_icu.h" | 13 #include "base/i18n/file_util_icu.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
21 #include "base/strings/sys_string_conversions.h" | 21 #include "base/strings/sys_string_conversions.h" |
22 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
23 #include "base/task_runner_util.h" | |
23 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
25 #include "base/threading/thread_restrictions.h" | |
24 #include "build/build_config.h" | 26 #include "build/build_config.h" |
25 #include "components/url_formatter/url_formatter.h" | 27 #include "components/url_formatter/url_formatter.h" |
26 #include "content/browser/bad_message.h" | 28 #include "content/browser/bad_message.h" |
27 #include "content/browser/download/download_item_impl.h" | 29 #include "content/browser/download/download_item_impl.h" |
28 #include "content/browser/download/download_manager_impl.h" | 30 #include "content/browser/download/download_manager_impl.h" |
29 #include "content/browser/download/download_stats.h" | 31 #include "content/browser/download/download_stats.h" |
32 #include "content/browser/download/download_task_runner.h" | |
30 #include "content/browser/download/save_file.h" | 33 #include "content/browser/download/save_file.h" |
31 #include "content/browser/download/save_file_manager.h" | 34 #include "content/browser/download/save_file_manager.h" |
32 #include "content/browser/download/save_item.h" | 35 #include "content/browser/download/save_item.h" |
33 #include "content/browser/frame_host/frame_tree.h" | 36 #include "content/browser/frame_host/frame_tree.h" |
34 #include "content/browser/frame_host/frame_tree_node.h" | 37 #include "content/browser/frame_host/frame_tree_node.h" |
35 #include "content/browser/frame_host/render_frame_host_impl.h" | 38 #include "content/browser/frame_host/render_frame_host_impl.h" |
36 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 39 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
37 #include "content/browser/renderer_host/render_process_host_impl.h" | 40 #include "content/browser/renderer_host/render_process_host_impl.h" |
38 #include "content/browser/renderer_host/render_view_host_delegate.h" | 41 #include "content/browser/renderer_host/render_view_host_delegate.h" |
39 #include "content/browser/renderer_host/render_view_host_impl.h" | 42 #include "content/browser/renderer_host/render_view_host_impl.h" |
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
593 save_item->Update(size); | 596 save_item->Update(size); |
594 | 597 |
595 // If we got disk error, cancel whole save page job. | 598 // If we got disk error, cancel whole save page job. |
596 if (!write_success) { | 599 if (!write_success) { |
597 // Cancel job with reason of disk error. | 600 // Cancel job with reason of disk error. |
598 Cancel(false); | 601 Cancel(false); |
599 } | 602 } |
600 return true; | 603 return true; |
601 } | 604 } |
602 | 605 |
603 // Stop all page saving jobs that are in progress and instruct the FILE thread | 606 // Stop all page saving jobs that are in progress and instruct the download |
604 // to delete all saved files. | 607 // sequence to delete all saved files. |
Avi (use Gerrit)
2017/07/21 17:30:09
may as well fix the double space between "saved" a
Sigurður Ásgeirsson
2017/07/21 17:38:20
Done.
| |
605 void SavePackage::Stop(bool cancel_download_item) { | 608 void SavePackage::Stop(bool cancel_download_item) { |
606 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 609 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
607 // If we haven't moved out of the initial state, there's nothing to cancel and | 610 // If we haven't moved out of the initial state, there's nothing to cancel and |
608 // there won't be valid pointers for |file_manager_| or |download_|. | 611 // there won't be valid pointers for |file_manager_| or |download_|. |
609 if (wait_state_ == INITIALIZE) | 612 if (wait_state_ == INITIALIZE) |
610 return; | 613 return; |
611 | 614 |
612 // When stopping, if it still has some items in in_progress, cancel them. | 615 // When stopping, if it still has some items in in_progress, cancel them. |
613 DCHECK(canceled()); | 616 DCHECK(canceled()); |
614 if (in_process_count()) { | 617 if (in_process_count()) { |
(...skipping 10 matching lines...) Expand all Loading... | |
625 } | 628 } |
626 | 629 |
627 // This vector contains the save ids of the save files which SaveFileManager | 630 // This vector contains the save ids of the save files which SaveFileManager |
628 // needs to remove from its save_file_map_. | 631 // needs to remove from its save_file_map_. |
629 std::vector<SaveItemId> save_item_ids; | 632 std::vector<SaveItemId> save_item_ids; |
630 for (const auto& it : saved_success_items_) | 633 for (const auto& it : saved_success_items_) |
631 save_item_ids.push_back(it.first); | 634 save_item_ids.push_back(it.first); |
632 for (const auto& it : saved_failed_items_) | 635 for (const auto& it : saved_failed_items_) |
633 save_item_ids.push_back(it.first); | 636 save_item_ids.push_back(it.first); |
634 | 637 |
635 BrowserThread::PostTask( | 638 GetDownloadTaskRunner()->PostTask( |
636 BrowserThread::FILE, FROM_HERE, | 639 FROM_HERE, base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, |
637 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, file_manager_, | 640 file_manager_, save_item_ids)); |
638 save_item_ids)); | |
639 | 641 |
640 finished_ = true; | 642 finished_ = true; |
641 wait_state_ = FAILED; | 643 wait_state_ = FAILED; |
642 | 644 |
643 // Inform the DownloadItem we have canceled whole save page job. | 645 // Inform the DownloadItem we have canceled whole save page job. |
644 if (download_) { | 646 if (download_) { |
645 if (cancel_download_item) | 647 if (cancel_download_item) |
646 download_->Cancel(false); | 648 download_->Cancel(false); |
647 FinalizeDownloadEntry(); | 649 FinalizeDownloadEntry(); |
648 } | 650 } |
649 } | 651 } |
650 | 652 |
651 void SavePackage::CheckFinish() { | 653 void SavePackage::CheckFinish() { |
652 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 654 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
653 if (in_process_count() || finished_) | 655 if (in_process_count() || finished_) |
654 return; | 656 return; |
655 | 657 |
656 base::FilePath dir = (save_type_ == SAVE_PAGE_TYPE_AS_COMPLETE_HTML && | 658 base::FilePath dir = (save_type_ == SAVE_PAGE_TYPE_AS_COMPLETE_HTML && |
657 saved_success_items_.size() > 1) ? | 659 saved_success_items_.size() > 1) ? |
658 saved_main_directory_path_ : base::FilePath(); | 660 saved_main_directory_path_ : base::FilePath(); |
659 | 661 |
660 FinalNamesMap final_names; | 662 FinalNamesMap final_names; |
661 for (const auto& it : saved_success_items_) | 663 for (const auto& it : saved_success_items_) |
662 final_names.insert(std::make_pair(it.first, it.second->full_path())); | 664 final_names.insert(std::make_pair(it.first, it.second->full_path())); |
663 | 665 |
664 BrowserThread::PostTask( | 666 GetDownloadTaskRunner()->PostTask( |
665 BrowserThread::FILE, FROM_HERE, | 667 FROM_HERE, |
666 base::Bind(&SaveFileManager::RenameAllFiles, | 668 base::Bind(&SaveFileManager::RenameAllFiles, file_manager_, final_names, |
667 file_manager_, | 669 dir, web_contents()->GetRenderProcessHost()->GetID(), |
668 final_names, | 670 web_contents()->GetMainFrame()->GetRoutingID(), id())); |
669 dir, | |
670 web_contents()->GetRenderProcessHost()->GetID(), | |
671 web_contents()->GetMainFrame()->GetRoutingID(), | |
672 id())); | |
673 } | 671 } |
674 | 672 |
675 // Successfully finished all items of this SavePackage. | 673 // Successfully finished all items of this SavePackage. |
676 void SavePackage::Finish() { | 674 void SavePackage::Finish() { |
677 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 675 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
678 // User may cancel the job when we're moving files to the final directory. | 676 // User may cancel the job when we're moving files to the final directory. |
679 if (canceled()) | 677 if (canceled()) |
680 return; | 678 return; |
681 | 679 |
682 wait_state_ = SUCCESSFUL; | 680 wait_state_ = SUCCESSFUL; |
(...skipping 11 matching lines...) Expand all Loading... | |
694 | 692 |
695 // This vector contains the save ids of the save files which SaveFileManager | 693 // This vector contains the save ids of the save files which SaveFileManager |
696 // needs to remove from its |save_file_map_|. | 694 // needs to remove from its |save_file_map_|. |
697 std::vector<SaveItemId> list_of_failed_save_item_ids; | 695 std::vector<SaveItemId> list_of_failed_save_item_ids; |
698 for (const auto& it : saved_failed_items_) { | 696 for (const auto& it : saved_failed_items_) { |
699 const SaveItem* save_item = it.second.get(); | 697 const SaveItem* save_item = it.second.get(); |
700 DCHECK_EQ(it.first, save_item->id()); | 698 DCHECK_EQ(it.first, save_item->id()); |
701 list_of_failed_save_item_ids.push_back(save_item->id()); | 699 list_of_failed_save_item_ids.push_back(save_item->id()); |
702 } | 700 } |
703 | 701 |
704 BrowserThread::PostTask( | 702 GetDownloadTaskRunner()->PostTask( |
705 BrowserThread::FILE, FROM_HERE, | 703 FROM_HERE, base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, |
706 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, file_manager_, | 704 file_manager_, list_of_failed_save_item_ids)); |
707 list_of_failed_save_item_ids)); | |
708 | 705 |
709 if (download_) { | 706 if (download_) { |
710 if (save_type_ != SAVE_PAGE_TYPE_AS_MHTML) { | 707 if (save_type_ != SAVE_PAGE_TYPE_AS_MHTML) { |
711 CHECK_EQ(download_->GetState(), DownloadItem::IN_PROGRESS); | 708 CHECK_EQ(download_->GetState(), DownloadItem::IN_PROGRESS); |
712 download_->DestinationUpdate( | 709 download_->DestinationUpdate( |
713 all_save_items_count_, CurrentSpeed(), | 710 all_save_items_count_, CurrentSpeed(), |
714 std::vector<DownloadItem::ReceivedSlice>()); | 711 std::vector<DownloadItem::ReceivedSlice>()); |
715 download_->OnAllDataSaved(all_save_items_count_, | 712 download_->OnAllDataSaved(all_save_items_count_, |
716 std::unique_ptr<crypto::SecureHash>()); | 713 std::unique_ptr<crypto::SecureHash>()); |
717 } | 714 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
761 // Continue processing the save page job. | 758 // Continue processing the save page job. |
762 DoSavingProcess(); | 759 DoSavingProcess(); |
763 | 760 |
764 // Check whether we can successfully finish whole job. | 761 // Check whether we can successfully finish whole job. |
765 CheckFinish(); | 762 CheckFinish(); |
766 } | 763 } |
767 | 764 |
768 void SavePackage::SaveCanceled(const SaveItem* save_item) { | 765 void SavePackage::SaveCanceled(const SaveItem* save_item) { |
769 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 766 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
770 file_manager_->RemoveSaveFile(save_item->id(), this); | 767 file_manager_->RemoveSaveFile(save_item->id(), this); |
771 BrowserThread::PostTask( | 768 GetDownloadTaskRunner()->PostTask( |
772 BrowserThread::FILE, FROM_HERE, | 769 FROM_HERE, |
773 base::Bind(&SaveFileManager::CancelSave, file_manager_, save_item->id())); | 770 base::Bind(&SaveFileManager::CancelSave, file_manager_, save_item->id())); |
774 } | 771 } |
775 | 772 |
776 void SavePackage::SaveNextFile(bool process_all_remaining_items) { | 773 void SavePackage::SaveNextFile(bool process_all_remaining_items) { |
777 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 774 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
778 DCHECK(web_contents()); | 775 DCHECK(web_contents()); |
779 DCHECK(!waiting_item_queue_.empty()); | 776 DCHECK(!waiting_item_queue_.empty()); |
780 | 777 |
781 do { | 778 do { |
782 // Pop SaveItem from waiting list. | 779 // Pop SaveItem from waiting list. |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
922 const SaveItem* save_item = item.second; | 919 const SaveItem* save_item = item.second; |
923 | 920 |
924 FrameTreeNode* frame_tree_node = frame_tree->FindByID(frame_tree_node_id); | 921 FrameTreeNode* frame_tree_node = frame_tree->FindByID(frame_tree_node_id); |
925 if (frame_tree_node && | 922 if (frame_tree_node && |
926 frame_tree_node->current_frame_host()->IsRenderFrameLive()) { | 923 frame_tree_node->current_frame_host()->IsRenderFrameLive()) { |
927 // Ask the frame for HTML to be written to the associated SaveItem. | 924 // Ask the frame for HTML to be written to the associated SaveItem. |
928 GetSerializedHtmlWithLocalLinksForFrame(frame_tree_node); | 925 GetSerializedHtmlWithLocalLinksForFrame(frame_tree_node); |
929 number_of_frames_pending_response_++; | 926 number_of_frames_pending_response_++; |
930 } else { | 927 } else { |
931 // Notify SaveFileManager about the failure to save this SaveItem. | 928 // Notify SaveFileManager about the failure to save this SaveItem. |
932 BrowserThread::PostTask( | 929 GetDownloadTaskRunner()->PostTask( |
933 BrowserThread::FILE, FROM_HERE, | 930 FROM_HERE, base::Bind(&SaveFileManager::SaveFinished, file_manager_, |
934 base::Bind(&SaveFileManager::SaveFinished, file_manager_, | 931 save_item->id(), id(), false)); |
935 save_item->id(), id(), false)); | |
936 } | 932 } |
937 } | 933 } |
938 if (number_of_frames_pending_response_ == 0) { | 934 if (number_of_frames_pending_response_ == 0) { |
939 // All frames disappeared since gathering of savable resources? | 935 // All frames disappeared since gathering of savable resources? |
940 // Treat this as cancellation. | 936 // Treat this as cancellation. |
941 Cancel(false); | 937 Cancel(false); |
942 } | 938 } |
943 } | 939 } |
944 | 940 |
945 void SavePackage::GetSerializedHtmlWithLocalLinksForFrame( | 941 void SavePackage::GetSerializedHtmlWithLocalLinksForFrame( |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1035 wrote_to_failed_file_ = true; | 1031 wrote_to_failed_file_ = true; |
1036 | 1032 |
1037 return; | 1033 return; |
1038 } | 1034 } |
1039 | 1035 |
1040 if (!data.empty()) { | 1036 if (!data.empty()) { |
1041 // Prepare buffer for saving HTML data. | 1037 // Prepare buffer for saving HTML data. |
1042 scoped_refptr<net::IOBuffer> new_data(new net::IOBuffer(data.size())); | 1038 scoped_refptr<net::IOBuffer> new_data(new net::IOBuffer(data.size())); |
1043 memcpy(new_data->data(), data.data(), data.size()); | 1039 memcpy(new_data->data(), data.data(), data.size()); |
1044 | 1040 |
1045 // Call write file functionality in FILE thread. | 1041 // Call write file functionality in download sequence. |
1046 BrowserThread::PostTask( | 1042 GetDownloadTaskRunner()->PostTask( |
1047 BrowserThread::FILE, FROM_HERE, | 1043 FROM_HERE, |
1048 base::Bind(&SaveFileManager::UpdateSaveProgress, file_manager_, | 1044 base::Bind(&SaveFileManager::UpdateSaveProgress, file_manager_, |
1049 save_item->id(), base::RetainedRef(new_data), | 1045 save_item->id(), base::RetainedRef(new_data), |
1050 static_cast<int>(data.size()))); | 1046 static_cast<int>(data.size()))); |
1051 } | 1047 } |
1052 | 1048 |
1053 // Current frame is completed saving, call finish in FILE thread. | 1049 // Current frame is completed saving, call finish in download sequence. |
1054 if (end_of_data) { | 1050 if (end_of_data) { |
1055 DVLOG(20) << __func__ << "() save_item_id = " << save_item->id() | 1051 DVLOG(20) << __func__ << "() save_item_id = " << save_item->id() |
1056 << " url = \"" << save_item->url().spec() << "\""; | 1052 << " url = \"" << save_item->url().spec() << "\""; |
1057 BrowserThread::PostTask( | 1053 GetDownloadTaskRunner()->PostTask( |
1058 BrowserThread::FILE, FROM_HERE, | 1054 FROM_HERE, base::Bind(&SaveFileManager::SaveFinished, file_manager_, |
1059 base::Bind(&SaveFileManager::SaveFinished, file_manager_, | 1055 save_item->id(), id(), true)); |
1060 save_item->id(), id(), true)); | |
1061 number_of_frames_pending_response_--; | 1056 number_of_frames_pending_response_--; |
1062 DCHECK_LE(0, number_of_frames_pending_response_); | 1057 DCHECK_LE(0, number_of_frames_pending_response_); |
1063 } | 1058 } |
1064 } | 1059 } |
1065 | 1060 |
1066 // Ask for all savable resource links from backend, include main frame and | 1061 // Ask for all savable resource links from backend, include main frame and |
1067 // sub-frame. | 1062 // sub-frame. |
1068 void SavePackage::GetSavableResourceLinks() { | 1063 void SavePackage::GetSavableResourceLinks() { |
1069 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1064 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1070 if (wait_state_ != START_PROCESS) | 1065 if (wait_state_ != START_PROCESS) |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1282 if (can_save_as_complete) | 1277 if (can_save_as_complete) |
1283 name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext); | 1278 name_with_proper_ext = EnsureHtmlExtension(name_with_proper_ext); |
1284 | 1279 |
1285 base::FilePath::StringType file_name = name_with_proper_ext.value(); | 1280 base::FilePath::StringType file_name = name_with_proper_ext.value(); |
1286 base::i18n::ReplaceIllegalCharactersInPath(&file_name, '_'); | 1281 base::i18n::ReplaceIllegalCharactersInPath(&file_name, '_'); |
1287 return base::FilePath(file_name); | 1282 return base::FilePath(file_name); |
1288 } | 1283 } |
1289 | 1284 |
1290 // static | 1285 // static |
1291 base::FilePath SavePackage::EnsureHtmlExtension(const base::FilePath& name) { | 1286 base::FilePath SavePackage::EnsureHtmlExtension(const base::FilePath& name) { |
1292 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 1287 base::ThreadRestrictions::AssertIOAllowed(); |
1293 | 1288 |
1294 base::FilePath::StringType ext = name.Extension(); | 1289 base::FilePath::StringType ext = name.Extension(); |
1295 if (!ext.empty()) | 1290 if (!ext.empty()) |
1296 ext.erase(ext.begin()); // Erase preceding '.'. | 1291 ext.erase(ext.begin()); // Erase preceding '.'. |
1297 std::string mime_type; | 1292 std::string mime_type; |
1298 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || | 1293 if (!net::GetMimeTypeFromExtension(ext, &mime_type) || |
1299 !CanSaveAsComplete(mime_type)) { | 1294 !CanSaveAsComplete(mime_type)) { |
1300 return base::FilePath(name.value() + FILE_PATH_LITERAL(".") + | 1295 return base::FilePath(name.value() + FILE_PATH_LITERAL(".") + |
1301 kDefaultHtmlExtension); | 1296 kDefaultHtmlExtension); |
1302 } | 1297 } |
1303 return name; | 1298 return name; |
1304 } | 1299 } |
1305 | 1300 |
1306 // static | 1301 // static |
1307 base::FilePath SavePackage::EnsureMimeExtension(const base::FilePath& name, | 1302 base::FilePath SavePackage::EnsureMimeExtension(const base::FilePath& name, |
1308 const std::string& contents_mime_type) { | 1303 const std::string& contents_mime_type) { |
1309 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 1304 base::ThreadRestrictions::AssertIOAllowed(); |
1310 | 1305 |
1311 // Start extension at 1 to skip over period if non-empty. | 1306 // Start extension at 1 to skip over period if non-empty. |
1312 base::FilePath::StringType ext = name.Extension(); | 1307 base::FilePath::StringType ext = name.Extension(); |
1313 if (!ext.empty()) | 1308 if (!ext.empty()) |
1314 ext = ext.substr(1); | 1309 ext = ext.substr(1); |
1315 base::FilePath::StringType suggested_extension = | 1310 base::FilePath::StringType suggested_extension = |
1316 ExtensionForMimeType(contents_mime_type); | 1311 ExtensionForMimeType(contents_mime_type); |
1317 std::string mime_type; | 1312 std::string mime_type; |
1318 if (!suggested_extension.empty() && | 1313 if (!suggested_extension.empty() && |
1319 !net::GetMimeTypeFromExtension(ext, &mime_type)) { | 1314 !net::GetMimeTypeFromExtension(ext, &mime_type)) { |
(...skipping 19 matching lines...) Expand all Loading... | |
1339 }; | 1334 }; |
1340 for (const auto& extension : extensions) { | 1335 for (const auto& extension : extensions) { |
1341 if (contents_mime_type == extension.mime_type) | 1336 if (contents_mime_type == extension.mime_type) |
1342 return extension.suggested_extension; | 1337 return extension.suggested_extension; |
1343 } | 1338 } |
1344 return FILE_PATH_LITERAL(""); | 1339 return FILE_PATH_LITERAL(""); |
1345 } | 1340 } |
1346 | 1341 |
1347 void SavePackage::GetSaveInfo() { | 1342 void SavePackage::GetSaveInfo() { |
1348 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1343 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1349 // Can't use |web_contents_| in the FILE thread, so get the data that we need | 1344 // Can't use |web_contents_| in the download sequence, so get the data that we |
1350 // before calling to it. | 1345 // need before calling to it. |
1351 base::FilePath website_save_dir; | 1346 base::FilePath website_save_dir; |
1352 base::FilePath download_save_dir; | 1347 base::FilePath download_save_dir; |
1353 bool skip_dir_check = false; | 1348 bool skip_dir_check = false; |
1354 auto* delegate = download_manager_->GetDelegate(); | 1349 auto* delegate = download_manager_->GetDelegate(); |
1355 if (delegate) { | 1350 if (delegate) { |
1356 delegate->GetSaveDir( | 1351 delegate->GetSaveDir( |
1357 web_contents()->GetBrowserContext(), &website_save_dir, | 1352 web_contents()->GetBrowserContext(), &website_save_dir, |
1358 &download_save_dir, &skip_dir_check); | 1353 &download_save_dir, &skip_dir_check); |
1359 } | 1354 } |
1360 std::string mime_type = web_contents()->GetContentsMimeType(); | 1355 std::string mime_type = web_contents()->GetContentsMimeType(); |
1361 bool can_save_as_complete = CanSaveAsComplete(mime_type); | 1356 bool can_save_as_complete = CanSaveAsComplete(mime_type); |
1362 BrowserThread::PostTaskAndReplyWithResult( | 1357 base::PostTaskAndReplyWithResult( |
1363 BrowserThread::FILE, FROM_HERE, | 1358 GetDownloadTaskRunner().get(), FROM_HERE, |
1364 base::Bind(&SavePackage::CreateDirectoryOnFileThread, title_, page_url_, | 1359 base::Bind(&SavePackage::CreateDirectoryOnFileThread, title_, page_url_, |
1365 can_save_as_complete, mime_type, website_save_dir, | 1360 can_save_as_complete, mime_type, website_save_dir, |
1366 download_save_dir, skip_dir_check), | 1361 download_save_dir, skip_dir_check), |
1367 base::Bind(&SavePackage::ContinueGetSaveInfo, this, | 1362 base::Bind(&SavePackage::ContinueGetSaveInfo, this, |
1368 can_save_as_complete)); | 1363 can_save_as_complete)); |
1369 } | 1364 } |
1370 | 1365 |
1371 // static | 1366 // static |
1372 base::FilePath SavePackage::CreateDirectoryOnFileThread( | 1367 base::FilePath SavePackage::CreateDirectoryOnFileThread( |
1373 const base::string16& title, | 1368 const base::string16& title, |
1374 const GURL& page_url, | 1369 const GURL& page_url, |
1375 bool can_save_as_complete, | 1370 bool can_save_as_complete, |
1376 const std::string& mime_type, | 1371 const std::string& mime_type, |
1377 const base::FilePath& website_save_dir, | 1372 const base::FilePath& website_save_dir, |
1378 const base::FilePath& download_save_dir, | 1373 const base::FilePath& download_save_dir, |
1379 bool skip_dir_check) { | 1374 bool skip_dir_check) { |
1380 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 1375 DCHECK(GetDownloadTaskRunner()->RunsTasksInCurrentSequence()); |
1381 | 1376 |
1382 base::FilePath suggested_filename = GetSuggestedNameForSaveAs( | 1377 base::FilePath suggested_filename = GetSuggestedNameForSaveAs( |
1383 title, page_url, can_save_as_complete, mime_type); | 1378 title, page_url, can_save_as_complete, mime_type); |
1384 | 1379 |
1385 base::FilePath save_dir; | 1380 base::FilePath save_dir; |
1386 // If the default html/websites save folder doesn't exist... | 1381 // If the default html/websites save folder doesn't exist... |
1387 // We skip the directory check for gdata directories on ChromeOS. | 1382 // We skip the directory check for gdata directories on ChromeOS. |
1388 if (!skip_dir_check && !base::DirectoryExists(website_save_dir)) { | 1383 if (!skip_dir_check && !base::DirectoryExists(website_save_dir)) { |
1389 // If the default download dir doesn't exist, create it. | 1384 // If the default download dir doesn't exist, create it. |
1390 if (!base::DirectoryExists(download_save_dir)) { | 1385 if (!base::DirectoryExists(download_save_dir)) { |
(...skipping 22 matching lines...) Expand all Loading... | |
1413 // will fail because returning means no save as popup for the user, which | 1408 // will fail because returning means no save as popup for the user, which |
1414 // is even more confusing. This case should be rare though. | 1409 // is even more confusing. This case should be rare though. |
1415 return save_dir.Append(suggested_filename); | 1410 return save_dir.Append(suggested_filename); |
1416 } | 1411 } |
1417 | 1412 |
1418 void SavePackage::ContinueGetSaveInfo(bool can_save_as_complete, | 1413 void SavePackage::ContinueGetSaveInfo(bool can_save_as_complete, |
1419 const base::FilePath& suggested_path) { | 1414 const base::FilePath& suggested_path) { |
1420 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1415 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1421 | 1416 |
1422 // The WebContents which owns this SavePackage may have disappeared during | 1417 // The WebContents which owns this SavePackage may have disappeared during |
1423 // the UI->FILE->UI thread hop of | 1418 // the UI->download sequence->UI thread hop of |
1424 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. | 1419 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. |
1425 if (!web_contents() || !download_manager_->GetDelegate()) | 1420 if (!web_contents() || !download_manager_->GetDelegate()) |
1426 return; | 1421 return; |
1427 | 1422 |
1428 base::FilePath::StringType default_extension; | 1423 base::FilePath::StringType default_extension; |
1429 if (can_save_as_complete) | 1424 if (can_save_as_complete) |
1430 default_extension = kDefaultHtmlExtension; | 1425 default_extension = kDefaultHtmlExtension; |
1431 | 1426 |
1432 download_manager_->GetDelegate()->ChooseSavePath( | 1427 download_manager_->GetDelegate()->ChooseSavePath( |
1433 web_contents(), | 1428 web_contents(), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1465 } | 1460 } |
1466 | 1461 |
1467 void SavePackage::FinalizeDownloadEntry() { | 1462 void SavePackage::FinalizeDownloadEntry() { |
1468 DCHECK(download_); | 1463 DCHECK(download_); |
1469 DCHECK(download_manager_); | 1464 DCHECK(download_manager_); |
1470 download_ = nullptr; | 1465 download_ = nullptr; |
1471 download_manager_ = nullptr; | 1466 download_manager_ = nullptr; |
1472 } | 1467 } |
1473 | 1468 |
1474 } // namespace content | 1469 } // namespace content |
OLD | NEW |