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 |
| 15 const int kExpectedEngagementSites = 200; | 16 const int kExpectedEngagementSites = 200; |
| 16 | 17 |
| 17 // Gets the quota that an origin deserves based on its site engagement. | 18 // Gets the quota that an origin deserves based on its site engagement. |
| 18 int64 GetSoftQuotaForOrigin(const GURL& origin, | 19 int64 GetSoftQuotaForOrigin(const GURL& origin, |
| 19 int score, | 20 int score, |
| 20 int total_engagement_points, | 21 int total_engagement_points, |
| 21 int64 global_quota) { | 22 int64 global_quota) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 origin, score_provider->GetScore(origin), | 61 origin, score_provider->GetScore(origin), |
| 61 total_engagement_points, global_quota); | 62 total_engagement_points, global_quota); |
| 62 if (overuse > max_overuse) { | 63 if (overuse > max_overuse) { |
| 63 max_overuse = overuse; | 64 max_overuse = overuse; |
| 64 origin_to_evict = origin; | 65 origin_to_evict = origin; |
| 65 } | 66 } |
| 66 } | 67 } |
| 67 return origin_to_evict; | 68 return origin_to_evict; |
| 68 } | 69 } |
| 69 | 70 |
| 71 SiteEngagementScoreProvider* GetSiteEngagementService( | |
| 72 content::BrowserContext* browser_context) { | |
| 73 Profile* profile = Profile::FromBrowserContext(browser_context); | |
| 74 return g_browser_process->profile_manager()->IsValidProfile(profile) | |
| 75 ? SiteEngagementService::Get(profile) | |
| 76 : nullptr; | |
| 77 } | |
| 78 | |
| 70 GURL GetSiteEngagementEvictionOriginOnUIThread( | 79 GURL GetSiteEngagementEvictionOriginOnUIThread( |
| 71 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 80 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 72 content::BrowserContext* browser_context, | 81 base::Callback<SiteEngagementScoreProvider*()> get_score_provider_callback, |
| 73 const std::map<GURL, int64>& usage_map, | 82 const std::map<GURL, int64>& usage_map, |
| 74 int64 global_quota) { | 83 int64 global_quota) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 76 Profile* profile = Profile::FromBrowserContext(browser_context); | 85 |
| 77 SiteEngagementService* service = | 86 SiteEngagementScoreProvider* score_provider = |
| 78 g_browser_process->profile_manager()->IsValidProfile(profile) | 87 get_score_provider_callback.Run(); |
| 79 ? SiteEngagementService::Get(profile) | 88 if (!score_provider) |
| 80 : nullptr; | |
| 81 if (!service) | |
| 82 return GURL(); | 89 return GURL(); |
| 83 | 90 |
| 84 return DoCalculateEvictionOrigin(special_storage_policy, service, usage_map, | 91 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 85 global_quota); | 92 usage_map, global_quota); |
| 86 } | 93 } |
| 87 | 94 |
| 88 } // namespace | 95 } // namespace |
| 89 | 96 |
| 97 // static | |
| 98 CR_DEFINE_STATIC_LOCAL( | |
|
raymes
2015/09/17 04:14:30
I think this should only be called from within a f
calamity
2015/09/17 06:06:46
Cleaned this up as discussed.
| |
| 99 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>, | |
| 100 score_provider_callback_, | |
|
raymes
2015/09/17 04:14:31
I believe the trailing underscore style is only us
calamity
2015/09/17 06:06:46
Fixed.
| |
| 101 ()); | |
| 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), | |
| 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() {} |
| 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) { |
|
raymes
2015/09/17 04:14:30
Sorry I can't recall which threads this stuff gets
calamity
2015/09/17 06:06:46
Yep, added DCHECKs to document.
Tests now use Thr
| |
| 119 DCHECK_EQ(0, remaining_tasks_); | |
| 120 | |
| 121 remaining_tasks_ = 2; | |
| 122 eviction_origin_callback_ = callback; | |
| 123 | |
| 124 // This will populate cached hosts and usage info. | |
| 125 manager_->GetUsageTracker(type_)->GetGlobalUsage( | |
| 126 base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalUsage, | |
| 127 weak_factory_.GetWeakPtr(), type_)); | |
| 128 manager_->GetTemporaryGlobalQuota( | |
| 129 base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalQuota, | |
| 130 weak_factory_.GetWeakPtr())); | |
| 131 } | |
| 132 | |
| 133 void SiteEngagementEvictionPolicy::DidGetGlobalUsage(storage::StorageType type, | |
| 134 int64, | |
| 135 int64) { | |
| 136 storage::UsageTracker* tracker = 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 if (status == storage::kQuotaStatusOk) | |
| 148 global_quota_ = quota; | |
| 149 | |
| 150 if (--remaining_tasks_ == 0) | |
| 151 OnQuotaTasksCompleted(); | |
| 152 } | |
| 153 | |
| 154 void SiteEngagementEvictionPolicy::OnQuotaTasksCompleted() { | |
| 155 base::Callback<SiteEngagementScoreProvider*()> score_provider_callback = | |
| 156 base::Bind(&GetSiteEngagementService, browser_context_); | |
| 157 if (!score_provider_callback_.is_null()) | |
| 158 score_provider_callback = | |
| 159 base::Bind(score_provider_callback_, browser_context_); | |
|
raymes
2015/09/17 04:14:30
nit: {} for multiline if
calamity
2015/09/17 06:06:46
Removed.
| |
| 160 | |
| 101 content::BrowserThread::PostTaskAndReplyWithResult( | 161 content::BrowserThread::PostTaskAndReplyWithResult( |
| 102 content::BrowserThread::UI, FROM_HERE, | 162 content::BrowserThread::UI, FROM_HERE, |
| 103 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, | 163 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| 104 special_storage_policy, browser_context_, usage_map, | 164 special_storage_policy_, score_provider_callback, |
| 105 global_quota), | 165 usage_map_, global_quota_), |
| 106 callback); | 166 eviction_origin_callback_); |
| 107 } | 167 } |
| 108 | 168 |
| 109 // static | 169 // static |
| 170 void SiteEngagementEvictionPolicy::SetSiteEngagementScoreProviderCallback( | |
|
raymes
2015/09/17 04:14:31
I can't see where this is called from, besides in
calamity
2015/09/17 06:06:46
Yeah, this is only for tests. Updated the name to
| |
| 171 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)> | |
| 172 callback) { | |
| 173 score_provider_callback_ = callback; | |
| 174 } | |
| 175 | |
| 176 // static | |
| 110 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin( | 177 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin( |
|
raymes
2015/09/17 04:14:31
We should probably actually name this CalculateEvi
calamity
2015/09/17 06:06:46
Done.
| |
| 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 |