OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/i18n/file_util_icu.h" | 11 #include "base/i18n/file_util_icu.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/stl_util-inl.h" | 14 #include "base/stl_util-inl.h" |
15 #include "base/string_piece.h" | 15 #include "base/string_piece.h" |
16 #include "base/string_split.h" | 16 #include "base/string_split.h" |
17 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "base/task.h" | 19 #include "base/task.h" |
20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
21 #include "chrome/browser/browser_process.h" | 21 #include "chrome/browser/browser_process.h" |
| 22 #include "chrome/browser/download/download_history.h" |
22 #include "chrome/browser/download/download_item.h" | 23 #include "chrome/browser/download/download_item.h" |
23 #include "chrome/browser/download/download_item_model.h" | 24 #include "chrome/browser/download/download_item_model.h" |
24 #include "chrome/browser/download/download_manager.h" | 25 #include "chrome/browser/download/download_manager.h" |
25 #include "chrome/browser/download/download_prefs.h" | 26 #include "chrome/browser/download/download_prefs.h" |
26 #include "chrome/browser/download/download_shelf.h" | 27 #include "chrome/browser/download/download_shelf.h" |
27 #include "chrome/browser/download/download_util.h" | 28 #include "chrome/browser/download/download_util.h" |
28 #include "chrome/browser/download/save_file.h" | 29 #include "chrome/browser/download/save_file.h" |
29 #include "chrome/browser/download/save_file_manager.h" | 30 #include "chrome/browser/download/save_file_manager.h" |
30 #include "chrome/browser/download/save_item.h" | 31 #include "chrome/browser/download/save_item.h" |
31 #include "chrome/browser/net/url_fixer_upper.h" | 32 #include "chrome/browser/net/url_fixer_upper.h" |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 | 307 |
307 // Initialize the request context and resource dispatcher. | 308 // Initialize the request context and resource dispatcher. |
308 Profile* profile = tab_contents()->profile(); | 309 Profile* profile = tab_contents()->profile(); |
309 if (!profile) { | 310 if (!profile) { |
310 NOTREACHED(); | 311 NOTREACHED(); |
311 return false; | 312 return false; |
312 } | 313 } |
313 | 314 |
314 request_context_getter_ = profile->GetRequestContext(); | 315 request_context_getter_ = profile->GetRequestContext(); |
315 | 316 |
316 // Create the fake DownloadItem and display the view. | 317 // Create the fake DownloadItem and add it to the download history. |
317 DownloadManager* download_manager = | 318 CreateDownloadItem(saved_main_file_path_, |
318 tab_contents()->profile()->GetDownloadManager(); | 319 page_url_, |
319 download_ = new DownloadItem(download_manager, | 320 profile->IsOffTheRecord()); |
320 saved_main_file_path_, | |
321 page_url_, | |
322 profile->IsOffTheRecord()); | |
323 | |
324 // Transfer the ownership to the download manager. We need the DownloadItem | |
325 // to be alive as long as the Profile is alive. | |
326 download_manager->SavePageAsDownloadStarted(download_); | |
327 | 321 |
328 tab_contents()->OnStartDownload(download_); | 322 tab_contents()->OnStartDownload(download_); |
329 | 323 |
330 // Check save type and process the save page job. | 324 // Check save type and process the save page job. |
331 if (save_type_ == SAVE_AS_COMPLETE_HTML) { | 325 if (save_type_ == SAVE_AS_COMPLETE_HTML) { |
332 // Get directory | 326 // Get directory |
333 DCHECK(!saved_main_directory_path_.empty()); | 327 DCHECK(!saved_main_directory_path_.empty()); |
334 GetAllSavableResourceLinksForCurrentPage(); | 328 GetAllSavableResourceLinksForCurrentPage(); |
335 } else { | 329 } else { |
336 wait_state_ = NET_FILES; | 330 wait_state_ = NET_FILES; |
337 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? | 331 SaveFileCreateInfo::SaveFileSource save_source = page_url_.SchemeIsFile() ? |
338 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : | 332 SaveFileCreateInfo::SAVE_FILE_FROM_FILE : |
339 SaveFileCreateInfo::SAVE_FILE_FROM_NET; | 333 SaveFileCreateInfo::SAVE_FILE_FROM_NET; |
340 SaveItem* save_item = new SaveItem(page_url_, | 334 SaveItem* save_item = new SaveItem(page_url_, |
341 GURL(), | 335 GURL(), |
342 this, | 336 this, |
343 save_source); | 337 save_source); |
344 // Add this item to waiting list. | 338 // Add this item to waiting list. |
345 waiting_item_queue_.push(save_item); | 339 waiting_item_queue_.push(save_item); |
346 all_save_items_count_ = 1; | 340 all_save_items_count_ = 1; |
347 download_->set_total_bytes(1); | 341 download_->set_total_bytes(1); |
348 | 342 |
349 DoSavingProcess(); | 343 DoSavingProcess(); |
350 } | 344 } |
351 | 345 |
352 return true; | 346 return true; |
353 } | 347 } |
354 | 348 |
| 349 void SavePackage::OnDownloadEntryAdded(DownloadCreateInfo info, |
| 350 int64 db_handle) { |
| 351 DownloadManager* download_manager = |
| 352 tab_contents()->profile()->GetDownloadManager(); |
| 353 |
| 354 download_manager->AddDownloadItemToHistory(download_, db_handle); |
| 355 } |
| 356 |
| 357 void SavePackage::CreateDownloadItem(const FilePath& path, |
| 358 const GURL& url, |
| 359 bool is_otr) { |
| 360 DownloadManager* download_manager = |
| 361 tab_contents()->profile()->GetDownloadManager(); |
| 362 |
| 363 download_ = new DownloadItem(download_manager, path, url, is_otr); |
| 364 |
| 365 // Transfer the ownership to the download manager. We need the DownloadItem |
| 366 // to be alive as long as the Profile is alive. |
| 367 download_manager->SavePageAsDownloadStarted(download_); |
| 368 |
| 369 // Copy over the fields used by the history service. |
| 370 DownloadCreateInfo info(download_->full_path(), |
| 371 download_->url(), |
| 372 download_->start_time(), |
| 373 0, 0, |
| 374 download_->state(), |
| 375 download_->id(), |
| 376 false); |
| 377 |
| 378 // Add entry to the history service. |
| 379 DownloadHistory* download_history = download_manager->download_history(); |
| 380 download_history->AddEntry(info, download_, |
| 381 NewCallback(this, &SavePackage::OnDownloadEntryAdded)); |
| 382 } |
| 383 |
355 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further | 384 // On POSIX, the length of |pure_file_name| + |file_name_ext| is further |
356 // restricted by NAME_MAX. The maximum allowed path looks like: | 385 // restricted by NAME_MAX. The maximum allowed path looks like: |
357 // '/path/to/save_dir' + '/' + NAME_MAX. | 386 // '/path/to/save_dir' + '/' + NAME_MAX. |
358 uint32 SavePackage::GetMaxPathLengthForDirectory(const FilePath& base_dir) { | 387 uint32 SavePackage::GetMaxPathLengthForDirectory(const FilePath& base_dir) { |
359 #if defined(OS_POSIX) | 388 #if defined(OS_POSIX) |
360 return std::min(kMaxFilePathLength, | 389 return std::min(kMaxFilePathLength, |
361 static_cast<uint32>(base_dir.value().length()) + | 390 static_cast<uint32>(base_dir.value().length()) + |
362 NAME_MAX + 1); | 391 NAME_MAX + 1); |
363 #else | 392 #else |
364 return kMaxFilePathLength; | 393 return kMaxFilePathLength; |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 BrowserThread::FILE, FROM_HERE, | 712 BrowserThread::FILE, FROM_HERE, |
684 NewRunnableMethod(file_manager_, | 713 NewRunnableMethod(file_manager_, |
685 &SaveFileManager::RemoveSavedFileFromFileMap, | 714 &SaveFileManager::RemoveSavedFileFromFileMap, |
686 save_ids)); | 715 save_ids)); |
687 | 716 |
688 finished_ = true; | 717 finished_ = true; |
689 wait_state_ = FAILED; | 718 wait_state_ = FAILED; |
690 | 719 |
691 // Inform the DownloadItem we have canceled whole save page job. | 720 // Inform the DownloadItem we have canceled whole save page job. |
692 download_->Cancel(false); | 721 download_->Cancel(false); |
| 722 DownloadManager* download_manager = |
| 723 tab_contents()->profile()->GetDownloadManager(); |
| 724 download_manager->download_history()->UpdateEntry(download_); |
693 } | 725 } |
694 | 726 |
695 void SavePackage::CheckFinish() { | 727 void SavePackage::CheckFinish() { |
696 if (in_process_count() || finished_) | 728 if (in_process_count() || finished_) |
697 return; | 729 return; |
698 | 730 |
699 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && | 731 FilePath dir = (save_type_ == SAVE_AS_COMPLETE_HTML && |
700 saved_success_items_.size() > 1) ? | 732 saved_success_items_.size() > 1) ? |
701 saved_main_directory_path_ : FilePath(); | 733 saved_main_directory_path_ : FilePath(); |
702 | 734 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 save_ids.push_back(it->second->save_id()); | 769 save_ids.push_back(it->second->save_id()); |
738 | 770 |
739 BrowserThread::PostTask( | 771 BrowserThread::PostTask( |
740 BrowserThread::FILE, FROM_HERE, | 772 BrowserThread::FILE, FROM_HERE, |
741 NewRunnableMethod(file_manager_, | 773 NewRunnableMethod(file_manager_, |
742 &SaveFileManager::RemoveSavedFileFromFileMap, | 774 &SaveFileManager::RemoveSavedFileFromFileMap, |
743 save_ids)); | 775 save_ids)); |
744 | 776 |
745 download_->OnAllDataSaved(all_save_items_count_); | 777 download_->OnAllDataSaved(all_save_items_count_); |
746 download_->MarkAsComplete(); | 778 download_->MarkAsComplete(); |
| 779 |
747 // Notify download observers that we are complete (the call | 780 // Notify download observers that we are complete (the call |
748 // to OnReadyToFinish() set the state to complete but did not notify). | 781 // to OnReadyToFinish() set the state to complete but did not notify). |
749 download_->UpdateObservers(); | 782 download_->UpdateObservers(); |
750 | 783 |
| 784 // Update the download history. |
| 785 DownloadManager* download_manager = |
| 786 tab_contents()->profile()->GetDownloadManager(); |
| 787 download_manager->download_history()->UpdateEntry(download_); |
| 788 |
751 NotificationService::current()->Notify( | 789 NotificationService::current()->Notify( |
752 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 790 NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
753 Source<SavePackage>(this), | 791 Source<SavePackage>(this), |
754 Details<GURL>(&page_url_)); | 792 Details<GURL>(&page_url_)); |
755 } | 793 } |
756 | 794 |
757 // Called for updating end state. | 795 // Called for updating end state. |
758 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { | 796 void SavePackage::SaveFinished(int32 save_id, int64 size, bool is_success) { |
759 // Because we might have canceled this saving job before, | 797 // Because we might have canceled this saving job before, |
760 // so we might not find corresponding SaveItem. Just ignore it. | 798 // so we might not find corresponding SaveItem. Just ignore it. |
(...skipping 695 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1456 } | 1494 } |
1457 | 1495 |
1458 // SelectFileDialog::Listener interface. | 1496 // SelectFileDialog::Listener interface. |
1459 void SavePackage::FileSelected(const FilePath& path, | 1497 void SavePackage::FileSelected(const FilePath& path, |
1460 int index, void* params) { | 1498 int index, void* params) { |
1461 ContinueSave(path, index); | 1499 ContinueSave(path, index); |
1462 } | 1500 } |
1463 | 1501 |
1464 void SavePackage::FileSelectionCanceled(void* params) { | 1502 void SavePackage::FileSelectionCanceled(void* params) { |
1465 } | 1503 } |
OLD | NEW |