Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: chrome/browser/budget_service/background_budget_service.cc

Issue 2058523003: Make BackgroundBudgetService calls asynchronous. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More test code cleanup Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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/budget_service/background_budget_service.h" 5 #include "chrome/browser/budget_service/background_budget_service.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/time/clock.h" 15 #include "base/time/clock.h"
16 #include "base/time/default_clock.h" 16 #include "base/time/default_clock.h"
17 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "chrome/browser/engagement/site_engagement_score.h" 18 #include "chrome/browser/engagement/site_engagement_score.h"
19 #include "chrome/browser/engagement/site_engagement_service.h" 19 #include "chrome/browser/engagement/site_engagement_service.h"
20 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
22 #include "components/pref_registry/pref_registry_syncable.h" 22 #include "components/pref_registry/pref_registry_syncable.h"
23 #include "components/prefs/pref_service.h" 23 #include "components/prefs/pref_service.h"
24 #include "components/prefs/scoped_user_pref_update.h" 24 #include "components/prefs/scoped_user_pref_update.h"
25 #include "content/public/browser/browser_thread.h"
26
27 using content::BrowserThread;
25 28
26 namespace { 29 namespace {
27 30
28 constexpr char kSeparator = '#'; 31 constexpr char kSeparator = '#';
29 32
30 // Calculate the ratio of the different components of a budget with respect 33 // Calculate the ratio of the different components of a budget with respect
31 // to a maximum time period of 10 days = 864000.0 seconds. 34 // to a maximum time period of 10 days = 864000.0 seconds.
32 constexpr double kSecondsToAccumulate = 864000.0; 35 constexpr double kSecondsToAccumulate = 864000.0;
33 36
34 bool GetBudgetDataFromPrefs(Profile* profile, 37 bool GetBudgetDataFromPrefs(Profile* profile,
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 double BackgroundBudgetService::GetCost(CostType type) { 105 double BackgroundBudgetService::GetCost(CostType type) {
103 switch (type) { 106 switch (type) {
104 case CostType::SILENT_PUSH: 107 case CostType::SILENT_PUSH:
105 return 2.0; 108 return 2.0;
106 // No default case. 109 // No default case.
107 } 110 }
108 NOTREACHED(); 111 NOTREACHED();
109 return SiteEngagementScore::kMaxPoints + 1.0; 112 return SiteEngagementScore::kMaxPoints + 1.0;
110 } 113 }
111 114
112 double BackgroundBudgetService::GetBudget(const GURL& origin) { 115 void BackgroundBudgetService::GetBudget(const GURL& origin,
116 const GetBudgetCallback& callback) {
113 DCHECK_EQ(origin, origin.GetOrigin()); 117 DCHECK_EQ(origin, origin.GetOrigin());
114 118
115 // Get the current SES score, which we'll use to set a new budget. 119 // Get the current SES score, which we'll use to set a new budget.
116 SiteEngagementService* service = SiteEngagementService::Get(profile_); 120 SiteEngagementService* service = SiteEngagementService::Get(profile_);
117 double ses_score = service->GetScore(origin); 121 double ses_score = service->GetScore(origin);
118 122
119 // Get the last used budget data. This is a triple of last calculated time, 123 // Get the last used budget data. This is a triple of last calculated time,
120 // budget at that time, and Site Engagement Score (ses) at that time. 124 // budget at that time, and Site Engagement Score (ses) at that time.
121 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; 125 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0;
122 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses, 126 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses,
123 &last_updated_msec)) { 127 &last_updated_msec)) {
124 // If there is no stored data or the data can't be parsed, just return the 128 // If there is no stored data or the data can't be parsed, just return the
125 // SES. 129 // SES.
126 return ses_score; 130 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
131 base::Bind(callback, ses_score));
132 return;
127 } 133 }
128 134
129 base::Time now = clock_->Now(); 135 base::Time now = clock_->Now();
130 base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec); 136 base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec);
131 137
132 // The user can set their clock backwards, so if the last updated time is in 138 // The user can set their clock backwards, so if the last updated time is in
133 // the future, don't update the budget based on elapsed time. Eventually the 139 // the future, don't update the budget based on elapsed time. Eventually the
134 // clock will reach the future, and the budget calculations will catch up. 140 // clock will reach the future, and the budget calculations will catch up.
135 // TODO(harkness): Consider what to do if the clock jumps forward by a 141 // TODO(harkness): Consider what to do if the clock jumps forward by a
136 // significant amount. 142 // significant amount.
137 if (elapsed.InMicroseconds() < 0) 143 if (elapsed.InMicroseconds() < 0) {
138 return old_budget; 144 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
145 base::Bind(callback, old_budget));
146 return;
147 }
139 148
140 // For each time period that elapses, calculate the carryover ratio as the 149 // For each time period that elapses, calculate the carryover ratio as the
141 // ratio of time remaining in our max period to the total period. 150 // ratio of time remaining in our max period to the total period.
142 // The carryover component is then the old budget multiplied by the ratio. 151 // The carryover component is then the old budget multiplied by the ratio.
143 double carryover_ratio = std::max( 152 double carryover_ratio = std::max(
144 0.0, 153 0.0,
145 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); 154 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate));
146 double budget_carryover = old_budget * carryover_ratio; 155 double budget_carryover = old_budget * carryover_ratio;
147 156
148 // The ses component is an average of the last ses score used for budget 157 // The ses component is an average of the last ses score used for budget
149 // calculation and the current ses score. 158 // calculation and the current ses score.
150 // The ses average is them multiplied by the ratio of time elapsed to the 159 // The ses average is them multiplied by the ratio of time elapsed to the
151 // total period. 160 // total period.
152 double ses_ratio = 161 double ses_ratio =
153 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); 162 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate));
154 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; 163 double ses_component = (old_ses + ses_score) / 2 * ses_ratio;
155 164
156 // Budget recalculation consists of a budget carryover component, which 165 // Budget recalculation consists of a budget carryover component, which
157 // rewards sites that don't use all their budgets every day, and a ses 166 // rewards sites that don't use all their budgets every day, and a ses
158 // component, which gives extra budget to sites that have a high ses score. 167 // component, which gives extra budget to sites that have a high ses score.
159 double budget = budget_carryover + ses_component; 168 double budget = budget_carryover + ses_component;
160 DCHECK_GE(budget, 0.0); 169 DCHECK_GE(budget, 0.0);
161 170
162 return budget; 171 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
172 base::Bind(callback, budget));
163 } 173 }
164 174
165 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) { 175 void BackgroundBudgetService::StoreBudget(const GURL& origin,
176 double budget,
177 const base::Closure& closure) {
166 DCHECK_EQ(origin, origin.GetOrigin()); 178 DCHECK_EQ(origin, origin.GetOrigin());
167 DCHECK_GE(budget, 0.0); 179 DCHECK_GE(budget, 0.0);
168 DCHECK_LE(budget, SiteEngagementService::GetMaxPoints()); 180 DCHECK_LE(budget, SiteEngagementService::GetMaxPoints());
169 181
170 // Get the current SES score to write into the prefs with the new budget. 182 // Get the current SES score to write into the prefs with the new budget.
171 SiteEngagementService* service = SiteEngagementService::Get(profile_); 183 SiteEngagementService* service = SiteEngagementService::Get(profile_);
172 double ses_score = service->GetScore(origin); 184 double ses_score = service->GetScore(origin);
173 185
174 base::Time time = clock_->Now(); 186 base::Time time = clock_->Now();
175 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); 187 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score);
188
189 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(closure));
176 } 190 }
177 191
178 // Override the default clock with the specified clock. Only used for testing. 192 // Override the default clock with the specified clock. Only used for testing.
179 void BackgroundBudgetService::SetClockForTesting( 193 void BackgroundBudgetService::SetClockForTesting(
180 std::unique_ptr<base::Clock> clock) { 194 std::unique_ptr<base::Clock> clock) {
181 clock_ = std::move(clock); 195 clock_ = std::move(clock);
182 } 196 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698