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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 : content::WebContentsObserver(web_contents), | 124 : content::WebContentsObserver(web_contents), |
| 125 file_manager_(NULL), | 125 file_manager_(NULL), |
| 126 download_manager_(NULL), | 126 download_manager_(NULL), |
| 127 download_(NULL), | 127 download_(NULL), |
| 128 page_url_(GetUrlToBeSaved()), | 128 page_url_(GetUrlToBeSaved()), |
| 129 saved_main_file_path_(file_full_path), | 129 saved_main_file_path_(file_full_path), |
| 130 saved_main_directory_path_(directory_full_path), | 130 saved_main_directory_path_(directory_full_path), |
| 131 title_(web_contents->GetTitle()), | 131 title_(web_contents->GetTitle()), |
| 132 start_tick_(base::TimeTicks::Now()), | 132 start_tick_(base::TimeTicks::Now()), |
| 133 finished_(false), | 133 finished_(false), |
| 134 mhtml_finishing_(false), | |
| 134 user_canceled_(false), | 135 user_canceled_(false), |
| 135 disk_error_occurred_(false), | 136 disk_error_occurred_(false), |
| 136 save_type_(save_type), | 137 save_type_(save_type), |
| 137 all_save_items_count_(0), | 138 all_save_items_count_(0), |
| 138 wait_state_(INITIALIZE), | 139 wait_state_(INITIALIZE), |
| 139 contents_id_(web_contents->GetRenderProcessHost()->GetID()), | 140 contents_id_(web_contents->GetRenderProcessHost()->GetID()), |
| 140 unique_id_(g_save_package_id++), | 141 unique_id_(g_save_package_id++), |
| 141 wrote_to_completed_file_(false), | 142 wrote_to_completed_file_(false), |
| 142 wrote_to_failed_file_(false) { | 143 wrote_to_failed_file_(false) { |
| 143 DCHECK(page_url_.is_valid()); | 144 DCHECK(page_url_.is_valid()); |
| 144 DCHECK(save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML || | 145 DCHECK((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| 145 save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML); | 146 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) || |
| 147 (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); | |
| 146 DCHECK(!saved_main_file_path_.empty() && | 148 DCHECK(!saved_main_file_path_.empty() && |
| 147 saved_main_file_path_.value().length() <= kMaxFilePathLength); | 149 saved_main_file_path_.value().length() <= kMaxFilePathLength); |
| 148 DCHECK(!saved_main_directory_path_.empty() && | 150 DCHECK(!saved_main_directory_path_.empty() && |
| 149 saved_main_directory_path_.value().length() < kMaxFilePathLength); | 151 saved_main_directory_path_.value().length() < kMaxFilePathLength); |
| 150 InternalInit(); | 152 InternalInit(); |
| 151 } | 153 } |
| 152 | 154 |
| 153 SavePackage::SavePackage(WebContents* web_contents) | 155 SavePackage::SavePackage(WebContents* web_contents) |
| 154 : content::WebContentsObserver(web_contents), | 156 : content::WebContentsObserver(web_contents), |
| 155 file_manager_(NULL), | 157 file_manager_(NULL), |
| 156 download_manager_(NULL), | 158 download_manager_(NULL), |
| 157 download_(NULL), | 159 download_(NULL), |
| 158 page_url_(GetUrlToBeSaved()), | 160 page_url_(GetUrlToBeSaved()), |
| 159 title_(web_contents->GetTitle()), | 161 title_(web_contents->GetTitle()), |
| 160 start_tick_(base::TimeTicks::Now()), | 162 start_tick_(base::TimeTicks::Now()), |
| 161 finished_(false), | 163 finished_(false), |
| 164 mhtml_finishing_(false), | |
| 162 user_canceled_(false), | 165 user_canceled_(false), |
| 163 disk_error_occurred_(false), | 166 disk_error_occurred_(false), |
| 164 save_type_(content::SAVE_PAGE_TYPE_UNKNOWN), | 167 save_type_(content::SAVE_PAGE_TYPE_UNKNOWN), |
| 165 all_save_items_count_(0), | 168 all_save_items_count_(0), |
| 166 wait_state_(INITIALIZE), | 169 wait_state_(INITIALIZE), |
| 167 contents_id_(web_contents->GetRenderProcessHost()->GetID()), | 170 contents_id_(web_contents->GetRenderProcessHost()->GetID()), |
| 168 unique_id_(g_save_package_id++), | 171 unique_id_(g_save_package_id++), |
| 169 wrote_to_completed_file_(false), | 172 wrote_to_completed_file_(false), |
| 170 wrote_to_failed_file_(false) { | 173 wrote_to_failed_file_(false) { |
| 171 DCHECK(page_url_.is_valid()); | 174 DCHECK(page_url_.is_valid()); |
| 172 InternalInit(); | 175 InternalInit(); |
| 173 } | 176 } |
| 174 | 177 |
| 175 // This is for testing use. Set |finished_| as true because we don't want | 178 // This is for testing use. Set |finished_| as true because we don't want |
| 176 // method Cancel to be be called in destructor in test mode. | 179 // method Cancel to be be called in destructor in test mode. |
| 177 // We also don't call InternalInit(). | 180 // We also don't call InternalInit(). |
| 178 SavePackage::SavePackage(WebContents* web_contents, | 181 SavePackage::SavePackage(WebContents* web_contents, |
| 179 const FilePath& file_full_path, | 182 const FilePath& file_full_path, |
| 180 const FilePath& directory_full_path) | 183 const FilePath& directory_full_path) |
| 181 : content::WebContentsObserver(web_contents), | 184 : content::WebContentsObserver(web_contents), |
| 182 file_manager_(NULL), | 185 file_manager_(NULL), |
| 183 download_manager_(NULL), | 186 download_manager_(NULL), |
| 184 download_(NULL), | 187 download_(NULL), |
| 185 saved_main_file_path_(file_full_path), | 188 saved_main_file_path_(file_full_path), |
| 186 saved_main_directory_path_(directory_full_path), | 189 saved_main_directory_path_(directory_full_path), |
| 187 start_tick_(base::TimeTicks::Now()), | 190 start_tick_(base::TimeTicks::Now()), |
| 188 finished_(true), | 191 finished_(true), |
| 192 mhtml_finishing_(false), | |
| 189 user_canceled_(false), | 193 user_canceled_(false), |
| 190 disk_error_occurred_(false), | 194 disk_error_occurred_(false), |
| 191 save_type_(content::SAVE_PAGE_TYPE_UNKNOWN), | 195 save_type_(content::SAVE_PAGE_TYPE_UNKNOWN), |
| 192 all_save_items_count_(0), | 196 all_save_items_count_(0), |
| 193 wait_state_(INITIALIZE), | 197 wait_state_(INITIALIZE), |
| 194 contents_id_(0), | 198 contents_id_(0), |
| 195 unique_id_(g_save_package_id++), | 199 unique_id_(g_save_package_id++), |
| 196 wrote_to_completed_file_(false), | 200 wrote_to_completed_file_(false), |
| 197 wrote_to_failed_file_(false) { | 201 wrote_to_failed_file_(false) { |
| 198 } | 202 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 | 262 |
| 259 file_manager_ = rdh->save_file_manager(); | 263 file_manager_ = rdh->save_file_manager(); |
| 260 DCHECK(file_manager_); | 264 DCHECK(file_manager_); |
| 261 | 265 |
| 262 download_manager_ = web_contents()->GetBrowserContext()->GetDownloadManager(); | 266 download_manager_ = web_contents()->GetBrowserContext()->GetDownloadManager(); |
| 263 DCHECK(download_manager_); | 267 DCHECK(download_manager_); |
| 264 | 268 |
| 265 download_stats::RecordSavePackageEvent(download_stats::SAVE_PACKAGE_STARTED); | 269 download_stats::RecordSavePackageEvent(download_stats::SAVE_PACKAGE_STARTED); |
| 266 } | 270 } |
| 267 | 271 |
| 268 bool SavePackage::Init() { | 272 bool SavePackage::Init( |
| 273 const content::SaveFileDownloadCreatedCallback& download_created_callback) { | |
| 269 // Set proper running state. | 274 // Set proper running state. |
| 270 if (wait_state_ != INITIALIZE) | 275 if (wait_state_ != INITIALIZE) |
| 271 return false; | 276 return false; |
| 272 | 277 |
| 273 wait_state_ = START_PROCESS; | 278 wait_state_ = START_PROCESS; |
| 274 | 279 |
| 275 // Initialize the request context and resource dispatcher. | 280 // Initialize the request context and resource dispatcher. |
| 276 content::BrowserContext* browser_context = | 281 content::BrowserContext* browser_context = |
| 277 web_contents()->GetBrowserContext(); | 282 web_contents()->GetBrowserContext(); |
| 278 if (!browser_context) { | 283 if (!browser_context) { |
| 279 NOTREACHED(); | 284 NOTREACHED(); |
| 280 return false; | 285 return false; |
| 281 } | 286 } |
| 282 | 287 |
| 283 // The download manager keeps ownership but adds us as an observer. | 288 // The download manager keeps ownership but adds us as an observer. |
| 284 download_ = download_manager_->CreateSavePackageDownloadItem( | 289 download_ = download_manager_->CreateSavePackageDownloadItem( |
| 285 saved_main_file_path_, page_url_, | 290 saved_main_file_path_, |
| 286 browser_context->IsOffTheRecord(), this); | 291 page_url_, |
| 292 browser_context->IsOffTheRecord(), | |
| 293 ((save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) ? | |
| 294 "multipart/related" : "text/html"), | |
| 295 this); | |
| 296 if (!download_created_callback.is_null()) | |
| 297 download_created_callback.Run(download_); | |
| 287 | 298 |
| 288 // Check save type and process the save page job. | 299 // Check save type and process the save page job. |
| 289 if (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { | 300 if (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { |
| 290 // Get directory | 301 // Get directory |
| 291 DCHECK(!saved_main_directory_path_.empty()); | 302 DCHECK(!saved_main_directory_path_.empty()); |
| 292 GetAllSavableResourceLinksForCurrentPage(); | 303 GetAllSavableResourceLinksForCurrentPage(); |
| 304 } else if (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) { | |
| 305 web_contents()->GenerateMHTML(saved_main_file_path_, base::Bind( | |
| 306 &SavePackage::OnMHTMLGenerated, this)); | |
| 293 } else { | 307 } else { |
| 294 wait_state_ = NET_FILES; | 308 wait_state_ = NET_FILES; |
| 295 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? | 309 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? |
| 296 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 310 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
| 297 SaveFileCreateInfo::SAVE_FILE_FROM_NET; | 311 SaveFileCreateInfo::SAVE_FILE_FROM_NET; |
| 298 SaveItem* save_item = new SaveItem(page_url_, | 312 SaveItem* save_item = new SaveItem(page_url_, |
| 299 GURL(), | 313 GURL(), |
| 300 this, | 314 this, |
| 301 save_source); | 315 save_source); |
| 302 // Add this item to waiting list. | 316 // Add this item to waiting list. |
| 303 waiting_item_queue_.push(save_item); | 317 waiting_item_queue_.push(save_item); |
| 304 all_save_items_count_ = 1; | 318 all_save_items_count_ = 1; |
| 305 download_->SetTotalBytes(1); | 319 download_->SetTotalBytes(1); |
| 306 | 320 |
| 307 DoSavingProcess(); | 321 DoSavingProcess(); |
| 308 } | 322 } |
| 309 | 323 |
| 310 return true; | 324 return true; |
| 311 } | 325 } |
| 312 | 326 |
| 327 void SavePackage::OnMHTMLGenerated(const FilePath& path, int64 size) { | |
| 328 if (size <= 0) { | |
| 329 Cancel(false); | |
| 330 return; | |
| 331 } | |
| 332 wrote_to_completed_file_ = true; | |
| 333 download_->SetTotalBytes(size); | |
| 334 download_->UpdateProgress(size, size, DownloadItem::kEmptyFileHash); | |
| 335 // Must call OnAllDataSaved here in order for | |
| 336 // GDataDownloadObserver::ShouldUpload() to return true. | |
| 337 // ShouldCompleteDownload() may depend on the gdata uploader to finish. | |
| 338 download_->OnAllDataSaved(size, DownloadItem::kEmptyFileHash); | |
| 339 // GDataDownloadObserver is waiting for the upload to complete. When that | |
| 340 // happens, it will call download_->MaybeCompleteDownload(), which will call | |
| 341 // through our OnDownloadUpdated() allowing us to Finish(). | |
| 342 // OnDownloadUpdated() may have been called in OnAllDataSaved(), so |this| may | |
| 343 // be deleted at this point. | |
| 344 } | |
| 345 | |
| 313 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further | 346 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further |
| 314 // restricted by NAME_MAX. The maximum allowed path looks like: | 347 // restricted by NAME_MAX. The maximum allowed path looks like: |
| 315 // '/path/to/save_dir' + '/' + NAME_MAX. | 348 // '/path/to/save_dir' + '/' + NAME_MAX. |
| 316 uint32 SavePackage::GetMaxPathLengthForDirectory(const FilePath& base_dir) { | 349 uint32 SavePackage::GetMaxPathLengthForDirectory(const FilePath& base_dir) { |
| 317 #if defined(OS_POSIX) | 350 #if defined(OS_POSIX) |
| 318 return std::min(kMaxFilePathLength, | 351 return std::min(kMaxFilePathLength, |
| 319 static_cast<uint32>(base_dir.value().length()) + | 352 static_cast<uint32>(base_dir.value().length()) + |
| 320 NAME_MAX + 1); | 353 NAME_MAX + 1); |
| 321 #else | 354 #else |
| 322 return kMaxFilePathLength; | 355 return kMaxFilePathLength; |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 698 it != saved_failed_items_.end(); ++it) | 731 it != saved_failed_items_.end(); ++it) |
| 699 save_ids.push_back(it->second->save_id()); | 732 save_ids.push_back(it->second->save_id()); |
| 700 | 733 |
| 701 BrowserThread::PostTask( | 734 BrowserThread::PostTask( |
| 702 BrowserThread::FILE, FROM_HERE, | 735 BrowserThread::FILE, FROM_HERE, |
| 703 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, | 736 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, |
| 704 file_manager_, | 737 file_manager_, |
| 705 save_ids)); | 738 save_ids)); |
| 706 | 739 |
| 707 if (download_) { | 740 if (download_) { |
| 708 download_->OnAllDataSaved(all_save_items_count_, | 741 if (save_type_ != content::SAVE_PAGE_TYPE_AS_MHTML) |
| 709 DownloadItem::kEmptyFileHash); | 742 download_->OnAllDataSaved(all_save_items_count_, |
| 743 DownloadItem::kEmptyFileHash); | |
| 710 download_->MarkAsComplete(); | 744 download_->MarkAsComplete(); |
| 711 FinalizeDownloadEntry(); | 745 FinalizeDownloadEntry(); |
| 712 } | 746 } |
| 713 } | 747 } |
| 714 | 748 |
| 715 // Called for updating end state. | 749 // Called for updating end state. |
| 716 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 750 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
| 717 // Because we might have canceled this saving job before, | 751 // Because we might have canceled this saving job before, |
| 718 // so we might not find corresponding SaveItem. Just ignore it. | 752 // so we might not find corresponding SaveItem. Just ignore it. |
| 719 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); | 753 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 767 | 801 |
| 768 save_item->Finish(0, false); | 802 save_item->Finish(0, false); |
| 769 | 803 |
| 770 PutInProgressItemToSavedMap(save_item); | 804 PutInProgressItemToSavedMap(save_item); |
| 771 | 805 |
| 772 // Inform the DownloadItem to update UI. | 806 // Inform the DownloadItem to update UI. |
| 773 // We use the received bytes as number of saved files. | 807 // We use the received bytes as number of saved files. |
| 774 if (download_) | 808 if (download_) |
| 775 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); | 809 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); |
| 776 | 810 |
| 777 if (save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML || | 811 if ((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| 778 save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) { | 812 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) || |
| 813 (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM)) { | |
| 779 // We got error when saving page. Treat it as disk error. | 814 // We got error when saving page. Treat it as disk error. |
| 780 Cancel(true); | 815 Cancel(true); |
| 781 } | 816 } |
| 782 | 817 |
| 783 if (canceled()) { | 818 if (canceled()) { |
| 784 DCHECK(finished_); | 819 DCHECK(finished_); |
| 785 return; | 820 return; |
| 786 } | 821 } |
| 787 | 822 |
| 788 // Continue processing the save page job. | 823 // Continue processing the save page job. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 874 wait_state_ = HTML_DATA; | 909 wait_state_ = HTML_DATA; |
| 875 // All non-HTML resources have been finished, start all remaining | 910 // All non-HTML resources have been finished, start all remaining |
| 876 // HTML files. | 911 // HTML files. |
| 877 SaveNextFile(true); | 912 SaveNextFile(true); |
| 878 } | 913 } |
| 879 } else if (in_process_count()) { | 914 } else if (in_process_count()) { |
| 880 // Continue asking for HTML data. | 915 // Continue asking for HTML data. |
| 881 DCHECK(wait_state_ == HTML_DATA); | 916 DCHECK(wait_state_ == HTML_DATA); |
| 882 } | 917 } |
| 883 } else { | 918 } else { |
| 884 // Save as HTML only. | 919 // Save as HTML only or MHTML. |
| 885 DCHECK(wait_state_ == NET_FILES); | 920 DCHECK(wait_state_ == NET_FILES); |
| 886 DCHECK(save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML); | 921 DCHECK((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| 922 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML)); | |
| 887 if (waiting_item_queue_.size()) { | 923 if (waiting_item_queue_.size()) { |
| 888 DCHECK(all_save_items_count_ == waiting_item_queue_.size()); | 924 DCHECK(all_save_items_count_ == waiting_item_queue_.size()); |
| 889 SaveNextFile(false); | 925 SaveNextFile(false); |
| 890 } | 926 } |
| 891 } | 927 } |
| 892 } | 928 } |
| 893 | 929 |
| 894 bool SavePackage::OnMessageReceived(const IPC::Message& message) { | 930 bool SavePackage::OnMessageReceived(const IPC::Message& message) { |
| 895 bool handled = true; | 931 bool handled = true; |
| 896 IPC_BEGIN_MESSAGE_MAP(SavePackage, message) | 932 IPC_BEGIN_MESSAGE_MAP(SavePackage, message) |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1260 // The WebContents which owns this SavePackage may have disappeared during | 1296 // The WebContents which owns this SavePackage may have disappeared during |
| 1261 // the UI->FILE->UI thread hop of | 1297 // the UI->FILE->UI thread hop of |
| 1262 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. | 1298 // GetSaveInfo->CreateDirectoryOnFileThread->ContinueGetSaveInfo. |
| 1263 if (!web_contents()) | 1299 if (!web_contents()) |
| 1264 return; | 1300 return; |
| 1265 | 1301 |
| 1266 FilePath::StringType default_extension; | 1302 FilePath::StringType default_extension; |
| 1267 if (can_save_as_complete) | 1303 if (can_save_as_complete) |
| 1268 default_extension = kDefaultHtmlExtension; | 1304 default_extension = kDefaultHtmlExtension; |
| 1269 | 1305 |
| 1270 // On ChromeOS, OnPathPicked is not invoked; SavePackageFilePickerChromeOS | |
| 1271 // handles the the save. | |
| 1272 download_manager_->delegate()->ChooseSavePath( | 1306 download_manager_->delegate()->ChooseSavePath( |
| 1273 web_contents(), suggested_path, default_extension, can_save_as_complete, | 1307 web_contents(), |
| 1308 suggested_path, | |
| 1309 default_extension, | |
| 1310 can_save_as_complete, | |
| 1274 base::Bind(&SavePackage::OnPathPicked, AsWeakPtr())); | 1311 base::Bind(&SavePackage::OnPathPicked, AsWeakPtr())); |
| 1275 } | 1312 } |
| 1276 | 1313 |
| 1277 void SavePackage::OnPathPicked(const FilePath& final_name, | 1314 void SavePackage::OnPathPicked( |
| 1278 content::SavePageType type) { | 1315 const FilePath& final_name, |
| 1316 content::SavePageType type, | |
| 1317 const content::SaveFileDownloadCreatedCallback& download_created_callback) { | |
| 1279 // Ensure the filename is safe. | 1318 // Ensure the filename is safe. |
| 1280 saved_main_file_path_ = final_name; | 1319 saved_main_file_path_ = final_name; |
| 1281 // TODO(asanka): This call may block on IO and shouldn't be made | 1320 // TODO(asanka): This call may block on IO and shouldn't be made |
| 1282 // from the UI thread. See http://crbug.com/61827. | 1321 // from the UI thread. See http://crbug.com/61827. |
| 1283 net::GenerateSafeFileName(web_contents()->GetContentsMimeType(), false, | 1322 net::GenerateSafeFileName(web_contents()->GetContentsMimeType(), false, |
| 1284 &saved_main_file_path_); | 1323 &saved_main_file_path_); |
| 1285 | 1324 |
| 1286 saved_main_directory_path_ = saved_main_file_path_.DirName(); | 1325 saved_main_directory_path_ = saved_main_file_path_.DirName(); |
| 1287 save_type_ = type; | 1326 save_type_ = type; |
| 1288 if (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { | 1327 if (save_type_ == content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML) { |
| 1289 // Make new directory for saving complete file. | 1328 // Make new directory for saving complete file. |
| 1290 saved_main_directory_path_ = saved_main_directory_path_.Append( | 1329 saved_main_directory_path_ = saved_main_directory_path_.Append( |
| 1291 saved_main_file_path_.RemoveExtension().BaseName().value() + | 1330 saved_main_file_path_.RemoveExtension().BaseName().value() + |
| 1292 FILE_PATH_LITERAL("_files")); | 1331 FILE_PATH_LITERAL("_files")); |
| 1293 } | 1332 } |
| 1294 | 1333 |
| 1295 Init(); | 1334 Init(download_created_callback); |
| 1296 } | 1335 } |
| 1297 | 1336 |
| 1298 void SavePackage::StopObservation() { | 1337 void SavePackage::StopObservation() { |
| 1299 DCHECK(download_); | 1338 DCHECK(download_); |
| 1300 DCHECK(download_manager_); | 1339 DCHECK(download_manager_); |
| 1301 | 1340 |
| 1302 download_->RemoveObserver(this); | 1341 download_->RemoveObserver(this); |
| 1303 download_ = NULL; | 1342 download_ = NULL; |
| 1304 download_manager_ = NULL; | 1343 download_manager_ = NULL; |
| 1305 } | 1344 } |
| 1306 | 1345 |
| 1307 void SavePackage::OnDownloadUpdated(DownloadItem* download) { | 1346 void SavePackage::OnDownloadUpdated(DownloadItem* download) { |
| 1308 DCHECK(download_); | 1347 DCHECK(download_); |
| 1309 DCHECK(download_ == download); | 1348 DCHECK(download_ == download); |
| 1310 DCHECK(download_manager_); | 1349 DCHECK(download_manager_); |
| 1311 | 1350 |
| 1312 // Check for removal. | 1351 // Check for removal. |
| 1313 if (download->GetState() == DownloadItem::REMOVING) | 1352 if (download_->GetState() == DownloadItem::REMOVING) { |
| 1314 StopObservation(); | 1353 StopObservation(); |
| 1354 } | |
| 1355 | |
| 1356 // MHTML saves may need to wait for GData to finish uploading. | |
| 1357 if ((save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) && | |
| 1358 download_->AllDataSaved() && | |
| 1359 !download_->IsComplete() && | |
| 1360 !mhtml_finishing_ && | |
| 1361 download_manager_->delegate()->ShouldCompleteDownload(download_)) { | |
| 1362 // Post a task to avoid re-entering OnDownloadUpdated. Set a flag to | |
| 1363 // prevent double-calling Finish() in case another OnDownloadUpdated happens | |
| 1364 // before Finish() runs. | |
| 1365 mhtml_finishing_ = true; | |
| 1366 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 1367 base::Bind(&SavePackage::Finish, this)); | |
|
Randy Smith (Not in Mondays)
2012/04/27 18:24:04
This makes me twitchy--a complicated conditional,
| |
| 1368 } | |
| 1315 } | 1369 } |
| 1316 | 1370 |
| 1317 void SavePackage::FinalizeDownloadEntry() { | 1371 void SavePackage::FinalizeDownloadEntry() { |
| 1318 DCHECK(download_); | 1372 DCHECK(download_); |
| 1319 DCHECK(download_manager_); | 1373 DCHECK(download_manager_); |
| 1320 | 1374 |
| 1321 download_manager_->SavePageDownloadFinished(download_); | 1375 download_manager_->SavePageDownloadFinished(download_); |
| 1322 StopObservation(); | 1376 StopObservation(); |
| 1323 } | 1377 } |
| OLD | NEW |