Chromium Code Reviews| Index: chrome/browser/engagement/site_engagement_eviction_policy.cc |
| diff --git a/chrome/browser/engagement/site_engagement_eviction_policy.cc b/chrome/browser/engagement/site_engagement_eviction_policy.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aef65e9089a3056816b4a7bdf863035428d073c1 |
| --- /dev/null |
| +++ b/chrome/browser/engagement/site_engagement_eviction_policy.cc |
| @@ -0,0 +1,130 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/barrier_closure.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/engagement/site_engagement_eviction_policy.h" |
| +#include "chrome/browser/engagement/site_engagement_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace { |
| + |
| +const int kExpectedEngagementSites = 200; |
| + |
| +// Gets the quota that an origin deserves based on its site engagement. |
| +int64 GetSoftQuotaForOrigin(const GURL& origin, |
| + int score, |
| + int total_engagement_points, |
| + int64 global_quota) { |
| + double quota_per_point = |
| + global_quota / |
| + std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints, |
| + static_cast<double>(total_engagement_points)); |
| + |
| + return score * quota_per_point; |
| +} |
| + |
| + |
| +} // namespace |
| + |
| +SiteEngagementEvictionPolicy::GetEvictionOriginTask::GetEvictionOriginTask( |
| + content::BrowserContext* browser_context, |
| + const std::map<GURL, int64>& usage_map, |
| + int64 global_quota, |
| + const storage::GetEvictionOriginCallback& result_callback) |
| + : usage_map_(usage_map), |
| + global_quota_(global_quota), |
| + browser_context_(browser_context), |
| + result_callback_(result_callback) { |
| +} |
| + |
| +SiteEngagementEvictionPolicy::GetEvictionOriginTask::~GetEvictionOriginTask() { |
| +} |
| + |
| +void SiteEngagementEvictionPolicy::GetEvictionOriginTask::Start() { |
| + // The callback keeps a reference to this task, keeping it alive. |
| + content::BrowserThread::PostTaskAndReplyWithResult( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind( |
| + &GetEvictionOriginTask::GetSiteEngagementEvictionOriginOnUIThread, |
| + browser_context_, usage_map_, global_quota_), |
| + base::Bind(&GetEvictionOriginTask::ReplyWithEvictionOrigin, |
| + 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.
|
| +} |
| + |
| +void SiteEngagementEvictionPolicy::GetEvictionOriginTask:: |
| + ReplyWithEvictionOrigin(const GURL& origin_to_evict) { |
| + result_callback_.Run(origin_to_evict); |
| + |
| + // Once this has run, the last reference will be released, deleting this |
| + // task. |
| +} |
| + |
| +// static |
| +GURL SiteEngagementEvictionPolicy::GetEvictionOriginTask:: |
| + GetSiteEngagementEvictionOriginOnUIThread( |
| + content::BrowserContext* browser_context, |
| + const std::map<GURL, int64>& usage_map, |
| + int64 global_quota) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + Profile* profile = Profile::FromBrowserContext(browser_context); |
| + SiteEngagementService* service = |
| + g_browser_process->profile_manager()->IsValidProfile(profile) ? |
| + SiteEngagementService::Get(profile) : nullptr; |
| + if (!service) |
| + return GURL(); |
| + |
| + return CalculateEvictionOrigin(service, usage_map, global_quota); |
| +} |
| + |
| +// static |
| +GURL SiteEngagementEvictionPolicy::GetEvictionOriginTask:: |
| + CalculateEvictionOrigin(SiteEngagementService* service, |
| + const std::map<GURL, int64>& usage_map, |
| + int64 global_quota) { |
| + // This heuristic is intended to optimize for two criteria: |
| + // - evict the site that the user cares about least |
| + // - evict the least number of sites to get under the quota limit |
| + // |
| + // The heuristic for deciding the next eviction origin calculates a soft |
| + // quota for each origin which is the amount the origin should be allowed to |
| + // use based on its engagement and the global quota. The origin that most |
| + // exceeds its soft quota is chosen. |
| + GURL origin_to_evict; |
| + int64 max_overuse = std::numeric_limits<int64>::min(); |
| + int total_engagement_points = service->GetTotalEngagementPoints(); |
| + |
| + for (const auto& usage : usage_map) { |
| + int64 overuse = |
| + usage.second - GetSoftQuotaForOrigin(usage.first, |
| + service->GetScore(usage.first), |
| + total_engagement_points, |
| + global_quota); |
| + if (overuse > max_overuse) { |
| + max_overuse = overuse; |
| + origin_to_evict = usage.first; |
| + } |
| + } |
| + return origin_to_evict; |
| +} |
|
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.
|
| + |
| +SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| + content::BrowserContext* browser_context) |
| + : browser_context_(browser_context) { |
| +} |
| + |
| +SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() { |
| +} |
| + |
| +void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| + const std::map<GURL, int64>& usage_map, |
| + int64 global_quota, |
| + const storage::GetEvictionOriginCallback& callback) { |
| + scoped_refptr<GetEvictionOriginTask> task = new GetEvictionOriginTask( |
| + browser_context_, usage_map, global_quota, callback); |
| + // The task will delete itself upon completion. |
| + task->Start(); |
| +} |
|
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.
|