| 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 "chrome/browser/browser_process.h" | 5 #include "chrome/browser/browser_process.h" |
| 6 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" | 6 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" |
| 7 #include "chrome/browser/engagement/site_engagement_service.h" | 7 #include "chrome/browser/engagement/site_engagement_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 global_quota / | 22 global_quota / |
| 23 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints, | 23 std::max(kExpectedEngagementSites * SiteEngagementScore::kMaxPoints, |
| 24 static_cast<double>(total_engagement_points)); | 24 static_cast<double>(total_engagement_points)); |
| 25 | 25 |
| 26 return score * quota_per_point; | 26 return score * quota_per_point; |
| 27 } | 27 } |
| 28 | 28 |
| 29 GURL DoCalculateEvictionOrigin( | 29 GURL DoCalculateEvictionOrigin( |
| 30 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 30 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 31 SiteEngagementScoreProvider* score_provider, | 31 SiteEngagementScoreProvider* score_provider, |
| 32 const std::set<GURL>& exceptions, |
| 32 const std::map<GURL, int64>& usage_map, | 33 const std::map<GURL, int64>& usage_map, |
| 33 int64 global_quota) { | 34 int64 global_quota) { |
| 34 // TODO(calamity): Integrate storage access frequency as an input to this | 35 // TODO(calamity): Integrate storage access frequency as an input to this |
| 35 // heuristic. | 36 // heuristic. |
| 36 | 37 |
| 37 // This heuristic is intended to optimize for two criteria: | 38 // This heuristic is intended to optimize for two criteria: |
| 38 // - evict the site that the user cares about least | 39 // - evict the site that the user cares about least |
| 39 // - evict the least number of sites to get under the quota limit | 40 // - evict the least number of sites to get under the quota limit |
| 40 // | 41 // |
| 41 // The heuristic for deciding the next eviction origin calculates a soft | 42 // The heuristic for deciding the next eviction origin calculates a soft |
| 42 // quota for each origin which is the amount the origin should be allowed to | 43 // quota for each origin which is the amount the origin should be allowed to |
| 43 // use based on its engagement and the global quota. The origin that most | 44 // use based on its engagement and the global quota. The origin that most |
| 44 // exceeds its soft quota is chosen. | 45 // exceeds its soft quota is chosen. |
| 45 GURL origin_to_evict; | 46 GURL origin_to_evict; |
| 46 int64 max_overuse = std::numeric_limits<int64>::min(); | 47 int64 max_overuse = std::numeric_limits<int64>::min(); |
| 47 int total_engagement_points = score_provider->GetTotalEngagementPoints(); | 48 int total_engagement_points = score_provider->GetTotalEngagementPoints(); |
| 48 | 49 |
| 49 for (const auto& usage : usage_map) { | 50 for (const auto& usage : usage_map) { |
| 50 GURL origin = usage.first; | 51 GURL origin = usage.first; |
| 51 if (special_storage_policy && | 52 if (special_storage_policy && |
| 52 (special_storage_policy->IsStorageUnlimited(origin) || | 53 (special_storage_policy->IsStorageUnlimited(origin) || |
| 53 special_storage_policy->IsStorageDurable(origin))) { | 54 special_storage_policy->IsStorageDurable(origin))) { |
| 54 continue; | 55 continue; |
| 55 } | 56 } |
| 56 | 57 |
| 57 // |overuse| can be negative if the soft quota exceeds the usage. | 58 // |overuse| can be negative if the soft quota exceeds the usage. |
| 58 int64 overuse = usage.second - GetSoftQuotaForOrigin( | 59 int64 overuse = usage.second - GetSoftQuotaForOrigin( |
| 59 origin, score_provider->GetScore(origin), | 60 origin, score_provider->GetScore(origin), |
| 60 total_engagement_points, global_quota); | 61 total_engagement_points, global_quota); |
| 61 if (overuse > max_overuse) { | 62 if (overuse > max_overuse && !ContainsKey(exceptions, origin)) { |
| 62 max_overuse = overuse; | 63 max_overuse = overuse; |
| 63 origin_to_evict = origin; | 64 origin_to_evict = origin; |
| 64 } | 65 } |
| 65 } | 66 } |
| 66 | 67 |
| 67 return origin_to_evict; | 68 return origin_to_evict; |
| 68 } | 69 } |
| 69 | 70 |
| 70 GURL GetSiteEngagementEvictionOriginOnUIThread( | 71 GURL GetSiteEngagementEvictionOriginOnUIThread( |
| 71 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 72 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 72 content::BrowserContext* browser_context, | 73 content::BrowserContext* browser_context, |
| 74 const std::set<GURL>& exceptions, |
| 73 const std::map<GURL, int64>& usage_map, | 75 const std::map<GURL, int64>& usage_map, |
| 74 int64 global_quota) { | 76 int64 global_quota) { |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 77 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 76 | 78 |
| 77 Profile* profile = Profile::FromBrowserContext(browser_context); | 79 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 78 SiteEngagementScoreProvider* score_provider = | 80 SiteEngagementScoreProvider* score_provider = |
| 79 g_browser_process->profile_manager()->IsValidProfile(profile) | 81 g_browser_process->profile_manager()->IsValidProfile(profile) |
| 80 ? SiteEngagementService::Get(profile) | 82 ? SiteEngagementService::Get(profile) |
| 81 : nullptr; | 83 : nullptr; |
| 82 | 84 |
| 83 if (!score_provider) | 85 if (!score_provider) |
| 84 return GURL(); | 86 return GURL(); |
| 85 | 87 |
| 86 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | 88 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 87 usage_map, global_quota); | 89 exceptions, usage_map, global_quota); |
| 88 } | 90 } |
| 89 | 91 |
| 90 } // namespace | 92 } // namespace |
| 91 | 93 |
| 92 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | 94 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( |
| 93 content::BrowserContext* browser_context) | 95 content::BrowserContext* browser_context) |
| 94 : browser_context_(browser_context) {} | 96 : browser_context_(browser_context) {} |
| 95 | 97 |
| 96 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} | 98 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} |
| 97 | 99 |
| 98 void SiteEngagementEvictionPolicy::GetEvictionOrigin( | 100 void SiteEngagementEvictionPolicy::GetEvictionOrigin( |
| 99 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 101 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 102 const std::set<GURL>& exceptions, |
| 100 const std::map<GURL, int64>& usage_map, | 103 const std::map<GURL, int64>& usage_map, |
| 101 int64 global_quota, | 104 int64 global_quota, |
| 102 const storage::GetOriginCallback& callback) { | 105 const storage::GetOriginCallback& callback) { |
| 103 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 106 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 104 | 107 |
| 105 content::BrowserThread::PostTaskAndReplyWithResult( | 108 content::BrowserThread::PostTaskAndReplyWithResult( |
| 106 content::BrowserThread::UI, FROM_HERE, | 109 content::BrowserThread::UI, FROM_HERE, |
| 107 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, | 110 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, |
| 108 special_storage_policy, browser_context_, usage_map, | 111 special_storage_policy, browser_context_, exceptions, |
| 109 global_quota), | 112 usage_map, global_quota), |
| 110 callback); | 113 callback); |
| 111 } | 114 } |
| 112 | 115 |
| 113 // static | 116 // static |
| 114 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( | 117 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( |
| 115 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | 118 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, |
| 116 SiteEngagementScoreProvider* score_provider, | 119 SiteEngagementScoreProvider* score_provider, |
| 120 const std::set<GURL>& exceptions, |
| 117 const std::map<GURL, int64>& usage_map, | 121 const std::map<GURL, int64>& usage_map, |
| 118 int64 global_quota) { | 122 int64 global_quota) { |
| 119 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | 123 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, |
| 120 usage_map, global_quota); | 124 exceptions, usage_map, global_quota); |
| 121 } | 125 } |
| OLD | NEW |