| 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" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "storage/browser/quota/usage_tracker.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 16 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>* |
| 17 g_test_score_provider_callback = nullptr; |
| 18 |
| 15 const int kExpectedEngagementSites = 200; | 19 const int kExpectedEngagementSites = 200; |
| 16 | 20 |
| 17 // 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. |
| 18 int64 GetSoftQuotaForOrigin(const GURL& origin, | 22 int64 GetSoftQuotaForOrigin(const GURL& origin, |
| 19 int score, | 23 int score, |
| 20 int total_engagement_points, | 24 int total_engagement_points, |
| 21 int64 global_quota) { | 25 int64 global_quota) { |
| 22 double quota_per_point = | 26 double quota_per_point = |
| 23 global_quota / | 27 global_quota / |
| 24 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints, | 28 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 } | 70 } |
| 67 return origin_to_evict; | 71 return origin_to_evict; |
| 68 } | 72 } |
| 69 | 73 |
| 70 GURL GetSiteEngagementEvictionOriginOnUIThread( | 74 GURL GetSiteEngagementEvictionOriginOnUIThread( |
| 71 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 75 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 72 content::BrowserContext* browser_context, | 76 content::BrowserContext* browser_context, |
| 73 const std::map<GURL, int64>& usage_map, | 77 const std::map<GURL, int64>& usage_map, |
| 74 int64 global_quota) { | 78 int64 global_quota) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 79 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 76 Profile* profile = Profile::FromBrowserContext(browser_context); | 80 |
| 77 SiteEngagementService* service = | 81 SiteEngagementScoreProvider* score_provider = nullptr; |
| 78 g_browser_process->profile_manager()->IsValidProfile(profile) | 82 if (g_test_score_provider_callback) { |
| 79 ? SiteEngagementService::Get(profile) | 83 score_provider = g_test_score_provider_callback->Run(browser_context); |
| 80 : nullptr; | 84 } else { |
| 81 if (!service) | 85 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 86 score_provider = |
| 87 g_browser_process->profile_manager()->IsValidProfile(profile) |
| 88 ? SiteEngagementService::Get(profile) |
| 89 : nullptr; |
| 90 } |
| 91 |
| 92 if (!score_provider) |
| 82 return GURL(); | 93 return GURL(); |
| 83 | 94 |
| 84 return DoCalculateEvictionOrigin(special_storage_policy, service, usage_map, | 95 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 85 global_quota); | 96 usage_map, global_quota); |
| 86 } | 97 } |
| 87 | 98 |
| 88 } // namespace | 99 } // namespace |
| 89 | 100 |
| 90 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | 101 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| 102 storage::StorageType type, |
| 103 storage::QuotaManager* quota_manager, |
| 91 content::BrowserContext* browser_context) | 104 content::BrowserContext* browser_context) |
| 92 : browser_context_(browser_context) {} | 105 : type_(type), |
| 106 quota_manager_(quota_manager), |
| 107 browser_context_(browser_context), |
| 108 global_quota_(0), |
| 109 remaining_tasks_(0), |
| 110 weak_factory_(this) {} |
| 93 | 111 |
| 94 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} | 112 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} |
| 95 | 113 |
| 96 void SiteEngagementEvictionPolicy::GetEvictionOrigin( | 114 void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| 97 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 115 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) { | 116 const storage::GetOriginCallback& callback) { |
| 117 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 118 DCHECK_EQ(0, remaining_tasks_); |
| 119 |
| 120 remaining_tasks_ = 2; |
| 121 eviction_origin_callback_ = callback; |
| 122 |
| 123 // This will populate cached hosts and usage info. |
| 124 quota_manager_->GetUsageTracker(type_)->GetGlobalUsage( |
| 125 base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalUsage, |
| 126 weak_factory_.GetWeakPtr(), type_)); |
| 127 quota_manager_->GetTemporaryGlobalQuota( |
| 128 base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalQuota, |
| 129 weak_factory_.GetWeakPtr())); |
| 130 } |
| 131 |
| 132 void SiteEngagementEvictionPolicy::DidGetGlobalUsage(storage::StorageType type, |
| 133 int64, |
| 134 int64) { |
| 135 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 136 storage::UsageTracker* tracker = quota_manager_->GetUsageTracker(type); |
| 137 DCHECK(tracker); |
| 138 tracker->GetCachedOriginsUsage(&usage_map_); |
| 139 |
| 140 if (--remaining_tasks_ == 0) |
| 141 OnQuotaTasksCompleted(); |
| 142 } |
| 143 |
| 144 void SiteEngagementEvictionPolicy::DidGetGlobalQuota( |
| 145 storage::QuotaStatusCode status, |
| 146 int64 quota) { |
| 147 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 148 if (status == storage::kQuotaStatusOk) |
| 149 global_quota_ = quota; |
| 150 |
| 151 if (--remaining_tasks_ == 0) |
| 152 OnQuotaTasksCompleted(); |
| 153 } |
| 154 |
| 155 void SiteEngagementEvictionPolicy::OnQuotaTasksCompleted() { |
| 156 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 157 |
| 101 content::BrowserThread::PostTaskAndReplyWithResult( | 158 content::BrowserThread::PostTaskAndReplyWithResult( |
| 102 content::BrowserThread::UI, FROM_HERE, | 159 content::BrowserThread::UI, FROM_HERE, |
| 103 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, | 160 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| 104 special_storage_policy, browser_context_, usage_map, | 161 special_storage_policy_, browser_context_, usage_map_, |
| 105 global_quota), | 162 global_quota_), |
| 106 callback); | 163 eviction_origin_callback_); |
| 107 } | 164 } |
| 108 | 165 |
| 109 // static | 166 // static |
| 110 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin( | 167 void SiteEngagementEvictionPolicy:: |
| 168 SetSiteEngagementScoreProviderCallbackForTests( |
| 169 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>* |
| 170 callback) { |
| 171 g_test_score_provider_callback = callback; |
| 172 } |
| 173 |
| 174 // static |
| 175 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( |
| 111 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 176 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 112 SiteEngagementScoreProvider* score_provider, | 177 SiteEngagementScoreProvider* score_provider, |
| 113 const std::map<GURL, int64>& usage_map, | 178 const std::map<GURL, int64>& usage_map, |
| 114 int64 global_quota) { | 179 int64 global_quota) { |
| 115 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | 180 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 116 usage_map, global_quota); | 181 usage_map, global_quota); |
| 117 } | 182 } |
| OLD | NEW |