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

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

Issue 1887623002: Replace the 1 in 10 grace period with an accumulating budget based on SES. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Integrated code review comments and refactored service constructors. Created 4 years, 7 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/push_messaging/background_budget_service.h" 5 #include "chrome/browser/push_messaging/background_budget_service.h"
6 6
7 #include <stdint.h>
8
7 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/time/clock.h"
15 #include "base/time/default_clock.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/engagement/site_engagement_service.h"
8 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
10 #include "components/pref_registry/pref_registry_syncable.h" 20 #include "components/pref_registry/pref_registry_syncable.h"
11 #include "components/prefs/pref_service.h" 21 #include "components/prefs/pref_service.h"
12 #include "components/prefs/scoped_user_pref_update.h" 22 #include "components/prefs/scoped_user_pref_update.h"
13 23
14 namespace { 24 namespace {
15 25
16 std::string GetBudgetStringForOrigin(Profile* profile, const GURL& origin) { 26 const char kSeparator = '#';
17 DCHECK_EQ(origin.GetOrigin(), origin); 27 // We calculate the ratio of the different components of a budget with respect
28 // to a maximum time period of 10 days = 864000.0 seconds.
29 const double kSecondsToAccumulate = 864000.0;
Peter Beverloo 2016/05/09 17:12:06 nit: you can use constexpr instead of const now if
harkness 2016/05/13 13:58:36 Done.
30
31 bool GetBudgetDataFromPrefs(Profile* profile,
32 const GURL& origin,
33 double* old_budget,
34 double* old_ses,
35 double* last_updated) {
18 const base::DictionaryValue* map = 36 const base::DictionaryValue* map =
19 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); 37 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap);
20 38
21 std::string map_string; 39 std::string map_string;
22 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); 40 map->GetStringWithoutPathExpansion(origin.spec(), &map_string);
23 return map_string; 41
42 std::vector<base::StringPiece> parts =
43 base::SplitStringPiece(map_string, base::StringPiece(&kSeparator, 1),
44 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
45 if (parts.size() != 3)
46 return false;
47
48 if (!base::StringToDouble(parts[0].as_string(), last_updated))
49 return false;
50
51 if (!base::StringToDouble(parts[1].as_string(), old_budget))
52 return false;
53
54 if (!base::StringToDouble(parts[2].as_string(), old_ses))
55 return false;
56
57 return true;
58 }
59
60 // The value stored in prefs is a concatenated string of last updated time, old
61 // budget, and old ses.
62 void SetBudgetDataInPrefs(Profile* profile,
63 const GURL& origin,
64 double last_updated,
65 double budget,
66 double ses) {
67 std::string s = base::StringPrintf("%f%c%f%c%f", last_updated, kSeparator,
68 budget, kSeparator, ses);
69 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap);
70 base::DictionaryValue* map = update.Get();
71
72 map->SetStringWithoutPathExpansion(origin.spec(), s);
24 } 73 }
25 74
26 } // namespace 75 } // namespace
27 76
28 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) 77 BackgroundBudgetService::BackgroundBudgetService(Profile* profile)
29 : profile_(profile) { 78 : clock_(base::WrapUnique(new base::DefaultClock)), profile_(profile) {
30 DCHECK(profile); 79 DCHECK(profile);
31 } 80 }
32 81
82 BackgroundBudgetService::~BackgroundBudgetService() {}
83
33 // static 84 // static
34 void BackgroundBudgetService::RegisterProfilePrefs( 85 void BackgroundBudgetService::RegisterProfilePrefs(
35 user_prefs::PrefRegistrySyncable* registry) { 86 user_prefs::PrefRegistrySyncable* registry) {
36 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); 87 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap);
37 } 88 }
38 89
39 std::string BackgroundBudgetService::GetBudget(const GURL& origin) { 90 double BackgroundBudgetService::GetBudget(const GURL& origin) {
40 return GetBudgetStringForOrigin(profile_, origin); 91 DCHECK_EQ(origin, origin.GetOrigin());
92
93 // Get the current SES score, which we'll use to set a new budget.
94 SiteEngagementService* service = SiteEngagementService::Get(profile_);
95 double ses_score = service->GetScore(origin);
96
97 // Get the last used budget data. This is a triple of last calculated time,
98 // budget at that time, and Site Engagement Score (ses) at that time.
99 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0;
100 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses,
101 &last_updated_msec)) {
102 // If there is no stored data or the data can't be parsed, just return the
103 // SES.
Peter Beverloo 2016/05/09 17:12:06 Sure, but is it *OK* to silently ignore parsing er
harkness 2016/05/13 13:58:36 As discussed offline, I've added an error log and
104 return ses_score;
105 }
106
107 base::Time now = clock_->Now();
108 base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec);
109
110 // For each time period that elapses, we calculate the carryover ratio as the
Peter Beverloo 2016/05/09 17:12:06 nit: Try to avoid the use of "we" in comments.
harkness 2016/05/13 13:58:36 Done.
111 // ratio of time remaining in our max period to the total period.
112 // The carryover component is then the old budget multiplied by the ratio.
113 double carryover_ratio = std::max(
114 0.0,
115 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate));
116 double budget_carryover = old_budget * carryover_ratio;
117
118 // The ses component is an average of the last ses score used for budget
119 // calculation and the current ses score.
120 // The ses average is them multiplied by the ratio of time elapsed to the
121 // total period.
122 double ses_ratio =
123 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate));
124 double ses_component = (old_ses + ses_score) / 2 * ses_ratio;
125
126 // Budget recalculation consists of a budget carryover component, which
127 // rewards sites that don't use all their budgets every day, and a ses
128 // component, which gives extra budget to sites that have a high ses score.
129 double budget = budget_carryover + ses_component;
Peter Beverloo 2016/05/09 17:12:06 Love the explanation, thank you :)
harkness 2016/05/13 13:58:36 Acknowledged.
130 DCHECK_GE(budget, 0.0);
131 return budget;
41 } 132 }
42 133
43 void BackgroundBudgetService::StoreBudget( 134 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) {
44 const GURL& origin, 135 DCHECK_EQ(origin, origin.GetOrigin());
45 const std::string& notifications_shown) { 136 DCHECK_GE(budget, 0.0);
46 DictionaryPrefUpdate update(profile_->GetPrefs(), 137 DCHECK_LE(budget, SiteEngagementScore::kMaxPoints);
47 prefs::kBackgroundBudgetMap); 138
48 base::DictionaryValue* map = update.Get(); 139 // Get the current SES score to write into the prefs with the new budget.
49 map->SetStringWithoutPathExpansion(origin.spec(), notifications_shown); 140 SiteEngagementService* service = SiteEngagementService::Get(profile_);
141 double ses_score = service->GetScore(origin);
142
143 base::Time t = clock_->Now();
Peter Beverloo 2016/05/09 17:12:06 nit: s/t/time/
harkness 2016/05/13 13:58:36 Done.
144 SetBudgetDataInPrefs(profile_, origin, t.ToDoubleT(), budget, ses_score);
50 } 145 }
146
147 // Override the default clock with the specified clock. Only used for testing.
148 void BackgroundBudgetService::setClock(std::unique_ptr<base::Clock> clock) {
149 clock_ = std::move(clock);
150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698