Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/budget_service/budget_database.h" | 5 #include "chrome/browser/budget_service/budget_database.h" |
| 6 | 6 |
| 7 #include "chrome/browser/budget_service/budget.pb.h" | 7 #include "chrome/browser/budget_service/budget.pb.h" |
| 8 #include "components/leveldb_proto/proto_database_impl.h" | 8 #include "components/leveldb_proto/proto_database_impl.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "url/gurl.h" | 10 #include "url/gurl.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 : db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>( | 24 : db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>( |
| 25 task_runner)), | 25 task_runner)), |
| 26 weak_ptr_factory_(this) { | 26 weak_ptr_factory_(this) { |
| 27 db_->Init(kDatabaseUMAName, database_dir, | 27 db_->Init(kDatabaseUMAName, database_dir, |
| 28 base::Bind(&BudgetDatabase::OnDatabaseInit, | 28 base::Bind(&BudgetDatabase::OnDatabaseInit, |
| 29 weak_ptr_factory_.GetWeakPtr())); | 29 weak_ptr_factory_.GetWeakPtr())); |
| 30 } | 30 } |
| 31 | 31 |
| 32 BudgetDatabase::~BudgetDatabase() {} | 32 BudgetDatabase::~BudgetDatabase() {} |
| 33 | 33 |
| 34 void BudgetDatabase::OnDatabaseInit(bool success) { | 34 // 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
| |
| 35 // TODO(harkness): Consider caching the budget database now? | |
| 36 } | |
| 37 | |
| 38 void BudgetDatabase::GetValue(const GURL& origin, | 35 void BudgetDatabase::GetValue(const GURL& origin, |
| 39 const GetValueCallback& callback) { | 36 const GetValueCallback& callback) { |
| 40 DCHECK_EQ(origin.GetOrigin(), origin); | 37 DCHECK_EQ(origin.GetOrigin(), origin); |
| 41 db_->GetEntry(origin.spec(), callback); | 38 db_->GetEntry(origin.spec(), callback); |
| 42 } | 39 } |
| 43 | 40 |
| 44 void BudgetDatabase::SetValue(const GURL& origin, | 41 void BudgetDatabase::SetValue(const GURL& origin, |
| 45 const budget_service::Budget& budget, | 42 const budget_service::Budget& budget, |
| 46 const SetValueCallback& callback) { | 43 const SetValueCallback& callback) { |
| 47 DCHECK_EQ(origin.GetOrigin(), origin); | 44 DCHECK_EQ(origin.GetOrigin(), origin); |
| 48 | 45 |
| 46 // TODO(harkness) Either remove this method or add the updated value to | |
| 47 // the cache. | |
| 48 | |
| 49 // Build structures to hold the updated values. | 49 // Build structures to hold the updated values. |
| 50 std::unique_ptr< | 50 std::unique_ptr< |
| 51 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> | 51 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> |
| 52 entries(new leveldb_proto::ProtoDatabase< | 52 entries(new leveldb_proto::ProtoDatabase< |
| 53 budget_service::Budget>::KeyEntryVector()); | 53 budget_service::Budget>::KeyEntryVector()); |
| 54 entries->push_back(std::make_pair(origin.spec(), budget)); | 54 entries->push_back(std::make_pair(origin.spec(), budget)); |
| 55 std::unique_ptr<std::vector<std::string>> keys_to_remove( | 55 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 56 new std::vector<std::string>()); | 56 new std::vector<std::string>()); |
| 57 | 57 |
| 58 // Send the updates to the database. | 58 // Send the updates to the database. |
| 59 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); | 59 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); |
| 60 } | 60 } |
| 61 | |
| 62 void BudgetDatabase::GetBudgetDetails( | |
| 63 const GURL& origin, | |
| 64 const GetBudgetDetailsCallback& callback) { | |
| 65 DCHECK_EQ(origin.GetOrigin(), origin); | |
| 66 | |
| 67 // If this origin is already in the cache, immediately return the data. | |
| 68 if (budget_map_.find(origin.spec()) != budget_map_.end()) | |
| 69 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
| |
| 70 | |
| 71 // Otherwise, query for the data, add it to the cache, then return the result. | |
| 72 AddToCacheCallback cache_callback = | |
| 73 base::Bind(&BudgetDatabase::ReturnBudgetDetails, | |
| 74 weak_ptr_factory_.GetWeakPtr(), origin, callback); | |
| 75 db_->GetEntry(origin.spec(), base::Bind(&BudgetDatabase::AddToCache, | |
| 76 weak_ptr_factory_.GetWeakPtr(), | |
| 77 origin, cache_callback)); | |
| 78 } | |
| 79 | |
| 80 void BudgetDatabase::OnDatabaseInit(bool success) { | |
| 81 // TODO(harkness): Consider caching the budget database now? | |
| 82 } | |
| 83 | |
| 84 void BudgetDatabase::AddToCache( | |
| 85 const GURL& origin, | |
| 86 const AddToCacheCallback& callback, | |
| 87 bool success, | |
| 88 std::unique_ptr<budget_service::Budget> budget_proto) { | |
| 89 // If the database read failed, there's nothing to add to the cache. | |
| 90 if (!success) | |
| 91 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. :)
| |
| 92 | |
| 93 // Add the data to the cache. | |
| 94 BudgetDatabase::BudgetInfo& info = budget_map_[origin.spec()]; | |
| 95 info.second.clear(); | |
| 96 info.first = budget_proto->debt(); | |
| 97 | |
| 98 // Convert from the proto format to an STL format which is better for removing | |
| 99 // things from the front of the list. | |
| 100 for (int i = 0; i < budget_proto->budget_size(); i++) { | |
| 101 const budget_service::BudgetChunk& chunk = budget_proto->budget(i); | |
| 102 info.second.push_back( | |
| 103 std::pair<double, double>(chunk.amount(), chunk.expiration())); | |
| 104 } | |
|
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
| |
| 105 callback.Run(success); | |
| 106 } | |
| 107 | |
| 108 void BudgetDatabase::ReturnBudgetDetails( | |
|
Peter Beverloo
2016/07/22 16:14:51
suggestion: DidGetBudgetChunks?
harkness
2016/07/26 16:42:07
I went with DidGetBudget
| |
| 109 const GURL& origin, | |
| 110 const GetBudgetDetailsCallback& callback, | |
| 111 bool success) { | |
| 112 // If the database wasn't able to read the information, return the | |
| 113 // failure and an empty BudgetExpectation. | |
| 114 if (!success) | |
| 115 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.
| |
| 116 | |
| 117 // Otherwise, build up the BudgetExpection. This is different from the format | |
| 118 // in which the cache stores the data. The cache stores chunks of budget and | |
| 119 // when that budget expires. The BudgetExpectation describes a set of times | |
| 120 // and the budget at those times. | |
| 121 const BudgetInfo& info = budget_map_[origin.spec()]; | |
| 122 BudgetExpectation expectation; | |
| 123 double total = 0.0; | |
| 124 | |
| 125 // Starting with the chunks that expire the farthest in the future, build up | |
| 126 // the budget expectations for those future times. | |
| 127 for (BudgetChunks::const_reverse_iterator iter = info.second.rbegin(); | |
| 128 iter != info.second.rend(); iter++) { | |
| 129 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.
| |
| 130 total += iter->first; | |
| 131 } | |
| 132 | |
| 133 // Always add one entry at the front of the list for the total budget right | |
| 134 // now. | |
| 135 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.
| |
| 136 | |
| 137 callback.Run(success, info.first, expectation); | |
| 138 } | |
| OLD | NEW |