OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/download/save_package.h" | 5 #include "chrome/browser/download/save_package.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 return false; | 260 return false; |
261 } | 261 } |
262 | 262 |
263 file_manager_ = rdh->save_file_manager(); | 263 file_manager_ = rdh->save_file_manager(); |
264 if (!file_manager_) { | 264 if (!file_manager_) { |
265 NOTREACHED(); | 265 NOTREACHED(); |
266 return false; | 266 return false; |
267 } | 267 } |
268 | 268 |
269 // Create the fake DownloadItem and display the view. | 269 // Create the fake DownloadItem and display the view. |
| 270 download_ = new DownloadItem(1, saved_main_file_path_, 0, page_url_, |
| 271 FilePath(), Time::Now(), 0, -1, -1, false); |
| 272 download_->set_manager(web_contents_->profile()->GetDownloadManager()); |
270 #if defined(OS_WIN) | 273 #if defined(OS_WIN) |
271 // TODO(port): We need to do something like this on posix, but avoid | 274 // TODO(port): We need to do something like this on posix, but avoid |
272 // using DownloadShelfView, which probably should not be ported directly. | 275 // using DownloadShelfView, which probably should not be ported directly. |
273 download_ = new DownloadItem(1, saved_main_file_path_, 0, page_url_, | |
274 FilePath(), Time::Now(), 0, -1, -1, false); | |
275 download_->set_manager(web_contents_->profile()->GetDownloadManager()); | |
276 DownloadShelfView* shelf = web_contents_->GetDownloadShelfView(); | 276 DownloadShelfView* shelf = web_contents_->GetDownloadShelfView(); |
277 shelf->AddDownloadView(new DownloadItemView( | 277 shelf->AddDownloadView(new DownloadItemView( |
278 download_, shelf, new SavePageModel(this, download_))); | 278 download_, shelf, new SavePageModel(this, download_))); |
279 web_contents_->SetDownloadShelfVisible(true); | 279 web_contents_->SetDownloadShelfVisible(true); |
280 #elif defined(OS_POSIX) | 280 #elif defined(OS_POSIX) |
281 NOTIMPLEMENTED(); | 281 NOTIMPLEMENTED(); |
282 #endif | 282 #endif |
283 | 283 |
284 // Check save type and process the save page job. | 284 // Check save type and process the save page job. |
285 if (save_type_ == SAVE_AS_COMPLETE_HTML) { | 285 if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 | 554 |
555 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, | 555 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, |
556 NewRunnableMethod(file_manager_, | 556 NewRunnableMethod(file_manager_, |
557 &SaveFileManager::RemoveSavedFileFromFileMap, | 557 &SaveFileManager::RemoveSavedFileFromFileMap, |
558 save_ids)); | 558 save_ids)); |
559 | 559 |
560 finished_ = true; | 560 finished_ = true; |
561 wait_state_ = FAILED; | 561 wait_state_ = FAILED; |
562 | 562 |
563 // Inform the DownloadItem we have canceled whole save page job. | 563 // Inform the DownloadItem we have canceled whole save page job. |
564 DCHECK(download_); | |
565 download_->Cancel(false); | 564 download_->Cancel(false); |
566 } | 565 } |
567 | 566 |
568 void SavePackage::CheckFinish() { | 567 void SavePackage::CheckFinish() { |
569 if (in_process_count() || finished_) | 568 if (in_process_count() || finished_) |
570 return; | 569 return; |
571 | 570 |
572 FilePath dir = save_type_ == SAVE_AS_COMPLETE_HTML ? | 571 FilePath dir = save_type_ == SAVE_AS_COMPLETE_HTML ? |
573 saved_main_directory_path_ : | 572 saved_main_directory_path_ : |
574 FilePath(); | 573 FilePath(); |
(...skipping 30 matching lines...) Expand all Loading... |
605 SaveIDList save_ids; | 604 SaveIDList save_ids; |
606 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); | 605 for (SaveUrlItemMap::iterator it = saved_failed_items_.begin(); |
607 it != saved_failed_items_.end(); ++it) | 606 it != saved_failed_items_.end(); ++it) |
608 save_ids.push_back(it->second->save_id()); | 607 save_ids.push_back(it->second->save_id()); |
609 | 608 |
610 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, | 609 file_manager_->GetSaveLoop()->PostTask(FROM_HERE, |
611 NewRunnableMethod(file_manager_, | 610 NewRunnableMethod(file_manager_, |
612 &SaveFileManager::RemoveSavedFileFromFileMap, | 611 &SaveFileManager::RemoveSavedFileFromFileMap, |
613 save_ids)); | 612 save_ids)); |
614 | 613 |
615 DCHECK(download_); | |
616 download_->Finished(all_save_items_count_); | 614 download_->Finished(all_save_items_count_); |
617 } | 615 } |
618 | 616 |
619 // Called for updating end state. | 617 // Called for updating end state. |
620 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 618 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
621 // Because we might have canceled this saving job before, | 619 // Because we might have canceled this saving job before, |
622 // so we might not find corresponding SaveItem. Just ignore it. | 620 // so we might not find corresponding SaveItem. Just ignore it. |
623 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); | 621 SaveItem* save_item = LookupItemInProcessBySaveId(save_id); |
624 if (!save_item) | 622 if (!save_item) |
625 return; | 623 return; |
626 | 624 |
627 // Let SaveItem set end state. | 625 // Let SaveItem set end state. |
628 save_item->Finish(size, is_success); | 626 save_item->Finish(size, is_success); |
629 // Remove the associated save id and SavePackage. | 627 // Remove the associated save id and SavePackage. |
630 file_manager_->RemoveSaveFile(save_id, save_item->url(), this); | 628 file_manager_->RemoveSaveFile(save_id, save_item->url(), this); |
631 | 629 |
632 PutInProgressItemToSavedMap(save_item); | 630 PutInProgressItemToSavedMap(save_item); |
633 | 631 |
634 // Inform the DownloadItem to update UI. | 632 // Inform the DownloadItem to update UI. |
635 DCHECK(download_); | |
636 // We use the received bytes as number of saved files. | 633 // We use the received bytes as number of saved files. |
637 download_->Update(completed_count()); | 634 download_->Update(completed_count()); |
638 | 635 |
639 if (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM && | 636 if (save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM && |
640 save_item->url() == page_url_ && !save_item->received_bytes()) { | 637 save_item->url() == page_url_ && !save_item->received_bytes()) { |
641 // If size of main HTML page is 0, treat it as disk error. | 638 // If size of main HTML page is 0, treat it as disk error. |
642 Cancel(false); | 639 Cancel(false); |
643 return; | 640 return; |
644 } | 641 } |
645 | 642 |
(...skipping 21 matching lines...) Expand all Loading... |
667 NOTREACHED(); // Should not exist! | 664 NOTREACHED(); // Should not exist! |
668 return; | 665 return; |
669 } | 666 } |
670 SaveItem* save_item = it->second; | 667 SaveItem* save_item = it->second; |
671 | 668 |
672 save_item->Finish(0, false); | 669 save_item->Finish(0, false); |
673 | 670 |
674 PutInProgressItemToSavedMap(save_item); | 671 PutInProgressItemToSavedMap(save_item); |
675 | 672 |
676 // Inform the DownloadItem to update UI. | 673 // Inform the DownloadItem to update UI. |
677 DCHECK(download_); | |
678 // We use the received bytes as number of saved files. | 674 // We use the received bytes as number of saved files. |
679 download_->Update(completed_count()); | 675 download_->Update(completed_count()); |
680 | 676 |
681 if (save_type_ == SAVE_AS_ONLY_HTML || | 677 if (save_type_ == SAVE_AS_ONLY_HTML || |
682 save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) { | 678 save_item->save_source() == SaveFileCreateInfo::SAVE_FILE_FROM_DOM) { |
683 // We got error when saving page. Treat it as disk error. | 679 // We got error when saving page. Treat it as disk error. |
684 Cancel(true); | 680 Cancel(true); |
685 } | 681 } |
686 | 682 |
687 if (canceled()) { | 683 if (canceled()) { |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 // Get the directory from preference. | 981 // Get the directory from preference. |
986 StringPrefMember save_file_path; | 982 StringPrefMember save_file_path; |
987 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); | 983 save_file_path.Init(prefs::kSaveFileDefaultDirectory, prefs, NULL); |
988 DCHECK(!(*save_file_path).empty()); | 984 DCHECK(!(*save_file_path).empty()); |
989 | 985 |
990 // Ask user for getting final saving name. | 986 // Ask user for getting final saving name. |
991 std::wstring file_name = name.ToWStringHack(); | 987 std::wstring file_name = name.ToWStringHack(); |
992 // TODO(port): we need a version of ReplaceIllegalCharacters() that takes | 988 // TODO(port): we need a version of ReplaceIllegalCharacters() that takes |
993 // FilePaths. | 989 // FilePaths. |
994 file_util::ReplaceIllegalCharacters(&file_name, L' '); | 990 file_util::ReplaceIllegalCharacters(&file_name, L' '); |
| 991 TrimWhitespace(file_name, TRIM_ALL, &file_name); |
995 FilePath suggest_name = FilePath::FromWStringHack(save_file_path.GetValue()); | 992 FilePath suggest_name = FilePath::FromWStringHack(save_file_path.GetValue()); |
996 suggest_name = suggest_name.Append(FilePath::FromWStringHack(file_name)); | 993 suggest_name = suggest_name.Append(FilePath::FromWStringHack(file_name)); |
997 | 994 |
998 return suggest_name; | 995 return suggest_name; |
999 } | 996 } |
1000 | 997 |
1001 // Static. | 998 // Static. |
1002 bool SavePackage::GetSaveInfo(const FilePath& suggest_name, | 999 bool SavePackage::GetSaveInfo(const FilePath& suggest_name, |
1003 gfx::NativeView container_window, | 1000 gfx::NativeView container_window, |
1004 SavePackageParam* param, | 1001 SavePackageParam* param, |
(...skipping 29 matching lines...) Expand all Loading... |
1034 &main_file_path, g_should_prompt_for_filename); | 1031 &main_file_path, g_should_prompt_for_filename); |
1035 param->saved_main_file_path = FilePath::FromWStringHack(main_file_path); | 1032 param->saved_main_file_path = FilePath::FromWStringHack(main_file_path); |
1036 if (!success) | 1033 if (!success) |
1037 return false; | 1034 return false; |
1038 | 1035 |
1039 // Set save-as type to only-HTML if the contents of current tab can not be | 1036 // Set save-as type to only-HTML if the contents of current tab can not be |
1040 // saved as complete-HTML. | 1037 // saved as complete-HTML. |
1041 index = 1; | 1038 index = 1; |
1042 } | 1039 } |
1043 | 1040 |
1044 DCHECK(download_manager); | |
1045 // Ensure the filename is safe. | 1041 // Ensure the filename is safe. |
1046 download_manager->GenerateSafeFilename(param->current_tab_mime_type, | 1042 download_manager->GenerateSafeFilename(param->current_tab_mime_type, |
1047 ¶m->saved_main_file_path); | 1043 ¶m->saved_main_file_path); |
1048 | 1044 |
1049 // The option index is not zero-based. | 1045 // The option index is not zero-based. |
1050 DCHECK(index > 0 && index < 3); | 1046 DCHECK(index > 0 && index < 3); |
1051 param->dir = param->saved_main_file_path.DirName(); | 1047 param->dir = param->saved_main_file_path.DirName(); |
1052 | 1048 |
1053 StringPrefMember save_file_path; | 1049 StringPrefMember save_file_path; |
1054 save_file_path.Init(prefs::kSaveFileDefaultDirectory, param->prefs, NULL); | 1050 save_file_path.Init(prefs::kSaveFileDefaultDirectory, param->prefs, NULL); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1114 *pure_file_name = | 1110 *pure_file_name = |
1115 pure_file_name->substr(0, available_length); | 1111 pure_file_name->substr(0, available_length); |
1116 return true; | 1112 return true; |
1117 } | 1113 } |
1118 | 1114 |
1119 // Not enough room to even use a shortened |pure_file_name|. | 1115 // Not enough room to even use a shortened |pure_file_name|. |
1120 pure_file_name->clear(); | 1116 pure_file_name->clear(); |
1121 return false; | 1117 return false; |
1122 } | 1118 } |
1123 | 1119 |
OLD | NEW |