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

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: address_comments_and_rebase 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;
michaeln 2015/07/29 00:46:45 Upon a first visit to a site, score is expected to
calamity 2015/07/29 05:00:36 Yes for now, but this may change. Even if it ends
28 }
29
30 GURL DoCalculateEvictionOrigin(
31 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy,
michaeln 2015/07/29 00:46:46 ditto
calamity 2015/07/29 05:00:36 Done.
32 SiteEngagementScoreProvider* score_provider,
33 const std::map<GURL, int64>& usage_map,
34 int64 global_quota) {
35 // TODO(calamity): Integrate storage access frequency as an input to this
36 // heuristic.
37
38 // This heuristic is intended to optimize for two criteria:
39 // - evict the site that the user cares about least
40 // - evict the least number of sites to get under the quota limit
41 //
42 // The heuristic for deciding the next eviction origin calculates a soft
43 // quota for each origin which is the amount the origin should be allowed to
44 // use based on its engagement and the global quota. The origin that most
45 // exceeds its soft quota is chosen.
46 GURL origin_to_evict;
47 int64 max_overuse = std::numeric_limits<int64>::min();
48 int total_engagement_points = score_provider->GetTotalEngagementPoints();
49
50 for (const auto& usage : usage_map) {
51 GURL origin = usage.first;
52 if (special_storage_policy &&
53 (special_storage_policy->IsStorageUnlimited(origin) ||
54 special_storage_policy->IsStorageDurable(origin))) {
55 continue;
56 }
57
58 // |overuse| can be negative if the soft quota exceeds the usage.
59 int64 overuse =
60 usage.second - GetSoftQuotaForOrigin(
61 origin, score_provider->GetScore(origin),
62 total_engagement_points, global_quota);
63 if (overuse > max_overuse) {
64 max_overuse = overuse;
65 origin_to_evict = origin;
66 }
67 }
68 return origin_to_evict;
69 }
70
71 GURL GetSiteEngagementEvictionOriginOnUIThread(
72 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy,
michaeln 2015/07/29 00:46:46 ditto const ref
calamity 2015/07/29 05:00:36 Done.
73 content::BrowserContext* browser_context,
74 const std::map<GURL, int64>& usage_map,
75 int64 global_quota) {
76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
77 Profile* profile = Profile::FromBrowserContext(browser_context);
78 SiteEngagementService* service =
79 g_browser_process->profile_manager()->IsValidProfile(profile)
80 ? SiteEngagementService::Get(profile)
81 : nullptr;
82 if (!service)
83 return GURL();
84
85 return DoCalculateEvictionOrigin(special_storage_policy, service, usage_map,
86 global_quota);
87 }
88
89 } // namespace
90
91 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy(
92 content::BrowserContext* browser_context)
93 : browser_context_(browser_context) {
94 }
95
96 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {
97 }
98
99 void SiteEngagementEvictionPolicy::GetEvictionOrigin(
100 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy,
michaeln 2015/07/29 00:46:46 how about using const scoped_refptr<T>& here inst
calamity 2015/07/29 05:00:36 Done.
101 const std::map<GURL, int64>& usage_map,
102 int64 global_quota,
103 const storage::GetOriginCallback& callback) {
104 content::BrowserThread::PostTaskAndReplyWithResult(
105 content::BrowserThread::UI, FROM_HERE,
106 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread,
107 special_storage_policy, browser_context_, usage_map,
108 global_quota),
109 callback);
110 }
111
112 // static
113 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin(
114 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy,
michaeln 2015/07/29 00:46:46 ditto
calamity 2015/07/29 05:00:36 Done.
115 SiteEngagementScoreProvider* score_provider,
116 const std::map<GURL, int64>& usage_map,
117 int64 global_quota) {
118 return DoCalculateEvictionOrigin(special_storage_policy, score_provider,
119 usage_map, global_quota);
120 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698