Chromium Code Reviews| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 origin, score_provider->GetScore(origin), | 64 origin, score_provider->GetScore(origin), |
| 61 total_engagement_points, global_quota); | 65 total_engagement_points, global_quota); |
| 62 if (overuse > max_overuse) { | 66 if (overuse > max_overuse) { |
| 63 max_overuse = overuse; | 67 max_overuse = overuse; |
| 64 origin_to_evict = origin; | 68 origin_to_evict = origin; |
| 65 } | 69 } |
| 66 } | 70 } |
| 67 return origin_to_evict; | 71 return origin_to_evict; |
| 68 } | 72 } |
| 69 | 73 |
| 74 SiteEngagementScoreProvider* GetSiteEngagementService( | |
| 75 content::BrowserContext* browser_context) { | |
| 76 Profile* profile = Profile::FromBrowserContext(browser_context); | |
| 77 return g_browser_process->profile_manager()->IsValidProfile(profile) | |
| 78 ? SiteEngagementService::Get(profile) | |
| 79 : nullptr; | |
| 80 } | |
| 81 | |
| 70 GURL GetSiteEngagementEvictionOriginOnUIThread( | 82 GURL GetSiteEngagementEvictionOriginOnUIThread( |
| 71 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 83 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 72 content::BrowserContext* browser_context, | 84 content::BrowserContext* browser_context, |
| 73 const std::map<GURL, int64>& usage_map, | 85 const std::map<GURL, int64>& usage_map, |
| 74 int64 global_quota) { | 86 int64 global_quota) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 87 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 76 Profile* profile = Profile::FromBrowserContext(browser_context); | 88 |
| 77 SiteEngagementService* service = | 89 SiteEngagementScoreProvider* score_provider = |
| 78 g_browser_process->profile_manager()->IsValidProfile(profile) | 90 g_test_score_provider_callback |
| 79 ? SiteEngagementService::Get(profile) | 91 ? g_test_score_provider_callback->Run(browser_context) |
| 80 : nullptr; | 92 : GetSiteEngagementService(browser_context); |
|
raymes
2015/09/22 06:08:48
nit: how about we fold this logic into GetSiteEnga
calamity
2015/09/23 01:46:24
I don't think that function was providing much val
| |
| 81 if (!service) | 93 |
| 94 if (!score_provider) | |
| 82 return GURL(); | 95 return GURL(); |
| 83 | 96 |
| 84 return DoCalculateEvictionOrigin(special_storage_policy, service, usage_map, | 97 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 85 global_quota); | 98 usage_map, global_quota); |
| 86 } | 99 } |
| 87 | 100 |
| 88 } // namespace | 101 } // namespace |
| 89 | 102 |
| 90 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | 103 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| 104 storage::StorageType type, | |
| 105 storage::QuotaManager* manager, | |
| 91 content::BrowserContext* browser_context) | 106 content::BrowserContext* browser_context) |
| 92 : browser_context_(browser_context) {} | 107 : type_(type), |
| 108 manager_(manager), | |
|
raymes
2015/09/22 06:08:49
nit: perhaps call this quota_manager_ for clarity?
calamity
2015/09/23 01:46:24
Done.
| |
| 109 browser_context_(browser_context), | |
| 110 global_quota_(0), | |
| 111 remaining_tasks_(0), | |
| 112 weak_factory_(this) {} | |
| 93 | 113 |
| 94 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} | 114 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} |
|
raymes
2015/09/22 06:08:49
Should we fire eviction_origin_callback_ if we're
calamity
2015/09/23 01:46:24
Hmm. Well, the quota manager is the one receiving
| |
| 95 | 115 |
| 96 void SiteEngagementEvictionPolicy::GetEvictionOrigin( | 116 void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| 97 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 117 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) { | 118 const storage::GetOriginCallback& callback) { |
| 119 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 120 DCHECK_EQ(0, remaining_tasks_); | |
| 121 | |
| 122 remaining_tasks_ = 2; | |
| 123 eviction_origin_callback_ = callback; | |
| 124 | |
| 125 // This will populate cached hosts and usage info. | |
| 126 manager_->GetUsageTracker(type_)->GetGlobalUsage( | |
| 127 base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalUsage, | |
| 128 weak_factory_.GetWeakPtr(), type_)); | |
| 129 manager_->GetTemporaryGlobalQuota( | |
| 130 base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalQuota, | |
| 131 weak_factory_.GetWeakPtr())); | |
| 132 } | |
| 133 | |
| 134 void SiteEngagementEvictionPolicy::DidGetGlobalUsage(storage::StorageType type, | |
| 135 int64, | |
| 136 int64) { | |
| 137 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 138 storage::UsageTracker* tracker = manager_->GetUsageTracker(type); | |
| 139 DCHECK(tracker); | |
| 140 tracker->GetCachedOriginsUsage(&usage_map_); | |
| 141 | |
| 142 if (--remaining_tasks_ == 0) | |
| 143 OnQuotaTasksCompleted(); | |
| 144 } | |
| 145 | |
| 146 void SiteEngagementEvictionPolicy::DidGetGlobalQuota( | |
| 147 storage::QuotaStatusCode status, | |
| 148 int64 quota) { | |
| 149 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 150 if (status == storage::kQuotaStatusOk) | |
| 151 global_quota_ = quota; | |
| 152 | |
| 153 if (--remaining_tasks_ == 0) | |
| 154 OnQuotaTasksCompleted(); | |
| 155 } | |
| 156 | |
| 157 void SiteEngagementEvictionPolicy::OnQuotaTasksCompleted() { | |
| 158 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 159 | |
| 101 content::BrowserThread::PostTaskAndReplyWithResult( | 160 content::BrowserThread::PostTaskAndReplyWithResult( |
| 102 content::BrowserThread::UI, FROM_HERE, | 161 content::BrowserThread::UI, FROM_HERE, |
| 103 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, | 162 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| 104 special_storage_policy, browser_context_, usage_map, | 163 special_storage_policy_, browser_context_, usage_map_, |
| 105 global_quota), | 164 global_quota_), |
| 106 callback); | 165 eviction_origin_callback_); |
| 107 } | 166 } |
| 108 | 167 |
| 109 // static | 168 // static |
| 110 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin( | 169 void SiteEngagementEvictionPolicy:: |
| 170 SetSiteEngagementScoreProviderCallbackForTests( | |
| 171 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>* | |
| 172 callback) { | |
| 173 g_test_score_provider_callback = callback; | |
| 174 } | |
| 175 | |
| 176 // static | |
| 177 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( | |
| 111 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 178 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 112 SiteEngagementScoreProvider* score_provider, | 179 SiteEngagementScoreProvider* score_provider, |
| 113 const std::map<GURL, int64>& usage_map, | 180 const std::map<GURL, int64>& usage_map, |
| 114 int64 global_quota) { | 181 int64 global_quota) { |
| 115 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | 182 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 116 usage_map, global_quota); | 183 usage_map, global_quota); |
| 117 } | 184 } |
| OLD | NEW |