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

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: refactor 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/engagement/site_engagement_eviction_policy.h"
7 #include "chrome/browser/engagement/site_engagement_service.h"
8 #include "content/public/browser/browser_thread.h"
9
10 using GetEvictionOriginTask =
11 SiteEngagementEvictionPolicy::GetEvictionOriginTask;
12
13 namespace {
14
15 const int kExpectedEngagementSites = 200;
16
17 } // namespace
18
19 GetEvictionOriginTask::GetEvictionOriginTask(
20 content::BrowserContext* browser_context,
21 const std::map<GURL, int64>& usage_map,
22 int64 global_quota,
23 const storage::GetEvictionOriginCallback& result_callback)
24 : usage_map_(usage_map),
25 global_quota_(global_quota),
26 total_engagement_points_(0),
27 proxy_(browser_context) {
28 }
29
30 GetEvictionOriginTask::~GetEvictionOriginTask() {
31 }
32
33 void GetEvictionOriginTask::Start() {
34 DCHECK(per_task_callback_.is_null());
35 per_task_callback_ = base::BarrierClosure(
36 2, base::Bind(&GetEvictionOriginTask::CalculateEvictionOriginAndReply,
37 base::Unretained(this)));
38
39 std::vector<GURL> origins;
40 for (const auto& usage : usage_map_)
41 origins.push_back(usage.first);
42
43 proxy_.GetScoresForOrigins(
44 origins, base::Bind(&GetEvictionOriginTask::OnDidGetScoresForOrigin,
45 base::Unretained(this)));
46 proxy_.GetTotalEngagementPoints(
47 base::Bind(&GetEvictionOriginTask::OnDidGetTotalEngagementPoints,
48 base::Unretained(this)));
49 }
50
51 void GetEvictionOriginTask::OnDidGetScoresForOrigin(
52 const std::map<GURL, int>& score_map) {
53 score_map_ = score_map;
54 per_task_callback_.Run();
55 }
56
57 void GetEvictionOriginTask::OnDidGetTotalEngagementPoints(
58 int total_engagement_points) {
59 total_engagement_points_ = total_engagement_points;
60 per_task_callback_.Run();
61 }
62
63 void GetEvictionOriginTask::CalculateEvictionOriginAndReply() {
64 // This heuristic is intended to optimize for two criteria:
65 // - evict the site that the user cares about least
66 // - evict the least number of sites to get under the quota limit
67 //
68 // The heuristic for deciding the next eviction origin calculates a soft
69 // quota for each origin which is the amount the origin should be allowed to
70 // use based on its engagement and the global quota. The origin that most
71 // exceeds its soft quota is chosen.
72 GURL origin_to_evict;
73 int64 max_overuse = std::numeric_limits<int64>::min();
74 for (const auto& usage : usage_map_) {
75 int64 overuse = usage.second - GetSoftQuotaForOrigin(usage.first);
76 if (overuse > max_overuse) {
77 max_overuse = overuse;
78 origin_to_evict = usage.first;
79 }
80 }
81 result_callback_.Run(origin_to_evict);
82
83 delete this;
84 }
85
86 int64 GetEvictionOriginTask::GetSoftQuotaForOrigin(const GURL& origin) {
87 double quota_per_point =
88 global_quota_ /
89 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints,
90 static_cast<double>(total_engagement_points_));
91
92 return score_map_[origin] * quota_per_point;
93 }
94
95 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy(
96 content::BrowserContext* browser_context)
97 : browser_context_(browser_context) {
98 }
99
100 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {
101 }
102
103 void SiteEngagementEvictionPolicy::GetEvictionOrigin(
104 const std::map<GURL, int64>& usage_map,
105 int64 global_quota,
106 const storage::GetEvictionOriginCallback& callback) {
107 GetEvictionOriginTask* task = new GetEvictionOriginTask(
108 browser_context_, usage_map, global_quota, callback);
109 // The task will delete itself upon completion.
110 task->Start();
111 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698