OLD | NEW |
| (Empty) |
1 // Copyright 2016 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/budget_service/background_budget_service.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include "base/callback.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/metrics/histogram_macros.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/string_split.h" | |
14 #include "base/strings/stringprintf.h" | |
15 #include "base/time/clock.h" | |
16 #include "base/time/default_clock.h" | |
17 #include "base/time/time.h" | |
18 #include "chrome/browser/engagement/site_engagement_score.h" | |
19 #include "chrome/browser/engagement/site_engagement_service.h" | |
20 #include "chrome/browser/profiles/profile.h" | |
21 #include "chrome/common/pref_names.h" | |
22 #include "components/pref_registry/pref_registry_syncable.h" | |
23 #include "components/prefs/pref_service.h" | |
24 #include "components/prefs/scoped_user_pref_update.h" | |
25 #include "content/public/browser/browser_thread.h" | |
26 | |
27 using content::BrowserThread; | |
28 | |
29 namespace { | |
30 | |
31 constexpr char kSeparator = '#'; | |
32 | |
33 // Calculate the ratio of the different components of a budget with respect | |
34 // to a maximum time period of 10 days = 864000.0 seconds. | |
35 constexpr double kSecondsToAccumulate = 864000.0; | |
36 | |
37 bool GetBudgetDataFromPrefs(Profile* profile, | |
38 const GURL& origin, | |
39 double* old_budget, | |
40 double* old_ses, | |
41 double* last_updated) { | |
42 const base::DictionaryValue* map = | |
43 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); | |
44 | |
45 std::string map_string; | |
46 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); | |
47 | |
48 // There is no data for the preference, return false and let the caller | |
49 // deal with that. | |
50 if (map_string.empty()) | |
51 return false; | |
52 | |
53 std::vector<base::StringPiece> parts = | |
54 base::SplitStringPiece(map_string, base::StringPiece(&kSeparator, 1), | |
55 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
56 if ((parts.size() != 3) || | |
57 (!base::StringToDouble(parts[0].as_string(), last_updated)) || | |
58 (!base::StringToDouble(parts[1].as_string(), old_budget)) || | |
59 (!base::StringToDouble(parts[2].as_string(), old_ses))) { | |
60 // Somehow the data stored in the preferences has become corrupted, log an | |
61 // error and remove the invalid data. | |
62 LOG(ERROR) << "Preferences data for background budget service is " | |
63 << "invalid for origin " << origin.possibly_invalid_spec(); | |
64 DictionaryPrefUpdate update(profile->GetPrefs(), | |
65 prefs::kBackgroundBudgetMap); | |
66 base::DictionaryValue* update_map = update.Get(); | |
67 update_map->RemoveWithoutPathExpansion(origin.spec(), nullptr); | |
68 return false; | |
69 } | |
70 | |
71 return true; | |
72 } | |
73 | |
74 // The value stored in prefs is a concatenated string of last updated time, old | |
75 // budget, and old ses. | |
76 void SetBudgetDataInPrefs(Profile* profile, | |
77 const GURL& origin, | |
78 double last_updated, | |
79 double budget, | |
80 double ses) { | |
81 std::string s = base::StringPrintf("%f%c%f%c%f", last_updated, kSeparator, | |
82 budget, kSeparator, ses); | |
83 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); | |
84 base::DictionaryValue* map = update.Get(); | |
85 | |
86 map->SetStringWithoutPathExpansion(origin.spec(), s); | |
87 } | |
88 | |
89 } // namespace | |
90 | |
91 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) | |
92 : clock_(base::WrapUnique(new base::DefaultClock)), profile_(profile) { | |
93 DCHECK(profile); | |
94 } | |
95 | |
96 BackgroundBudgetService::~BackgroundBudgetService() {} | |
97 | |
98 // static | |
99 void BackgroundBudgetService::RegisterProfilePrefs( | |
100 user_prefs::PrefRegistrySyncable* registry) { | |
101 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | |
102 } | |
103 | |
104 // static | |
105 double BackgroundBudgetService::GetCost(CostType type) { | |
106 switch (type) { | |
107 case CostType::SILENT_PUSH: | |
108 return 2.0; | |
109 // No default case. | |
110 } | |
111 NOTREACHED(); | |
112 return SiteEngagementScore::kMaxPoints + 1.0; | |
113 } | |
114 | |
115 void BackgroundBudgetService::GetBudget(const GURL& origin, | |
116 const GetBudgetCallback& callback) { | |
117 DCHECK_EQ(origin, origin.GetOrigin()); | |
118 | |
119 // Get the current SES score, which we'll use to set a new budget. | |
120 SiteEngagementService* service = SiteEngagementService::Get(profile_); | |
121 double ses_score = service->GetScore(origin); | |
122 | |
123 // Get the last used budget data. This is a triple of last calculated time, | |
124 // budget at that time, and Site Engagement Score (ses) at that time. | |
125 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; | |
126 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses, | |
127 &last_updated_msec)) { | |
128 // If there is no stored data or the data can't be parsed, just return the | |
129 // SES. | |
130 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
131 base::Bind(callback, ses_score)); | |
132 return; | |
133 } | |
134 | |
135 base::Time now = clock_->Now(); | |
136 base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec); | |
137 | |
138 // The user can set their clock backwards, so if the last updated time is in | |
139 // the future, don't update the budget based on elapsed time. Eventually the | |
140 // clock will reach the future, and the budget calculations will catch up. | |
141 // TODO(harkness): Consider what to do if the clock jumps forward by a | |
142 // significant amount. | |
143 if (elapsed.InMicroseconds() < 0) { | |
144 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
145 base::Bind(callback, old_budget)); | |
146 return; | |
147 } | |
148 | |
149 // For each time period that elapses, calculate the carryover ratio as the | |
150 // ratio of time remaining in our max period to the total period. | |
151 // The carryover component is then the old budget multiplied by the ratio. | |
152 double carryover_ratio = std::max( | |
153 0.0, | |
154 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); | |
155 double budget_carryover = old_budget * carryover_ratio; | |
156 | |
157 // The ses component is an average of the last ses score used for budget | |
158 // calculation and the current ses score. | |
159 // The ses average is them multiplied by the ratio of time elapsed to the | |
160 // total period. | |
161 double ses_ratio = | |
162 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); | |
163 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; | |
164 | |
165 // Budget recalculation consists of a budget carryover component, which | |
166 // rewards sites that don't use all their budgets every day, and a ses | |
167 // component, which gives extra budget to sites that have a high ses score. | |
168 double budget = budget_carryover + ses_component; | |
169 DCHECK_GE(budget, 0.0); | |
170 | |
171 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
172 base::Bind(callback, budget)); | |
173 } | |
174 | |
175 void BackgroundBudgetService::StoreBudget(const GURL& origin, | |
176 double budget, | |
177 const base::Closure& closure) { | |
178 DCHECK_EQ(origin, origin.GetOrigin()); | |
179 DCHECK_GE(budget, 0.0); | |
180 DCHECK_LE(budget, SiteEngagementService::GetMaxPoints()); | |
181 | |
182 // Get the current SES score to write into the prefs with the new budget. | |
183 SiteEngagementService* service = SiteEngagementService::Get(profile_); | |
184 double ses_score = service->GetScore(origin); | |
185 | |
186 base::Time time = clock_->Now(); | |
187 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); | |
188 | |
189 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(closure)); | |
190 } | |
191 | |
192 // Override the default clock with the specified clock. Only used for testing. | |
193 void BackgroundBudgetService::SetClockForTesting( | |
194 std::unique_ptr<base::Clock> clock) { | |
195 clock_ = std::move(clock); | |
196 } | |
OLD | NEW |