| 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 GURL DoCalculateEvictionOrigin( |
| 31 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 32 SiteEngagementScoreProvider* score_provider, |
| 33 const std::map<GURL, int64>& usage_map, |
| 34 int64 global_quota) { |
| 35 // TODO(calamity): Integrate storage access frequency as an input to this |
| 36 // heuristic. |
| 37 |
| 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 |
| 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. |
| 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 GURL origin = usage.first; |
| 52 if (special_storage_policy && |
| 53 (special_storage_policy->IsStorageUnlimited(origin) || |
| 54 special_storage_policy->IsStorageDurable(origin))) { |
| 55 continue; |
| 56 } |
| 57 |
| 58 // |overuse| can be negative if the soft quota exceeds the usage. |
| 59 int64 overuse = usage.second - GetSoftQuotaForOrigin( |
| 60 origin, score_provider->GetScore(origin), |
| 61 total_engagement_points, global_quota); |
| 62 if (overuse > max_overuse) { |
| 63 max_overuse = overuse; |
| 64 origin_to_evict = origin; |
| 65 } |
| 66 } |
| 67 return origin_to_evict; |
| 68 } |
| 69 |
| 70 GURL GetSiteEngagementEvictionOriginOnUIThread( |
| 71 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 72 content::BrowserContext* browser_context, |
| 73 const std::map<GURL, int64>& usage_map, |
| 74 int64 global_quota) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 76 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 77 SiteEngagementService* service = |
| 78 g_browser_process->profile_manager()->IsValidProfile(profile) |
| 79 ? SiteEngagementService::Get(profile) |
| 80 : nullptr; |
| 81 if (!service) |
| 82 return GURL(); |
| 83 |
| 84 return DoCalculateEvictionOrigin(special_storage_policy, service, usage_map, |
| 85 global_quota); |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| 91 content::BrowserContext* browser_context) |
| 92 : browser_context_(browser_context) {} |
| 93 |
| 94 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} |
| 95 |
| 96 void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| 97 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 98 const std::map<GURL, int64>& usage_map, |
| 99 int64 global_quota, |
| 100 const storage::GetOriginCallback& callback) { |
| 101 content::BrowserThread::PostTaskAndReplyWithResult( |
| 102 content::BrowserThread::UI, FROM_HERE, |
| 103 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| 104 special_storage_policy, browser_context_, usage_map, |
| 105 global_quota), |
| 106 callback); |
| 107 } |
| 108 |
| 109 // static |
| 110 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin( |
| 111 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 112 SiteEngagementScoreProvider* score_provider, |
| 113 const std::map<GURL, int64>& usage_map, |
| 114 int64 global_quota) { |
| 115 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 116 usage_map, global_quota); |
| 117 } |
| OLD | NEW |