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

Unified Diff: components/offline_pages/offline_page_storage_manager.cc

Issue 1970953002: [Offline Pages] Adding more expiration logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@interface
Patch Set: More comments. Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: components/offline_pages/offline_page_storage_manager.cc
diff --git a/components/offline_pages/offline_page_storage_manager.cc b/components/offline_pages/offline_page_storage_manager.cc
index a4465e833d2fda1c386d2b640f21c462407d2b46..249427d6069ea4b772fd444a940dfc20df53c7b2 100644
--- a/components/offline_pages/offline_page_storage_manager.cc
+++ b/components/offline_pages/offline_page_storage_manager.cc
@@ -4,7 +4,12 @@
#include "components/offline_pages/offline_page_storage_manager.h"
+#include <algorithm>
+
#include "base/bind.h"
+#include "base/time/clock.h"
+#include "base/time/default_clock.h"
+#include "base/time/time.h"
#include "components/offline_pages/client_policy_controller.h"
#include "components/offline_pages/offline_page_client_policy.h"
#include "components/offline_pages/offline_page_item.h"
@@ -18,6 +23,7 @@ OfflinePageStorageManager::OfflinePageStorageManager(
: client_(client),
policy_controller_(policy_controller),
in_progress_(false),
+ clock_(new base::DefaultClock()),
weak_ptr_factory_(this) {}
OfflinePageStorageManager::~OfflinePageStorageManager() {}
@@ -43,12 +49,47 @@ void OfflinePageStorageManager::ClearExpiredPages(
weak_ptr_factory_.GetWeakPtr(), callback, offline_ids.size()));
}
+void OfflinePageStorageManager::SetClockForTesting(
+ std::unique_ptr<base::Clock> clock) {
+ clock_ = std::move(clock);
+}
+
void OfflinePageStorageManager::GetExpiredPageIds(
const MultipleOfflinePageItemResult& pages,
std::vector<int64_t>& offline_ids) {
- for (const auto& page : pages) {
- if (IsPageExpired(page))
- offline_ids.push_back(page.offline_id);
+ base::Time now = clock_->Now();
+
+ // Creating a map from namespace to a vector of page items.
+ // Sort each vector based on last accessed time and all pages after index
+ // min{size(), page_limit} should be expired. And then start iterating
+ // backwards to expire pages.
+ std::map<std::string, std::vector<OfflinePageItem>> pages_map;
+
+ for (const auto& page : pages)
+ pages_map[page.client_id.name_space].push_back(page);
+
+ for (auto& iter : pages_map) {
+ std::string name_space = iter.first;
+ std::vector<OfflinePageItem>& page_list = iter.second;
+
+ LifetimePolicy policy =
+ policy_controller_->GetPolicy(name_space).lifetime_policy;
+
+ std::sort(page_list.begin(), page_list.end(),
+ [](const OfflinePageItem& a, const OfflinePageItem& b) -> bool {
+ return a.last_access_time > b.last_access_time;
+ });
+
+ int page_list_size = page_list.size();
+ int pos = 0;
+ while (pos < page_list_size &&
+ (policy.page_limit == kUnlimitedPages || pos < policy.page_limit) &&
+ !ShouldBeExpired(now, page_list.at(pos))) {
+ pos++;
+ }
+
+ for (int i = pos; i < page_list_size; i++)
+ offline_ids.push_back(page_list.at(i).offline_id);
}
}
@@ -64,8 +105,8 @@ bool OfflinePageStorageManager::ShouldClearPages() {
return !in_progress_;
}
-bool OfflinePageStorageManager::IsPageExpired(const OfflinePageItem& page) {
- base::Time now = base::Time::Now();
+bool OfflinePageStorageManager::ShouldBeExpired(const base::Time& now,
+ const OfflinePageItem& page) {
const LifetimePolicy& policy =
policy_controller_->GetPolicy(page.client_id.name_space).lifetime_policy;
return now - page.last_access_time > policy.expiration_period;

Powered by Google App Engine
This is Rietveld 408576698