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 | |
| 31 } // namespace | |
| 32 | |
| 33 SiteEngagementEvictionPolicy::GetEvictionOriginTask::GetEvictionOriginTask( | |
| 34 content::BrowserContext* browser_context, | |
| 35 const std::map<GURL, int64>& usage_map, | |
| 36 int64 global_quota, | |
| 37 const storage::GetEvictionOriginCallback& result_callback) | |
| 38 : usage_map_(usage_map), | |
| 39 global_quota_(global_quota), | |
| 40 browser_context_(browser_context), | |
| 41 result_callback_(result_callback) { | |
| 42 } | |
| 43 | |
| 44 SiteEngagementEvictionPolicy::GetEvictionOriginTask::~GetEvictionOriginTask() { | |
| 45 } | |
| 46 | |
| 47 void SiteEngagementEvictionPolicy::GetEvictionOriginTask::Start() { | |
| 48 // The callback keeps a reference to this task, keeping it alive. | |
| 49 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 50 content::BrowserThread::UI, FROM_HERE, | |
| 51 base::Bind( | |
| 52 &GetEvictionOriginTask::GetSiteEngagementEvictionOriginOnUIThread, | |
| 53 browser_context_, usage_map_, global_quota_), | |
| 54 base::Bind(&GetEvictionOriginTask::ReplyWithEvictionOrigin, | |
| 55 base::Unretained(this))); | |
|
raymes
2015/07/21 03:16:00
I think we need to pass "this" rather than base::U
calamity
2015/07/21 04:57:09
Done.
| |
| 56 } | |
| 57 | |
| 58 void SiteEngagementEvictionPolicy::GetEvictionOriginTask:: | |
| 59 ReplyWithEvictionOrigin(const GURL& origin_to_evict) { | |
| 60 result_callback_.Run(origin_to_evict); | |
| 61 | |
| 62 // Once this has run, the last reference will be released, deleting this | |
| 63 // task. | |
| 64 } | |
| 65 | |
| 66 // static | |
| 67 GURL SiteEngagementEvictionPolicy::GetEvictionOriginTask:: | |
| 68 GetSiteEngagementEvictionOriginOnUIThread( | |
| 69 content::BrowserContext* browser_context, | |
| 70 const std::map<GURL, int64>& usage_map, | |
| 71 int64 global_quota) { | |
| 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 73 Profile* profile = Profile::FromBrowserContext(browser_context); | |
| 74 SiteEngagementService* service = | |
| 75 g_browser_process->profile_manager()->IsValidProfile(profile) ? | |
| 76 SiteEngagementService::Get(profile) : nullptr; | |
| 77 if (!service) | |
| 78 return GURL(); | |
| 79 | |
| 80 return CalculateEvictionOrigin(service, usage_map, global_quota); | |
| 81 } | |
| 82 | |
| 83 // static | |
| 84 GURL SiteEngagementEvictionPolicy::GetEvictionOriginTask:: | |
| 85 CalculateEvictionOrigin(SiteEngagementService* service, | |
| 86 const std::map<GURL, int64>& usage_map, | |
| 87 int64 global_quota) { | |
| 88 // This heuristic is intended to optimize for two criteria: | |
| 89 // - evict the site that the user cares about least | |
| 90 // - evict the least number of sites to get under the quota limit | |
| 91 // | |
| 92 // The heuristic for deciding the next eviction origin calculates a soft | |
| 93 // quota for each origin which is the amount the origin should be allowed to | |
| 94 // use based on its engagement and the global quota. The origin that most | |
| 95 // exceeds its soft quota is chosen. | |
| 96 GURL origin_to_evict; | |
| 97 int64 max_overuse = std::numeric_limits<int64>::min(); | |
| 98 int total_engagement_points = service->GetTotalEngagementPoints(); | |
| 99 | |
| 100 for (const auto& usage : usage_map) { | |
| 101 int64 overuse = | |
| 102 usage.second - GetSoftQuotaForOrigin(usage.first, | |
| 103 service->GetScore(usage.first), | |
| 104 total_engagement_points, | |
| 105 global_quota); | |
| 106 if (overuse > max_overuse) { | |
| 107 max_overuse = overuse; | |
| 108 origin_to_evict = usage.first; | |
| 109 } | |
| 110 } | |
| 111 return origin_to_evict; | |
| 112 } | |
|
raymes
2015/07/21 03:16:00
nit: I think we can make the above 2 functions sta
calamity
2015/07/21 04:57:09
Done.
| |
| 113 | |
| 114 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | |
| 115 content::BrowserContext* browser_context) | |
| 116 : browser_context_(browser_context) { | |
| 117 } | |
| 118 | |
| 119 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() { | |
| 120 } | |
| 121 | |
| 122 void SiteEngagementEvictionPolicy::GetEvictionOrigin( | |
| 123 const std::map<GURL, int64>& usage_map, | |
| 124 int64 global_quota, | |
| 125 const storage::GetEvictionOriginCallback& callback) { | |
| 126 scoped_refptr<GetEvictionOriginTask> task = new GetEvictionOriginTask( | |
| 127 browser_context_, usage_map, global_quota, callback); | |
| 128 // The task will delete itself upon completion. | |
| 129 task->Start(); | |
| 130 } | |
|
raymes
2015/07/21 03:16:00
nit: I think we should reorder this to match the h
calamity
2015/07/21 04:57:09
Done.
| |
| OLD | NEW |