| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" | 5 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/engagement/site_engagement_service.h" | 11 #include "chrome/browser/engagement/site_engagement_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const int kExpectedEngagementSites = 200; | 19 const int kExpectedEngagementSites = 200; |
| 20 | 20 |
| 21 // Gets the quota that an origin deserves based on its site engagement. | 21 // Gets the quota that an origin deserves based on its site engagement. |
| 22 int64 GetSoftQuotaForOrigin(const GURL& origin, | 22 int64_t GetSoftQuotaForOrigin(const GURL& origin, |
| 23 int score, | 23 int score, |
| 24 int total_engagement_points, | 24 int total_engagement_points, |
| 25 int64 global_quota) { | 25 int64_t global_quota) { |
| 26 double quota_per_point = | 26 double quota_per_point = |
| 27 global_quota / | 27 global_quota / |
| 28 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints, | 28 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints, |
| 29 static_cast<double>(total_engagement_points)); | 29 static_cast<double>(total_engagement_points)); |
| 30 | 30 |
| 31 return score * quota_per_point; | 31 return score * quota_per_point; |
| 32 } | 32 } |
| 33 | 33 |
| 34 GURL DoCalculateEvictionOrigin( | 34 GURL DoCalculateEvictionOrigin( |
| 35 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 35 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 36 SiteEngagementScoreProvider* score_provider, | 36 SiteEngagementScoreProvider* score_provider, |
| 37 const std::set<GURL>& exceptions, | 37 const std::set<GURL>& exceptions, |
| 38 const std::map<GURL, int64>& usage_map, | 38 const std::map<GURL, int64_t>& usage_map, |
| 39 int64 global_quota) { | 39 int64_t global_quota) { |
| 40 // TODO(calamity): Integrate storage access frequency as an input to this | 40 // TODO(calamity): Integrate storage access frequency as an input to this |
| 41 // heuristic. | 41 // heuristic. |
| 42 | 42 |
| 43 // This heuristic is intended to optimize for two criteria: | 43 // This heuristic is intended to optimize for two criteria: |
| 44 // - evict the site that the user cares about least | 44 // - evict the site that the user cares about least |
| 45 // - evict the least number of sites to get under the quota limit | 45 // - evict the least number of sites to get under the quota limit |
| 46 // | 46 // |
| 47 // The heuristic for deciding the next eviction origin calculates a soft | 47 // The heuristic for deciding the next eviction origin calculates a soft |
| 48 // quota for each origin which is the amount the origin should be allowed to | 48 // quota for each origin which is the amount the origin should be allowed to |
| 49 // use based on its engagement and the global quota. The origin that most | 49 // use based on its engagement and the global quota. The origin that most |
| 50 // exceeds its soft quota is chosen. | 50 // exceeds its soft quota is chosen. |
| 51 GURL origin_to_evict; | 51 GURL origin_to_evict; |
| 52 int64 max_overuse = std::numeric_limits<int64>::min(); | 52 int64_t max_overuse = std::numeric_limits<int64_t>::min(); |
| 53 int total_engagement_points = score_provider->GetTotalEngagementPoints(); | 53 int total_engagement_points = score_provider->GetTotalEngagementPoints(); |
| 54 | 54 |
| 55 for (const auto& usage : usage_map) { | 55 for (const auto& usage : usage_map) { |
| 56 GURL origin = usage.first; | 56 GURL origin = usage.first; |
| 57 if (special_storage_policy && | 57 if (special_storage_policy && |
| 58 (special_storage_policy->IsStorageUnlimited(origin) || | 58 (special_storage_policy->IsStorageUnlimited(origin) || |
| 59 special_storage_policy->IsStorageDurable(origin))) { | 59 special_storage_policy->IsStorageDurable(origin))) { |
| 60 continue; | 60 continue; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // |overuse| can be negative if the soft quota exceeds the usage. | 63 // |overuse| can be negative if the soft quota exceeds the usage. |
| 64 int64 overuse = usage.second - GetSoftQuotaForOrigin( | 64 int64_t overuse = |
| 65 origin, score_provider->GetScore(origin), | 65 usage.second - |
| 66 total_engagement_points, global_quota); | 66 GetSoftQuotaForOrigin(origin, score_provider->GetScore(origin), |
| 67 total_engagement_points, global_quota); |
| 67 if (overuse > max_overuse && !ContainsKey(exceptions, origin)) { | 68 if (overuse > max_overuse && !ContainsKey(exceptions, origin)) { |
| 68 max_overuse = overuse; | 69 max_overuse = overuse; |
| 69 origin_to_evict = origin; | 70 origin_to_evict = origin; |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 | 73 |
| 73 return origin_to_evict; | 74 return origin_to_evict; |
| 74 } | 75 } |
| 75 | 76 |
| 76 GURL GetSiteEngagementEvictionOriginOnUIThread( | 77 GURL GetSiteEngagementEvictionOriginOnUIThread( |
| 77 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 78 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 78 content::BrowserContext* browser_context, | 79 content::BrowserContext* browser_context, |
| 79 const std::set<GURL>& exceptions, | 80 const std::set<GURL>& exceptions, |
| 80 const std::map<GURL, int64>& usage_map, | 81 const std::map<GURL, int64_t>& usage_map, |
| 81 int64 global_quota) { | 82 int64_t global_quota) { |
| 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 83 | 84 |
| 84 Profile* profile = Profile::FromBrowserContext(browser_context); | 85 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 85 SiteEngagementScoreProvider* score_provider = | 86 SiteEngagementScoreProvider* score_provider = |
| 86 g_browser_process->profile_manager()->IsValidProfile(profile) | 87 g_browser_process->profile_manager()->IsValidProfile(profile) |
| 87 ? SiteEngagementService::Get(profile) | 88 ? SiteEngagementService::Get(profile) |
| 88 : nullptr; | 89 : nullptr; |
| 89 | 90 |
| 90 if (!score_provider) | 91 if (!score_provider) |
| 91 return GURL(); | 92 return GURL(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 111 | 112 |
| 112 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | 113 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| 113 content::BrowserContext* browser_context) | 114 content::BrowserContext* browser_context) |
| 114 : browser_context_(browser_context) {} | 115 : browser_context_(browser_context) {} |
| 115 | 116 |
| 116 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} | 117 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} |
| 117 | 118 |
| 118 void SiteEngagementEvictionPolicy::GetEvictionOrigin( | 119 void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| 119 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 120 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 120 const std::set<GURL>& exceptions, | 121 const std::set<GURL>& exceptions, |
| 121 const std::map<GURL, int64>& usage_map, | 122 const std::map<GURL, int64_t>& usage_map, |
| 122 int64 global_quota, | 123 int64_t global_quota, |
| 123 const storage::GetOriginCallback& callback) { | 124 const storage::GetOriginCallback& callback) { |
| 124 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 125 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 125 | 126 |
| 126 content::BrowserThread::PostTaskAndReplyWithResult( | 127 content::BrowserThread::PostTaskAndReplyWithResult( |
| 127 content::BrowserThread::UI, FROM_HERE, | 128 content::BrowserThread::UI, FROM_HERE, |
| 128 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, | 129 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| 129 special_storage_policy, browser_context_, exceptions, | 130 special_storage_policy, browser_context_, exceptions, |
| 130 usage_map, global_quota), | 131 usage_map, global_quota), |
| 131 callback); | 132 callback); |
| 132 } | 133 } |
| 133 | 134 |
| 134 // static | 135 // static |
| 135 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( | 136 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( |
| 136 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 137 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 137 SiteEngagementScoreProvider* score_provider, | 138 SiteEngagementScoreProvider* score_provider, |
| 138 const std::set<GURL>& exceptions, | 139 const std::set<GURL>& exceptions, |
| 139 const std::map<GURL, int64>& usage_map, | 140 const std::map<GURL, int64_t>& usage_map, |
| 140 int64 global_quota) { | 141 int64_t global_quota) { |
| 141 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | 142 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 142 exceptions, usage_map, global_quota); | 143 exceptions, usage_map, global_quota); |
| 143 } | 144 } |
| OLD | NEW |