| 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..4b3b140afb261fe5a9454b083b3a9e416d1e383a
|
| --- /dev/null
|
| +++ b/chrome/browser/engagement/site_engagement_eviction_policy.cc
|
| @@ -0,0 +1,111 @@
|
| +// 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/engagement/site_engagement_eviction_policy.h"
|
| +#include "chrome/browser/engagement/site_engagement_service.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +
|
| +using GetEvictionOriginTask =
|
| + SiteEngagementEvictionPolicy::GetEvictionOriginTask;
|
| +
|
| +namespace {
|
| +
|
| +const int kExpectedEngagementSites = 200;
|
| +
|
| +} // namespace
|
| +
|
| +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),
|
| + total_engagement_points_(0),
|
| + proxy_(browser_context) {
|
| +}
|
| +
|
| +GetEvictionOriginTask::~GetEvictionOriginTask() {
|
| +}
|
| +
|
| +void GetEvictionOriginTask::Start() {
|
| + DCHECK(per_task_callback_.is_null());
|
| + per_task_callback_ = base::BarrierClosure(
|
| + 2, base::Bind(&GetEvictionOriginTask::CalculateEvictionOriginAndReply,
|
| + base::Unretained(this)));
|
| +
|
| + std::vector<GURL> origins;
|
| + for (const auto& usage : usage_map_)
|
| + origins.push_back(usage.first);
|
| +
|
| + proxy_.GetScoresForOrigins(
|
| + origins, base::Bind(&GetEvictionOriginTask::OnDidGetScoresForOrigin,
|
| + base::Unretained(this)));
|
| + proxy_.GetTotalEngagementPoints(
|
| + base::Bind(&GetEvictionOriginTask::OnDidGetTotalEngagementPoints,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void GetEvictionOriginTask::OnDidGetScoresForOrigin(
|
| + const std::map<GURL, int>& score_map) {
|
| + score_map_ = score_map;
|
| + per_task_callback_.Run();
|
| +}
|
| +
|
| +void GetEvictionOriginTask::OnDidGetTotalEngagementPoints(
|
| + int total_engagement_points) {
|
| + total_engagement_points_ = total_engagement_points;
|
| + per_task_callback_.Run();
|
| +}
|
| +
|
| +void GetEvictionOriginTask::CalculateEvictionOriginAndReply() {
|
| + // 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();
|
| + for (const auto& usage : usage_map_) {
|
| + int64 overuse = usage.second - GetSoftQuotaForOrigin(usage.first);
|
| + if (overuse > max_overuse) {
|
| + max_overuse = overuse;
|
| + origin_to_evict = usage.first;
|
| + }
|
| + }
|
| + result_callback_.Run(origin_to_evict);
|
| +
|
| + delete this;
|
| +}
|
| +
|
| +int64 GetEvictionOriginTask::GetSoftQuotaForOrigin(const GURL& origin) {
|
| + double quota_per_point =
|
| + global_quota_ /
|
| + std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints,
|
| + static_cast<double>(total_engagement_points_));
|
| +
|
| + return score_map_[origin] * quota_per_point;
|
| +}
|
| +
|
| +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) {
|
| + GetEvictionOriginTask* task = new GetEvictionOriginTask(
|
| + browser_context_, usage_map, global_quota, callback);
|
| + // The task will delete itself upon completion.
|
| + task->Start();
|
| +}
|
|
|