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) { | |
Peter Beverloo
2016/08/09 13:12:42
nit: make 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 double debt = budget_proto->has_debt() ? budget_proto->debt() : 0; | 108 double debt = budget_proto->has_debt() ? budget_proto->debt() : 0; |
140 budget_map_[origin.spec()] = std::make_pair(debt, std::move(chunks)); | 109 budget_map_[origin.spec()] = std::make_pair(debt, std::move(chunks)); |
141 | 110 |
142 callback.Run(success); | 111 callback.Run(success); |
143 } | 112 } |
144 | 113 |
145 void BudgetDatabase::DidGetBudget(const GURL& origin, | 114 void BudgetDatabase::DidGetBudget(const GURL& origin, |
146 const GetBudgetDetailsCallback& callback, | 115 const GetBudgetDetailsCallback& callback, |
147 bool success) { | 116 bool success) { |
148 // If the database wasn't able to read the information, return the | 117 // If the database wasn't able to read the information, return the |
149 // failure and an empty BudgetExpectation. | 118 // failure and an empty BudgetExpectation. |
150 if (!success) { | 119 if (!success) { |
151 callback.Run(success, 0, BudgetExpectation()); | 120 callback.Run(success, 0, BudgetExpectation()); |
152 return; | 121 return; |
153 } | 122 } |
154 | 123 |
155 // Otherwise, build up the BudgetExpection. This is different from the format | 124 // First, cleanup any expired budget chunks for the origin. |
125 CleanupExpiredBudget(origin); | |
126 | |
127 // Now, build up the BudgetExpection. This is different from the format | |
156 // in which the cache stores the data. The cache stores chunks of budget and | 128 // in which the cache stores the data. The cache stores chunks of budget and |
157 // when that budget expires. The BudgetExpectation describes a set of times | 129 // when that budget expires. The BudgetExpectation describes a set of times |
158 // and the budget at those times. | 130 // and the budget at those times. |
159 const BudgetInfo& info = budget_map_[origin.spec()]; | |
160 BudgetExpectation expectation; | 131 BudgetExpectation expectation; |
161 double total = 0; | 132 double total = 0; |
133 double debt = 0; | |
162 | 134 |
163 // Starting with the chunks that expire the farthest in the future, build up | 135 if (IsCached(origin)) { |
164 // the budget expectations for those future times. | 136 // Starting with the chunks that expire the farthest in the future, build up |
165 for (const auto& chunk : base::Reversed(info.second)) { | 137 // the budget expectations for those future times. |
166 expectation.push_front(std::make_pair(total, chunk.second)); | 138 const BudgetInfo& info = budget_map_[origin.spec()]; |
167 total += chunk.first; | 139 for (const auto& chunk : base::Reversed(info.second)) { |
140 expectation.push_front(std::make_pair(total, chunk.second)); | |
141 total += chunk.first; | |
142 } | |
143 debt = info.first; | |
168 } | 144 } |
169 | 145 |
170 // Always add one entry at the front of the list for the total budget right | 146 // Always add one entry at the front of the list for the total budget now. |
171 // now. | 147 expectation.push_front(std::make_pair(total, clock_->Now())); |
172 expectation.push_front(std::make_pair(total, 0)); | |
173 | 148 |
174 callback.Run(true /* success */, info.first, expectation); | 149 callback.Run(true /* success */, debt, expectation); |
175 } | 150 } |
176 | 151 |
177 // Override the default clock with the specified clock. Only used for testing. | 152 // Override the default clock with the specified clock. Only used for testing. |
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 |