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/budget_manager.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/threading/thread_task_runner_handle.h" |
15 #include "base/time/clock.h" | 16 #include "base/time/clock.h" |
16 #include "base/time/default_clock.h" | 17 #include "base/time/default_clock.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "chrome/browser/engagement/site_engagement_score.h" | 19 #include "chrome/browser/engagement/site_engagement_score.h" |
19 #include "chrome/browser/engagement/site_engagement_service.h" | 20 #include "chrome/browser/engagement/site_engagement_service.h" |
20 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
22 #include "components/pref_registry/pref_registry_syncable.h" | 23 #include "components/pref_registry/pref_registry_syncable.h" |
23 #include "components/prefs/pref_service.h" | 24 #include "components/prefs/pref_service.h" |
24 #include "components/prefs/scoped_user_pref_update.h" | 25 #include "components/prefs/scoped_user_pref_update.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 budget, kSeparator, ses); | 84 budget, kSeparator, ses); |
84 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); | 85 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); |
85 base::DictionaryValue* map = update.Get(); | 86 base::DictionaryValue* map = update.Get(); |
86 | 87 |
87 map->SetStringWithoutPathExpansion(origin.spec(), s); | 88 map->SetStringWithoutPathExpansion(origin.spec(), s); |
88 } | 89 } |
89 | 90 |
90 } // namespace | 91 } // namespace |
91 | 92 |
92 BudgetManager::BudgetManager(Profile* profile) | 93 BudgetManager::BudgetManager(Profile* profile) |
93 : clock_(base::WrapUnique(new base::DefaultClock)), profile_(profile) { | 94 : clock_(base::WrapUnique(new base::DefaultClock)), |
94 DCHECK(profile); | 95 profile_(profile), |
95 } | 96 db_(profile, |
| 97 profile->GetPath().Append(FILE_PATH_LITERAL("BudgetDatabase")), |
| 98 base::ThreadTaskRunnerHandle::Get()), |
| 99 weak_ptr_factory_(this) {} |
96 | 100 |
97 BudgetManager::~BudgetManager() {} | 101 BudgetManager::~BudgetManager() {} |
98 | 102 |
99 // static | 103 // static |
100 void BudgetManager::RegisterProfilePrefs( | 104 void BudgetManager::RegisterProfilePrefs( |
101 user_prefs::PrefRegistrySyncable* registry) { | 105 user_prefs::PrefRegistrySyncable* registry) { |
102 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | 106 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); |
103 } | 107 } |
104 | 108 |
105 // static | 109 // static |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 // Get the current SES score to write into the prefs with the new budget. | 187 // Get the current SES score to write into the prefs with the new budget. |
184 SiteEngagementService* service = SiteEngagementService::Get(profile_); | 188 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
185 double ses_score = service->GetScore(origin); | 189 double ses_score = service->GetScore(origin); |
186 | 190 |
187 base::Time time = clock_->Now(); | 191 base::Time time = clock_->Now(); |
188 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); | 192 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); |
189 | 193 |
190 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(closure)); | 194 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(closure)); |
191 } | 195 } |
192 | 196 |
| 197 void BudgetManager::Reserve(const GURL& origin, |
| 198 blink::mojom::BudgetOperationType type, |
| 199 const ReserveCallback& callback) { |
| 200 DCHECK_EQ(origin, origin.GetOrigin()); |
| 201 |
| 202 BudgetDatabase::StoreBudgetCallback reserve_callback = |
| 203 base::Bind(&BudgetManager::DidReserve, weak_ptr_factory_.GetWeakPtr(), |
| 204 origin, type, callback); |
| 205 db_.SpendBudget(origin, GetCost(type), callback); |
| 206 } |
| 207 |
| 208 void BudgetManager::Consume(const GURL& origin, |
| 209 blink::mojom::BudgetOperationType type, |
| 210 const ConsumeCallback& callback) { |
| 211 DCHECK_EQ(origin, origin.GetOrigin()); |
| 212 bool found_reservation = false; |
| 213 |
| 214 // First, see if there is a reservation already. |
| 215 auto count = reservation_map_.find(origin.spec()); |
| 216 if (count != reservation_map_.end()) { |
| 217 if (count->second == 1) |
| 218 reservation_map_.erase(origin.spec()); |
| 219 else |
| 220 reservation_map_[origin.spec()]--; |
| 221 found_reservation = true; |
| 222 } |
| 223 |
| 224 if (found_reservation) { |
| 225 callback.Run(true); |
| 226 return; |
| 227 } |
| 228 |
| 229 // If there wasn't a reservation already, try to directly consume budget. |
| 230 // The callback will return directly to the caller. |
| 231 db_.SpendBudget(origin, GetCost(type), callback); |
| 232 } |
| 233 |
| 234 void BudgetManager::DidReserve(const GURL& origin, |
| 235 blink::mojom::BudgetOperationType type, |
| 236 const ReserveCallback& callback, |
| 237 bool success) { |
| 238 if (!success) { |
| 239 callback.Run(false); |
| 240 return; |
| 241 } |
| 242 |
| 243 // Write the new reservation into the map. |
| 244 reservation_map_[origin.spec()]++; |
| 245 callback.Run(true); |
| 246 } |
| 247 |
193 // Override the default clock with the specified clock. Only used for testing. | 248 // Override the default clock with the specified clock. Only used for testing. |
194 void BudgetManager::SetClockForTesting(std::unique_ptr<base::Clock> clock) { | 249 void BudgetManager::SetClockForTesting(std::unique_ptr<base::Clock> clock) { |
195 clock_ = std::move(clock); | 250 clock_ = std::move(clock); |
196 } | 251 } |
OLD | NEW |