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/budget_service/background_budget_service.h" | 5 #include "chrome/browser/budget_service/budget_manager.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" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 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, |
82 budget, kSeparator, ses); | 82 budget, kSeparator, ses); |
83 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); | 83 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); |
84 base::DictionaryValue* map = update.Get(); | 84 base::DictionaryValue* map = update.Get(); |
85 | 85 |
86 map->SetStringWithoutPathExpansion(origin.spec(), s); | 86 map->SetStringWithoutPathExpansion(origin.spec(), s); |
87 } | 87 } |
88 | 88 |
89 } // namespace | 89 } // namespace |
90 | 90 |
91 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) | 91 BudgetManager::BudgetManager(Profile* profile) |
92 : clock_(base::WrapUnique(new base::DefaultClock)), profile_(profile) { | 92 : clock_(base::WrapUnique(new base::DefaultClock)), profile_(profile) { |
93 DCHECK(profile); | 93 DCHECK(profile); |
94 } | 94 } |
95 | 95 |
96 BackgroundBudgetService::~BackgroundBudgetService() {} | 96 BudgetManager::~BudgetManager() {} |
97 | 97 |
98 // static | 98 // static |
99 void BackgroundBudgetService::RegisterProfilePrefs( | 99 void BudgetManager::RegisterProfilePrefs( |
100 user_prefs::PrefRegistrySyncable* registry) { | 100 user_prefs::PrefRegistrySyncable* registry) { |
101 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | 101 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); |
102 } | 102 } |
103 | 103 |
104 // static | 104 // static |
105 double BackgroundBudgetService::GetCost(CostType type) { | 105 double BudgetManager::GetCost(CostType type) { |
106 switch (type) { | 106 switch (type) { |
107 case CostType::SILENT_PUSH: | 107 case CostType::SILENT_PUSH: |
108 return 2.0; | 108 return 2.0; |
109 // No default case. | 109 // No default case. |
110 } | 110 } |
111 NOTREACHED(); | 111 NOTREACHED(); |
112 return SiteEngagementScore::kMaxPoints + 1.0; | 112 return SiteEngagementScore::kMaxPoints + 1.0; |
113 } | 113 } |
114 | 114 |
115 void BackgroundBudgetService::GetBudget(const GURL& origin, | 115 void BudgetManager::GetBudget(const GURL& origin, |
116 const GetBudgetCallback& callback) { | 116 const GetBudgetCallback& callback) { |
117 DCHECK_EQ(origin, origin.GetOrigin()); | 117 DCHECK_EQ(origin, origin.GetOrigin()); |
118 | 118 |
119 // 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. |
120 SiteEngagementService* service = SiteEngagementService::Get(profile_); | 120 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
121 double ses_score = service->GetScore(origin); | 121 double ses_score = service->GetScore(origin); |
122 | 122 |
123 // 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, |
124 // 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. |
125 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; |
126 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses, | 126 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 // Budget recalculation consists of a budget carryover component, which | 165 // Budget recalculation consists of a budget carryover component, which |
166 // 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 |
167 // 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. |
168 double budget = budget_carryover + ses_component; | 168 double budget = budget_carryover + ses_component; |
169 DCHECK_GE(budget, 0.0); | 169 DCHECK_GE(budget, 0.0); |
170 | 170 |
171 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 171 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
172 base::Bind(callback, budget)); | 172 base::Bind(callback, budget)); |
173 } | 173 } |
174 | 174 |
175 void BackgroundBudgetService::StoreBudget(const GURL& origin, | 175 void BudgetManager::StoreBudget(const GURL& origin, |
176 double budget, | 176 double budget, |
177 const base::Closure& closure) { | 177 const base::Closure& closure) { |
178 DCHECK_EQ(origin, origin.GetOrigin()); | 178 DCHECK_EQ(origin, origin.GetOrigin()); |
179 DCHECK_GE(budget, 0.0); | 179 DCHECK_GE(budget, 0.0); |
180 DCHECK_LE(budget, SiteEngagementService::GetMaxPoints()); | 180 DCHECK_LE(budget, SiteEngagementService::GetMaxPoints()); |
181 | 181 |
182 // 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. |
183 SiteEngagementService* service = SiteEngagementService::Get(profile_); | 183 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
184 double ses_score = service->GetScore(origin); | 184 double ses_score = service->GetScore(origin); |
185 | 185 |
186 base::Time time = clock_->Now(); | 186 base::Time time = clock_->Now(); |
187 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); | 187 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); |
188 | 188 |
189 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(closure)); | 189 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(closure)); |
190 } | 190 } |
191 | 191 |
192 // 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. |
193 void BackgroundBudgetService::SetClockForTesting( | 193 void BudgetManager::SetClockForTesting(std::unique_ptr<base::Clock> clock) { |
194 std::unique_ptr<base::Clock> clock) { | |
195 clock_ = std::move(clock); | 194 clock_ = std::move(clock); |
196 } | 195 } |
OLD | NEW |