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