harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/budget_service/budget_database.h" |
| 6 | |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 7 | #include "base/metrics/histogram_macros.h" |
harkness | f8c9343 | 2016-08-08 15:41:59 | [diff] [blame] | 8 | #include "base/time/clock.h" |
| 9 | #include "base/time/default_clock.h" |
harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 10 | #include "chrome/browser/budget_service/budget.pb.h" |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 11 | #include "chrome/browser/engagement/site_engagement_score.h" |
| 12 | #include "chrome/browser/engagement/site_engagement_service.h" |
| 13 | #include "chrome/browser/profiles/profile.h" |
harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 14 | #include "components/leveldb_proto/proto_database_impl.h" |
| 15 | #include "content/public/browser/browser_thread.h" |
| 16 | #include "url/gurl.h" |
| 17 | |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 18 | using content::BrowserThread; |
| 19 | |
harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | // UMA are logged for the database with this string as part of the name. |
harkness | bea56c2 | 2016-08-16 07:23:00 | [diff] [blame] | 23 | // They will be LevelDB.*.BudgetManager. Changes here should be synchronized |
| 24 | // with histograms.xml. |
| 25 | const char kDatabaseUMAName[] = "BudgetManager"; |
harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 26 | |
harkness | f8c9343 | 2016-08-08 15:41:59 | [diff] [blame] | 27 | // The default amount of time during which a budget will be valid. |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 28 | // This is 10 days = 240 hours. |
| 29 | constexpr double kBudgetDurationInHours = 240; |
harkness | f8c9343 | 2016-08-08 15:41:59 | [diff] [blame] | 30 | |
harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 31 | } // namespace |
| 32 | |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 33 | BudgetDatabase::BudgetInfo::BudgetInfo() {} |
| 34 | |
| 35 | BudgetDatabase::BudgetInfo::BudgetInfo(const BudgetInfo&& other) |
| 36 | : last_engagement_award(other.last_engagement_award) { |
| 37 | chunks = std::move(other.chunks); |
| 38 | } |
| 39 | |
| 40 | BudgetDatabase::BudgetInfo::~BudgetInfo() {} |
| 41 | |
harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 42 | BudgetDatabase::BudgetDatabase( |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 43 | Profile* profile, |
harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 44 | const base::FilePath& database_dir, |
| 45 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 46 | : profile_(profile), |
| 47 | db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>( |
harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 48 | task_runner)), |
harkness | f8c9343 | 2016-08-08 15:41:59 | [diff] [blame] | 49 | clock_(base::WrapUnique(new base::DefaultClock)), |
harkness | 883658b | 2016-07-18 11:37:53 | [diff] [blame] | 50 | weak_ptr_factory_(this) { |
| 51 | db_->Init(kDatabaseUMAName, database_dir, |
| 52 | base::Bind(&BudgetDatabase::OnDatabaseInit, |
| 53 | weak_ptr_factory_.GetWeakPtr())); |
| 54 | } |
| 55 | |
| 56 | BudgetDatabase::~BudgetDatabase() {} |
| 57 | |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 58 | void BudgetDatabase::GetBudgetDetails(const GURL& origin, |
| 59 | const GetBudgetCallback& callback) { |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 60 | DCHECK_EQ(origin.GetOrigin(), origin); |
| 61 | |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 62 | SyncCache(origin, |
| 63 | base::Bind(&BudgetDatabase::GetBudgetAfterSync, |
| 64 | weak_ptr_factory_.GetWeakPtr(), origin, callback)); |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void BudgetDatabase::SpendBudget(const GURL& origin, |
| 68 | double amount, |
| 69 | const StoreBudgetCallback& callback) { |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 70 | SyncCache(origin, base::Bind(&BudgetDatabase::SpendBudgetAfterSync, |
| 71 | weak_ptr_factory_.GetWeakPtr(), origin, amount, |
| 72 | callback)); |
| 73 | } |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 74 | |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 75 | void BudgetDatabase::SetClockForTesting(std::unique_ptr<base::Clock> clock) { |
| 76 | clock_ = std::move(clock); |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 77 | } |
| 78 | |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 79 | void BudgetDatabase::OnDatabaseInit(bool success) { |
| 80 | // TODO(harkness): Consider caching the budget database now? |
| 81 | } |
| 82 | |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 83 | bool BudgetDatabase::IsCached(const GURL& origin) const { |
| 84 | return budget_map_.find(origin.spec()) != budget_map_.end(); |
| 85 | } |
| 86 | |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 87 | double BudgetDatabase::GetBudget(const GURL& origin) const { |
| 88 | double total = 0; |
| 89 | auto iter = budget_map_.find(origin.spec()); |
| 90 | if (iter == budget_map_.end()) |
| 91 | return total; |
| 92 | |
| 93 | const BudgetInfo& info = iter->second; |
| 94 | for (const BudgetChunk& chunk : info.chunks) |
| 95 | total += chunk.amount; |
| 96 | return total; |
| 97 | } |
| 98 | |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 99 | void BudgetDatabase::AddToCache( |
| 100 | const GURL& origin, |
| 101 | const AddToCacheCallback& callback, |
| 102 | bool success, |
| 103 | std::unique_ptr<budget_service::Budget> budget_proto) { |
| 104 | // If the database read failed, there's nothing to add to the cache. |
harkness | f8c9343 | 2016-08-08 15:41:59 | [diff] [blame] | 105 | if (!success || !budget_proto) { |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 106 | callback.Run(success); |
| 107 | return; |
| 108 | } |
| 109 | |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 110 | // If there were two simultaneous loads, don't overwrite the cache value, |
| 111 | // which might have been updated after the previous load. |
| 112 | if (IsCached(origin)) { |
| 113 | callback.Run(success); |
| 114 | return; |
| 115 | } |
| 116 | |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 117 | // Add the data to the cache, converting from the proto format to an STL |
| 118 | // format which is better for removing things from the list. |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 119 | BudgetInfo& info = budget_map_[origin.spec()]; |
harkness | bf8b385 | 2016-08-10 18:42:02 | [diff] [blame] | 120 | for (const auto& chunk : budget_proto->budget()) { |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 121 | info.chunks.emplace_back(chunk.amount(), |
| 122 | base::Time::FromInternalValue(chunk.expiration())); |
harkness | bf8b385 | 2016-08-10 18:42:02 | [diff] [blame] | 123 | } |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 124 | |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 125 | info.last_engagement_award = |
| 126 | base::Time::FromInternalValue(budget_proto->engagement_last_updated()); |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 127 | |
| 128 | callback.Run(success); |
| 129 | } |
| 130 | |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 131 | void BudgetDatabase::GetBudgetAfterSync(const GURL& origin, |
| 132 | const GetBudgetCallback& callback, |
| 133 | bool success) { |
| 134 | mojo::Array<blink::mojom::BudgetStatePtr> predictions; |
| 135 | |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 136 | // If the database wasn't able to read the information, return the |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 137 | // failure and an empty predictions array. |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 138 | if (!success) { |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 139 | callback.Run(blink::mojom::BudgetServiceErrorType::DATABASE_ERROR, |
| 140 | std::move(predictions)); |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 144 | // Now, build up the BudgetExpection. This is different from the format |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 145 | // in which the cache stores the data. The cache stores chunks of budget and |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 146 | // when that budget expires. The mojo array describes a set of times |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 147 | // and the budget at those times. |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 148 | double total = GetBudget(origin); |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 149 | |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 150 | // Always add one entry at the front of the list for the total budget now. |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 151 | blink::mojom::BudgetStatePtr prediction(blink::mojom::BudgetState::New()); |
| 152 | prediction->budget_at = total; |
| 153 | prediction->time = clock_->Now().ToDoubleT(); |
| 154 | predictions.push_back(std::move(prediction)); |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 155 | |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 156 | // Starting with the soonest expiring chunks, add entries for the |
| 157 | // expiration times going forward. |
| 158 | const BudgetChunks& chunks = budget_map_[origin.spec()].chunks; |
| 159 | for (const auto& chunk : chunks) { |
| 160 | blink::mojom::BudgetStatePtr prediction(blink::mojom::BudgetState::New()); |
| 161 | total -= chunk.amount; |
| 162 | prediction->budget_at = total; |
| 163 | prediction->time = chunk.expiration.ToDoubleT(); |
| 164 | predictions.push_back(std::move(prediction)); |
| 165 | } |
| 166 | |
| 167 | DCHECK_EQ(0, total); |
| 168 | |
| 169 | callback.Run(blink::mojom::BudgetServiceErrorType::NONE, |
| 170 | std::move(predictions)); |
harkness | 804b612a6 | 2016-07-27 12:53:31 | [diff] [blame] | 171 | } |
harkness | f8c9343 | 2016-08-08 15:41:59 | [diff] [blame] | 172 | |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 173 | void BudgetDatabase::SpendBudgetAfterSync(const GURL& origin, |
| 174 | double amount, |
| 175 | const StoreBudgetCallback& callback, |
| 176 | bool success) { |
| 177 | if (!success) { |
| 178 | callback.Run(false /* success */); |
| 179 | return; |
| 180 | } |
| 181 | |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 182 | // Get the current SES score, to generate UMA. |
| 183 | SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 184 | double score = service->GetScore(origin); |
| 185 | |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 186 | // Walk the list of budget chunks to see if the origin has enough budget. |
| 187 | double total = 0; |
| 188 | BudgetInfo& info = budget_map_[origin.spec()]; |
| 189 | for (const BudgetChunk& chunk : info.chunks) |
| 190 | total += chunk.amount; |
| 191 | |
| 192 | if (total < amount) { |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 193 | UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForNoBudgetOrigin", score); |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 194 | callback.Run(false /* success */); |
| 195 | return; |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 196 | } else if (total < amount * 2) { |
| 197 | UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForLowBudgetOrigin", score); |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Walk the chunks and remove enough budget to cover the needed amount. |
| 201 | double bill = amount; |
| 202 | for (auto iter = info.chunks.begin(); iter != info.chunks.end();) { |
| 203 | if (iter->amount > bill) { |
| 204 | iter->amount -= bill; |
| 205 | bill = 0; |
| 206 | break; |
| 207 | } |
| 208 | bill -= iter->amount; |
| 209 | iter = info.chunks.erase(iter); |
| 210 | } |
| 211 | |
| 212 | // There should have been enough budget to cover the entire bill. |
| 213 | DCHECK_EQ(0, bill); |
| 214 | |
| 215 | // Now that the cache is updated, write the data to the database. |
| 216 | // TODO(harkness): Consider adding a second parameter to the callback so the |
| 217 | // caller can distinguish between not enough budget and a failed database |
| 218 | // write. |
| 219 | // TODO(harkness): If the database write fails, the cache will be out of sync |
| 220 | // with the database. Consider ways to mitigate this. |
| 221 | WriteCachedValuesToDatabase(origin, callback); |
harkness | f8c9343 | 2016-08-08 15:41:59 | [diff] [blame] | 222 | } |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 223 | |
| 224 | void BudgetDatabase::WriteCachedValuesToDatabase( |
| 225 | const GURL& origin, |
| 226 | const StoreBudgetCallback& callback) { |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 227 | // Create the data structures that are passed to the ProtoDatabase. |
| 228 | std::unique_ptr< |
| 229 | leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector> |
| 230 | entries(new leveldb_proto::ProtoDatabase< |
| 231 | budget_service::Budget>::KeyEntryVector()); |
| 232 | std::unique_ptr<std::vector<std::string>> keys_to_remove( |
| 233 | new std::vector<std::string>()); |
| 234 | |
| 235 | // Each operation can either update the existing budget or remove the origin's |
| 236 | // budget information. |
| 237 | if (IsCached(origin)) { |
| 238 | // Build the Budget proto object. |
| 239 | budget_service::Budget budget; |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 240 | const BudgetInfo& info = budget_map_[origin.spec()]; |
| 241 | for (const auto& chunk : info.chunks) { |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 242 | budget_service::BudgetChunk* budget_chunk = budget.add_budget(); |
harkness | bf8b385 | 2016-08-10 18:42:02 | [diff] [blame] | 243 | budget_chunk->set_amount(chunk.amount); |
| 244 | budget_chunk->set_expiration(chunk.expiration.ToInternalValue()); |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 245 | } |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 246 | budget.set_engagement_last_updated( |
| 247 | info.last_engagement_award.ToInternalValue()); |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 248 | entries->push_back(std::make_pair(origin.spec(), budget)); |
| 249 | } else { |
| 250 | // If the origin doesn't exist in the cache, this is a remove operation. |
| 251 | keys_to_remove->push_back(origin.spec()); |
| 252 | } |
| 253 | |
| 254 | // Send the updates to the database. |
| 255 | db_->UpdateEntries(std::move(entries), std::move(keys_to_remove), callback); |
| 256 | } |
| 257 | |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 258 | void BudgetDatabase::SyncCache(const GURL& origin, |
| 259 | const SyncCacheCallback& callback) { |
| 260 | DCHECK_EQ(origin, origin.GetOrigin()); |
| 261 | |
| 262 | // If the origin isn't already cached, add it to the cache. |
| 263 | if (!IsCached(origin)) { |
| 264 | AddToCacheCallback add_callback = |
| 265 | base::Bind(&BudgetDatabase::SyncLoadedCache, |
| 266 | weak_ptr_factory_.GetWeakPtr(), origin, callback); |
| 267 | db_->GetEntry(origin.spec(), base::Bind(&BudgetDatabase::AddToCache, |
| 268 | weak_ptr_factory_.GetWeakPtr(), |
| 269 | origin, add_callback)); |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 270 | return; |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 271 | } |
| 272 | SyncLoadedCache(origin, callback, true /* success */); |
| 273 | } |
| 274 | |
| 275 | void BudgetDatabase::SyncLoadedCache(const GURL& origin, |
| 276 | const SyncCacheCallback& callback, |
| 277 | bool success) { |
| 278 | if (!success) { |
| 279 | callback.Run(false /* success */); |
| 280 | return; |
| 281 | } |
| 282 | |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 283 | // Now, cleanup any expired budget chunks for the origin. |
| 284 | bool needs_write = CleanupExpiredBudget(origin); |
| 285 | |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 286 | // Get the SES score and add engagement budget for the site. |
| 287 | AddEngagementBudget(origin); |
| 288 | |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 289 | if (needs_write) |
| 290 | WriteCachedValuesToDatabase(origin, callback); |
| 291 | else |
| 292 | callback.Run(success); |
| 293 | } |
| 294 | |
| 295 | void BudgetDatabase::AddEngagementBudget(const GURL& origin) { |
| 296 | // Get the current SES score, which we'll use to set a new budget. |
| 297 | SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 298 | double score = service->GetScore(origin); |
| 299 | |
| 300 | // By default we award the "full" award. Then that ratio is decreased if |
| 301 | // there have been other awards recently. |
| 302 | double ratio = 1.0; |
| 303 | |
| 304 | // Calculate how much budget should be awarded. If there is no entry in the |
| 305 | // cache then we award a full amount. |
| 306 | if (IsCached(origin)) { |
| 307 | base::TimeDelta elapsed = |
| 308 | clock_->Now() - budget_map_[origin.spec()].last_engagement_award; |
| 309 | int elapsed_hours = elapsed.InHours(); |
| 310 | // Don't give engagement awards for periods less than an hour. |
| 311 | if (elapsed_hours < 1) |
| 312 | return; |
| 313 | if (elapsed_hours < kBudgetDurationInHours) |
| 314 | ratio = elapsed_hours / kBudgetDurationInHours; |
| 315 | } |
| 316 | |
| 317 | // Update the last_engagement_award to the current time. If the origin wasn't |
| 318 | // already in the map, this adds a new entry for it. |
| 319 | budget_map_[origin.spec()].last_engagement_award = clock_->Now(); |
| 320 | |
| 321 | // Add a new chunk of budget for the origin at the default expiration time. |
| 322 | base::Time expiration = |
| 323 | clock_->Now() + base::TimeDelta::FromHours(kBudgetDurationInHours); |
| 324 | budget_map_[origin.spec()].chunks.emplace_back(ratio * score, expiration); |
harkness | 6f6b4143 | 2016-09-08 09:17:28 | [diff] [blame^] | 325 | |
| 326 | // Any time we award engagement budget, which is done at most once an hour |
| 327 | // whenever any budget action is taken, record the budget. |
| 328 | double budget = GetBudget(origin); |
| 329 | UMA_HISTOGRAM_COUNTS_100("PushMessaging.BackgroundBudget", budget); |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Cleans up budget in the cache. Relies on the caller eventually writing the |
| 333 | // cache back to the database. |
| 334 | bool BudgetDatabase::CleanupExpiredBudget(const GURL& origin) { |
| 335 | if (!IsCached(origin)) |
| 336 | return false; |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 337 | |
| 338 | base::Time now = clock_->Now(); |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 339 | BudgetChunks& chunks = budget_map_[origin.spec()].chunks; |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 340 | auto cleanup_iter = chunks.begin(); |
| 341 | |
| 342 | // This relies on the list of chunks being in timestamp order. |
harkness | bf8b385 | 2016-08-10 18:42:02 | [diff] [blame] | 343 | while (cleanup_iter != chunks.end() && cleanup_iter->expiration <= now) |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 344 | cleanup_iter = chunks.erase(cleanup_iter); |
| 345 | |
harkness | e0e2609 | 2016-08-24 06:29:32 | [diff] [blame] | 346 | // If the entire budget is empty now AND there have been no engagements |
| 347 | // in the last kBudgetDurationInHours hours, remove this from the cache. |
| 348 | if (chunks.empty() && |
| 349 | budget_map_[origin.spec()].last_engagement_award < |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 350 | clock_->Now() - base::TimeDelta::FromHours(kBudgetDurationInHours)) { |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 351 | budget_map_.erase(origin.spec()); |
harkness | c3fb045 | 2016-09-02 14:08:37 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | |
| 355 | // Although some things may have expired, there are some chunks still valid. |
| 356 | // Don't write to the DB now, write either when all chunks expire or when the |
| 357 | // origin spends some budget. |
| 358 | return false; |
harkness | 5186e6d82 | 2016-08-09 16:54:14 | [diff] [blame] | 359 | } |