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

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

Issue 2173833002: Expand the functionality of the BudgetDatabase. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reverted change I decided against. 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 c786ea34058e2982275890be0abcac5606b6ff9d..5e98166f15600eb17d1ba1369d45ec1e063b2896 100644
--- a/chrome/browser/budget_service/budget_database.cc
+++ b/chrome/browser/budget_service/budget_database.cc
@@ -31,10 +31,7 @@ BudgetDatabase::BudgetDatabase(
BudgetDatabase::~BudgetDatabase() {}
-void BudgetDatabase::OnDatabaseInit(bool success) {
- // TODO(harkness): Consider caching the budget database now?
-}
-
+// TODO(harkness): Either remove this method or add caching.
Peter Beverloo 2016/07/22 16:14:51 Based on what will you make this decision? Dito re
harkness 2016/07/26 16:42:07 Discussed in person, this will be a lazy system. T
void BudgetDatabase::GetValue(const GURL& origin,
const GetValueCallback& callback) {
DCHECK_EQ(origin.GetOrigin(), origin);
@@ -46,6 +43,9 @@ void BudgetDatabase::SetValue(const GURL& origin,
const SetValueCallback& callback) {
DCHECK_EQ(origin.GetOrigin(), origin);
+ // TODO(harkness) Either remove this method or add the updated value to
+ // the cache.
+
// Build structures to hold the updated values.
std::unique_ptr<
leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector>
@@ -58,3 +58,81 @@ void BudgetDatabase::SetValue(const GURL& origin,
// Send the updates to the database.
db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback);
}
+
+void BudgetDatabase::GetBudgetDetails(
+ const GURL& origin,
+ const GetBudgetDetailsCallback& callback) {
+ DCHECK_EQ(origin.GetOrigin(), origin);
+
+ // If this origin is already in the cache, immediately return the data.
+ if (budget_map_.find(origin.spec()) != budget_map_.end())
+ ReturnBudgetDetails(origin, callback, true);
Peter Beverloo 2016/07/22 16:14:51 This is a risk of prefixing your method names with
harkness 2016/07/26 16:42:07 Nope, this is just me being stupid because I have
+
+ // Otherwise, query for the data, add it to the cache, then return the result.
+ AddToCacheCallback cache_callback =
+ base::Bind(&BudgetDatabase::ReturnBudgetDetails,
+ weak_ptr_factory_.GetWeakPtr(), origin, callback);
+ db_->GetEntry(origin.spec(), base::Bind(&BudgetDatabase::AddToCache,
+ weak_ptr_factory_.GetWeakPtr(),
+ origin, cache_callback));
+}
+
+void BudgetDatabase::OnDatabaseInit(bool success) {
+ // TODO(harkness): Consider caching the budget database now?
+}
+
+void BudgetDatabase::AddToCache(
+ const GURL& origin,
+ const AddToCacheCallback& callback,
+ bool success,
+ std::unique_ptr<budget_service::Budget> budget_proto) {
+ // If the database read failed, there's nothing to add to the cache.
+ if (!success)
+ callback.Run(success);
Peter Beverloo 2016/07/22 16:14:51 Control flow still continues after this check, so
harkness 2016/07/26 16:42:07 or just being stupid. :)
+
+ // Add the data to the cache.
+ BudgetDatabase::BudgetInfo& info = budget_map_[origin.spec()];
+ info.second.clear();
+ info.first = budget_proto->debt();
+
+ // Convert from the proto format to an STL format which is better for removing
+ // things from the front of the list.
+ for (int i = 0; i < budget_proto->budget_size(); i++) {
+ const budget_service::BudgetChunk& chunk = budget_proto->budget(i);
+ info.second.push_back(
+ std::pair<double, double>(chunk.amount(), chunk.expiration()));
+ }
Peter Beverloo 2016/07/22 16:14:52 Since you're clearing the |info| after getting it,
harkness 2016/07/26 16:42:07 Yes, the two have equivalent effects. In terms of
Peter Beverloo 2016/07/26 17:26:20 Semantically you're replacing the entry in |budget
+ callback.Run(success);
+}
+
+void BudgetDatabase::ReturnBudgetDetails(
Peter Beverloo 2016/07/22 16:14:51 suggestion: DidGetBudgetChunks?
harkness 2016/07/26 16:42:07 I went with DidGetBudget
+ const GURL& origin,
+ const GetBudgetDetailsCallback& callback,
+ bool success) {
+ // If the database wasn't able to read the information, return the
+ // failure and an empty BudgetExpectation.
+ if (!success)
+ callback.Run(success, 0.0, BudgetExpectation());
Peter Beverloo 2016/07/22 16:14:51 nit: s/0.0/0/, elsewhere too
Peter Beverloo 2016/07/22 16:14:51 dito - control flow still continues...
harkness 2016/07/26 16:42:07 Acknowledged.
harkness 2016/07/26 16:42:07 Done.
+
+ // Otherwise, 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.
+ const BudgetInfo& info = budget_map_[origin.spec()];
+ BudgetExpectation expectation;
+ double total = 0.0;
+
+ // Starting with the chunks that expire the farthest in the future, build up
+ // the budget expectations for those future times.
+ for (BudgetChunks::const_reverse_iterator iter = info.second.rbegin();
+ iter != info.second.rend(); iter++) {
+ expectation.push_front(std::pair<double, double>(total, iter->second));
Peter Beverloo 2016/07/22 16:14:51 std::make_pair
harkness 2016/07/26 16:42:07 Done.
+ total += iter->first;
+ }
+
+ // Always add one entry at the front of the list for the total budget right
+ // now.
+ expectation.push_front(std::pair<double, double>(total, 0));
Peter Beverloo 2016/07/22 16:14:51 std::make_pair
harkness 2016/07/26 16:42:07 Done.
+
+ callback.Run(success, info.first, expectation);
+}

Powered by Google App Engine
This is Rietveld 408576698