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

Side by Side Diff: components/offline_pages/offline_page_model_impl.cc

Issue 2339693002: [Offline pages] Splitting Add and Update methods in OPMStore (Closed)
Patch Set: Addressing feedback Created 4 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
OLDNEW
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 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 398
399 // Make a copy of the cached item and update it. The cached item should only 399 // Make a copy of the cached item and update it. The cached item should only
400 // be updated upon the successful store operation. 400 // be updated upon the successful store operation.
401 OfflinePageItem offline_page_item = iter->second; 401 OfflinePageItem offline_page_item = iter->second;
402 402
403 ReportPageHistogramsAfterAccess(offline_page_item, GetCurrentTime()); 403 ReportPageHistogramsAfterAccess(offline_page_item, GetCurrentTime());
404 404
405 offline_page_item.last_access_time = GetCurrentTime(); 405 offline_page_item.last_access_time = GetCurrentTime();
406 offline_page_item.access_count++; 406 offline_page_item.access_count++;
407 407
408 store_->AddOrUpdateOfflinePage( 408 std::vector<OfflinePageItem> items = { offline_page_item };
409 offline_page_item, 409 store_->UpdateOfflinePages(
410 base::Bind(&OfflinePageModelImpl::OnMarkPageAccesseDone, 410 items, base::Bind(&OfflinePageModelImpl::OnMarkPageAccesseDone,
411 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); 411 weak_ptr_factory_.GetWeakPtr(), offline_page_item));
412 } 412 }
413 413
414 void OfflinePageModelImpl::DeletePagesByOfflineId( 414 void OfflinePageModelImpl::DeletePagesByOfflineId(
415 const std::vector<int64_t>& offline_ids, 415 const std::vector<int64_t>& offline_ids,
416 const DeletePageCallback& callback) { 416 const DeletePageCallback& callback) {
417 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::DoDeletePagesByOfflineId, 417 RunWhenLoaded(base::Bind(&OfflinePageModelImpl::DoDeletePagesByOfflineId,
418 weak_ptr_factory_.GetWeakPtr(), offline_ids, 418 weak_ptr_factory_.GetWeakPtr(), offline_ids,
419 callback)); 419 callback));
420 } 420 }
421 421
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 archive_manager_->GetAllArchives( 694 archive_manager_->GetAllArchives(
695 base::Bind(&OfflinePageModelImpl::CheckMetadataConsistencyForArchivePaths, 695 base::Bind(&OfflinePageModelImpl::CheckMetadataConsistencyForArchivePaths,
696 weak_ptr_factory_.GetWeakPtr())); 696 weak_ptr_factory_.GetWeakPtr()));
697 } 697 }
698 698
699 void OfflinePageModelImpl::ExpirePages( 699 void OfflinePageModelImpl::ExpirePages(
700 const std::vector<int64_t>& offline_ids, 700 const std::vector<int64_t>& offline_ids,
701 const base::Time& expiration_time, 701 const base::Time& expiration_time,
702 const base::Callback<void(bool)>& callback) { 702 const base::Callback<void(bool)>& callback) {
703 std::vector<base::FilePath> paths_to_delete; 703 std::vector<base::FilePath> paths_to_delete;
704 std::vector<OfflinePageItem> items_to_update;
704 for (int64_t offline_id : offline_ids) { 705 for (int64_t offline_id : offline_ids) {
705 auto iter = offline_pages_.find(offline_id); 706 auto iter = offline_pages_.find(offline_id);
706 if (iter == offline_pages_.end()) 707 if (iter == offline_pages_.end())
707 continue; 708 continue;
708 709
709 OfflinePageItem offline_page = iter->second; 710 OfflinePageItem offline_page = iter->second;
710 paths_to_delete.push_back(offline_page.file_path); 711 paths_to_delete.push_back(offline_page.file_path);
711 offline_page.expiration_time = expiration_time; 712 offline_page.expiration_time = expiration_time;
712 713
713 store_->AddOrUpdateOfflinePage( 714 items_to_update.push_back(offline_page);
714 offline_page, base::Bind(&OfflinePageModelImpl::OnExpirePageDone,
715 weak_ptr_factory_.GetWeakPtr(), offline_id,
716 expiration_time));
717 } 715 }
716
717 store_->UpdateOfflinePages(items_to_update,
718 base::Bind(&OfflinePageModelImpl::OnExpirePageDone,
719 weak_ptr_factory_.GetWeakPtr(),
720 items_to_update, expiration_time));
721
718 if (paths_to_delete.empty()) { 722 if (paths_to_delete.empty()) {
719 callback.Run(true); 723 callback.Run(true);
720 return; 724 return;
721 } 725 }
722 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback); 726 archive_manager_->DeleteMultipleArchives(paths_to_delete, callback);
723 } 727 }
724 728
725 void OfflinePageModelImpl::OnExpirePageDone(int64_t offline_id, 729 void OfflinePageModelImpl::OnExpirePageDone(
726 const base::Time& expiration_time, 730 const std::vector<OfflinePageItem>& expired_pages,
727 bool success) { 731 const base::Time& expiration_time,
732 bool success) {
728 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ExpirePage.StoreUpdateResult", success); 733 UMA_HISTOGRAM_BOOLEAN("OfflinePages.ExpirePage.StoreUpdateResult", success);
729 if (!success) 734 if (!success)
730 return; 735 return;
731 const auto& iter = offline_pages_.find(offline_id); 736 for (auto& expired_page : expired_pages) {
732 if (iter != offline_pages_.end()) { 737 const auto& iter = offline_pages_.find(expired_page.offline_id);
738 if (iter == offline_pages_.end())
739 continue;
740
733 iter->second.expiration_time = expiration_time; 741 iter->second.expiration_time = expiration_time;
734 ClientId client_id = iter->second.client_id; 742 ClientId client_id = iter->second.client_id;
735 offline_event_logger_.RecordPageExpired(std::to_string(offline_id)); 743 offline_event_logger_.RecordPageExpired(
744 std::to_string(expired_page.offline_id));
736 base::HistogramBase* histogram = base::Histogram::FactoryGet( 745 base::HistogramBase* histogram = base::Histogram::FactoryGet(
737 AddHistogramSuffix(client_id, "OfflinePages.ExpirePage.PageLifetime"), 746 AddHistogramSuffix(client_id, "OfflinePages.ExpirePage.PageLifetime"),
738 1, base::TimeDelta::FromDays(30).InMinutes(), 50, 747 1, base::TimeDelta::FromDays(30).InMinutes(), 50,
739 base::HistogramBase::kUmaTargetedHistogramFlag); 748 base::HistogramBase::kUmaTargetedHistogramFlag);
740 histogram->Add((expiration_time - iter->second.creation_time).InMinutes()); 749 histogram->Add((expiration_time - iter->second.creation_time).InMinutes());
741 histogram = base::Histogram::FactoryGet( 750 histogram = base::Histogram::FactoryGet(
742 AddHistogramSuffix(client_id, 751 AddHistogramSuffix(client_id,
743 "OfflinePages.ExpirePage.TimeSinceLastAccess"), 752 "OfflinePages.ExpirePage.TimeSinceLastAccess"),
744 1, base::TimeDelta::FromDays(30).InMinutes(), 50, 753 1, base::TimeDelta::FromDays(30).InMinutes(), 50,
745 base::HistogramBase::kUmaTargetedHistogramFlag); 754 base::HistogramBase::kUmaTargetedHistogramFlag);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 800
792 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { 801 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) {
793 SavePageResult result = ToSavePageResult(archiver_result); 802 SavePageResult result = ToSavePageResult(archiver_result);
794 InformSavePageDone(callback, result, client_id, offline_id); 803 InformSavePageDone(callback, result, client_id, offline_id);
795 DeletePendingArchiver(archiver); 804 DeletePendingArchiver(archiver);
796 return; 805 return;
797 } 806 }
798 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, 807 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path,
799 file_size, start_time); 808 file_size, start_time);
800 offline_page_item.title = title; 809 offline_page_item.title = title;
801 store_->AddOrUpdateOfflinePage( 810 store_->AddOfflinePage(offline_page_item,
802 offline_page_item, base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone, 811 base::Bind(&OfflinePageModelImpl::OnAddOfflinePageDone,
803 weak_ptr_factory_.GetWeakPtr(), archiver, 812 weak_ptr_factory_.GetWeakPtr(), archiver,
804 callback, offline_page_item)); 813 callback, offline_page_item));
805 } 814 }
806 815
807 void OfflinePageModelImpl::OnAddOfflinePageDone( 816 void OfflinePageModelImpl::OnAddOfflinePageDone(
808 OfflinePageArchiver* archiver, 817 OfflinePageArchiver* archiver,
809 const SavePageCallback& callback, 818 const SavePageCallback& callback,
810 const OfflinePageItem& offline_page, 819 const OfflinePageItem& offline_page,
811 bool success) { 820 OfflinePageMetadataStore::ItemActionStatus status) {
812 SavePageResult result; 821 SavePageResult result;
813 if (success) { 822 if (status == OfflinePageMetadataStore::SUCCESS) {
814 offline_pages_[offline_page.offline_id] = offline_page; 823 offline_pages_[offline_page.offline_id] = offline_page;
815 result = SavePageResult::SUCCESS; 824 result = SavePageResult::SUCCESS;
816 ReportPageHistogramAfterSave(offline_pages_, offline_page, 825 ReportPageHistogramAfterSave(offline_pages_, offline_page,
817 GetCurrentTime()); 826 GetCurrentTime());
818 offline_event_logger_.RecordPageSaved( 827 offline_event_logger_.RecordPageSaved(
819 offline_page.client_id.name_space, offline_page.url.spec(), 828 offline_page.client_id.name_space, offline_page.url.spec(),
820 std::to_string(offline_page.offline_id)); 829 std::to_string(offline_page.offline_id));
830 } else if (status == OfflinePageMetadataStore::ALREADY_EXISTS) {
831 result = SavePageResult::ALREADY_EXISTS;
821 } else { 832 } else {
822 result = SavePageResult::STORE_FAILURE; 833 result = SavePageResult::STORE_FAILURE;
823 } 834 }
824 InformSavePageDone(callback, result, offline_page.client_id, 835 InformSavePageDone(callback, result, offline_page.client_id,
825 offline_page.offline_id); 836 offline_page.offline_id);
826 if (result == SavePageResult::SUCCESS) { 837 if (result == SavePageResult::SUCCESS) {
827 DeleteExistingPagesWithSameURL(offline_page); 838 DeleteExistingPagesWithSameURL(offline_page);
828 } else { 839 } else {
829 PostClearStorageIfNeededTask(); 840 PostClearStorageIfNeededTask();
830 } 841 }
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 } 1166 }
1156 1167
1157 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task); 1168 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, task);
1158 } 1169 }
1159 1170
1160 base::Time OfflinePageModelImpl::GetCurrentTime() const { 1171 base::Time OfflinePageModelImpl::GetCurrentTime() const {
1161 return testing_clock_ ? testing_clock_->Now() : base::Time::Now(); 1172 return testing_clock_ ? testing_clock_->Now() : base::Time::Now();
1162 } 1173 }
1163 1174
1164 } // namespace offline_pages 1175 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_model_impl.h ('k') | components/offline_pages/offline_page_test_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698