Chromium Code Reviews| 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 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 user_canceled_(false), | 134 user_canceled_(false), |
| 135 disk_error_occurred_(false), | 135 disk_error_occurred_(false), |
| 136 save_type_(save_type), | 136 save_type_(save_type), |
| 137 all_save_items_count_(0), | 137 all_save_items_count_(0), |
| 138 wait_state_(INITIALIZE), | 138 wait_state_(INITIALIZE), |
| 139 contents_id_(web_contents->GetRenderProcessHost()->GetID()), | 139 contents_id_(web_contents->GetRenderProcessHost()->GetID()), |
| 140 unique_id_(g_save_package_id++), | 140 unique_id_(g_save_package_id++), |
| 141 wrote_to_completed_file_(false), | 141 wrote_to_completed_file_(false), |
| 142 wrote_to_failed_file_(false) { | 142 wrote_to_failed_file_(false) { |
| 143 DCHECK(page_url_.is_valid()); | 143 DCHECK(page_url_.is_valid()); |
| 144 DCHECK(save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML || | 144 DCHECK((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| 145 save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML); | 145 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) || |
| 146 (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); | |
| 146 DCHECK(!saved_main_file_path_.empty() && | 147 DCHECK(!saved_main_file_path_.empty() && |
| 147 saved_main_file_path_.value().length() <= kMaxFilePathLength); | 148 saved_main_file_path_.value().length() <= kMaxFilePathLength); |
| 148 DCHECK(!saved_main_directory_path_.empty() && | 149 DCHECK(!saved_main_directory_path_.empty() && |
| 149 saved_main_directory_path_.value().length() < kMaxFilePathLength); | 150 saved_main_directory_path_.value().length() < kMaxFilePathLength); |
| 150 InternalInit(); | 151 InternalInit(); |
| 151 } | 152 } |
| 152 | 153 |
| 153 SavePackage::SavePackage(WebContents* web_contents) | 154 SavePackage::SavePackage(WebContents* web_contents) |
| 154 : content::WebContentsObserver(web_contents), | 155 : content::WebContentsObserver(web_contents), |
| 155 file_manager_(NULL), | 156 file_manager_(NULL), |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 // The download manager keeps ownership but adds us as an observer. | 284 // The download manager keeps ownership but adds us as an observer. |
| 284 download_ = download_manager_->CreateSavePackageDownloadItem( | 285 download_ = download_manager_->CreateSavePackageDownloadItem( |
| 285 saved_main_file_path_, page_url_, | 286 saved_main_file_path_, page_url_, |
| 286 browser_context->IsOffTheRecord(), this); | 287 browser_context->IsOffTheRecord(), this); |
| 287 | 288 |
| 288 // Check save type and process the save page job. | 289 // Check save type and process the save page job. |
| 289 if (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { | 290 if (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { |
| 290 // Get directory | 291 // Get directory |
| 291 DCHECK(!saved_main_directory_path_.empty()); | 292 DCHECK(!saved_main_directory_path_.empty()); |
| 292 GetAllSavableResourceLinksForCurrentPage(); | 293 GetAllSavableResourceLinksForCurrentPage(); |
| 294 } else if (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) { | |
| 295 web_contents()->GenerateMHTML(saved_main_file_path_, base::Bind( | |
| 296 &SavePackage::MHTMLGenerated, this)); | |
| 293 } else { | 297 } else { |
| 294 wait_state_ = NET_FILES; | 298 wait_state_ = NET_FILES; |
| 295 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? | 299 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? |
| 296 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 300 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
| 297 SaveFileCreateInfo::SAVE_FILE_FROM_NET; | 301 SaveFileCreateInfo::SAVE_FILE_FROM_NET; |
| 298 SaveItem* save_item = new SaveItem(page_url_, | 302 SaveItem* save_item = new SaveItem(page_url_, |
| 299 GURL(), | 303 GURL(), |
| 300 this, | 304 this, |
| 301 save_source); | 305 save_source); |
| 302 // Add this item to waiting list. | 306 // Add this item to waiting list. |
| 303 waiting_item_queue_.push(save_item); | 307 waiting_item_queue_.push(save_item); |
| 304 all_save_items_count_ = 1; | 308 all_save_items_count_ = 1; |
| 305 download_->SetTotalBytes(1); | 309 download_->SetTotalBytes(1); |
| 306 | 310 |
| 307 DoSavingProcess(); | 311 DoSavingProcess(); |
| 308 } | 312 } |
| 309 | 313 |
| 310 return true; | 314 return true; |
| 311 } | 315 } |
| 312 | 316 |
| 317 void SavePackage::MHTMLGenerated(const FilePath& path, int64 size) { | |
| 318 if (size <= 0) { | |
| 319 Cancel(false); | |
| 320 } else { | |
| 321 download_->SetTotalBytes(1); | |
|
cbentzel
2012/04/17 14:45:34
Could you SetTotalBytes at ::Init time instead? Th
benjhayden
2012/04/17 17:14:36
Done.
| |
| 322 download_->UpdateProgress(1, 0, ""); | |
| 323 wrote_to_completed_file_ = true; | |
| 324 Finish(); | |
| 325 } | |
| 326 } | |
| 327 | |
| 313 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further | 328 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further |
| 314 // restricted by NAME_MAX. The maximum allowed path looks like: | 329 // restricted by NAME_MAX. The maximum allowed path looks like: |
| 315 // '/path/to/save_dir' + '/' + NAME_MAX. | 330 // '/path/to/save_dir' + '/' + NAME_MAX. |
| 316 uint32 SavePackage::GetMaxPathLengthForDirectory(const FilePath& base_dir) { | 331 uint32 SavePackage::GetMaxPathLengthForDirectory(const FilePath& base_dir) { |
| 317 #if defined(OS_POSIX) | 332 #if defined(OS_POSIX) |
| 318 return std::min(kMaxFilePathLength, | 333 return std::min(kMaxFilePathLength, |
| 319 static_cast<uint32>(base_dir.value().length()) + | 334 static_cast<uint32>(base_dir.value().length()) + |
| 320 NAME_MAX + 1); | 335 NAME_MAX + 1); |
| 321 #else | 336 #else |
| 322 return kMaxFilePathLength; | 337 return kMaxFilePathLength; |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 698 it != saved_failed_items_.end(); ++it) | 713 it != saved_failed_items_.end(); ++it) |
| 699 save_ids.push_back(it->second->save_id()); | 714 save_ids.push_back(it->second->save_id()); |
| 700 | 715 |
| 701 BrowserThread::PostTask( | 716 BrowserThread::PostTask( |
| 702 BrowserThread::FILE, FROM_HERE, | 717 BrowserThread::FILE, FROM_HERE, |
| 703 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, | 718 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, |
| 704 file_manager_, | 719 file_manager_, |
| 705 save_ids)); | 720 save_ids)); |
| 706 | 721 |
| 707 if (download_) { | 722 if (download_) { |
| 708 download_->OnAllDataSaved(all_save_items_count_, | 723 download_->OnAllDataSaved( |
| 709 DownloadItem::kEmptyFileHash); | 724 ((save_type_ != content::SAVE_PAGE_TYPE_AS_MHTML) ? |
| 725 all_save_items_count_ : 1), | |
| 726 DownloadItem::kEmptyFileHash); | |
| 710 download_->MarkAsComplete(); | 727 download_->MarkAsComplete(); |
| 711 FinalizeDownloadEntry(); | 728 FinalizeDownloadEntry(); |
| 712 } | 729 } |
| 713 } | 730 } |
| 714 | 731 |
| 715 // Called for updating end state. | 732 // Called for updating end state. |
| 716 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 733 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
| 717 // Because we might have canceled this saving job before, | 734 // Because we might have canceled this saving job before, |
| 718 // so we might not find corresponding SaveItem. Just ignore it. | 735 // so we might not find corresponding SaveItem. Just ignore it. |
| 719 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); | 736 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 767 | 784 |
| 768 save_item->Finish(0, false); | 785 save_item->Finish(0, false); |
| 769 | 786 |
| 770 PutInProgressItemToSavedMap(save_item); | 787 PutInProgressItemToSavedMap(save_item); |
| 771 | 788 |
| 772 // Inform the DownloadItem to update UI. | 789 // Inform the DownloadItem to update UI. |
| 773 // We use the received bytes as number of saved files. | 790 // We use the received bytes as number of saved files. |
| 774 if (download_) | 791 if (download_) |
| 775 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); | 792 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); |
| 776 | 793 |
| 777 if (save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML || | 794 if ((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| 778 save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) { | 795 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) || |
| 796 (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM)) { | |
| 779 // We got error when saving page. Treat it as disk error. | 797 // We got error when saving page. Treat it as disk error. |
| 780 Cancel(true); | 798 Cancel(true); |
| 781 } | 799 } |
| 782 | 800 |
| 783 if (canceled()) { | 801 if (canceled()) { |
| 784 DCHECK(finished_); | 802 DCHECK(finished_); |
| 785 return; | 803 return; |
| 786 } | 804 } |
| 787 | 805 |
| 788 // Continue processing the save page job. | 806 // Continue processing the save page job. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 874 wait_state_ = HTML_DATA; | 892 wait_state_ = HTML_DATA; |
| 875 // All non-HTML resources have been finished, start all remaining | 893 // All non-HTML resources have been finished, start all remaining |
| 876 // HTML files. | 894 // HTML files. |
| 877 SaveNextFile(true); | 895 SaveNextFile(true); |
| 878 } | 896 } |
| 879 } else if (in_process_count()) { | 897 } else if (in_process_count()) { |
| 880 // Continue asking for HTML data. | 898 // Continue asking for HTML data. |
| 881 DCHECK(wait_state_ == HTML_DATA); | 899 DCHECK(wait_state_ == HTML_DATA); |
| 882 } | 900 } |
| 883 } else { | 901 } else { |
| 884 // Save as HTML only. | 902 // Save as HTML only or MHTML. |
| 885 DCHECK(wait_state_ == NET_FILES); | 903 DCHECK(wait_state_ == NET_FILES); |
| 886 DCHECK(save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML); | 904 DCHECK((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| 905 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML)); | |
| 887 if (waiting_item_queue_.size()) { | 906 if (waiting_item_queue_.size()) { |
| 888 DCHECK(all_save_items_count_ == waiting_item_queue_.size()); | 907 DCHECK(all_save_items_count_ == waiting_item_queue_.size()); |
| 889 SaveNextFile(false); | 908 SaveNextFile(false); |
| 890 } | 909 } |
| 891 } | 910 } |
| 892 } | 911 } |
| 893 | 912 |
| 894 bool SavePackage::OnMessageReceived(const IPC::Message& message) { | 913 bool SavePackage::OnMessageReceived(const IPC::Message& message) { |
| 895 bool handled = true; | 914 bool handled = true; |
| 896 IPC_BEGIN_MESSAGE_MAP(SavePackage, message) | 915 IPC_BEGIN_MESSAGE_MAP(SavePackage, message) |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1314 StopObservation(); | 1333 StopObservation(); |
| 1315 } | 1334 } |
| 1316 | 1335 |
| 1317 void SavePackage::FinalizeDownloadEntry() { | 1336 void SavePackage::FinalizeDownloadEntry() { |
| 1318 DCHECK(download_); | 1337 DCHECK(download_); |
| 1319 DCHECK(download_manager_); | 1338 DCHECK(download_manager_); |
| 1320 | 1339 |
| 1321 download_manager_->SavePageDownloadFinished(download_); | 1340 download_manager_->SavePageDownloadFinished(download_); |
| 1322 StopObservation(); | 1341 StopObservation(); |
| 1323 } | 1342 } |
| OLD | NEW |