| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/offline_pages/core/offline_page_model_impl.h" | 5 #include "components/offline_pages/core/offline_page_model_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 | 333 |
| 334 void OfflinePageModelImpl::RemoveObserver(Observer* observer) { | 334 void OfflinePageModelImpl::RemoveObserver(Observer* observer) { |
| 335 observers_.RemoveObserver(observer); | 335 observers_.RemoveObserver(observer); |
| 336 } | 336 } |
| 337 | 337 |
| 338 void OfflinePageModelImpl::SavePage( | 338 void OfflinePageModelImpl::SavePage( |
| 339 const SavePageParams& save_page_params, | 339 const SavePageParams& save_page_params, |
| 340 std::unique_ptr<OfflinePageArchiver> archiver, | 340 std::unique_ptr<OfflinePageArchiver> archiver, |
| 341 const SavePageCallback& callback) { | 341 const SavePageCallback& callback) { |
| 342 DCHECK(is_loaded_); | 342 DCHECK(is_loaded_); |
| 343 base::FilePath empty_file_path; |
| 343 | 344 |
| 344 // Skip saving the page that is not intended to be saved, like local file | 345 // Skip saving the page that is not intended to be saved, like local file |
| 345 // page. | 346 // page. |
| 346 if (!OfflinePageModel::CanSaveURL(save_page_params.url)) { | 347 if (!OfflinePageModel::CanSaveURL(save_page_params.url)) { |
| 347 InformSavePageDone(callback, SavePageResult::SKIPPED, | 348 InformSavePageDone(callback, SavePageResult::SKIPPED, |
| 348 save_page_params.client_id, kInvalidOfflineId); | 349 save_page_params.client_id, kInvalidOfflineId, |
| 350 empty_file_path); |
| 349 return; | 351 return; |
| 350 } | 352 } |
| 351 | 353 |
| 352 // The web contents is not available if archiver is not created and passed. | 354 // The web contents is not available if archiver is not created and passed. |
| 353 if (!archiver.get()) { | 355 if (!archiver.get()) { |
| 354 InformSavePageDone(callback, SavePageResult::CONTENT_UNAVAILABLE, | 356 InformSavePageDone(callback, SavePageResult::CONTENT_UNAVAILABLE, |
| 355 save_page_params.client_id, kInvalidOfflineId); | 357 save_page_params.client_id, kInvalidOfflineId, |
| 358 empty_file_path); |
| 356 return; | 359 return; |
| 357 } | 360 } |
| 358 | 361 |
| 359 // If we already have an offline id, use it. If not, generate one. | 362 // If we already have an offline id, use it. If not, generate one. |
| 360 int64_t offline_id = save_page_params.proposed_offline_id; | 363 int64_t offline_id = save_page_params.proposed_offline_id; |
| 361 if (offline_id == kInvalidOfflineId) | 364 if (offline_id == kInvalidOfflineId) |
| 362 offline_id = GenerateOfflineId(); | 365 offline_id = GenerateOfflineId(); |
| 363 | 366 |
| 364 OfflinePageArchiver::CreateArchiveParams create_archive_params; | 367 OfflinePageArchiver::CreateArchiveParams create_archive_params; |
| 365 // If the page is being saved in the background, we should try to remove the | 368 // If the page is being saved in the background, we should try to remove the |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 ArchiverResult archiver_result, | 689 ArchiverResult archiver_result, |
| 687 const GURL& url, | 690 const GURL& url, |
| 688 const base::FilePath& file_path, | 691 const base::FilePath& file_path, |
| 689 const base::string16& title, | 692 const base::string16& title, |
| 690 int64_t file_size) { | 693 int64_t file_size) { |
| 691 if (save_page_params.url != url) { | 694 if (save_page_params.url != url) { |
| 692 DVLOG(1) << "Saved URL does not match requested URL."; | 695 DVLOG(1) << "Saved URL does not match requested URL."; |
| 693 // TODO(fgorski): We have created an archive for a wrong URL. It should be | 696 // TODO(fgorski): We have created an archive for a wrong URL. It should be |
| 694 // deleted from here, once archiver has the right functionality. | 697 // deleted from here, once archiver has the right functionality. |
| 695 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, | 698 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED, |
| 696 save_page_params.client_id, offline_id); | 699 save_page_params.client_id, offline_id, file_path); |
| 697 DeletePendingArchiver(archiver); | 700 DeletePendingArchiver(archiver); |
| 698 return; | 701 return; |
| 699 } | 702 } |
| 700 | 703 |
| 701 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { | 704 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { |
| 702 SavePageResult result = ToSavePageResult(archiver_result); | 705 SavePageResult result = ToSavePageResult(archiver_result); |
| 703 InformSavePageDone( | 706 InformSavePageDone(callback, result, save_page_params.client_id, offline_id, |
| 704 callback, result, save_page_params.client_id, offline_id); | 707 file_path); |
| 705 DeletePendingArchiver(archiver); | 708 DeletePendingArchiver(archiver); |
| 706 return; | 709 return; |
| 707 } | 710 } |
| 708 OfflinePageItem offline_page_item(url, offline_id, save_page_params.client_id, | 711 OfflinePageItem offline_page_item(url, offline_id, save_page_params.client_id, |
| 709 file_path, file_size, start_time); | 712 file_path, file_size, start_time); |
| 710 offline_page_item.title = title; | 713 offline_page_item.title = title; |
| 711 offline_page_item.original_url = save_page_params.original_url; | 714 offline_page_item.original_url = save_page_params.original_url; |
| 712 store_->AddOfflinePage(offline_page_item, | 715 store_->AddOfflinePage(offline_page_item, |
| 713 base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone, | 716 base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone, |
| 714 weak_ptr_factory_.GetWeakPtr(), archiver, | 717 weak_ptr_factory_.GetWeakPtr(), archiver, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 732 offline_page.url.spec(), | 735 offline_page.url.spec(), |
| 733 offline_page.offline_id); | 736 offline_page.offline_id); |
| 734 } else if (status == ItemActionStatus::ALREADY_EXISTS) { | 737 } else if (status == ItemActionStatus::ALREADY_EXISTS) { |
| 735 // Remove the orphaned archive. No callback necessary. | 738 // Remove the orphaned archive. No callback necessary. |
| 736 archive_manager_->DeleteArchive(file_path, base::Bind([](bool) {})); | 739 archive_manager_->DeleteArchive(file_path, base::Bind([](bool) {})); |
| 737 result = SavePageResult::ALREADY_EXISTS; | 740 result = SavePageResult::ALREADY_EXISTS; |
| 738 } else { | 741 } else { |
| 739 result = SavePageResult::STORE_FAILURE; | 742 result = SavePageResult::STORE_FAILURE; |
| 740 } | 743 } |
| 741 InformSavePageDone(callback, result, offline_page.client_id, | 744 InformSavePageDone(callback, result, offline_page.client_id, |
| 742 offline_page.offline_id); | 745 offline_page.offline_id, file_path); |
| 743 if (result == SavePageResult::SUCCESS) { | 746 if (result == SavePageResult::SUCCESS) { |
| 744 DeleteExistingPagesWithSameURL(offline_page); | 747 DeleteExistingPagesWithSameURL(offline_page); |
| 745 } else { | 748 } else { |
| 746 PostClearStorageIfNeededTask(false /* delayed */); | 749 PostClearStorageIfNeededTask(false /* delayed */); |
| 747 } | 750 } |
| 748 | 751 |
| 749 DeletePendingArchiver(archiver); | 752 DeletePendingArchiver(archiver); |
| 750 | 753 |
| 751 // We don't want to notify observers if the add failed. | 754 // We don't want to notify observers if the add failed. |
| 752 if (result != SavePageResult::SUCCESS) | 755 if (result != SavePageResult::SUCCESS) |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 847 delayed_task.Run(); | 850 delayed_task.Run(); |
| 848 delayed_tasks_.clear(); | 851 delayed_tasks_.clear(); |
| 849 | 852 |
| 850 // Clear storage. | 853 // Clear storage. |
| 851 PostClearStorageIfNeededTask(true /* delayed */); | 854 PostClearStorageIfNeededTask(true /* delayed */); |
| 852 } | 855 } |
| 853 | 856 |
| 854 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, | 857 void OfflinePageModelImpl::InformSavePageDone(const SavePageCallback& callback, |
| 855 SavePageResult result, | 858 SavePageResult result, |
| 856 const ClientId& client_id, | 859 const ClientId& client_id, |
| 857 int64_t offline_id) { | 860 int64_t offline_id, |
| 861 const base::FilePath& file_path) { |
| 858 ReportSavePageResultHistogramAfterSave(client_id, result); | 862 ReportSavePageResultHistogramAfterSave(client_id, result); |
| 859 archive_manager_->GetStorageStats( | 863 archive_manager_->GetStorageStats( |
| 860 base::Bind(&ReportStorageHistogramsAfterSave)); | 864 base::Bind(&ReportStorageHistogramsAfterSave)); |
| 861 callback.Run(result, offline_id); | 865 callback.Run(result, offline_id, file_path); |
| 862 } | 866 } |
| 863 | 867 |
| 864 void OfflinePageModelImpl::DeleteExistingPagesWithSameURL( | 868 void OfflinePageModelImpl::DeleteExistingPagesWithSameURL( |
| 865 const OfflinePageItem& offline_page) { | 869 const OfflinePageItem& offline_page) { |
| 866 // Remove existing pages generated by the same policy and with same url. | 870 // Remove existing pages generated by the same policy and with same url. |
| 867 size_t pages_allowed = | 871 size_t pages_allowed = |
| 868 policy_controller_->GetPolicy(offline_page.client_id.name_space) | 872 policy_controller_->GetPolicy(offline_page.client_id.name_space) |
| 869 .pages_allowed_per_url; | 873 .pages_allowed_per_url; |
| 870 if (pages_allowed == kUnlimitedPages) | 874 if (pages_allowed == kUnlimitedPages) |
| 871 return; | 875 return; |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1095 } | 1099 } |
| 1096 | 1100 |
| 1097 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1101 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1098 } | 1102 } |
| 1099 | 1103 |
| 1100 base::Time OfflinePageModelImpl::GetCurrentTime() const { | 1104 base::Time OfflinePageModelImpl::GetCurrentTime() const { |
| 1101 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); | 1105 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); |
| 1102 } | 1106 } |
| 1103 | 1107 |
| 1104 } // namespace offline_pages | 1108 } // namespace offline_pages |
| OLD | NEW |