| 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 |
| 15 const int kExpectedEngagementSites = 200; | 14 const int kExpectedEngagementSites = 200; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 56 |
| 58 // |overuse| can be negative if the soft quota exceeds the usage. | 57 // |overuse| can be negative if the soft quota exceeds the usage. |
| 59 int64 overuse = usage.second - GetSoftQuotaForOrigin( | 58 int64 overuse = usage.second - GetSoftQuotaForOrigin( |
| 60 origin, score_provider->GetScore(origin), | 59 origin, score_provider->GetScore(origin), |
| 61 total_engagement_points, global_quota); | 60 total_engagement_points, global_quota); |
| 62 if (overuse > max_overuse) { | 61 if (overuse > max_overuse) { |
| 63 max_overuse = overuse; | 62 max_overuse = overuse; |
| 64 origin_to_evict = origin; | 63 origin_to_evict = origin; |
| 65 } | 64 } |
| 66 } | 65 } |
| 66 |
| 67 return origin_to_evict; | 67 return origin_to_evict; |
| 68 } | 68 } |
| 69 | 69 |
| 70 GURL GetSiteEngagementEvictionOriginOnUIThread( | 70 GURL GetSiteEngagementEvictionOriginOnUIThread( |
| 71 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 71 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 72 content::BrowserContext* browser_context, | 72 content::BrowserContext* browser_context, |
| 73 const std::map<GURL, int64>& usage_map, | 73 const std::map<GURL, int64>& usage_map, |
| 74 int64 global_quota) { | 74 int64 global_quota) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 76 |
| 76 Profile* profile = Profile::FromBrowserContext(browser_context); | 77 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 77 SiteEngagementService* service = | 78 SiteEngagementScoreProvider* score_provider = |
| 78 g_browser_process->profile_manager()->IsValidProfile(profile) | 79 g_browser_process->profile_manager()->IsValidProfile(profile) |
| 79 ? SiteEngagementService::Get(profile) | 80 ? SiteEngagementService::Get(profile) |
| 80 : nullptr; | 81 : nullptr; |
| 81 if (!service) | 82 |
| 83 if (!score_provider) |
| 82 return GURL(); | 84 return GURL(); |
| 83 | 85 |
| 84 return DoCalculateEvictionOrigin(special_storage_policy, service, usage_map, | 86 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 85 global_quota); | 87 usage_map, global_quota); |
| 86 } | 88 } |
| 87 | 89 |
| 88 } // namespace | 90 } // namespace |
| 89 | 91 |
| 90 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | 92 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| 91 content::BrowserContext* browser_context) | 93 content::BrowserContext* browser_context) |
| 92 : browser_context_(browser_context) {} | 94 : browser_context_(browser_context) {} |
| 93 | 95 |
| 94 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} | 96 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} |
| 95 | 97 |
| 96 void SiteEngagementEvictionPolicy::GetEvictionOrigin( | 98 void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| 97 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 99 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 98 const std::map<GURL, int64>& usage_map, | 100 const std::map<GURL, int64>& usage_map, |
| 99 int64 global_quota, | 101 int64 global_quota, |
| 100 const storage::GetOriginCallback& callback) { | 102 const storage::GetOriginCallback& callback) { |
| 103 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 104 |
| 101 content::BrowserThread::PostTaskAndReplyWithResult( | 105 content::BrowserThread::PostTaskAndReplyWithResult( |
| 102 content::BrowserThread::UI, FROM_HERE, | 106 content::BrowserThread::UI, FROM_HERE, |
| 103 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, | 107 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| 104 special_storage_policy, browser_context_, usage_map, | 108 special_storage_policy, browser_context_, usage_map, |
| 105 global_quota), | 109 global_quota), |
| 106 callback); | 110 callback); |
| 107 } | 111 } |
| 108 | 112 |
| 109 // static | 113 // static |
| 110 GURL SiteEngagementEvictionPolicy::CalculateEvictionOrigin( | 114 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( |
| 111 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 115 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 112 SiteEngagementScoreProvider* score_provider, | 116 SiteEngagementScoreProvider* score_provider, |
| 113 const std::map<GURL, int64>& usage_map, | 117 const std::map<GURL, int64>& usage_map, |
| 114 int64 global_quota) { | 118 int64 global_quota) { |
| 115 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | 119 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 116 usage_map, global_quota); | 120 usage_map, global_quota); |
| 117 } | 121 } |
| OLD | NEW |