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 |
| 31 bool GetBudgetDataFromPrefs(Profile* profile, | 33 bool GetBudgetDataFromPrefs(Profile* profile, |
| 32 const GURL& origin, | 34 const GURL& origin, |
| 33 double* old_budget, | 35 double* old_budget, |
| 34 double* old_ses, | 36 double* old_ses, |
| 35 double* last_updated) { | 37 double* last_updated) { |
| 36 const base::DictionaryValue* map = | 38 const base::DictionaryValue* map = |
| 37 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); | 39 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 } | 90 } |
| 89 | 91 |
| 90 BackgroundBudgetService::~BackgroundBudgetService() {} | 92 BackgroundBudgetService::~BackgroundBudgetService() {} |
| 91 | 93 |
| 92 // static | 94 // static |
| 93 void BackgroundBudgetService::RegisterProfilePrefs( | 95 void BackgroundBudgetService::RegisterProfilePrefs( |
| 94 user_prefs::PrefRegistrySyncable* registry) { | 96 user_prefs::PrefRegistrySyncable* registry) { |
| 95 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | 97 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); |
| 96 } | 98 } |
| 97 | 99 |
| 100 // static | |
| 101 double BackgroundBudgetService::GetCost(CostType type) { | |
| 102 switch (type) { | |
| 103 case CostType::SILENT_PUSH: | |
| 104 return 2.0; | |
| 105 // No default case. | |
|
Peter Beverloo
2016/05/17 15:54:28
nit: -2 indent
harkness
2016/05/17 16:18:05
Done.
| |
| 106 } | |
| 107 DCHECK(false); | |
|
Peter Beverloo
2016/05/17 15:54:28
Since DCHECKS() are removed from release builds, t
harkness
2016/05/17 16:18:05
Done.
| |
| 108 } | |
| 109 | |
| 98 double BackgroundBudgetService::GetBudget(const GURL& origin) { | 110 double BackgroundBudgetService::GetBudget(const GURL& origin) { |
| 99 DCHECK_EQ(origin, origin.GetOrigin()); | 111 DCHECK_EQ(origin, origin.GetOrigin()); |
| 100 | 112 |
| 101 // Get the current SES score, which we'll use to set a new budget. | 113 // Get the current SES score, which we'll use to set a new budget. |
| 102 SiteEngagementService* service = SiteEngagementService::Get(profile_); | 114 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 103 double ses_score = service->GetScore(origin); | 115 double ses_score = service->GetScore(origin); |
| 104 | 116 |
| 105 // Get the last used budget data. This is a triple of last calculated time, | 117 // 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. | 118 // 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; | 119 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 129 // total period. | 141 // total period. |
| 130 double ses_ratio = | 142 double ses_ratio = |
| 131 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); | 143 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); |
| 132 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; | 144 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; |
| 133 | 145 |
| 134 // Budget recalculation consists of a budget carryover component, which | 146 // Budget recalculation consists of a budget carryover component, which |
| 135 // rewards sites that don't use all their budgets every day, and a ses | 147 // 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. | 148 // component, which gives extra budget to sites that have a high ses score. |
| 137 double budget = budget_carryover + ses_component; | 149 double budget = budget_carryover + ses_component; |
| 138 DCHECK_GE(budget, 0.0); | 150 DCHECK_GE(budget, 0.0); |
| 151 | |
| 139 return budget; | 152 return budget; |
| 140 } | 153 } |
| 141 | 154 |
| 142 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) { | 155 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) { |
| 143 DCHECK_EQ(origin, origin.GetOrigin()); | 156 DCHECK_EQ(origin, origin.GetOrigin()); |
| 144 DCHECK_GE(budget, 0.0); | 157 DCHECK_GE(budget, 0.0); |
| 145 DCHECK_LE(budget, SiteEngagementScore::kMaxPoints); | 158 DCHECK_LE(budget, SiteEngagementScore::kMaxPoints); |
| 146 | 159 |
| 147 // Get the current SES score to write into the prefs with the new budget. | 160 // Get the current SES score to write into the prefs with the new budget. |
| 148 SiteEngagementService* service = SiteEngagementService::Get(profile_); | 161 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 149 double ses_score = service->GetScore(origin); | 162 double ses_score = service->GetScore(origin); |
| 150 | 163 |
| 151 base::Time time = clock_->Now(); | 164 base::Time time = clock_->Now(); |
| 152 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); | 165 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); |
| 153 } | 166 } |
| 154 | 167 |
| 155 // Override the default clock with the specified clock. Only used for testing. | 168 // Override the default clock with the specified clock. Only used for testing. |
| 156 void BackgroundBudgetService::SetClockForTesting( | 169 void BackgroundBudgetService::SetClockForTesting( |
| 157 std::unique_ptr<base::Clock> clock) { | 170 std::unique_ptr<base::Clock> clock) { |
| 158 clock_ = std::move(clock); | 171 clock_ = std::move(clock); |
| 159 } | 172 } |
| OLD | NEW |