Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 const double kSecondsToAccumulate = 864000.0; // 10 days of seconds. |
| 28 | |
| 29 bool GetBudgetDataFromPrefs(Profile* profile, | |
| 30 const GURL& origin, | |
| 31 double* old_budget, | |
| 32 double* old_ses, | |
| 33 double* last_updated) { | |
|
Peter Beverloo
2016/05/04 15:41:51
Something to consider for the longer term: store t
harkness
2016/05/09 15:33:27
Acknowledged.
| |
| 18 const base::DictionaryValue* map = | 34 const base::DictionaryValue* map = |
| 19 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); | 35 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); |
| 20 | 36 |
| 21 std::string map_string; | 37 std::string map_string; |
| 22 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); | 38 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); |
| 23 return map_string; | 39 |
| 40 std::vector<base::StringPiece> parts = | |
| 41 base::SplitStringPiece(map_string, std::string(1, kSeparator), | |
|
Peter Beverloo
2016/05/04 15:41:52
nit: StringPieceSplit takes a StringPiece as the s
harkness
2016/05/09 15:33:27
Done.
| |
| 42 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 43 if (parts.size() != 3) | |
| 44 return false; | |
| 45 | |
| 46 if (!base::StringToDouble(parts[0].as_string(), last_updated)) | |
| 47 return false; | |
| 48 | |
| 49 if (!base::StringToDouble(parts[1].as_string(), old_budget)) | |
| 50 return false; | |
| 51 | |
| 52 if (!base::StringToDouble(parts[2].as_string(), old_ses)) | |
| 53 return false; | |
| 54 | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 // The value stored in prefs is a concatenated string of last updated time, old | |
| 59 // budget, and old ses. | |
| 60 void SetBudgetDataInPrefs(Profile* profile, | |
| 61 const GURL& origin, | |
| 62 double last_updated, | |
| 63 double budget, | |
| 64 double ses) { | |
| 65 std::string s = base::StringPrintf("%f%c%f%c%f", last_updated, kSeparator, | |
| 66 budget, kSeparator, ses); | |
| 67 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); | |
| 68 base::DictionaryValue* map = update.Get(); | |
| 69 | |
| 70 map->SetStringWithoutPathExpansion(origin.spec(), s); | |
| 24 } | 71 } |
| 25 | 72 |
| 26 } // namespace | 73 } // namespace |
| 27 | 74 |
| 28 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) | 75 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) |
| 29 : profile_(profile) { | 76 : BackgroundBudgetService(profile, |
| 77 base::WrapUnique(new base::DefaultClock)) {} | |
| 78 | |
| 79 BackgroundBudgetService::BackgroundBudgetService( | |
| 80 Profile* profile, | |
| 81 std::unique_ptr<base::Clock> clock) | |
| 82 : clock_(std::move(clock)), profile_(profile) { | |
| 30 DCHECK(profile); | 83 DCHECK(profile); |
| 31 } | 84 } |
| 32 | 85 |
| 86 BackgroundBudgetService::~BackgroundBudgetService() {} | |
| 87 | |
| 33 // static | 88 // static |
| 34 void BackgroundBudgetService::RegisterProfilePrefs( | 89 void BackgroundBudgetService::RegisterProfilePrefs( |
| 35 user_prefs::PrefRegistrySyncable* registry) { | 90 user_prefs::PrefRegistrySyncable* registry) { |
| 36 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | 91 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); |
| 37 } | 92 } |
| 38 | 93 |
| 39 std::string BackgroundBudgetService::GetBudget(const GURL& origin) { | 94 double BackgroundBudgetService::GetBudget(const GURL& origin) { |
|
Peter Beverloo
2016/05/04 15:41:52
DCHECK_EQ(origin, origin.GetOrigin());
(also in St
harkness
2016/05/09 15:33:27
Done.
| |
| 40 return GetBudgetStringForOrigin(profile_, origin); | 95 // Get the current SES score, which we'll use to set a new budget. |
| 96 SiteEngagementService* service = SiteEngagementService::Get(profile_); | |
| 97 double ses_score = service->GetScore(origin); | |
| 98 | |
| 99 // Get the last used budget data. This is a triple of last calculated time, | |
| 100 // budget at that time, and Site Engagement Score (ses) at that time. | |
| 101 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; | |
| 102 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses, | |
| 103 &last_updated_msec)) { | |
|
Peter Beverloo
2016/05/04 15:41:52
Is it OK to silently ignore parsing errors? (I.e.
harkness
2016/05/09 15:33:27
I've updated the comment.
| |
| 104 // If there is no stored data, just return data based on the SES. | |
| 105 return ses_score; | |
| 106 } | |
| 107 | |
| 108 base::Time now = clock_->Now(); | |
| 109 base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec); | |
| 110 | |
| 111 // Budget recalculation consists of a budget carryover component, which | |
| 112 // rewards sites that don't use all their budgets every day, and a ses | |
| 113 // component, which gives extra budget to sites that have a high ses score. | |
|
Peter Beverloo
2016/05/04 15:41:51
This would really benefit from an explanation of t
harkness
2016/05/09 15:33:27
I've updated it, which involved moving a few thing
| |
| 114 double carryover_ratio = std::max( | |
| 115 0.0, | |
| 116 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); | |
| 117 double budget_carryover = old_budget * carryover_ratio; | |
| 118 double ses_ratio = | |
| 119 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); | |
| 120 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; | |
| 121 double budget = budget_carryover + ses_component; | |
| 122 DCHECK(budget >= 0.0); | |
|
Peter Beverloo
2016/05/04 15:41:52
DCHECK_GE
harkness
2016/05/09 15:33:27
Done.
| |
| 123 return budget; | |
| 41 } | 124 } |
| 42 | 125 |
| 43 void BackgroundBudgetService::StoreBudget( | 126 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) { |
| 44 const GURL& origin, | 127 DCHECK(budget >= 0.0); |
|
Peter Beverloo
2016/05/04 15:41:51
DCHECK_GE
harkness
2016/05/09 15:33:27
Done.
| |
| 45 const std::string& notifications_shown) { | 128 DCHECK(budget <= SiteEngagementScore::kMaxPoints); |
|
Peter Beverloo
2016/05/04 15:41:52
DCHECK_LE
harkness
2016/05/09 15:33:27
Done.
| |
| 46 DictionaryPrefUpdate update(profile_->GetPrefs(), | 129 |
| 47 prefs::kBackgroundBudgetMap); | 130 // Get the current SES score to write into the prefs with the new budget. |
| 48 base::DictionaryValue* map = update.Get(); | 131 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 49 map->SetStringWithoutPathExpansion(origin.spec(), notifications_shown); | 132 double ses_score = service->GetScore(origin); |
| 133 | |
| 134 base::Time t = clock_->Now(); | |
| 135 SetBudgetDataInPrefs(profile_, origin, t.ToDoubleT(), budget, ses_score); | |
| 50 } | 136 } |
| OLD | NEW |