OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/engagement/site_engagement_eviction_policy.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/metrics/field_trial.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/engagement/site_engagement_service.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/profiles/profile_manager.h" | |
14 #include "chrome/common/chrome_switches.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 | |
17 namespace { | |
18 | |
19 const int kExpectedEngagementSites = 200; | |
20 | |
21 // Gets the quota that an origin deserves based on its site engagement. | |
22 int64_t GetSoftQuotaForOrigin(const GURL& origin, | |
23 int score, | |
24 int total_engagement_points, | |
25 int64_t global_quota) { | |
26 double quota_per_point = | |
27 global_quota / | |
28 std::max(kExpectedEngagementSites * SiteEngagementService::GetMaxPoints(), | |
29 static_cast<double>(total_engagement_points)); | |
30 | |
31 return score * quota_per_point; | |
32 } | |
33 | |
34 GURL DoCalculateEvictionOrigin( | |
35 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | |
36 SiteEngagementScoreProvider* score_provider, | |
37 const std::set<GURL>& exceptions, | |
38 const std::map<GURL, int64_t>& usage_map, | |
39 int64_t global_quota) { | |
40 // TODO(calamity): Integrate storage access frequency as an input to this | |
41 // heuristic. | |
42 | |
43 // This heuristic is intended to optimize for two criteria: | |
44 // - evict the site that the user cares about least | |
45 // - evict the least number of sites to get under the quota limit | |
46 // | |
47 // The heuristic for deciding the next eviction origin calculates a soft | |
48 // quota for each origin which is the amount the origin should be allowed to | |
49 // use based on its engagement and the global quota. The origin that most | |
50 // exceeds its soft quota is chosen. | |
51 GURL origin_to_evict; | |
52 int64_t max_overuse = std::numeric_limits<int64_t>::min(); | |
53 int total_engagement_points = score_provider->GetTotalEngagementPoints(); | |
54 | |
55 for (const auto& usage : usage_map) { | |
56 GURL origin = usage.first; | |
57 if (special_storage_policy && | |
58 (special_storage_policy->IsStorageUnlimited(origin) || | |
59 special_storage_policy->IsStorageDurable(origin))) { | |
60 continue; | |
61 } | |
62 | |
63 // |overuse| can be negative if the soft quota exceeds the usage. | |
64 int64_t overuse = | |
65 usage.second - | |
66 GetSoftQuotaForOrigin(origin, score_provider->GetScore(origin), | |
67 total_engagement_points, global_quota); | |
68 if (overuse > max_overuse && !base::ContainsKey(exceptions, origin)) { | |
69 max_overuse = overuse; | |
70 origin_to_evict = origin; | |
71 } | |
72 } | |
73 | |
74 return origin_to_evict; | |
75 } | |
76 | |
77 GURL GetSiteEngagementEvictionOriginOnUIThread( | |
78 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | |
79 content::BrowserContext* browser_context, | |
80 const std::set<GURL>& exceptions, | |
81 const std::map<GURL, int64_t>& usage_map, | |
82 int64_t global_quota) { | |
83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
84 | |
85 Profile* profile = Profile::FromBrowserContext(browser_context); | |
86 SiteEngagementScoreProvider* score_provider = | |
87 g_browser_process->profile_manager()->IsValidProfile(profile) | |
88 ? SiteEngagementService::Get(profile) | |
89 : nullptr; | |
90 | |
91 if (!score_provider) | |
92 return GURL(); | |
93 | |
94 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | |
95 exceptions, usage_map, global_quota); | |
96 } | |
97 | |
98 } // namespace | |
99 | |
100 // static | |
101 bool SiteEngagementEvictionPolicy::IsEnabled() { | |
102 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
103 switches::kEnableSiteEngagementEvictionPolicy)) { | |
104 return true; | |
105 } | |
106 | |
107 const std::string group_name = base::FieldTrialList::FindFullName( | |
108 SiteEngagementService::kEngagementParams); | |
109 return base::StartsWith(group_name, "StorageEvictionEnabled", | |
110 base::CompareCase::SENSITIVE); | |
111 } | |
112 | |
113 SiteEngagementEvictionPolicy::SiteEngagementEvictionPolicy( | |
114 content::BrowserContext* browser_context) | |
115 : browser_context_(browser_context) {} | |
116 | |
117 SiteEngagementEvictionPolicy::~SiteEngagementEvictionPolicy() {} | |
118 | |
119 void SiteEngagementEvictionPolicy::GetEvictionOrigin( | |
120 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | |
121 const std::set<GURL>& exceptions, | |
122 const std::map<GURL, int64_t>& usage_map, | |
123 int64_t global_quota, | |
124 const storage::GetOriginCallback& callback) { | |
125 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
126 | |
127 content::BrowserThread::PostTaskAndReplyWithResult( | |
128 content::BrowserThread::UI, FROM_HERE, | |
129 base::Bind(&GetSiteEngagementEvictionOriginOnUIThread, | |
130 special_storage_policy, browser_context_, exceptions, | |
131 usage_map, global_quota), | |
132 callback); | |
133 } | |
134 | |
135 // static | |
136 GURL SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests( | |
137 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy, | |
138 SiteEngagementScoreProvider* score_provider, | |
139 const std::set<GURL>& exceptions, | |
140 const std::map<GURL, int64_t>& usage_map, | |
141 int64_t global_quota) { | |
142 return DoCalculateEvictionOrigin(special_storage_policy, score_provider, | |
143 exceptions, usage_map, global_quota); | |
144 } | |
OLD | NEW |