Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: chrome/browser/budget_service/budget_manager.cc

Issue 2272563005: Start plumbing connections from the BudgetManager to the BudgetDatabase (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix the task runner Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 BrowserThread::GetBlockingPool()
99 ->GetSequencedTaskRunnerWithShutdownBehavior(
100 base::SequencedWorkerPool::GetSequenceToken(),
101 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)),
102 weak_ptr_factory_(this) {}
96 103
97 BudgetManager::~BudgetManager() {} 104 BudgetManager::~BudgetManager() {}
98 105
99 // static 106 // static
100 void BudgetManager::RegisterProfilePrefs( 107 void BudgetManager::RegisterProfilePrefs(
101 user_prefs::PrefRegistrySyncable* registry) { 108 user_prefs::PrefRegistrySyncable* registry) {
102 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); 109 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap);
103 } 110 }
104 111
105 // static 112 // static
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 // Get the current SES score to write into the prefs with the new budget. 190 // Get the current SES score to write into the prefs with the new budget.
184 SiteEngagementService* service = SiteEngagementService::Get(profile_); 191 SiteEngagementService* service = SiteEngagementService::Get(profile_);
185 double ses_score = service->GetScore(origin); 192 double ses_score = service->GetScore(origin);
186 193
187 base::Time time = clock_->Now(); 194 base::Time time = clock_->Now();
188 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); 195 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score);
189 196
190 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(closure)); 197 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(closure));
191 } 198 }
192 199
200 void BudgetManager::Reserve(const GURL& origin,
201 blink::mojom::BudgetOperationType type,
202 const ReserveCallback& callback) {
203 DCHECK_EQ(origin, origin.GetOrigin());
204
205 BudgetDatabase::StoreBudgetCallback reserve_callback =
206 base::Bind(&BudgetManager::DidReserve, weak_ptr_factory_.GetWeakPtr(),
207 origin, type, callback);
208 db_.SpendBudget(origin, GetCost(type), callback);
209 }
210
211 void BudgetManager::Consume(const GURL& origin,
212 blink::mojom::BudgetOperationType type,
213 const ConsumeCallback& callback) {
214 DCHECK_EQ(origin, origin.GetOrigin());
215 bool found_reservation = false;
216
217 // First, see if there is a reservation already.
218 auto count = reservation_map_.find(origin.spec());
219 if (count != reservation_map_.end()) {
220 if (count->second == 1)
221 reservation_map_.erase(origin.spec());
222 else
223 reservation_map_[origin.spec()]--;
224 found_reservation = true;
225 }
226
227 if (found_reservation) {
228 callback.Run(true);
229 return;
230 }
231
232 // If there wasn't a reservation already, try to directly consume budget.
233 // The callback will return directly to the caller.
234 db_.SpendBudget(origin, GetCost(type), callback);
235 }
236
237 void BudgetManager::DidReserve(const GURL& origin,
238 blink::mojom::BudgetOperationType type,
239 const ReserveCallback& callback,
240 bool success) {
241 if (!success) {
242 callback.Run(false);
243 return;
244 }
245
246 // Write the new reservation into the map.
247 reservation_map_[origin.spec()]++;
248 callback.Run(true);
249 }
250
193 // Override the default clock with the specified clock. Only used for testing. 251 // Override the default clock with the specified clock. Only used for testing.
194 void BudgetManager::SetClockForTesting(std::unique_ptr<base::Clock> clock) { 252 void BudgetManager::SetClockForTesting(std::unique_ptr<base::Clock> clock) {
195 clock_ = std::move(clock); 253 clock_ = std::move(clock);
196 } 254 }
OLDNEW
« no previous file with comments | « chrome/browser/budget_service/budget_manager.h ('k') | chrome/browser/budget_service/budget_manager_factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698