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

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: Code review comments 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..763604e0beb67add9eb0f79f05a417794d0bf392 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): Either remove this method or add caching.
void BudgetDatabase::GetValue(const GURL& origin,
const GetValueCallback& callback) {
DCHECK_EQ(origin.GetOrigin(), origin);
@@ -46,6 +45,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.
Peter Beverloo 2016/07/26 17:26:21 I think we decided on this one, so remove the ambi
harkness 2016/07/27 10:59:03 Done.
+
// Build structures to hold the updated values.
std::unique_ptr<
leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector>
@@ -58,3 +60,90 @@ 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));
Peter Beverloo 2016/07/26 17:26:20 true -> true /* success */
harkness 2016/07/27 10:59:03 Done.
+ 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.
+ 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()));
+ }
+
+ 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();
Peter Beverloo 2016/07/26 17:26:20 const auto&
harkness 2016/07/27 10:59:02 I believe there are issues with const auto& in thi
Peter Beverloo 2016/07/27 11:21:40 You're right re: const, because we're incrementing
+ 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);
+}

Powered by Google App Engine
This is Rietveld 408576698