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 e962c659e37baf122dcd172ff619ecb6f0a107a0..bcd6df5f974b84eb5c01fa5e994a77027e63e1d2 100644 |
| --- a/chrome/browser/budget_service/budget_database.cc |
| +++ b/chrome/browser/budget_service/budget_database.cc |
| @@ -82,39 +82,6 @@ void BudgetDatabase::AddBudget(const GURL& origin, |
| WriteCachedValuesToDatabase(origin, callback); |
| } |
| -void BudgetDatabase::WriteCachedValuesToDatabase( |
| - const GURL& origin, |
| - const StoreBudgetCallback& callback) { |
| - // Create the data structures that are passed to the ProtoDatabase. |
| - std::unique_ptr< |
| - leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> |
| - entries(new leveldb_proto::ProtoDatabase< |
| - budget_service::Budget>::KeyEntryVector()); |
| - std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| - new std::vector<std::string>()); |
| - |
| - // Each operation can either update the existing budget or remove the origin's |
| - // budget information. |
| - if (budget_map_.find(origin.spec()) == budget_map_.end()) { |
| - // If the origin doesn't exist in the cache, this is a remove operation. |
| - keys_to_remove->push_back(origin.spec()); |
| - } else { |
| - // 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) { |
| - budget_service::BudgetChunk* budget_chunk = budget.add_budget(); |
| - budget_chunk->set_amount(chunk.first); |
| - budget_chunk->set_expiration(chunk.second); |
| - } |
| - entries->push_back(std::make_pair(origin.spec(), budget)); |
| - } |
| - |
| - // Send the updates to the database. |
| - db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); |
| -} |
| - |
| void BudgetDatabase::OnDatabaseInit(bool success) { |
| // TODO(harkness): Consider caching the budget database now? |
| } |
| @@ -152,7 +119,10 @@ void BudgetDatabase::DidGetBudget(const GURL& origin, |
| return; |
| } |
| - // Otherwise, build up the BudgetExpection. This is different from the format |
| + // First, cleanup any expired budget chunks for the origin. |
| + CleanupExpiredBudget(origin); |
| + |
| + // Now, build up the BudgetExpection. This is different from the format |
| // in which the cache stores the data. The cache stores chunks of budget and |
| // when that budget expires. The BudgetExpectation describes a set of times |
| // and the budget at those times. |
| @@ -163,13 +133,13 @@ void BudgetDatabase::DidGetBudget(const GURL& origin, |
| // Starting with the chunks that expire the farthest in the future, build up |
| // the budget expectations for those future times. |
| for (const auto& chunk : base::Reversed(info.second)) { |
| - expectation.push_front(std::make_pair(total, chunk.second)); |
| + expectation.push_front( |
| + std::make_pair(total, base::Time::FromInternalValue(chunk.second))); |
| total += chunk.first; |
| } |
| - // Always add one entry at the front of the list for the total budget right |
| - // now. |
| - expectation.push_front(std::make_pair(total, 0)); |
| + // Always add one entry at the front of the list for the total budget now. |
| + expectation.push_front(std::make_pair(total, clock_->Now())); |
| callback.Run(true /* success */, info.first, expectation); |
| } |
| @@ -178,3 +148,58 @@ void BudgetDatabase::DidGetBudget(const GURL& origin, |
| void BudgetDatabase::SetClockForTesting(std::unique_ptr<base::Clock> clock) { |
| clock_ = std::move(clock); |
| } |
| + |
| +void BudgetDatabase::WriteCachedValuesToDatabase( |
| + const GURL& origin, |
| + const StoreBudgetCallback& callback) { |
| + // First, cleanup any expired budget chunks for the origin. |
| + CleanupExpiredBudget(origin); |
| + |
| + // Create the data structures that are passed to the ProtoDatabase. |
| + std::unique_ptr< |
| + leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> |
| + entries(new leveldb_proto::ProtoDatabase< |
| + budget_service::Budget>::KeyEntryVector()); |
| + std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| + new std::vector<std::string>()); |
| + |
| + // Each operation can either update the existing budget or remove the origin's |
| + // budget information. |
| + if (budget_map_.find(origin.spec()) == budget_map_.end()) { |
| + // If the origin doesn't exist in the cache, this is a remove operation. |
| + keys_to_remove->push_back(origin.spec()); |
| + } else { |
| + // 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) { |
| + budget_service::BudgetChunk* budget_chunk = budget.add_budget(); |
| + budget_chunk->set_amount(chunk.first); |
| + budget_chunk->set_expiration(chunk.second); |
| + } |
| + entries->push_back(std::make_pair(origin.spec(), budget)); |
| + } |
| + |
| + // Send the updates to the database. |
| + db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); |
| +} |
| + |
| +void BudgetDatabase::CleanupExpiredBudget(const GURL& origin) { |
| + if (budget_map_.find(origin.spec()) == budget_map_.end()) |
| + return; |
| + |
| + double now = clock_->Now().ToInternalValue(); |
|
Peter Beverloo
2016/08/08 15:02:55
Supposedly we'd like to change the second double i
harkness
2016/08/09 12:57:40
I was thinking we would have the cache be a true m
|
| + |
| + BudgetChunks& chunks = budget_map_[origin.spec()].second; |
| + 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) { |
|
Peter Beverloo
2016/08/08 15:02:55
nit: remove {} for one-line statements
Peter Beverloo
2016/08/08 15:02:55
Not for this CL, but I'd hope you agree that all t
harkness
2016/08/09 12:57:40
Done.
harkness
2016/08/09 12:57:40
As we discussed in person yesterday, I'm planning
|
| + cleanup_iter = chunks.erase(cleanup_iter); |
| + } |
| + |
| + // If the entire budget is empty now, cleanup the map. |
| + if (chunks.empty()) |
| + budget_map_.erase(origin.spec()); |
|
Peter Beverloo
2016/08/08 15:02:55
Maybe add a test to verify that we actually remove
harkness
2016/08/09 12:57:40
Added a test and found a bug/incomplete implementa
Peter Beverloo
2016/08/09 13:12:42
As the British would say: "woop!"
|
| +} |