| 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" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/i18n/file_util_icu.h" | 12 #include "base/i18n/file_util_icu.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
| 17 #include "base/string_split.h" | 17 #include "base/string_split.h" |
| 18 #include "base/sys_string_conversions.h" | 18 #include "base/sys_string_conversions.h" |
| 19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 21 #include "content/browser/download/download_file_manager.h" | |
| 22 #include "content/browser/download/download_item_impl.h" | 21 #include "content/browser/download/download_item_impl.h" |
| 23 #include "content/browser/download/download_manager_impl.h" | 22 #include "content/browser/download/download_manager_impl.h" |
| 24 #include "content/browser/download/download_stats.h" | 23 #include "content/browser/download/download_stats.h" |
| 25 #include "content/browser/download/save_file.h" | 24 #include "content/browser/download/save_file.h" |
| 26 #include "content/browser/download/save_file_manager.h" | 25 #include "content/browser/download/save_file_manager.h" |
| 27 #include "content/browser/download/save_item.h" | 26 #include "content/browser/download/save_item.h" |
| 28 #include "content/browser/renderer_host/render_process_host_impl.h" | 27 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 29 #include "content/browser/renderer_host/render_view_host_delegate.h" | 28 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| 30 #include "content/browser/renderer_host/render_view_host_impl.h" | 29 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 31 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 30 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
| 32 #include "content/common/view_messages.h" | 31 #include "content/common/view_messages.h" |
| 33 #include "content/public/browser/browser_context.h" | 32 #include "content/public/browser/browser_context.h" |
| 34 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 35 #include "content/public/browser/content_browser_client.h" | 34 #include "content/public/browser/content_browser_client.h" |
| 36 #include "content/public/browser/download_manager_delegate.h" | 35 #include "content/public/browser/download_manager_delegate.h" |
| 37 #include "content/public/browser/navigation_entry.h" | 36 #include "content/public/browser/navigation_entry.h" |
| 37 #include "content/public/browser/notification_service.h" |
| 38 #include "content/public/browser/notification_types.h" |
| 38 #include "content/public/browser/resource_context.h" | 39 #include "content/public/browser/resource_context.h" |
| 39 #include "content/public/browser/web_contents.h" | 40 #include "content/public/browser/web_contents.h" |
| 40 #include "content/public/common/url_constants.h" | 41 #include "content/public/common/url_constants.h" |
| 41 #include "net/base/io_buffer.h" | 42 #include "net/base/io_buffer.h" |
| 42 #include "net/base/mime_util.h" | 43 #include "net/base/mime_util.h" |
| 43 #include "net/base/net_util.h" | 44 #include "net/base/net_util.h" |
| 44 #include "net/url_request/url_request_context.h" | 45 #include "net/url_request/url_request_context.h" |
| 45 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPageSerializerClie
nt.h" | 46 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPageSerializerClie
nt.h" |
| 46 | 47 |
| 47 using base::Time; | 48 using base::Time; |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 | 327 |
| 327 return true; | 328 return true; |
| 328 } | 329 } |
| 329 | 330 |
| 330 void SavePackage::OnMHTMLGenerated(const FilePath& path, int64 size) { | 331 void SavePackage::OnMHTMLGenerated(const FilePath& path, int64 size) { |
| 331 if (size <= 0) { | 332 if (size <= 0) { |
| 332 Cancel(false); | 333 Cancel(false); |
| 333 return; | 334 return; |
| 334 } | 335 } |
| 335 wrote_to_completed_file_ = true; | 336 wrote_to_completed_file_ = true; |
| 336 download_->SetTotalBytes(size); | 337 |
| 337 download_->UpdateProgress(size, size, DownloadItem::kEmptyFileHash); | 338 // Hack to avoid touching download_ after user cancel. |
| 338 // Must call OnAllDataSaved here in order for | 339 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem |
| 339 // GDataDownloadObserver::ShouldUpload() to return true. | 340 // with SavePackage flow. |
| 340 // ShouldCompleteDownload() may depend on the gdata uploader to finish. | 341 if (download_->IsInProgress()) { |
| 341 download_->OnAllDataSaved(size, DownloadItem::kEmptyFileHash); | 342 download_->SetTotalBytes(size); |
| 343 download_->UpdateProgress(size, size, DownloadItem::kEmptyFileHash); |
| 344 // Must call OnAllDataSaved here in order for |
| 345 // GDataDownloadObserver::ShouldUpload() to return true. |
| 346 // ShouldCompleteDownload() may depend on the gdata uploader to finish. |
| 347 download_->OnAllDataSaved(DownloadItem::kEmptyFileHash); |
| 348 } |
| 349 |
| 342 if (!download_manager_->GetDelegate() || | 350 if (!download_manager_->GetDelegate() || |
| 343 download_manager_->GetDelegate()->ShouldCompleteDownload( | 351 download_manager_->GetDelegate()->ShouldCompleteDownload( |
| 344 download_, base::Bind(&SavePackage::Finish, this))) { | 352 download_, base::Bind(&SavePackage::Finish, this))) { |
| 345 Finish(); | 353 Finish(); |
| 346 } | 354 } |
| 347 } | 355 } |
| 348 | 356 |
| 349 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further | 357 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further |
| 350 // restricted by NAME_MAX. The maximum allowed path looks like: | 358 // restricted by NAME_MAX. The maximum allowed path looks like: |
| 351 // '/path/to/save_dir' + '/' + NAME_MAX. | 359 // '/path/to/save_dir' + '/' + NAME_MAX. |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); | 741 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); |
| 734 it != saved_failed_items_.end(); ++it) | 742 it != saved_failed_items_.end(); ++it) |
| 735 save_ids.push_back(it->second->save_id()); | 743 save_ids.push_back(it->second->save_id()); |
| 736 | 744 |
| 737 BrowserThread::PostTask( | 745 BrowserThread::PostTask( |
| 738 BrowserThread::FILE, FROM_HERE, | 746 BrowserThread::FILE, FROM_HERE, |
| 739 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, | 747 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, |
| 740 file_manager_, | 748 file_manager_, |
| 741 save_ids)); | 749 save_ids)); |
| 742 | 750 |
| 743 if (download_) { | 751 // Hack to avoid touching download_ after user cancel. |
| 744 if (save_type_ != content::SAVE_PAGE_TYPE_AS_MHTML) | 752 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem |
| 745 download_->OnAllDataSaved(all_save_items_count_, | 753 // with SavePackage flow. |
| 746 DownloadItem::kEmptyFileHash); | 754 if (download_ && download_->IsInProgress()) { |
| 755 if (save_type_ != content::SAVE_PAGE_TYPE_AS_MHTML) { |
| 756 download_->UpdateProgress(all_save_items_count_, CurrentSpeed(), ""); |
| 757 download_->OnAllDataSaved(DownloadItem::kEmptyFileHash); |
| 758 } |
| 747 download_->MarkAsComplete(); | 759 download_->MarkAsComplete(); |
| 748 FinalizeDownloadEntry(); | |
| 749 } | 760 } |
| 761 FinalizeDownloadEntry(); |
| 750 } | 762 } |
| 751 | 763 |
| 752 // Called for updating end state. | 764 // Called for updating end state. |
| 753 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 765 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
| 754 // Because we might have canceled this saving job before, | 766 // Because we might have canceled this saving job before, |
| 755 // so we might not find corresponding SaveItem. Just ignore it. | 767 // so we might not find corresponding SaveItem. Just ignore it. |
| 756 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); | 768 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); |
| 757 if (!save_item) | 769 if (!save_item) |
| 758 return; | 770 return; |
| 759 | 771 |
| 760 // Let SaveItem set end state. | 772 // Let SaveItem set end state. |
| 761 save_item->Finish(size, is_success); | 773 save_item->Finish(size, is_success); |
| 762 // Remove the associated save id and SavePackage. | 774 // Remove the associated save id and SavePackage. |
| 763 file_manager_->RemoveSaveFile(save_id, save_item->url(), this); | 775 file_manager_->RemoveSaveFile(save_id, save_item->url(), this); |
| 764 | 776 |
| 765 PutInProgressItemToSavedMap(save_item); | 777 PutInProgressItemToSavedMap(save_item); |
| 766 | 778 |
| 767 // Inform the DownloadItem to update UI. | 779 // Inform the DownloadItem to update UI. |
| 768 // We use the received bytes as number of saved files. | 780 // We use the received bytes as number of saved files. |
| 769 if (download_) | 781 // Hack to avoid touching download_ after user cancel. |
| 782 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem |
| 783 // with SavePackage flow. |
| 784 if (download_ && download_->IsInProgress()) |
| 770 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); | 785 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); |
| 771 | 786 |
| 772 if (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM && | 787 if (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM && |
| 773 save_item->url() == page_url_ && !save_item->received_bytes()) { | 788 save_item->url() == page_url_ && !save_item->received_bytes()) { |
| 774 // If size of main HTML page is 0, treat it as disk error. | 789 // If size of main HTML page is 0, treat it as disk error. |
| 775 Cancel(false); | 790 Cancel(false); |
| 776 return; | 791 return; |
| 777 } | 792 } |
| 778 | 793 |
| 779 if (canceled()) { | 794 if (canceled()) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 801 return; | 816 return; |
| 802 } | 817 } |
| 803 SaveItem* save_item = it->second; | 818 SaveItem* save_item = it->second; |
| 804 | 819 |
| 805 save_item->Finish(0, false); | 820 save_item->Finish(0, false); |
| 806 | 821 |
| 807 PutInProgressItemToSavedMap(save_item); | 822 PutInProgressItemToSavedMap(save_item); |
| 808 | 823 |
| 809 // Inform the DownloadItem to update UI. | 824 // Inform the DownloadItem to update UI. |
| 810 // We use the received bytes as number of saved files. | 825 // We use the received bytes as number of saved files. |
| 811 if (download_) | 826 // Hack to avoid touching download_ after user cancel. |
| 827 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem |
| 828 // with SavePackage flow. |
| 829 if (download_ && download_->IsInProgress()) |
| 812 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); | 830 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); |
| 813 | 831 |
| 814 if ((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || | 832 if ((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || |
| 815 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) || | 833 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) || |
| 816 (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM)) { | 834 (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM)) { |
| 817 // We got error when saving page. Treat it as disk error. | 835 // We got error when saving page. Treat it as disk error. |
| 818 Cancel(true); | 836 Cancel(true); |
| 819 } | 837 } |
| 820 | 838 |
| 821 if (canceled()) { | 839 if (canceled()) { |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1089 const std::vector<content::Referrer>& referrers_list, | 1107 const std::vector<content::Referrer>& referrers_list, |
| 1090 const std::vector<GURL>& frames_list) { | 1108 const std::vector<GURL>& frames_list) { |
| 1091 if (wait_state_ != RESOURCES_LIST) | 1109 if (wait_state_ != RESOURCES_LIST) |
| 1092 return; | 1110 return; |
| 1093 | 1111 |
| 1094 DCHECK(resources_list.size() == referrers_list.size()); | 1112 DCHECK(resources_list.size() == referrers_list.size()); |
| 1095 all_save_items_count_ = static_cast<int>(resources_list.size()) + | 1113 all_save_items_count_ = static_cast<int>(resources_list.size()) + |
| 1096 static_cast<int>(frames_list.size()); | 1114 static_cast<int>(frames_list.size()); |
| 1097 | 1115 |
| 1098 // We use total bytes as the total number of files we want to save. | 1116 // We use total bytes as the total number of files we want to save. |
| 1099 if (download_) | 1117 // Hack to avoid touching download_ after user cancel. |
| 1118 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem |
| 1119 // with SavePackage flow. |
| 1120 if (download_ && download_->IsInProgress()) |
| 1100 download_->SetTotalBytes(all_save_items_count_); | 1121 download_->SetTotalBytes(all_save_items_count_); |
| 1101 | 1122 |
| 1102 if (all_save_items_count_) { | 1123 if (all_save_items_count_) { |
| 1103 // Put all sub-resources to wait list. | 1124 // Put all sub-resources to wait list. |
| 1104 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) { | 1125 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) { |
| 1105 const GURL& u = resources_list[i]; | 1126 const GURL& u = resources_list[i]; |
| 1106 DCHECK(u.is_valid()); | 1127 DCHECK(u.is_valid()); |
| 1107 SaveFileCreateInfo::SaveFileSource save_source = u.SchemeIsFile() ? | 1128 SaveFileCreateInfo::SaveFileSource save_source = u.SchemeIsFile() ? |
| 1108 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 1129 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
| 1109 SaveFileCreateInfo::SAVE_FILE_FROM_NET; | 1130 SaveFileCreateInfo::SAVE_FILE_FROM_NET; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1358 } | 1379 } |
| 1359 | 1380 |
| 1360 void SavePackage::OnDownloadDestroyed(DownloadItem* download) { | 1381 void SavePackage::OnDownloadDestroyed(DownloadItem* download) { |
| 1361 StopObservation(); | 1382 StopObservation(); |
| 1362 } | 1383 } |
| 1363 | 1384 |
| 1364 void SavePackage::FinalizeDownloadEntry() { | 1385 void SavePackage::FinalizeDownloadEntry() { |
| 1365 DCHECK(download_); | 1386 DCHECK(download_); |
| 1366 DCHECK(download_manager_); | 1387 DCHECK(download_manager_); |
| 1367 | 1388 |
| 1389 content::NotificationService::current()->Notify( |
| 1390 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 1391 // We use the DownloadManager as the source as that's a |
| 1392 // central SavePackage related location that observers can |
| 1393 // get to if they want to wait for notifications for a |
| 1394 // particular BrowserContext. Alternatively, we could make |
| 1395 // it come from the WebContents, which would be more specific |
| 1396 // but less useful to (current) customers. |
| 1397 content::Source<content::DownloadManager>(download_manager_), |
| 1398 content::Details<content::DownloadItem>(download_)); |
| 1368 download_manager_->SavePageDownloadFinished(download_); | 1399 download_manager_->SavePageDownloadFinished(download_); |
| 1369 StopObservation(); | 1400 StopObservation(); |
| 1370 } | 1401 } |
| OLD | NEW |