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

Unified Diff: chrome/browser/budget_service/budget_database.cc

Issue 2233783002: Cleanup budget_database based on evolving API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated comments and added emplace_ usage Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/budget_service/budget_database.cc
diff --git a/chrome/browser/budget_service/budget_database.cc b/chrome/browser/budget_service/budget_database.cc
index bfb672059591ec8859d5fb55978303254c3fad17..aaada9642e98d3e5ef40b38914a01cad269269c7 100644
--- a/chrome/browser/budget_service/budget_database.cc
+++ b/chrome/browser/budget_service/budget_database.cc
@@ -72,8 +72,7 @@ void BudgetDatabase::AddBudget(const GURL& origin,
// Add a new chunk of budget for the origin at the default expiration time.
base::Time expiration =
clock_->Now() + base::TimeDelta::FromHours(kBudgetDurationInHours);
- budget_map_[origin.spec()].second.push_back(
- std::make_pair(amount, expiration));
+ budget_map_[origin.spec()].emplace_back(amount, expiration);
// Now that the cache is updated, write the data to the database.
WriteCachedValuesToDatabase(origin, callback);
@@ -101,13 +100,12 @@ void BudgetDatabase::AddToCache(
// Add the data to the cache, converting from the proto format to an STL
// format which is better for removing things from the list.
BudgetChunks chunks;
- for (const auto& chunk : budget_proto->budget())
- chunks.push_back(std::make_pair(
- chunk.amount(), base::Time::FromInternalValue(chunk.expiration())));
+ for (const auto& chunk : budget_proto->budget()) {
+ chunks.emplace_back(chunk.amount(),
+ base::Time::FromInternalValue(chunk.expiration()));
+ }
- DCHECK(budget_proto->has_debt());
- budget_map_[origin.spec()] =
- std::make_pair(budget_proto->debt(), std::move(chunks));
+ budget_map_[origin.spec()] = std::move(chunks);
callback.Run(success);
}
@@ -118,7 +116,7 @@ void BudgetDatabase::DidGetBudget(const GURL& origin,
// If the database wasn't able to read the information, return the
// failure and an empty BudgetExpectation.
if (!success) {
- callback.Run(success, 0, BudgetExpectation());
+ callback.Run(success, BudgetExpectation());
return;
}
@@ -131,23 +129,21 @@ void BudgetDatabase::DidGetBudget(const GURL& origin,
// and the budget at those times.
BudgetExpectation expectation;
double total = 0;
- double debt = 0;
if (IsCached(origin)) {
// Starting with the chunks that expire the farthest in the future, build up
// the budget expectations for those future times.
- const BudgetInfo& info = budget_map_[origin.spec()];
- for (const auto& chunk : base::Reversed(info.second)) {
- expectation.push_front(std::make_pair(total, chunk.second));
- total += chunk.first;
+ const BudgetChunks& chunks = budget_map_[origin.spec()];
+ for (const auto& chunk : base::Reversed(chunks)) {
+ expectation.emplace_front(total, chunk.expiration);
+ total += chunk.amount;
}
- debt = info.first;
}
// Always add one entry at the front of the list for the total budget now.
- expectation.push_front(std::make_pair(total, clock_->Now()));
+ expectation.emplace_front(total, clock_->Now());
- callback.Run(true /* success */, debt, expectation);
+ callback.Run(true /* success */, expectation);
}
void BudgetDatabase::SetClockForTesting(std::unique_ptr<base::Clock> clock) {
@@ -173,12 +169,11 @@ void BudgetDatabase::WriteCachedValuesToDatabase(
if (IsCached(origin)) {
// Build the Budget proto object.
budget_service::Budget budget;
- const BudgetInfo& info = budget_map_[origin.spec()];
- budget.set_debt(info.first);
- for (const auto& chunk : info.second) {
+ const BudgetChunks& chunks = budget_map_[origin.spec()];
+ for (const auto& chunk : chunks) {
budget_service::BudgetChunk* budget_chunk = budget.add_budget();
- budget_chunk->set_amount(chunk.first);
- budget_chunk->set_expiration(chunk.second.ToInternalValue());
+ budget_chunk->set_amount(chunk.amount);
+ budget_chunk->set_expiration(chunk.expiration.ToInternalValue());
}
entries->push_back(std::make_pair(origin.spec(), budget));
} else {
@@ -196,11 +191,11 @@ void BudgetDatabase::CleanupExpiredBudget(const GURL& origin) {
base::Time now = clock_->Now();
- BudgetChunks& chunks = budget_map_[origin.spec()].second;
+ BudgetChunks& chunks = budget_map_[origin.spec()];
auto cleanup_iter = chunks.begin();
// This relies on the list of chunks being in timestamp order.
- while (cleanup_iter != chunks.end() && cleanup_iter->second <= now)
+ while (cleanup_iter != chunks.end() && cleanup_iter->expiration <= now)
cleanup_iter = chunks.erase(cleanup_iter);
// If the entire budget is empty now, cleanup the map.
« no previous file with comments | « chrome/browser/budget_service/budget_database.h ('k') | chrome/browser/budget_service/budget_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698