Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: content/browser/download/save_package.cc

Issue 10916201: Make the SavePackage use of the fake download item cleaner around cancellation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged to LKGR (156083) Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/download/download_manager_impl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // Must call OnAllDataSaved here in order for
344 // GDataDownloadObserver::ShouldUpload() to return true.
345 // ShouldCompleteDownload() may depend on the gdata uploader to finish.
346 download_->OnAllDataSaved(size, DownloadItem::kEmptyFileHash);
347 }
348
342 if (!download_manager_->GetDelegate() || 349 if (!download_manager_->GetDelegate() ||
343 download_manager_->GetDelegate()->ShouldCompleteDownload( 350 download_manager_->GetDelegate()->ShouldCompleteDownload(
344 download_, base::Bind(&SavePackage::Finish, this))) { 351 download_, base::Bind(&SavePackage::Finish, this))) {
345 Finish(); 352 Finish();
346 } 353 }
347 } 354 }
348 355
349 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further 356 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further
350 // restricted by NAME_MAX. The maximum allowed path looks like: 357 // restricted by NAME_MAX. The maximum allowed path looks like:
351 // '/path/to/save_dir' + '/' + NAME_MAX. 358 // '/path/to/save_dir' + '/' + NAME_MAX.
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); 740 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin();
734 it != saved_failed_items_.end(); ++it) 741 it != saved_failed_items_.end(); ++it)
735 save_ids.push_back(it->second->save_id()); 742 save_ids.push_back(it->second->save_id());
736 743
737 BrowserThread::PostTask( 744 BrowserThread::PostTask(
738 BrowserThread::FILE, FROM_HERE, 745 BrowserThread::FILE, FROM_HERE,
739 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap, 746 base::Bind(&SaveFileManager::RemoveSavedFileFromFileMap,
740 file_manager_, 747 file_manager_,
741 save_ids)); 748 save_ids));
742 749
743 if (download_) { 750 // Hack to avoid touching download_ after user cancel.
744 if (save_type_ != content::SAVE_PAGE_TYPE_AS_MHTML) 751 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem
752 // with SavePackage flow.
753 if (download_ && download_->IsInProgress()) {
754 if (save_type_ != content::SAVE_PAGE_TYPE_AS_MHTML) {
745 download_->OnAllDataSaved(all_save_items_count_, 755 download_->OnAllDataSaved(all_save_items_count_,
746 DownloadItem::kEmptyFileHash); 756 DownloadItem::kEmptyFileHash);
757 }
747 download_->MarkAsComplete(); 758 download_->MarkAsComplete();
748 FinalizeDownloadEntry();
749 } 759 }
760 FinalizeDownloadEntry();
750 } 761 }
751 762
752 // Called for updating end state. 763 // Called for updating end state.
753 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { 764 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) {
754 // Because we might have canceled this saving job before, 765 // Because we might have canceled this saving job before,
755 // so we might not find corresponding SaveItem. Just ignore it. 766 // so we might not find corresponding SaveItem. Just ignore it.
756 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); 767 SaveItem* save_item = LookupItemInProcessBySaveId(save_id);
757 if (!save_item) 768 if (!save_item)
758 return; 769 return;
759 770
760 // Let SaveItem set end state. 771 // Let SaveItem set end state.
761 save_item->Finish(size, is_success); 772 save_item->Finish(size, is_success);
762 // Remove the associated save id and SavePackage. 773 // Remove the associated save id and SavePackage.
763 file_manager_->RemoveSaveFile(save_id, save_item->url(), this); 774 file_manager_->RemoveSaveFile(save_id, save_item->url(), this);
764 775
765 PutInProgressItemToSavedMap(save_item); 776 PutInProgressItemToSavedMap(save_item);
766 777
767 // Inform the DownloadItem to update UI. 778 // Inform the DownloadItem to update UI.
768 // We use the received bytes as number of saved files. 779 // We use the received bytes as number of saved files.
769 if (download_) 780 // Hack to avoid touching download_ after user cancel.
781 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem
782 // with SavePackage flow.
783 if (download_ && download_->IsInProgress())
770 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); 784 download_->UpdateProgress(completed_count(), CurrentSpeed(), "");
771 785
772 if (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM && 786 if (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM &&
773 save_item->url() == page_url_ && !save_item->received_bytes()) { 787 save_item->url() == page_url_ && !save_item->received_bytes()) {
774 // If size of main HTML page is 0, treat it as disk error. 788 // If size of main HTML page is 0, treat it as disk error.
775 Cancel(false); 789 Cancel(false);
776 return; 790 return;
777 } 791 }
778 792
779 if (canceled()) { 793 if (canceled()) {
(...skipping 21 matching lines...) Expand all
801 return; 815 return;
802 } 816 }
803 SaveItem* save_item = it->second; 817 SaveItem* save_item = it->second;
804 818
805 save_item->Finish(0, false); 819 save_item->Finish(0, false);
806 820
807 PutInProgressItemToSavedMap(save_item); 821 PutInProgressItemToSavedMap(save_item);
808 822
809 // Inform the DownloadItem to update UI. 823 // Inform the DownloadItem to update UI.
810 // We use the received bytes as number of saved files. 824 // We use the received bytes as number of saved files.
811 if (download_) 825 // Hack to avoid touching download_ after user cancel.
826 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem
827 // with SavePackage flow.
828 if (download_ && download_->IsInProgress())
812 download_->UpdateProgress(completed_count(), CurrentSpeed(), ""); 829 download_->UpdateProgress(completed_count(), CurrentSpeed(), "");
813 830
814 if ((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) || 831 if ((save_type_ == content::SAVE_PAGE_TYPE_AS_ONLY_HTML) ||
815 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) || 832 (save_type_ == content::SAVE_PAGE_TYPE_AS_MHTML) ||
816 (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM)) { 833 (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM)) {
817 // We got error when saving page. Treat it as disk error. 834 // We got error when saving page. Treat it as disk error.
818 Cancel(true); 835 Cancel(true);
819 } 836 }
820 837
821 if (canceled()) { 838 if (canceled()) {
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 const std::vector<content::Referrer>& referrers_list, 1106 const std::vector<content::Referrer>& referrers_list,
1090 const std::vector<GURL>& frames_list) { 1107 const std::vector<GURL>& frames_list) {
1091 if (wait_state_ != RESOURCES_LIST) 1108 if (wait_state_ != RESOURCES_LIST)
1092 return; 1109 return;
1093 1110
1094 DCHECK(resources_list.size() == referrers_list.size()); 1111 DCHECK(resources_list.size() == referrers_list.size());
1095 all_save_items_count_ = static_cast<int>(resources_list.size()) + 1112 all_save_items_count_ = static_cast<int>(resources_list.size()) +
1096 static_cast<int>(frames_list.size()); 1113 static_cast<int>(frames_list.size());
1097 1114
1098 // We use total bytes as the total number of files we want to save. 1115 // We use total bytes as the total number of files we want to save.
1099 if (download_) 1116 // Hack to avoid touching download_ after user cancel.
1117 // TODO(rdsmith/benjhayden): Integrate canceling on DownloadItem
1118 // with SavePackage flow.
1119 if (download_ && download_->IsInProgress())
1100 download_->SetTotalBytes(all_save_items_count_); 1120 download_->SetTotalBytes(all_save_items_count_);
1101 1121
1102 if (all_save_items_count_) { 1122 if (all_save_items_count_) {
1103 // Put all sub-resources to wait list. 1123 // Put all sub-resources to wait list.
1104 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) { 1124 for (int i = 0; i < static_cast<int>(resources_list.size()); ++i) {
1105 const GURL& u = resources_list[i]; 1125 const GURL& u = resources_list[i];
1106 DCHECK(u.is_valid()); 1126 DCHECK(u.is_valid());
1107 SaveFileCreateInfo::SaveFileSource save_source = u.SchemeIsFile() ? 1127 SaveFileCreateInfo::SaveFileSource save_source = u.SchemeIsFile() ?
1108 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : 1128 SaveFileCreateInfo::SAVE_FILE_FROM_FILE :
1109 SaveFileCreateInfo::SAVE_FILE_FROM_NET; 1129 SaveFileCreateInfo::SAVE_FILE_FROM_NET;
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
1358 } 1378 }
1359 1379
1360 void SavePackage::OnDownloadDestroyed(DownloadItem* download) { 1380 void SavePackage::OnDownloadDestroyed(DownloadItem* download) {
1361 StopObservation(); 1381 StopObservation();
1362 } 1382 }
1363 1383
1364 void SavePackage::FinalizeDownloadEntry() { 1384 void SavePackage::FinalizeDownloadEntry() {
1365 DCHECK(download_); 1385 DCHECK(download_);
1366 DCHECK(download_manager_); 1386 DCHECK(download_manager_);
1367 1387
1388 content::NotificationService::current()->Notify(
1389 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
1390 // We use the DownloadManager as the source as that's a
1391 // central SavePackage related location that observers can
1392 // get to if they want to wait for notifications for a
1393 // particular BrowserContext. Alternatively, we could make
1394 // it come from the WebContents, which would be more specific
1395 // but less useful to (current) customers.
1396 content::Source<content::DownloadManager>(download_manager_),
1397 content::Details<content::DownloadItem>(download_));
1368 download_manager_->SavePageDownloadFinished(download_); 1398 download_manager_->SavePageDownloadFinished(download_);
1369 StopObservation(); 1399 StopObservation();
1370 } 1400 }
OLDNEW
« no previous file with comments | « content/browser/download/download_manager_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698