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

Unified Diff: chrome/browser/engagement/site_engagement_eviction_policy.cc

Issue 1221523003: Add a SiteEngagementEvictionPolicy. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@get_total_engagement_points
Patch Set: Extract SiteEngagementScoreProvider interface Created 5 years, 5 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: chrome/browser/engagement/site_engagement_eviction_policy.cc
diff --git a/chrome/browser/engagement/site_engagement_eviction_policy.cc b/chrome/browser/engagement/site_engagement_eviction_policy.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bcc40cfbc5f47238d85ef70462f41dd78d75d518
--- /dev/null
+++ b/chrome/browser/engagement/site_engagement_eviction_policy.cc
@@ -0,0 +1,107 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/barrier_closure.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/engagement/site_engagement_eviction_policy.h"
+#include "chrome/browser/engagement/site_engagement_service.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_manager.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace {
+
+const int kExpectedEngagementSites = 200;
+
+// Gets the quota that an origin deserves based on its site engagement.
+int64 GetSoftQuotaForOrigin(const GURL& origin,
+ int score,
+ int total_engagement_points,
+ int64 global_quota) {
+ double quota_per_point =
+ global_quota /
+ std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints,
+ static_cast<double>(total_engagement_points));
+
+ return score * quota_per_point;
+}
+
+void ReplyWithEvictionOrigin(const storage::GetEvictionOriginCallback& callback,
+ const GURL& origin_to_evict) {
+ callback.Run(origin_to_evict);
+}
+
+GURL DoCalculateEvictionOrigin(SiteEngagementScoreProvider* score_provider,
+ const std::map<GURL, int64>& usage_map,
+ int64 global_quota) {
+ // This heuristic is intended to optimize for two criteria:
+ // - evict the site that the user cares about least
+ // - evict the least number of sites to get under the quota limit
+ //
+ // The heuristic for deciding the next eviction origin calculates a soft
michaeln 2015/07/23 02:22:32 It's very hard to evaluate the specifics of this h
calamity 2015/07/28 01:07:59 Here's the doc I've been working from. https://do
michaeln 2015/07/29 00:46:45 Thnx for the ptr to the doc.
+ // quota for each origin which is the amount the origin should be allowed to
+ // use based on its engagement and the global quota. The origin that most
+ // exceeds its soft quota is chosen.
michaeln 2015/07/23 02:22:32 An input missing from this heuristic is whether th
calamity 2015/07/28 01:07:59 Good point. I think we'll look at this in a future
michaeln 2015/07/29 00:46:45 ok, how about we also file a bug about this too, i
+ GURL origin_to_evict;
+ int64 max_overuse = std::numeric_limits<int64>::min();
+ int total_engagement_points = score_provider->GetTotalEngagementPoints();
+
+ for (const auto& usage : usage_map) {
+ // |overuse| can be negative if the soft quota exceeds the usage.
+ int64 overuse =
+ usage.second - GetSoftQuotaForOrigin(
+ usage.first, score_provider->GetScore(usage.first),
+ total_engagement_points, global_quota);
+ if (overuse > max_overuse) {
+ max_overuse = overuse;
+ origin_to_evict = usage.first;
+ }
+ }
+ return origin_to_evict;
+}
+
+GURL GetSiteEngagementEvictionOriginOnUIThread(
+ content::BrowserContext* browser_context,
michaeln 2015/07/23 02:22:32 I don't think this is necessarily a problem but wo
calamity 2015/07/28 01:07:59 Acknowledged.
+ const std::map<GURL, int64>& usage_map,
+ int64 global_quota) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ Profile* profile = Profile::FromBrowserContext(browser_context);
+ SiteEngagementService* service =
+ g_browser_process->profile_manager()->IsValidProfile(profile)
+ ? SiteEngagementService::Get(profile)
+ : nullptr;
+ if (!service)
+ return GURL();
+
+ return DoCalculateEvictionOrigin(service, usage_map, global_quota);
+}
+
+} // namespace
+
+SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy(
+ content::BrowserContext* browser_context)
+ : browser_context_(browser_context) {
+}
+
+SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {
+}
+
+void SiteEngagementEvictionPolicy::GetEvictionOrigin(
michaeln 2015/07/23 02:22:32 I think this needs to be informed by the SpecialSt
calamity 2015/07/28 01:07:59 Good point.
+ const std::map<GURL, int64>& usage_map,
+ int64 global_quota,
+ const storage::GetEvictionOriginCallback& callback) {
+ content::BrowserThread::PostTaskAndReplyWithResult(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, browser_context_,
+ usage_map, global_quota),
+ base::Bind(&ReplyWithEvictionOrigin, callback));
+}
+
+// static
+GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin(
+ SiteEngagementScoreProvider* score_provider,
+ const std::map<GURL, int64>& usage_map,
+ int64 global_quota) {
+ return DoCalculateEvictionOrigin(score_provider, usage_map, global_quota);
+}

Powered by Google App Engine
This is Rietveld 408576698