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