Chromium Code Reviews| Index: chrome/browser/engagement/site_engagement_eviction_policy.cc |
| diff --git a/chrome/browser/engagement/site_engagement_eviction_policy.cc b/chrome/browser/engagement/site_engagement_eviction_policy.cc |
| index ba7a885fae5d8088978d2e9a18d8627894667c41..ea99604ed279e6dd660bfa7289b2a320577d6793 100644 |
| --- a/chrome/browser/engagement/site_engagement_eviction_policy.cc |
| +++ b/chrome/browser/engagement/site_engagement_eviction_policy.cc |
| @@ -9,9 +9,13 @@ |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/profiles/profile_manager.h" |
| #include "content/public/browser/browser_thread.h" |
| +#include "storage/browser/quota/usage_tracker.h" |
| namespace { |
| +base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>* |
| + g_test_score_provider_callback = nullptr; |
| + |
| const int kExpectedEngagementSites = 200; |
| // Gets the quota that an origin deserves based on its site engagement. |
| @@ -67,47 +71,110 @@ GURL DoCalculateEvictionOrigin( |
| return origin_to_evict; |
| } |
| +SiteEngagementScoreProvider* GetSiteEngagementService( |
| + content::BrowserContext* browser_context) { |
| + Profile* profile = Profile::FromBrowserContext(browser_context); |
| + return g_browser_process->profile_manager()->IsValidProfile(profile) |
| + ? SiteEngagementService::Get(profile) |
| + : nullptr; |
| +} |
| + |
| GURL GetSiteEngagementEvictionOriginOnUIThread( |
| const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| content::BrowserContext* browser_context, |
| const std::map<GURL, int64>& usage_map, |
| int64 global_quota) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| - Profile* profile = Profile::FromBrowserContext(browser_context); |
| - SiteEngagementService* service = |
| - g_browser_process->profile_manager()->IsValidProfile(profile) |
| - ? SiteEngagementService::Get(profile) |
| - : nullptr; |
| - if (!service) |
| + |
| + SiteEngagementScoreProvider* score_provider = |
| + g_test_score_provider_callback |
| + ? g_test_score_provider_callback->Run(browser_context) |
| + : 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
|
| + |
| + if (!score_provider) |
| return GURL(); |
| - return DoCalculateEvictionOrigin(special_storage_policy, service, usage_map, |
| - global_quota); |
| + return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| + usage_map, global_quota); |
| } |
| } // namespace |
| SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| + storage::StorageType type, |
| + storage::QuotaManager* manager, |
| content::BrowserContext* browser_context) |
| - : browser_context_(browser_context) {} |
| + : type_(type), |
| + 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.
|
| + browser_context_(browser_context), |
| + global_quota_(0), |
| + remaining_tasks_(0), |
| + weak_factory_(this) {} |
| 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
|
| void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| - const std::map<GURL, int64>& usage_map, |
| - int64 global_quota, |
| const storage::GetOriginCallback& callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + DCHECK_EQ(0, remaining_tasks_); |
| + |
| + remaining_tasks_ = 2; |
| + eviction_origin_callback_ = callback; |
| + |
| + // This will populate cached hosts and usage info. |
| + manager_->GetUsageTracker(type_)->GetGlobalUsage( |
| + base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalUsage, |
| + weak_factory_.GetWeakPtr(), type_)); |
| + manager_->GetTemporaryGlobalQuota( |
| + base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalQuota, |
| + weak_factory_.GetWeakPtr())); |
| +} |
| + |
| +void SiteEngagementEvictionPolicy::DidGetGlobalUsage(storage::StorageType type, |
| + int64, |
| + int64) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + storage::UsageTracker* tracker = manager_->GetUsageTracker(type); |
| + DCHECK(tracker); |
| + tracker->GetCachedOriginsUsage(&usage_map_); |
| + |
| + if (--remaining_tasks_ == 0) |
| + OnQuotaTasksCompleted(); |
| +} |
| + |
| +void SiteEngagementEvictionPolicy::DidGetGlobalQuota( |
| + storage::QuotaStatusCode status, |
| + int64 quota) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + if (status == storage::kQuotaStatusOk) |
| + global_quota_ = quota; |
| + |
| + if (--remaining_tasks_ == 0) |
| + OnQuotaTasksCompleted(); |
| +} |
| + |
| +void SiteEngagementEvictionPolicy::OnQuotaTasksCompleted() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + |
| content::BrowserThread::PostTaskAndReplyWithResult( |
| content::BrowserThread::UI, FROM_HERE, |
| base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| - special_storage_policy, browser_context_, usage_map, |
| - global_quota), |
| - callback); |
| + special_storage_policy_, browser_context_, usage_map_, |
| + global_quota_), |
| + eviction_origin_callback_); |
| +} |
| + |
| +// static |
| +void SiteEngagementEvictionPolicy:: |
| + SetSiteEngagementScoreProviderCallbackForTests( |
| + base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>* |
| + callback) { |
| + g_test_score_provider_callback = callback; |
| } |
| // static |
| -GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin( |
| +GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( |
| const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| SiteEngagementScoreProvider* score_provider, |
| const std::map<GURL, int64>& usage_map, |