| 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 "base/barrier_closure.h" | 5 #include "base/barrier_closure.h" |
| 6 #include "chrome/browser/browser_process.h" | 6 #include "chrome/browser/browser_process.h" |
| 7 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" | 7 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" |
| 8 #include "chrome/browser/engagement/site_engagement_service.h" | 8 #include "chrome/browser/engagement/site_engagement_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 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::map<GURL, int64>& usage_map, | 38 const std::map<GURL, int64>& usage_map, |
| 38 int64 global_quota) { | 39 int64 global_quota) { |
| 39 // TODO(calamity): Integrate storage access frequency as an input to this | 40 // TODO(calamity): Integrate storage access frequency as an input to this |
| 40 // heuristic. | 41 // heuristic. |
| 41 | 42 |
| 42 // This heuristic is intended to optimize for two criteria: | 43 // This heuristic is intended to optimize for two criteria: |
| 43 // - evict the site that the user cares about least | 44 // - evict the site that the user cares about least |
| 44 // - 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 |
| 45 // | 46 // |
| 46 // The heuristic for deciding the next eviction origin calculates a soft | 47 // The heuristic for deciding the next eviction origin calculates a soft |
| 47 // 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 |
| 48 // 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 |
| 49 // exceeds its soft quota is chosen. | 50 // exceeds its soft quota is chosen. |
| 50 GURL origin_to_evict; | 51 GURL origin_to_evict; |
| 51 int64 max_overuse = std::numeric_limits<int64>::min(); | 52 int64 max_overuse = std::numeric_limits<int64>::min(); |
| 52 int total_engagement_points = score_provider->GetTotalEngagementPoints(); | 53 int total_engagement_points = score_provider->GetTotalEngagementPoints(); |
| 53 | 54 |
| 54 for (const auto& usage : usage_map) { | 55 for (const auto& usage : usage_map) { |
| 55 GURL origin = usage.first; | 56 GURL origin = usage.first; |
| 56 if (special_storage_policy && | 57 if (special_storage_policy && |
| 57 (special_storage_policy->IsStorageUnlimited(origin) || | 58 (special_storage_policy->IsStorageUnlimited(origin) || |
| 58 special_storage_policy->IsStorageDurable(origin))) { | 59 special_storage_policy->IsStorageDurable(origin))) { |
| 59 continue; | 60 continue; |
| 60 } | 61 } |
| 61 | 62 |
| 62 // |overuse| can be negative if the soft quota exceeds the usage. | 63 // |overuse| can be negative if the soft quota exceeds the usage. |
| 63 int64 overuse = usage.second - GetSoftQuotaForOrigin( | 64 int64 overuse = usage.second - GetSoftQuotaForOrigin( |
| 64 origin, score_provider->GetScore(origin), | 65 origin, score_provider->GetScore(origin), |
| 65 total_engagement_points, global_quota); | 66 total_engagement_points, global_quota); |
| 66 if (overuse > max_overuse) { | 67 if (overuse > max_overuse && exceptions.count(origin) == 0) { |
| 67 max_overuse = overuse; | 68 max_overuse = overuse; |
| 68 origin_to_evict = origin; | 69 origin_to_evict = origin; |
| 69 } | 70 } |
| 70 } | 71 } |
| 71 return origin_to_evict; | 72 return origin_to_evict; |
| 72 } | 73 } |
| 73 | 74 |
| 74 SiteEngagementScoreProvider* GetSiteEngagementService( | 75 SiteEngagementScoreProvider* GetSiteEngagementService( |
| 75 content::BrowserContext* browser_context) { | 76 content::BrowserContext* browser_context) { |
| 76 Profile* profile = Profile::FromBrowserContext(browser_context); | 77 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 77 return g_browser_process->profile_manager()->IsValidProfile(profile) | 78 return g_browser_process->profile_manager()->IsValidProfile(profile) |
| 78 ? SiteEngagementService::Get(profile) | 79 ? SiteEngagementService::Get(profile) |
| 79 : nullptr; | 80 : nullptr; |
| 80 } | 81 } |
| 81 | 82 |
| 82 GURL GetSiteEngagementEvictionOriginOnUIThread( | 83 GURL GetSiteEngagementEvictionOriginOnUIThread( |
| 83 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 84 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 84 content::BrowserContext* browser_context, | 85 content::BrowserContext* browser_context, |
| 86 const std::set<GURL>& exceptions, |
| 85 const std::map<GURL, int64>& usage_map, | 87 const std::map<GURL, int64>& usage_map, |
| 86 int64 global_quota) { | 88 int64 global_quota) { |
| 87 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 88 | 90 |
| 89 SiteEngagementScoreProvider* score_provider = | 91 SiteEngagementScoreProvider* score_provider = |
| 90 g_test_score_provider_callback | 92 g_test_score_provider_callback |
| 91 ? g_test_score_provider_callback->Run(browser_context) | 93 ? g_test_score_provider_callback->Run(browser_context) |
| 92 : GetSiteEngagementService(browser_context); | 94 : GetSiteEngagementService(browser_context); |
| 93 | 95 |
| 94 if (!score_provider) | 96 if (!score_provider) |
| 95 return GURL(); | 97 return GURL(); |
| 96 | 98 |
| 97 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | 99 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 98 usage_map, global_quota); | 100 exceptions, usage_map, global_quota); |
| 99 } | 101 } |
| 100 | 102 |
| 101 } // namespace | 103 } // namespace |
| 102 | 104 |
| 103 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | 105 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| 104 storage::StorageType type, | 106 storage::StorageType type, |
| 105 storage::QuotaManager* manager, | 107 storage::QuotaManager* manager, |
| 106 content::BrowserContext* browser_context) | 108 content::BrowserContext* browser_context) |
| 107 : type_(type), | 109 : type_(type), |
| 108 manager_(manager), | 110 manager_(manager), |
| 109 browser_context_(browser_context), | 111 browser_context_(browser_context), |
| 110 global_quota_(0), | 112 global_quota_(0), |
| 111 remaining_tasks_(0), | 113 remaining_tasks_(0), |
| 112 weak_factory_(this) {} | 114 weak_factory_(this) {} |
| 113 | 115 |
| 114 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} | 116 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} |
| 115 | 117 |
| 116 void SiteEngagementEvictionPolicy::GetEvictionOrigin( | 118 void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| 117 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 119 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 120 const std::set<GURL>& exceptions, |
| 118 const storage::GetOriginCallback& callback) { | 121 const storage::GetOriginCallback& callback) { |
| 119 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 122 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 120 DCHECK_EQ(0, remaining_tasks_); | 123 DCHECK_EQ(0, remaining_tasks_); |
| 121 | 124 |
| 122 remaining_tasks_ = 2; | 125 remaining_tasks_ = 2; |
| 123 eviction_origin_callback_ = callback; | 126 eviction_origin_callback_ = callback; |
| 124 | 127 |
| 125 // This will populate cached hosts and usage info. | 128 // This will populate cached hosts and usage info. |
| 126 manager_->GetUsageTracker(type_)->GetGlobalUsage( | 129 manager_->GetUsageTracker(type_)->GetGlobalUsage( |
| 127 base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalUsage, | 130 base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalUsage, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 153 if (--remaining_tasks_ == 0) | 156 if (--remaining_tasks_ == 0) |
| 154 OnQuotaTasksCompleted(); | 157 OnQuotaTasksCompleted(); |
| 155 } | 158 } |
| 156 | 159 |
| 157 void SiteEngagementEvictionPolicy::OnQuotaTasksCompleted() { | 160 void SiteEngagementEvictionPolicy::OnQuotaTasksCompleted() { |
| 158 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 161 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 159 | 162 |
| 160 content::BrowserThread::PostTaskAndReplyWithResult( | 163 content::BrowserThread::PostTaskAndReplyWithResult( |
| 161 content::BrowserThread::UI, FROM_HERE, | 164 content::BrowserThread::UI, FROM_HERE, |
| 162 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, | 165 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| 163 special_storage_policy_, browser_context_, usage_map_, | 166 special_storage_policy_, browser_context_, exceptions_, |
| 164 global_quota_), | 167 usage_map_, global_quota_), |
| 165 eviction_origin_callback_); | 168 eviction_origin_callback_); |
| 166 } | 169 } |
| 167 | 170 |
| 168 // static | 171 // static |
| 169 void SiteEngagementEvictionPolicy:: | 172 void SiteEngagementEvictionPolicy:: |
| 170 SetSiteEngagementScoreProviderCallbackForTests( | 173 SetSiteEngagementScoreProviderCallbackForTests( |
| 171 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>* | 174 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>* |
| 172 callback) { | 175 callback) { |
| 173 g_test_score_provider_callback = callback; | 176 g_test_score_provider_callback = callback; |
| 174 } | 177 } |
| 175 | 178 |
| 176 // static | 179 // static |
| 177 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( | 180 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( |
| 178 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 181 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 179 SiteEngagementScoreProvider* score_provider, | 182 SiteEngagementScoreProvider* score_provider, |
| 183 const std::set<GURL>& exceptions, |
| 180 const std::map<GURL, int64>& usage_map, | 184 const std::map<GURL, int64>& usage_map, |
| 181 int64 global_quota) { | 185 int64 global_quota) { |
| 182 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | 186 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 183 usage_map, global_quota); | 187 exceptions, usage_map, global_quota); |
| 184 } | 188 } |
| OLD | NEW |