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

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

Issue 2199763002: Add in expiring budget (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@budget_database
Patch Set: Created 4 years, 5 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 4281c13cf4fe524c2161d64c8bbbcebe2fdfe418..d8fb1eede540387e15913137fb4e07404dcd44f7 100644
--- a/chrome/browser/budget_service/budget_database.cc
+++ b/chrome/browser/budget_service/budget_database.cc
@@ -83,39 +83,6 @@ void BudgetDatabase::AddBudget(const GURL& origin,
WriteCachedValuesToDatabase(origin, callback);
}
-void BudgetDatabase::WriteCachedValuesToDatabase(
harkness 2016/08/01 09:49:06 This just moved to the bottom of the file to mirro
- 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?
}
@@ -153,7 +120,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.
@@ -170,7 +140,8 @@ void BudgetDatabase::DidGetBudget(const GURL& origin,
// Always add one entry at the front of the list for the total budget right
// now.
- expectation.push_front(std::make_pair(total, 0));
+ expectation.push_front(
+ std::make_pair(total, clock_->Now().ToInternalValue()));
Peter Beverloo 2016/08/01 17:35:28 Sorry that I missed this before- the GetBudgetDeta
harkness 2016/08/08 14:42:43 Done.
callback.Run(true /* success */, info.first, expectation);
}
@@ -179,3 +150,56 @@ 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()) {
+ // Nothing to cleanup.
+ return;
Peter Beverloo 2016/08/01 17:35:28 style nit: no comment (redundant), no brackets for
harkness 2016/08/08 14:42:43 Done.
+ }
+
+ double now = clock_->Now().ToInternalValue();
+
+ 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) {
+ cleanup_iter = chunks.erase(cleanup_iter);
+ }
Peter Beverloo 2016/08/01 17:35:28 What if |chunks.empty()| is true now?
harkness 2016/08/08 14:42:43 I don't expect budget_map_ to become so large that
+}

Powered by Google App Engine
This is Rietveld 408576698