Chromium Code Reviews| 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..6d6600210251a945d7a9373ef890201164681667 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()].push_back(BudgetChunk(amount, expiration)); |
|
Peter Beverloo
2016/08/10 15:36:44
nit: could rewrite this as:
budget_map_[origin.sp
harkness
2016/08/10 16:41:54
Done.
|
| // Now that the cache is updated, write the data to the database. |
| WriteCachedValuesToDatabase(origin, callback); |
| @@ -102,12 +101,10 @@ void BudgetDatabase::AddToCache( |
| // 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( |
| + chunks.push_back(BudgetChunk( |
| chunk.amount(), base::Time::FromInternalValue(chunk.expiration()))); |
|
Peter Beverloo
2016/08/10 15:36:44
micro nit: {} since this no longer is a single-lin
harkness
2016/08/10 16:41:54
Done.
|
| - 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 +115,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 +128,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.push_front(BudgetStatus(total, chunk.expiration)); |
|
Peter Beverloo
2016/08/10 15:36:44
dito, but with emplace_front() instead. Also on li
harkness
2016/08/10 16:41:55
Done.
|
| + 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.push_front(BudgetStatus(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 +168,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 +190,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. |