| 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 "base/containers/adapters.h" | 7 #include "base/containers/adapters.h" |
| 8 #include "base/time/clock.h" | 8 #include "base/time/clock.h" |
| 9 #include "base/time/default_clock.h" | 9 #include "base/time/default_clock.h" |
| 10 #include "chrome/browser/budget_service/budget.pb.h" | 10 #include "chrome/browser/budget_service/budget.pb.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 | 41 |
| 42 BudgetDatabase::~BudgetDatabase() {} | 42 BudgetDatabase::~BudgetDatabase() {} |
| 43 | 43 |
| 44 void BudgetDatabase::GetBudgetDetails( | 44 void BudgetDatabase::GetBudgetDetails( |
| 45 const GURL& origin, | 45 const GURL& origin, |
| 46 const GetBudgetDetailsCallback& callback) { | 46 const GetBudgetDetailsCallback& callback) { |
| 47 DCHECK_EQ(origin.GetOrigin(), origin); | 47 DCHECK_EQ(origin.GetOrigin(), origin); |
| 48 | 48 |
| 49 // If this origin is already in the cache, immediately return the data. | 49 // If this origin is already in the cache, immediately return the data. |
| 50 if (budget_map_.find(origin.spec()) != budget_map_.end()) { | 50 if (IsCached(origin)) { |
| 51 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 51 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 52 base::Bind(&BudgetDatabase::DidGetBudget, | 52 base::Bind(&BudgetDatabase::DidGetBudget, |
| 53 weak_ptr_factory_.GetWeakPtr(), origin, | 53 weak_ptr_factory_.GetWeakPtr(), origin, |
| 54 callback, true /* success */)); | 54 callback, true /* success */)); |
| 55 return; | 55 return; |
| 56 } | 56 } |
| 57 | 57 |
| 58 // Otherwise, query for the data, add it to the cache, then return the result. | 58 // Otherwise, query for the data, add it to the cache, then return the result. |
| 59 AddToCacheCallback cache_callback = | 59 AddToCacheCallback cache_callback = |
| 60 base::Bind(&BudgetDatabase::DidGetBudget, weak_ptr_factory_.GetWeakPtr(), | 60 base::Bind(&BudgetDatabase::DidGetBudget, weak_ptr_factory_.GetWeakPtr(), |
| 61 origin, callback); | 61 origin, callback); |
| 62 db_->GetEntry(origin.spec(), base::Bind(&BudgetDatabase::AddToCache, | 62 db_->GetEntry(origin.spec(), base::Bind(&BudgetDatabase::AddToCache, |
| 63 weak_ptr_factory_.GetWeakPtr(), | 63 weak_ptr_factory_.GetWeakPtr(), |
| 64 origin, cache_callback)); | 64 origin, cache_callback)); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void BudgetDatabase::AddBudget(const GURL& origin, | 67 void BudgetDatabase::AddBudget(const GURL& origin, |
| 68 double amount, | 68 double amount, |
| 69 const StoreBudgetCallback& callback) { | 69 const StoreBudgetCallback& callback) { |
| 70 DCHECK_EQ(origin.GetOrigin(), origin); | 70 DCHECK_EQ(origin.GetOrigin(), origin); |
| 71 | 71 |
| 72 // Look up the origin in our cache. Adding budget without first querying the | 72 // Add a new chunk of budget for the origin at the default expiration time. |
| 73 // existing budget is not suported. | |
| 74 DCHECK_GT(budget_map_.count(origin.spec()), 0U); | |
| 75 | |
| 76 base::Time expiration = | 73 base::Time expiration = |
| 77 clock_->Now() + base::TimeDelta::FromHours(kBudgetDurationInHours); | 74 clock_->Now() + base::TimeDelta::FromHours(kBudgetDurationInHours); |
| 78 budget_map_[origin.spec()].second.push_back( | 75 budget_map_[origin.spec()].second.push_back( |
| 79 std::make_pair(amount, expiration.ToInternalValue())); | 76 std::make_pair(amount, expiration)); |
| 80 | 77 |
| 81 // Now that the cache is updated, write the data to the database. | 78 // Now that the cache is updated, write the data to the database. |
| 82 WriteCachedValuesToDatabase(origin, callback); | 79 WriteCachedValuesToDatabase(origin, callback); |
| 83 } | 80 } |
| 84 | 81 |
| 85 void BudgetDatabase::WriteCachedValuesToDatabase( | |
| 86 const GURL& origin, | |
| 87 const StoreBudgetCallback& callback) { | |
| 88 // Create the data structures that are passed to the ProtoDatabase. | |
| 89 std::unique_ptr< | |
| 90 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> | |
| 91 entries(new leveldb_proto::ProtoDatabase< | |
| 92 budget_service::Budget>::KeyEntryVector()); | |
| 93 std::unique_ptr<std::vector<std::string>> keys_to_remove( | |
| 94 new std::vector<std::string>()); | |
| 95 | |
| 96 // Each operation can either update the existing budget or remove the origin's | |
| 97 // budget information. | |
| 98 if (budget_map_.find(origin.spec()) == budget_map_.end()) { | |
| 99 // If the origin doesn't exist in the cache, this is a remove operation. | |
| 100 keys_to_remove->push_back(origin.spec()); | |
| 101 } else { | |
| 102 // Build the Budget proto object. | |
| 103 budget_service::Budget budget; | |
| 104 const BudgetInfo& info = budget_map_[origin.spec()]; | |
| 105 budget.set_debt(info.first); | |
| 106 for (const auto& chunk : info.second) { | |
| 107 budget_service::BudgetChunk* budget_chunk = budget.add_budget(); | |
| 108 budget_chunk->set_amount(chunk.first); | |
| 109 budget_chunk->set_expiration(chunk.second); | |
| 110 } | |
| 111 entries->push_back(std::make_pair(origin.spec(), budget)); | |
| 112 } | |
| 113 | |
| 114 // Send the updates to the database. | |
| 115 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); | |
| 116 } | |
| 117 | |
| 118 void BudgetDatabase::OnDatabaseInit(bool success) { | 82 void BudgetDatabase::OnDatabaseInit(bool success) { |
| 119 // TODO(harkness): Consider caching the budget database now? | 83 // TODO(harkness): Consider caching the budget database now? |
| 120 } | 84 } |
| 121 | 85 |
| 86 bool BudgetDatabase::IsCached(const GURL& origin) const { |
| 87 return budget_map_.find(origin.spec()) != budget_map_.end(); |
| 88 } |
| 89 |
| 122 void BudgetDatabase::AddToCache( | 90 void BudgetDatabase::AddToCache( |
| 123 const GURL& origin, | 91 const GURL& origin, |
| 124 const AddToCacheCallback& callback, | 92 const AddToCacheCallback& callback, |
| 125 bool success, | 93 bool success, |
| 126 std::unique_ptr<budget_service::Budget> budget_proto) { | 94 std::unique_ptr<budget_service::Budget> budget_proto) { |
| 127 // If the database read failed, there's nothing to add to the cache. | 95 // If the database read failed, there's nothing to add to the cache. |
| 128 if (!success || !budget_proto) { | 96 if (!success || !budget_proto) { |
| 129 callback.Run(success); | 97 callback.Run(success); |
| 130 return; | 98 return; |
| 131 } | 99 } |
| 132 | 100 |
| 133 // Add the data to the cache, converting from the proto format to an STL | 101 // Add the data to the cache, converting from the proto format to an STL |
| 134 // format which is better for removing things from the list. | 102 // format which is better for removing things from the list. |
| 135 BudgetChunks chunks; | 103 BudgetChunks chunks; |
| 136 for (const auto& chunk : budget_proto->budget()) | 104 for (const auto& chunk : budget_proto->budget()) |
| 137 chunks.push_back(std::make_pair(chunk.amount(), chunk.expiration())); | 105 chunks.push_back(std::make_pair( |
| 106 chunk.amount(), base::Time::FromInternalValue(chunk.expiration()))); |
| 138 | 107 |
| 139 DCHECK(budget_proto->has_debt()); | 108 DCHECK(budget_proto->has_debt()); |
| 140 budget_map_[origin.spec()] = | 109 budget_map_[origin.spec()] = |
| 141 std::make_pair(budget_proto->debt(), std::move(chunks)); | 110 std::make_pair(budget_proto->debt(), std::move(chunks)); |
| 142 | 111 |
| 143 callback.Run(success); | 112 callback.Run(success); |
| 144 } | 113 } |
| 145 | 114 |
| 146 void BudgetDatabase::DidGetBudget(const GURL& origin, | 115 void BudgetDatabase::DidGetBudget(const GURL& origin, |
| 147 const GetBudgetDetailsCallback& callback, | 116 const GetBudgetDetailsCallback& callback, |
| 148 bool success) { | 117 bool success) { |
| 149 // If the database wasn't able to read the information, return the | 118 // If the database wasn't able to read the information, return the |
| 150 // failure and an empty BudgetExpectation. | 119 // failure and an empty BudgetExpectation. |
| 151 if (!success) { | 120 if (!success) { |
| 152 callback.Run(success, 0, BudgetExpectation()); | 121 callback.Run(success, 0, BudgetExpectation()); |
| 153 return; | 122 return; |
| 154 } | 123 } |
| 155 | 124 |
| 156 // Otherwise, build up the BudgetExpection. This is different from the format | 125 // First, cleanup any expired budget chunks for the origin. |
| 126 CleanupExpiredBudget(origin); |
| 127 |
| 128 // Now, build up the BudgetExpection. This is different from the format |
| 157 // in which the cache stores the data. The cache stores chunks of budget and | 129 // in which the cache stores the data. The cache stores chunks of budget and |
| 158 // when that budget expires. The BudgetExpectation describes a set of times | 130 // when that budget expires. The BudgetExpectation describes a set of times |
| 159 // and the budget at those times. | 131 // and the budget at those times. |
| 160 const BudgetInfo& info = budget_map_[origin.spec()]; | |
| 161 BudgetExpectation expectation; | 132 BudgetExpectation expectation; |
| 162 double total = 0; | 133 double total = 0; |
| 134 double debt = 0; |
| 163 | 135 |
| 164 // Starting with the chunks that expire the farthest in the future, build up | 136 if (IsCached(origin)) { |
| 165 // the budget expectations for those future times. | 137 // Starting with the chunks that expire the farthest in the future, build up |
| 166 for (const auto& chunk : base::Reversed(info.second)) { | 138 // the budget expectations for those future times. |
| 167 expectation.push_front(std::make_pair(total, chunk.second)); | 139 const BudgetInfo& info = budget_map_[origin.spec()]; |
| 168 total += chunk.first; | 140 for (const auto& chunk : base::Reversed(info.second)) { |
| 141 expectation.push_front(std::make_pair(total, chunk.second)); |
| 142 total += chunk.first; |
| 143 } |
| 144 debt = info.first; |
| 169 } | 145 } |
| 170 | 146 |
| 171 // Always add one entry at the front of the list for the total budget right | 147 // Always add one entry at the front of the list for the total budget now. |
| 172 // now. | 148 expectation.push_front(std::make_pair(total, clock_->Now())); |
| 173 expectation.push_front(std::make_pair(total, 0)); | |
| 174 | 149 |
| 175 callback.Run(true /* success */, info.first, expectation); | 150 callback.Run(true /* success */, debt, expectation); |
| 176 } | 151 } |
| 177 | 152 |
| 178 void BudgetDatabase::SetClockForTesting(std::unique_ptr<base::Clock> clock) { | 153 void BudgetDatabase::SetClockForTesting(std::unique_ptr<base::Clock> clock) { |
| 179 clock_ = std::move(clock); | 154 clock_ = std::move(clock); |
| 180 } | 155 } |
| 156 |
| 157 void BudgetDatabase::WriteCachedValuesToDatabase( |
| 158 const GURL& origin, |
| 159 const StoreBudgetCallback& callback) { |
| 160 // First, cleanup any expired budget chunks for the origin. |
| 161 CleanupExpiredBudget(origin); |
| 162 |
| 163 // Create the data structures that are passed to the ProtoDatabase. |
| 164 std::unique_ptr< |
| 165 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> |
| 166 entries(new leveldb_proto::ProtoDatabase< |
| 167 budget_service::Budget>::KeyEntryVector()); |
| 168 std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 169 new std::vector<std::string>()); |
| 170 |
| 171 // Each operation can either update the existing budget or remove the origin's |
| 172 // budget information. |
| 173 if (IsCached(origin)) { |
| 174 // Build the Budget proto object. |
| 175 budget_service::Budget budget; |
| 176 const BudgetInfo& info = budget_map_[origin.spec()]; |
| 177 budget.set_debt(info.first); |
| 178 for (const auto& chunk : info.second) { |
| 179 budget_service::BudgetChunk* budget_chunk = budget.add_budget(); |
| 180 budget_chunk->set_amount(chunk.first); |
| 181 budget_chunk->set_expiration(chunk.second.ToInternalValue()); |
| 182 } |
| 183 entries->push_back(std::make_pair(origin.spec(), budget)); |
| 184 } else { |
| 185 // If the origin doesn't exist in the cache, this is a remove operation. |
| 186 keys_to_remove->push_back(origin.spec()); |
| 187 } |
| 188 |
| 189 // Send the updates to the database. |
| 190 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); |
| 191 } |
| 192 |
| 193 void BudgetDatabase::CleanupExpiredBudget(const GURL& origin) { |
| 194 if (!IsCached(origin)) |
| 195 return; |
| 196 |
| 197 base::Time now = clock_->Now(); |
| 198 |
| 199 BudgetChunks& chunks = budget_map_[origin.spec()].second; |
| 200 auto cleanup_iter = chunks.begin(); |
| 201 |
| 202 // This relies on the list of chunks being in timestamp order. |
| 203 while (cleanup_iter != chunks.end() && cleanup_iter->second <= now) |
| 204 cleanup_iter = chunks.erase(cleanup_iter); |
| 205 |
| 206 // If the entire budget is empty now, cleanup the map. |
| 207 if (chunks.empty()) |
| 208 budget_map_.erase(origin.spec()); |
| 209 } |
| OLD | NEW |