Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 GURL DoCalculateEvictionOrigin(SiteEngagementScoreProvider* score_provider, | |
| 36 const std::map<GURL, int64>& usage_map, | |
| 37 int64 global_quota) { | |
| 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 | |
|
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.
| |
| 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. | |
|
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
| |
| 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 // |overuse| can be negative if the soft quota exceeds the usage. | |
| 52 int64 overuse = | |
| 53 usage.second - GetSoftQuotaForOrigin( | |
| 54 usage.first, score_provider->GetScore(usage.first), | |
| 55 total_engagement_points, global_quota); | |
| 56 if (overuse > max_overuse) { | |
| 57 max_overuse = overuse; | |
| 58 origin_to_evict = usage.first; | |
| 59 } | |
| 60 } | |
| 61 return origin_to_evict; | |
| 62 } | |
| 63 | |
| 64 GURL GetSiteEngagementEvictionOriginOnUIThread( | |
| 65 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.
| |
| 66 const std::map<GURL, int64>& usage_map, | |
| 67 int64 global_quota) { | |
| 68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 69 Profile* profile = Profile::FromBrowserContext(browser_context); | |
| 70 SiteEngagementService* service = | |
| 71 g_browser_process->profile_manager()->IsValidProfile(profile) | |
| 72 ? SiteEngagementService::Get(profile) | |
| 73 : nullptr; | |
| 74 if (!service) | |
| 75 return GURL(); | |
| 76 | |
| 77 return DoCalculateEvictionOrigin(service, usage_map, global_quota); | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 82 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | |
| 83 content::BrowserContext* browser_context) | |
| 84 : browser_context_(browser_context) { | |
| 85 } | |
| 86 | |
| 87 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() { | |
| 88 } | |
| 89 | |
| 90 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.
| |
| 91 const std::map<GURL, int64>& usage_map, | |
| 92 int64 global_quota, | |
| 93 const storage::GetEvictionOriginCallback& callback) { | |
| 94 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 95 content::BrowserThread::UI, FROM_HERE, | |
| 96 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, browser_context_, | |
| 97 usage_map, global_quota), | |
| 98 base::Bind(&ReplyWithEvictionOrigin, callback)); | |
| 99 } | |
| 100 | |
| 101 // static | |
| 102 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin( | |
| 103 SiteEngagementScoreProvider* score_provider, | |
| 104 const std::map<GURL, int64>& usage_map, | |
| 105 int64 global_quota) { | |
| 106 return DoCalculateEvictionOrigin(score_provider, usage_map, global_quota); | |
| 107 } | |
| OLD | NEW |