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

Side by Side 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: fix test 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/barrier_closure.h"
6 #include "chrome/browser/browser_process.h"
7 #include "chrome/browser/engagement/site_engagement_eviction_policy.h"
8 #include "chrome/browser/engagement/site_engagement_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "content/public/browser/browser_thread.h"
12
13 namespace {
14
15 const int kExpectedEngagementSites = 200;
16
17 // Gets the quota that an origin deserves based on its site engagement.
18 int64 GetSoftQuotaForOrigin(const GURL& origin,
19 int score,
20 int total_engagement_points,
21 int64 global_quota) {
22 double quota_per_point =
23 global_quota /
24 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints,
25 static_cast<double>(total_engagement_points));
26
27 return score * quota_per_point;
28 }
29
30 void ReplyWithEvictionOrigin(const storage::GetEvictionOriginCallback& callback,
31 const GURL& origin_to_evict) {
32 callback.Run(origin_to_evict);
33 }
34
35 } // namespace
36
37 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy(
38 content::BrowserContext* browser_context)
39 : browser_context_(browser_context) {
40 }
41
42 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {
43 }
44
45 void SiteEngagementEvictionPolicy::GetEvictionOrigin(
46 const std::map<GURL, int64>& usage_map,
47 int64 global_quota,
48 const storage::GetEvictionOriginCallback& callback) {
49 content::BrowserThread::PostTaskAndReplyWithResult(
50 content::BrowserThread::UI, FROM_HERE,
51 base::Bind(
52 &GetSiteEngagementEvictionOriginOnUIThread,
53 browser_context_, usage_map, global_quota),
54 base::Bind(&ReplyWithEvictionOrigin, callback));
michaeln 2015/07/23 02:22:32 would it work to use 'callback' here directly with
calamity 2015/07/28 01:07:59 Done.
55 }
56
57 // static
58 GURL SiteEngagementEvictionPolicy::GetSiteEngagementEvictionOriginOnUIThread(
raymes 2015/07/21 09:22:20 nit: I think we can make this a standalone functio
calamity 2015/07/22 01:57:38 It won't be able to see the private static method
59 content::BrowserContext* browser_context,
60 const std::map<GURL, int64>& usage_map,
61 int64 global_quota) {
62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
63 Profile* profile = Profile::FromBrowserContext(browser_context);
64 SiteEngagementService* service =
65 g_browser_process->profile_manager()->IsValidProfile(profile) ?
66 SiteEngagementService::Get(profile) : nullptr;
67 if (!service)
68 return GURL();
69
70 return SiteEngagementEvictionPolicy::CalculateEvictionOrigin(
71 service, usage_map, global_quota);
72 }
73
74 // static
75 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin(
76 SiteEngagementService* service,
77 const std::map<GURL, int64>& usage_map,
78 int64 global_quota) {
79 // This heuristic is intended to optimize for two criteria:
80 // - evict the site that the user cares about least
81 // - evict the least number of sites to get under the quota limit
82 //
83 // The heuristic for deciding the next eviction origin calculates a soft
84 // quota for each origin which is the amount the origin should be allowed to
85 // use based on its engagement and the global quota. The origin that most
86 // exceeds its soft quota is chosen.
87 GURL origin_to_evict;
88 int64 max_overuse = std::numeric_limits<int64>::min();
89 int total_engagement_points = service->GetTotalEngagementPoints();
90
91 for (const auto& usage : usage_map) {
92 // |overuse| can be negative if the soft quota exceeds the usage.
93 int64 overuse =
94 usage.second - GetSoftQuotaForOrigin(usage.first,
95 service->GetScore(usage.first),
96 total_engagement_points,
97 global_quota);
98 if (overuse > max_overuse) {
99 max_overuse = overuse;
100 origin_to_evict = usage.first;
101 }
102 }
103 return origin_to_evict;
104 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698