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