Chromium Code Reviews| 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/offline_page_model_impl.h" | 5 #include "components/offline_pages/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 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 | 325 |
| 326 // Make a copy of the cached item and update it. The cached item should only | 326 // Make a copy of the cached item and update it. The cached item should only |
| 327 // be updated upon the successful store operation. | 327 // be updated upon the successful store operation. |
| 328 OfflinePageItem offline_page_item = iter->second; | 328 OfflinePageItem offline_page_item = iter->second; |
| 329 | 329 |
| 330 ReportPageHistogramsAfterAccess(offline_page_item, GetCurrentTime()); | 330 ReportPageHistogramsAfterAccess(offline_page_item, GetCurrentTime()); |
| 331 | 331 |
| 332 offline_page_item.last_access_time = GetCurrentTime(); | 332 offline_page_item.last_access_time = GetCurrentTime(); |
| 333 offline_page_item.access_count++; | 333 offline_page_item.access_count++; |
| 334 | 334 |
| 335 store_->AddOrUpdateOfflinePage( | 335 std::vector<OfflinePageItem> items; |
|
dewittj
2016/09/14 00:24:11
nit:
std::vector<OfflinePageItem> items = { offlin
fgorski
2016/09/14 03:54:22
Done.
| |
| 336 offline_page_item, | 336 items.push_back(offline_page_item); |
| 337 base::Bind(&OfflinePageModelImpl::OnMarkPageAccesseDone, | 337 store_->UpdateOfflinePages( |
| 338 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | 338 items, base::Bind(&OfflinePageModelImpl::OnMarkPageAccesseDone, |
| 339 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | |
| 339 } | 340 } |
| 340 | 341 |
| 341 void OfflinePageModelImpl::DeletePagesByOfflineId( | 342 void OfflinePageModelImpl::DeletePagesByOfflineId( |
| 342 const std::vector<int64_t>& offline_ids, | 343 const std::vector<int64_t>& offline_ids, |
| 343 const DeletePageCallback& callback) { | 344 const DeletePageCallback& callback) { |
| 344 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::DoDeletePagesByOfflineId, | 345 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::DoDeletePagesByOfflineId, |
| 345 weak_ptr_factory_.GetWeakPtr(), offline_ids, | 346 weak_ptr_factory_.GetWeakPtr(), offline_ids, |
| 346 callback)); | 347 callback)); |
| 347 } | 348 } |
| 348 | 349 |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 621 archive_manager_->GetAllArchives( | 622 archive_manager_->GetAllArchives( |
| 622 base::Bind(&OfflinePageModelImpl::CheckMetadataConsistencyForArchivePaths, | 623 base::Bind(&OfflinePageModelImpl::CheckMetadataConsistencyForArchivePaths, |
| 623 weak_ptr_factory_.GetWeakPtr())); | 624 weak_ptr_factory_.GetWeakPtr())); |
| 624 } | 625 } |
| 625 | 626 |
| 626 void OfflinePageModelImpl::ExpirePages( | 627 void OfflinePageModelImpl::ExpirePages( |
| 627 const std::vector<int64_t>& offline_ids, | 628 const std::vector<int64_t>& offline_ids, |
| 628 const base::Time& expiration_time, | 629 const base::Time& expiration_time, |
| 629 const base::Callback<void(bool)>& callback) { | 630 const base::Callback<void(bool)>& callback) { |
| 630 std::vector<base::FilePath> paths_to_delete; | 631 std::vector<base::FilePath> paths_to_delete; |
| 632 std::vector<OfflinePageItem> items_to_update; | |
| 631 for (int64_t offline_id : offline_ids) { | 633 for (int64_t offline_id : offline_ids) { |
| 632 auto iter = offline_pages_.find(offline_id); | 634 auto iter = offline_pages_.find(offline_id); |
| 633 if (iter == offline_pages_.end()) | 635 if (iter == offline_pages_.end()) |
| 634 continue; | 636 continue; |
| 635 | 637 |
| 636 OfflinePageItem offline_page = iter->second; | 638 OfflinePageItem offline_page = iter->second; |
| 637 paths_to_delete.push_back(offline_page.file_path); | 639 paths_to_delete.push_back(offline_page.file_path); |
| 638 offline_page.expiration_time = expiration_time; | 640 offline_page.expiration_time = expiration_time; |
| 639 | 641 |
| 640 store_->AddOrUpdateOfflinePage( | 642 items_to_update.push_back(offline_page); |
| 641 offline_page, base::Bind(&OfflinePageModelImpl::OnExpirePageDone, | |
| 642 weak_ptr_factory_.GetWeakPtr(), offline_id, | |
| 643 expiration_time)); | |
| 644 } | 643 } |
| 644 | |
| 645 store_->UpdateOfflinePages(items_to_update, | |
| 646 base::Bind(&OfflinePageModelImpl::OnExpirePageDone, | |
| 647 weak_ptr_factory_.GetWeakPtr(), | |
| 648 items_to_update, expiration_time)); | |
| 649 | |
| 645 if (paths_to_delete.empty()) { | 650 if (paths_to_delete.empty()) { |
| 646 callback.Run(true); | 651 callback.Run(true); |
| 647 return; | 652 return; |
| 648 } | 653 } |
| 649 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback); | 654 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback); |
| 650 } | 655 } |
| 651 | 656 |
| 652 void OfflinePageModelImpl::OnExpirePageDone(int64_t offline_id, | 657 void OfflinePageModelImpl::OnExpirePageDone( |
| 653 const base::Time& expiration_time, | 658 const std::vector<OfflinePageItem>& expired_pages, |
| 654 bool success) { | 659 const base::Time& expiration_time, |
| 660 bool success) { | |
| 655 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ExpirePage.StoreUpdateResult", success); | 661 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ExpirePage.StoreUpdateResult", success); |
| 656 if (!success) | 662 if (!success) |
| 657 return; | 663 return; |
| 658 const auto& iter = offline_pages_.find(offline_id); | 664 for (auto& expired_page : expired_pages) { |
| 659 if (iter != offline_pages_.end()) { | 665 const auto& iter = offline_pages_.find(expired_page.offline_id); |
| 666 if (iter == offline_pages_.end()) | |
| 667 continue; | |
| 668 | |
| 660 iter->second.expiration_time = expiration_time; | 669 iter->second.expiration_time = expiration_time; |
| 661 ClientId client_id = iter->second.client_id; | 670 ClientId client_id = iter->second.client_id; |
| 662 offline_event_logger_.RecordPageExpired(std::to_string(offline_id)); | 671 offline_event_logger_.RecordPageExpired( |
| 672 std::to_string(expired_page.offline_id)); | |
| 663 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 673 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| 664 AddHistogramSuffix(client_id, "OfflinePages.ExpirePage.PageLifetime"), | 674 AddHistogramSuffix(client_id, "OfflinePages.ExpirePage.PageLifetime"), |
| 665 1, base::TimeDelta::FromDays(30).InMinutes(), 50, | 675 1, base::TimeDelta::FromDays(30).InMinutes(), 50, |
| 666 base::HistogramBase::kUmaTargetedHistogramFlag); | 676 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 667 histogram->Add((expiration_time - iter->second.creation_time).InMinutes()); | 677 histogram->Add((expiration_time - iter->second.creation_time).InMinutes()); |
| 668 histogram = base::Histogram::FactoryGet( | 678 histogram = base::Histogram::FactoryGet( |
| 669 AddHistogramSuffix(client_id, | 679 AddHistogramSuffix(client_id, |
| 670 "OfflinePages.ExpirePage.TimeSinceLastAccess"), | 680 "OfflinePages.ExpirePage.TimeSinceLastAccess"), |
| 671 1, base::TimeDelta::FromDays(30).InMinutes(), 50, | 681 1, base::TimeDelta::FromDays(30).InMinutes(), 50, |
| 672 base::HistogramBase::kUmaTargetedHistogramFlag); | 682 base::HistogramBase::kUmaTargetedHistogramFlag); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 718 | 728 |
| 719 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { | 729 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { |
| 720 SavePageResult result = ToSavePageResult(archiver_result); | 730 SavePageResult result = ToSavePageResult(archiver_result); |
| 721 InformSavePageDone(callback, result, client_id, offline_id); | 731 InformSavePageDone(callback, result, client_id, offline_id); |
| 722 DeletePendingArchiver(archiver); | 732 DeletePendingArchiver(archiver); |
| 723 return; | 733 return; |
| 724 } | 734 } |
| 725 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, | 735 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, |
| 726 file_size, start_time); | 736 file_size, start_time); |
| 727 offline_page_item.title = title; | 737 offline_page_item.title = title; |
| 728 store_->AddOrUpdateOfflinePage( | 738 store_->AddOfflinePage(offline_page_item, |
| 729 offline_page_item, base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone, | 739 base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone, |
| 730 weak_ptr_factory_.GetWeakPtr(), archiver, | 740 weak_ptr_factory_.GetWeakPtr(), archiver, |
| 731 callback, offline_page_item)); | 741 callback, offline_page_item)); |
| 732 } | 742 } |
| 733 | 743 |
| 734 void OfflinePageModelImpl::OnAddOfflinePageDone( | 744 void OfflinePageModelImpl::OnAddOfflinePageDone( |
| 735 OfflinePageArchiver* archiver, | 745 OfflinePageArchiver* archiver, |
| 736 const SavePageCallback& callback, | 746 const SavePageCallback& callback, |
| 737 const OfflinePageItem& offline_page, | 747 const OfflinePageItem& offline_page, |
| 738 bool success) { | 748 OfflinePageMetadataStore::ItemActionStatus status) { |
| 739 SavePageResult result; | 749 SavePageResult result; |
| 740 if (success) { | 750 if (status == OfflinePageMetadataStore::SUCCESS) { |
| 741 offline_pages_[offline_page.offline_id] = offline_page; | 751 offline_pages_[offline_page.offline_id] = offline_page; |
| 742 result = SavePageResult::SUCCESS; | 752 result = SavePageResult::SUCCESS; |
| 743 ReportPageHistogramAfterSave(offline_page, GetCurrentTime()); | 753 ReportPageHistogramAfterSave(offline_page, GetCurrentTime()); |
| 744 offline_event_logger_.RecordPageSaved( | 754 offline_event_logger_.RecordPageSaved( |
| 745 offline_page.client_id.name_space, offline_page.url.spec(), | 755 offline_page.client_id.name_space, offline_page.url.spec(), |
| 746 std::to_string(offline_page.offline_id)); | 756 std::to_string(offline_page.offline_id)); |
| 747 } else { | 757 } else { |
| 748 result = SavePageResult::STORE_FAILURE; | 758 result = status == OfflinePageMetadataStore::ALREADY_EXISTS |
|
dewittj
2016/09/14 00:24:11
nit: make an else if or a switch statement and rem
fgorski
2016/09/14 03:54:22
Done.
| |
| 759 ? SavePageResult::ALREADY_EXISTS | |
| 760 : SavePageResult::STORE_FAILURE; | |
| 749 } | 761 } |
| 750 InformSavePageDone(callback, result, offline_page.client_id, | 762 InformSavePageDone(callback, result, offline_page.client_id, |
| 751 offline_page.offline_id); | 763 offline_page.offline_id); |
| 752 if (result == SavePageResult::SUCCESS) { | 764 if (result == SavePageResult::SUCCESS) { |
| 753 DeleteExistingPagesWithSameURL(offline_page); | 765 DeleteExistingPagesWithSameURL(offline_page); |
| 754 } else { | 766 } else { |
| 755 PostClearStorageIfNeededTask(); | 767 PostClearStorageIfNeededTask(); |
| 756 } | 768 } |
| 757 | 769 |
| 758 DeletePendingArchiver(archiver); | 770 DeletePendingArchiver(archiver); |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1081 } | 1093 } |
| 1082 | 1094 |
| 1083 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); | 1095 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); |
| 1084 } | 1096 } |
| 1085 | 1097 |
| 1086 base::Time OfflinePageModelImpl::GetCurrentTime() const { | 1098 base::Time OfflinePageModelImpl::GetCurrentTime() const { |
| 1087 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); | 1099 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); |
| 1088 } | 1100 } |
| 1089 | 1101 |
| 1090 } // namespace offline_pages | 1102 } // namespace offline_pages |
| OLD | NEW |