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..824851275a32044d38d734e6c413d93e81ee6b1a |
| --- /dev/null |
| +++ b/chrome/browser/engagement/site_engagement_eviction_policy.cc |
| @@ -0,0 +1,104 @@ |
| +// 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; |
| +} |
| + |
| +void ReplyWithEvictionOrigin(const storage::GetEvictionOriginCallback& callback, |
| + const GURL& origin_to_evict) { |
| + callback.Run(origin_to_evict); |
| +} |
| + |
| +} // namespace |
| + |
| +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) { |
| + content::BrowserThread::PostTaskAndReplyWithResult( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind( |
| + &GetSiteEngagementEvictionOriginOnUIThread, |
| + browser_context_, usage_map, global_quota), |
| + base::Bind(&ReplyWithEvictionOrigin, callback)); |
|
michaeln
2015/07/23 02:22:32
would it work to use 'callback' here directly with
calamity
2015/07/28 01:07:59
Done.
|
| +} |
| + |
| +// static |
| +GURL SiteEngagementEvictionPolicy::GetSiteEngagementEvictionOriginOnUIThread( |
|
raymes
2015/07/21 09:22:20
nit: I think we can make this a standalone functio
calamity
2015/07/22 01:57:38
It won't be able to see the private static method
|
| + 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 SiteEngagementEvictionPolicy::CalculateEvictionOrigin( |
| + service, usage_map, global_quota); |
| +} |
| + |
| +// static |
| +GURL SiteEngagementEvictionPolicy::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) { |
| + // |overuse| can be negative if the soft quota exceeds the usage. |
| + 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; |
| +} |