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> | 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/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 14 #include "base/time/clock.h" | 15 #include "base/time/clock.h" |
| 15 #include "base/time/default_clock.h" | 16 #include "base/time/default_clock.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "chrome/browser/engagement/site_engagement_service.h" | 18 #include "chrome/browser/engagement/site_engagement_service.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "components/pref_registry/pref_registry_syncable.h" | 21 #include "components/pref_registry/pref_registry_syncable.h" |
| 21 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
| 22 #include "components/prefs/scoped_user_pref_update.h" | 23 #include "components/prefs/scoped_user_pref_update.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 constexpr char kSeparator = '#'; | 27 constexpr char kSeparator = '#'; |
| 27 // We calculate the ratio of the different components of a budget with respect | 28 |
| 29 // Calculate the ratio of the different components of a budget with respect | |
| 28 // to a maximum time period of 10 days = 864000.0 seconds. | 30 // to a maximum time period of 10 days = 864000.0 seconds. |
| 29 constexpr double kSecondsToAccumulate = 864000.0; | 31 constexpr double kSecondsToAccumulate = 864000.0; |
| 30 | 32 |
| 33 // Currently this just costs 2.0, but it may expand to include things like | |
| 34 // whether wifi is available in the mobile case. | |
| 35 const double kBackgroundProcessingCost = 2.0; | |
|
Peter Beverloo
2016/05/17 11:04:34
What's your take on explicitly calling out that th
harkness
2016/05/17 13:08:16
I've split it out now, although it makes the SES r
| |
| 36 | |
| 31 bool GetBudgetDataFromPrefs(Profile* profile, | 37 bool GetBudgetDataFromPrefs(Profile* profile, |
| 32 const GURL& origin, | 38 const GURL& origin, |
| 33 double* old_budget, | 39 double* old_budget, |
| 34 double* old_ses, | 40 double* old_ses, |
| 35 double* last_updated) { | 41 double* last_updated) { |
| 36 const base::DictionaryValue* map = | 42 const base::DictionaryValue* map = |
| 37 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); | 43 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); |
| 38 | 44 |
| 39 std::string map_string; | 45 std::string map_string; |
| 40 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); | 46 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 double budget, | 79 double budget, |
| 74 double ses) { | 80 double ses) { |
| 75 std::string s = base::StringPrintf("%f%c%f%c%f", last_updated, kSeparator, | 81 std::string s = base::StringPrintf("%f%c%f%c%f", last_updated, kSeparator, |
| 76 budget, kSeparator, ses); | 82 budget, kSeparator, ses); |
| 77 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); | 83 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); |
| 78 base::DictionaryValue* map = update.Get(); | 84 base::DictionaryValue* map = update.Get(); |
| 79 | 85 |
| 80 map->SetStringWithoutPathExpansion(origin.spec(), s); | 86 map->SetStringWithoutPathExpansion(origin.spec(), s); |
| 81 } | 87 } |
| 82 | 88 |
| 89 void RecordSESForNoBudgetOrigin(double ses) { | |
| 90 UMA_HISTOGRAM_COUNTS("PushMessaging.SESForNoBudgetOrigin", ses); | |
| 91 } | |
| 92 | |
| 93 void RecordSESForLowBudgetOrigin(double ses) { | |
| 94 UMA_HISTOGRAM_COUNTS("PushMessaging.SESForLowBudgetOrigin", ses); | |
|
Peter Beverloo
2016/05/17 11:04:34
Can we just inline these calls? No need for them t
harkness
2016/05/17 13:08:16
Done.
| |
| 95 } | |
| 96 | |
| 83 } // namespace | 97 } // namespace |
| 84 | 98 |
| 85 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) | 99 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) |
| 86 : clock_(base::WrapUnique(new base::DefaultClock)), profile_(profile) { | 100 : clock_(base::WrapUnique(new base::DefaultClock)), profile_(profile) { |
| 87 DCHECK(profile); | 101 DCHECK(profile); |
| 88 } | 102 } |
| 89 | 103 |
| 90 BackgroundBudgetService::~BackgroundBudgetService() {} | 104 BackgroundBudgetService::~BackgroundBudgetService() {} |
| 91 | 105 |
| 92 // static | 106 // static |
| 93 void BackgroundBudgetService::RegisterProfilePrefs( | 107 void BackgroundBudgetService::RegisterProfilePrefs( |
| 94 user_prefs::PrefRegistrySyncable* registry) { | 108 user_prefs::PrefRegistrySyncable* registry) { |
| 95 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | 109 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); |
| 96 } | 110 } |
| 97 | 111 |
| 112 // static | |
| 113 double BackgroundBudgetService::GetBackgroundProcessingCost() { | |
| 114 return kBackgroundProcessingCost; | |
| 115 } | |
| 116 | |
| 98 double BackgroundBudgetService::GetBudget(const GURL& origin) { | 117 double BackgroundBudgetService::GetBudget(const GURL& origin) { |
| 99 DCHECK_EQ(origin, origin.GetOrigin()); | 118 DCHECK_EQ(origin, origin.GetOrigin()); |
| 100 | 119 |
| 101 // Get the current SES score, which we'll use to set a new budget. | 120 // Get the current SES score, which we'll use to set a new budget. |
| 102 SiteEngagementService* service = SiteEngagementService::Get(profile_); | 121 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 103 double ses_score = service->GetScore(origin); | 122 double ses_score = service->GetScore(origin); |
| 104 | 123 |
| 105 // Get the last used budget data. This is a triple of last calculated time, | 124 // Get the last used budget data. This is a triple of last calculated time, |
| 106 // budget at that time, and Site Engagement Score (ses) at that time. | 125 // budget at that time, and Site Engagement Score (ses) at that time. |
| 107 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; | 126 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 129 // total period. | 148 // total period. |
| 130 double ses_ratio = | 149 double ses_ratio = |
| 131 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); | 150 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); |
| 132 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; | 151 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; |
| 133 | 152 |
| 134 // Budget recalculation consists of a budget carryover component, which | 153 // Budget recalculation consists of a budget carryover component, which |
| 135 // rewards sites that don't use all their budgets every day, and a ses | 154 // rewards sites that don't use all their budgets every day, and a ses |
| 136 // component, which gives extra budget to sites that have a high ses score. | 155 // component, which gives extra budget to sites that have a high ses score. |
| 137 double budget = budget_carryover + ses_component; | 156 double budget = budget_carryover + ses_component; |
| 138 DCHECK_GE(budget, 0.0); | 157 DCHECK_GE(budget, 0.0); |
| 158 | |
| 159 // Generate histograms for the GetBudget calls which would return "no budget" | |
| 160 // or "low budget" if an API was available to app developers. | |
| 161 if (budget < GetBackgroundProcessingCost()) | |
| 162 RecordSESForNoBudgetOrigin(ses_score); | |
| 163 else if (budget < 2.0 * GetBackgroundProcessingCost()) | |
| 164 RecordSESForLowBudgetOrigin(ses_score); | |
| 165 | |
| 139 return budget; | 166 return budget; |
| 140 } | 167 } |
| 141 | 168 |
| 142 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) { | 169 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) { |
| 143 DCHECK_EQ(origin, origin.GetOrigin()); | 170 DCHECK_EQ(origin, origin.GetOrigin()); |
| 144 DCHECK_GE(budget, 0.0); | 171 DCHECK_GE(budget, 0.0); |
| 145 DCHECK_LE(budget, SiteEngagementScore::kMaxPoints); | 172 DCHECK_LE(budget, SiteEngagementScore::kMaxPoints); |
| 146 | 173 |
| 147 // Get the current SES score to write into the prefs with the new budget. | 174 // Get the current SES score to write into the prefs with the new budget. |
| 148 SiteEngagementService* service = SiteEngagementService::Get(profile_); | 175 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 149 double ses_score = service->GetScore(origin); | 176 double ses_score = service->GetScore(origin); |
| 150 | 177 |
| 151 base::Time time = clock_->Now(); | 178 base::Time time = clock_->Now(); |
| 152 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); | 179 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); |
| 153 } | 180 } |
| 154 | 181 |
| 155 // Override the default clock with the specified clock. Only used for testing. | 182 // Override the default clock with the specified clock. Only used for testing. |
| 156 void BackgroundBudgetService::SetClockForTesting( | 183 void BackgroundBudgetService::SetClockForTesting( |
| 157 std::unique_ptr<base::Clock> clock) { | 184 std::unique_ptr<base::Clock> clock) { |
| 158 clock_ = std::move(clock); | 185 clock_ = std::move(clock); |
| 159 } | 186 } |
| OLD | NEW |