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

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: More code review cleanup 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..096333d7ef8eb4edf667c9ca45db4578b859cb04 100644
--- a/chrome/browser/budget_service/budget_database.cc
+++ b/chrome/browser/budget_service/budget_database.cc
@@ -9,6 +9,8 @@
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"
+using content::BrowserThread;
+
namespace {
// UMA are logged for the database with this string as part of the name.
@@ -31,10 +33,7 @@ BudgetDatabase::BudgetDatabase(
BudgetDatabase::~BudgetDatabase() {}
-void BudgetDatabase::OnDatabaseInit(bool success) {
- // TODO(harkness): Consider caching the budget database now?
-}
-
+// TODO(harkness): Remove this method once the replacement is available.
void BudgetDatabase::GetValue(const GURL& origin,
const GetValueCallback& callback) {
DCHECK_EQ(origin.GetOrigin(), origin);
@@ -46,6 +45,8 @@ void BudgetDatabase::SetValue(const GURL& origin,
const SetValueCallback& callback) {
DCHECK_EQ(origin.GetOrigin(), origin);
+ // TODO(harkness) Remove this method once the replacement is available.
+
// Build structures to hold the updated values.
std::unique_ptr<
leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector>
@@ -58,3 +59,86 @@ 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()) {
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(&BudgetDatabase::DidGetBudget,
+ weak_ptr_factory_.GetWeakPtr(), origin,
+ callback, true /* success */));
+ return;
+ }
+
+ // Otherwise, query for the data, add it to the cache, then return the result.
+ AddToCacheCallback cache_callback =
+ base::Bind(&BudgetDatabase::DidGetBudget, 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);
+ return;
+ }
+
+ // 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(), chunk.expiration()));
+
+ budget_map_[origin.spec()] =
+ std::make_pair(budget_proto->debt(), std::move(chunks));
+
+ callback.Run(success);
+}
+
+void BudgetDatabase::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, BudgetExpectation());
+ return;
+ }
+
+ // 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;
+
+ // 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::make_pair(total, iter->second));
+ total += iter->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));
+
+ callback.Run(success, info.first, expectation);
Peter Beverloo 2016/07/27 11:21:40 nit: success -> true /* success */ Might be a wee
harkness 2016/07/27 12:23:41 Done.
+}

Powered by Google App Engine
This is Rietveld 408576698