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