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" | |
| 6 #include "chrome/browser/browser_process.h" | 5 #include "chrome/browser/browser_process.h" |
| 7 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" | 6 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" |
| 8 #include "chrome/browser/engagement/site_engagement_service.h" | 7 #include "chrome/browser/engagement/site_engagement_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
| 11 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 12 | 11 |
| 13 namespace { | 12 namespace { |
| 14 | 13 |
| 14 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>* | |
| 15 g_test_score_provider_callback = nullptr; | |
| 16 | |
| 15 const int kExpectedEngagementSites = 200; | 17 const int kExpectedEngagementSites = 200; |
| 16 | 18 |
| 17 // Gets the quota that an origin deserves based on its site engagement. | 19 // Gets the quota that an origin deserves based on its site engagement. |
| 18 int64 GetSoftQuotaForOrigin(const GURL& origin, | 20 int64 GetSoftQuotaForOrigin(const GURL& origin, |
| 19 int score, | 21 int score, |
| 20 int total_engagement_points, | 22 int total_engagement_points, |
| 21 int64 global_quota) { | 23 int64 global_quota) { |
| 22 double quota_per_point = | 24 double quota_per_point = |
| 23 global_quota / | 25 global_quota / |
| 24 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints, | 26 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 | 59 |
| 58 // |overuse| can be negative if the soft quota exceeds the usage. | 60 // |overuse| can be negative if the soft quota exceeds the usage. |
| 59 int64 overuse = usage.second - GetSoftQuotaForOrigin( | 61 int64 overuse = usage.second - GetSoftQuotaForOrigin( |
| 60 origin, score_provider->GetScore(origin), | 62 origin, score_provider->GetScore(origin), |
| 61 total_engagement_points, global_quota); | 63 total_engagement_points, global_quota); |
| 62 if (overuse > max_overuse) { | 64 if (overuse > max_overuse) { |
| 63 max_overuse = overuse; | 65 max_overuse = overuse; |
| 64 origin_to_evict = origin; | 66 origin_to_evict = origin; |
| 65 } | 67 } |
| 66 } | 68 } |
| 69 | |
| 67 return origin_to_evict; | 70 return origin_to_evict; |
| 68 } | 71 } |
| 69 | 72 |
| 70 GURL GetSiteEngagementEvictionOriginOnUIThread( | 73 GURL GetSiteEngagementEvictionOriginOnUIThread( |
| 71 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 74 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 72 content::BrowserContext* browser_context, | 75 content::BrowserContext* browser_context, |
| 73 const std::map<GURL, int64>& usage_map, | 76 const std::map<GURL, int64>& usage_map, |
| 74 int64 global_quota) { | 77 int64 global_quota) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 76 Profile* profile = Profile::FromBrowserContext(browser_context); | 79 |
| 77 SiteEngagementService* service = | 80 SiteEngagementScoreProvider* score_provider = nullptr; |
| 78 g_browser_process->profile_manager()->IsValidProfile(profile) | 81 if (g_test_score_provider_callback) { |
| 79 ? SiteEngagementService::Get(profile) | 82 score_provider = g_test_score_provider_callback->Run(browser_context); |
| 80 : nullptr; | 83 } else { |
| 81 if (!service) | 84 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 85 score_provider = | |
| 86 g_browser_process->profile_manager()->IsValidProfile(profile) | |
| 87 ? SiteEngagementService::Get(profile) | |
| 88 : nullptr; | |
| 89 } | |
| 90 | |
| 91 if (!score_provider) | |
| 82 return GURL(); | 92 return GURL(); |
| 83 | 93 |
| 84 return DoCalculateEvictionOrigin(special_storage_policy, service, usage_map, | 94 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 85 global_quota); | 95 usage_map, global_quota); |
| 86 } | 96 } |
| 87 | 97 |
| 88 } // namespace | 98 } // namespace |
| 89 | 99 |
| 90 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | 100 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| 91 content::BrowserContext* browser_context) | 101 content::BrowserContext* browser_context) |
| 92 : browser_context_(browser_context) {} | 102 : browser_context_(browser_context) {} |
| 93 | 103 |
| 94 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} | 104 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} |
| 95 | 105 |
| 96 void SiteEngagementEvictionPolicy::GetEvictionOrigin( | 106 void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| 107 scoped_ptr<QuotaEvictionPolicy::Info> info, | |
| 97 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 108 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) { | 109 const storage::GetOriginCallback& callback) { |
| 110 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 111 | |
| 101 content::BrowserThread::PostTaskAndReplyWithResult( | 112 content::BrowserThread::PostTaskAndReplyWithResult( |
| 102 content::BrowserThread::UI, FROM_HERE, | 113 content::BrowserThread::UI, FROM_HERE, |
| 103 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, | 114 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| 104 special_storage_policy, browser_context_, usage_map, | 115 special_storage_policy, browser_context_, |
| 105 global_quota), | 116 info->origin_usage_map, info->global_quota), |
|
michaeln
2015/10/06 00:35:09
I thought you might have created the |info| struct
calamity
2015/10/07 02:42:46
Done.
| |
| 106 callback); | 117 callback); |
| 107 } | 118 } |
| 108 | 119 |
| 120 bool SiteEngagementEvictionPolicy::NeedsOriginUsageMap() { | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 bool SiteEngagementEvictionPolicy::NeedsGlobalQuota() { | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 109 // static | 128 // static |
| 110 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin( | 129 void SiteEngagementEvictionPolicy:: |
| 130 SetSiteEngagementScoreProviderCallbackForTests( | |
| 131 base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>* | |
| 132 callback) { | |
| 133 g_test_score_provider_callback = callback; | |
| 134 } | |
| 135 | |
| 136 // static | |
| 137 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( | |
| 111 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 138 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 112 SiteEngagementScoreProvider* score_provider, | 139 SiteEngagementScoreProvider* score_provider, |
| 113 const std::map<GURL, int64>& usage_map, | 140 const std::map<GURL, int64>& usage_map, |
| 114 int64 global_quota) { | 141 int64 global_quota) { |
| 115 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | 142 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 116 usage_map, global_quota); | 143 usage_map, global_quota); |
| 117 } | 144 } |
| OLD | NEW |