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..6a3af69eb9c93021b0cb6fb910e5615d9402a3c4 100644 |
--- a/chrome/browser/budget_service/budget_database.cc |
+++ b/chrome/browser/budget_service/budget_database.cc |
@@ -4,11 +4,14 @@ |
#include "chrome/browser/budget_service/budget_database.h" |
+#include "base/containers/adapters.h" |
#include "chrome/browser/budget_service/budget.pb.h" |
#include "components/leveldb_proto/proto_database_impl.h" |
#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 +34,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 +46,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 +60,85 @@ 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 (const auto& chunk : base::Reversed(info.second)) { |
+ expectation.push_front(std::make_pair(total, 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)); |
+ |
+ callback.Run(true /* success */, info.first, expectation); |
+} |