| 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..61b8248a867a5b651b367d21e1b671288dd7b666 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.
|
| @@ -73,41 +77,102 @@ GURL GetSiteEngagementEvictionOriginOnUIThread(
|
| 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 = nullptr;
|
| + if (g_test_score_provider_callback) {
|
| + score_provider = g_test_score_provider_callback->Run(browser_context);
|
| + } else {
|
| + Profile* profile = Profile::FromBrowserContext(browser_context);
|
| + score_provider =
|
| + g_browser_process->profile_manager()->IsValidProfile(profile)
|
| + ? SiteEngagementService::Get(profile)
|
| + : nullptr;
|
| + }
|
| +
|
| + 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* quota_manager,
|
| content::BrowserContext* browser_context)
|
| - : browser_context_(browser_context) {}
|
| + : type_(type),
|
| + quota_manager_(quota_manager),
|
| + browser_context_(browser_context),
|
| + global_quota_(0),
|
| + remaining_tasks_(0),
|
| + weak_factory_(this) {}
|
|
|
| SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {}
|
|
|
| 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.
|
| + quota_manager_->GetUsageTracker(type_)->GetGlobalUsage(
|
| + base::Bind(&SiteEngagementEvictionPolicy::DidGetGlobalUsage,
|
| + weak_factory_.GetWeakPtr(), type_));
|
| + quota_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 = quota_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,
|
|
|