blob: 8d71b8f3ed4ede4b9e93a9f8b2864bf3067dffa2 [file] [log] [blame]
harkness883658b2016-07-18 11:37:531// 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 Beverlooafb1ab42018-08-20 13:03:455#include "chrome/browser/push_messaging/budget_database.h"
harkness883658b2016-07-18 11:37:536
fdorayba121422016-12-23 19:51:487#include "base/memory/ptr_util.h"
harkness6f6b41432016-09-08 09:17:288#include "base/metrics/histogram_macros.h"
Gabriel Charette44db1422018-08-06 11:19:339#include "base/task/post_task.h"
harknessf8c93432016-08-08 15:41:5910#include "base/time/clock.h"
11#include "base/time/default_clock.h"
harknessc3fb0452016-09-02 14:08:3712#include "chrome/browser/engagement/site_engagement_score.h"
13#include "chrome/browser/engagement/site_engagement_service.h"
14#include "chrome/browser/profiles/profile.h"
Peter Beverlooafb1ab42018-08-20 13:03:4515#include "chrome/browser/push_messaging/budget.pb.h"
harkness883658b2016-07-18 11:37:5316#include "components/leveldb_proto/proto_database_impl.h"
17#include "content/public/browser/browser_thread.h"
18#include "url/gurl.h"
harkness03be8a1d2016-09-09 16:01:1219#include "url/origin.h"
harkness883658b2016-07-18 11:37:5320
harkness804b612a62016-07-27 12:53:3121using content::BrowserThread;
22
harkness883658b2016-07-18 11:37:5323namespace {
24
25// UMA are logged for the database with this string as part of the name.
harknessbea56c22016-08-16 07:23:0026// They will be LevelDB.*.BudgetManager. Changes here should be synchronized
27// with histograms.xml.
28const char kDatabaseUMAName[] = "BudgetManager";
harkness883658b2016-07-18 11:37:5329
harknessf8c93432016-08-08 15:41:5930// The default amount of time during which a budget will be valid.
harknessccdc3f3a2017-01-16 12:42:1731constexpr 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.
37constexpr double kMaximumHourlyBudget = 12.0 / 24.0;
harknessf8c93432016-08-08 15:41:5938
harkness883658b2016-07-18 11:37:5339} // namespace
40
Peter Beverlooafb1ab42018-08-20 13:03:4541BudgetState::BudgetState() = default;
42BudgetState::BudgetState(const BudgetState& other) = default;
43BudgetState::~BudgetState() = default;
44
45BudgetState& BudgetState::operator=(const BudgetState& other) = default;
46
47BudgetDatabase::BudgetInfo::BudgetInfo() = default;
harknesse0e26092016-08-24 06:29:3248
49BudgetDatabase::BudgetInfo::BudgetInfo(const BudgetInfo&& other)
50 : last_engagement_award(other.last_engagement_award) {
51 chunks = std::move(other.chunks);
52}
53
Peter Beverlooafb1ab42018-08-20 13:03:4554BudgetDatabase::BudgetInfo::~BudgetInfo() = default;
harknesse0e26092016-08-24 06:29:3255
Peter Beverlooafb1ab42018-08-20 13:03:4556BudgetDatabase::BudgetDatabase(Profile* profile)
harknessc3fb0452016-09-02 14:08:3757 : profile_(profile),
58 db_(new leveldb_proto::ProtoDatabaseImpl<budget_service::Budget>(
fdorayad78154a2017-04-03 17:11:0959 base::CreateSequencedTaskRunnerWithTraits(
Gabriel Charetteb10aeebc2018-07-26 20:15:0060 {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
fdoray4ad14932017-05-03 21:21:1161 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}))),
harknessf8c93432016-08-08 15:41:5962 clock_(base::WrapUnique(new base::DefaultClock)),
harkness883658b2016-07-18 11:37:5363 weak_ptr_factory_(this) {
Peter Beverlooafb1ab42018-08-20 13:03:4564 db_->Init(kDatabaseUMAName,
65 profile->GetPath().Append(FILE_PATH_LITERAL("BudgetDatabase")),
Chris Mumford29f2cf52017-09-21 00:35:4066 leveldb_proto::CreateSimpleOptions(),
67 base::BindOnce(&BudgetDatabase::OnDatabaseInit,
68 weak_ptr_factory_.GetWeakPtr()));
harkness883658b2016-07-18 11:37:5369}
70
Peter Beverlooafb1ab42018-08-20 13:03:4571BudgetDatabase::~BudgetDatabase() = default;
harkness883658b2016-07-18 11:37:5372
harkness03be8a1d2016-09-09 16:01:1273void BudgetDatabase::GetBudgetDetails(const url::Origin& origin,
tzikcf7bcd652017-06-15 04:19:3074 GetBudgetCallback callback) {
tzika8668f8c2017-06-26 08:58:0575 SyncCache(origin, base::BindOnce(&BudgetDatabase::GetBudgetAfterSync,
76 weak_ptr_factory_.GetWeakPtr(), origin,
tzik6d3cd7562018-02-21 12:07:2277 std::move(callback)));
harknesse0e26092016-08-24 06:29:3278}
79
harkness03be8a1d2016-09-09 16:01:1280void BudgetDatabase::SpendBudget(const url::Origin& origin,
Peter Beverlooafb1ab42018-08-20 13:03:4581 SpendBudgetCallback callback,
82 double amount) {
tzika8668f8c2017-06-26 08:58:0583 SyncCache(origin, base::BindOnce(&BudgetDatabase::SpendBudgetAfterSync,
84 weak_ptr_factory_.GetWeakPtr(), origin,
tzik6d3cd7562018-02-21 12:07:2285 amount, std::move(callback)));
harknessc3fb0452016-09-02 14:08:3786}
harknesse0e26092016-08-24 06:29:3287
harknessc3fb0452016-09-02 14:08:3788void BudgetDatabase::SetClockForTesting(std::unique_ptr<base::Clock> clock) {
89 clock_ = std::move(clock);
harknesse0e26092016-08-24 06:29:3290}
91
harkness804b612a62016-07-27 12:53:3192void BudgetDatabase::OnDatabaseInit(bool success) {
93 // TODO(harkness): Consider caching the budget database now?
94}
95
harkness03be8a1d2016-09-09 16:01:1296bool BudgetDatabase::IsCached(const url::Origin& origin) const {
97 return budget_map_.find(origin) != budget_map_.end();
harkness5186e6d822016-08-09 16:54:1498}
99
harkness03be8a1d2016-09-09 16:01:12100double BudgetDatabase::GetBudget(const url::Origin& origin) const {
harkness6f6b41432016-09-08 09:17:28101 double total = 0;
harkness03be8a1d2016-09-09 16:01:12102 auto iter = budget_map_.find(origin);
harkness6f6b41432016-09-08 09:17:28103 if (iter == budget_map_.end())
104 return total;
105
106 const BudgetInfo& info = iter->second;
107 for (const BudgetChunk& chunk : info.chunks)
108 total += chunk.amount;
109 return total;
110}
111
harkness804b612a62016-07-27 12:53:31112void BudgetDatabase::AddToCache(
harkness03be8a1d2016-09-09 16:01:12113 const url::Origin& origin,
tzikcf7bcd652017-06-15 04:19:30114 CacheCallback callback,
harkness804b612a62016-07-27 12:53:31115 bool success,
116 std::unique_ptr<budget_service::Budget> budget_proto) {
harkness0f9724a2016-09-13 14:43:12117 // If the database read failed or there's nothing to add, just return.
harknessf8c93432016-08-08 15:41:59118 if (!success || !budget_proto) {
tzikcf7bcd652017-06-15 04:19:30119 std::move(callback).Run(success);
harkness804b612a62016-07-27 12:53:31120 return;
121 }
122
harknessc3fb0452016-09-02 14:08:37123 // If there were two simultaneous loads, don't overwrite the cache value,
124 // which might have been updated after the previous load.
125 if (IsCached(origin)) {
tzikcf7bcd652017-06-15 04:19:30126 std::move(callback).Run(success);
harknessc3fb0452016-09-02 14:08:37127 return;
128 }
129
harkness804b612a62016-07-27 12:53:31130 // Add the data to the cache, converting from the proto format to an STL
131 // format which is better for removing things from the list.
harkness03be8a1d2016-09-09 16:01:12132 BudgetInfo& info = budget_map_[origin];
harknessbf8b3852016-08-10 18:42:02133 for (const auto& chunk : budget_proto->budget()) {
harknesse0e26092016-08-24 06:29:32134 info.chunks.emplace_back(chunk.amount(),
135 base::Time::FromInternalValue(chunk.expiration()));
harknessbf8b3852016-08-10 18:42:02136 }
harkness804b612a62016-07-27 12:53:31137
harknesse0e26092016-08-24 06:29:32138 info.last_engagement_award =
139 base::Time::FromInternalValue(budget_proto->engagement_last_updated());
harkness804b612a62016-07-27 12:53:31140
tzikcf7bcd652017-06-15 04:19:30141 std::move(callback).Run(success);
harkness804b612a62016-07-27 12:53:31142}
143
harkness03be8a1d2016-09-09 16:01:12144void BudgetDatabase::GetBudgetAfterSync(const url::Origin& origin,
tzikcf7bcd652017-06-15 04:19:30145 GetBudgetCallback callback,
harkness6f6b41432016-09-08 09:17:28146 bool success) {
Peter Beverlooafb1ab42018-08-20 13:03:45147 std::vector<BudgetState> predictions;
harkness6f6b41432016-09-08 09:17:28148
harkness804b612a62016-07-27 12:53:31149 // If the database wasn't able to read the information, return the
harkness6f6b41432016-09-08 09:17:28150 // failure and an empty predictions array.
harkness804b612a62016-07-27 12:53:31151 if (!success) {
Peter Beverlooafb1ab42018-08-20 13:03:45152 std::move(callback).Run(std::move(predictions));
harkness804b612a62016-07-27 12:53:31153 return;
154 }
155
harkness5186e6d822016-08-09 16:54:14156 // Now, build up the BudgetExpection. This is different from the format
harkness804b612a62016-07-27 12:53:31157 // in which the cache stores the data. The cache stores chunks of budget and
harkness6f6b41432016-09-08 09:17:28158 // when that budget expires. The mojo array describes a set of times
harkness804b612a62016-07-27 12:53:31159 // and the budget at those times.
harkness6f6b41432016-09-08 09:17:28160 double total = GetBudget(origin);
harkness804b612a62016-07-27 12:53:31161
harkness5186e6d822016-08-09 16:54:14162 // Always add one entry at the front of the list for the total budget now.
Peter Beverlooafb1ab42018-08-20 13:03:45163 BudgetState prediction;
164 prediction.budget_at = total;
165 prediction.time = clock_->Now().ToJsTime();
166 predictions.push_back(prediction);
harkness804b612a62016-07-27 12:53:31167
harkness6f6b41432016-09-08 09:17:28168 // Starting with the soonest expiring chunks, add entries for the
169 // expiration times going forward.
harkness03be8a1d2016-09-09 16:01:12170 const BudgetChunks& chunks = budget_map_[origin].chunks;
harkness6f6b41432016-09-08 09:17:28171 for (const auto& chunk : chunks) {
Peter Beverlooafb1ab42018-08-20 13:03:45172 BudgetState prediction;
harkness6f6b41432016-09-08 09:17:28173 total -= chunk.amount;
Peter Beverlooafb1ab42018-08-20 13:03:45174 prediction.budget_at = total;
175 prediction.time = chunk.expiration.ToJsTime();
176 predictions.push_back(prediction);
harkness6f6b41432016-09-08 09:17:28177 }
178
Peter Beverlooafb1ab42018-08-20 13:03:45179 std::move(callback).Run(std::move(predictions));
harkness804b612a62016-07-27 12:53:31180}
harknessf8c93432016-08-08 15:41:59181
harkness03be8a1d2016-09-09 16:01:12182void BudgetDatabase::SpendBudgetAfterSync(const url::Origin& origin,
harknessc3fb0452016-09-02 14:08:37183 double amount,
tzikcf7bcd652017-06-15 04:19:30184 SpendBudgetCallback callback,
harknessc3fb0452016-09-02 14:08:37185 bool success) {
186 if (!success) {
Peter Beverlooafb1ab42018-08-20 13:03:45187 std::move(callback).Run(false /* success */);
harknessc3fb0452016-09-02 14:08:37188 return;
189 }
190
harkness6f6b41432016-09-08 09:17:28191 // Get the current SES score, to generate UMA.
peter91882282017-05-25 17:57:33192 double score = GetSiteEngagementScoreForOrigin(origin);
harkness6f6b41432016-09-08 09:17:28193
harknessc3fb0452016-09-02 14:08:37194 // Walk the list of budget chunks to see if the origin has enough budget.
195 double total = 0;
harkness03be8a1d2016-09-09 16:01:12196 BudgetInfo& info = budget_map_[origin];
harknessc3fb0452016-09-02 14:08:37197 for (const BudgetChunk& chunk : info.chunks)
198 total += chunk.amount;
199
200 if (total < amount) {
harkness6f6b41432016-09-08 09:17:28201 UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForNoBudgetOrigin", score);
Peter Beverlooafb1ab42018-08-20 13:03:45202 std::move(callback).Run(false /* success */);
harknessc3fb0452016-09-02 14:08:37203 return;
harkness6f6b41432016-09-08 09:17:28204 } else if (total < amount * 2) {
205 UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForLowBudgetOrigin", score);
harknessc3fb0452016-09-02 14:08:37206 }
207
208 // Walk the chunks and remove enough budget to cover the needed amount.
209 double bill = amount;
210 for (auto iter = info.chunks.begin(); iter != info.chunks.end();) {
211 if (iter->amount > bill) {
212 iter->amount -= bill;
213 bill = 0;
214 break;
215 }
216 bill -= iter->amount;
217 iter = info.chunks.erase(iter);
218 }
219
220 // There should have been enough budget to cover the entire bill.
221 DCHECK_EQ(0, bill);
222
223 // Now that the cache is updated, write the data to the database.
harkness0f9724a2016-09-13 14:43:12224 WriteCachedValuesToDatabase(
tzikcf7bcd652017-06-15 04:19:30225 origin,
226 base::BindOnce(&BudgetDatabase::SpendBudgetAfterWrite,
227 weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
harkness0f9724a2016-09-13 14:43:12228}
229
230// This converts the bool value which is returned from the database to a Mojo
231// error type.
tzikcf7bcd652017-06-15 04:19:30232void BudgetDatabase::SpendBudgetAfterWrite(SpendBudgetCallback callback,
harkness0f9724a2016-09-13 14:43:12233 bool write_successful) {
harknessc3fb0452016-09-02 14:08:37234 // TODO(harkness): If the database write fails, the cache will be out of sync
235 // with the database. Consider ways to mitigate this.
harkness0f9724a2016-09-13 14:43:12236 if (!write_successful) {
Peter Beverlooafb1ab42018-08-20 13:03:45237 std::move(callback).Run(false /* success */);
harkness0f9724a2016-09-13 14:43:12238 return;
239 }
Peter Beverlooafb1ab42018-08-20 13:03:45240 std::move(callback).Run(true /* success */);
harknessf8c93432016-08-08 15:41:59241}
harkness5186e6d822016-08-09 16:54:14242
tzikcf7bcd652017-06-15 04:19:30243void BudgetDatabase::WriteCachedValuesToDatabase(const url::Origin& origin,
244 StoreBudgetCallback callback) {
harkness5186e6d822016-08-09 16:54:14245 // Create the data structures that are passed to the ProtoDatabase.
246 std::unique_ptr<
247 leveldb_proto::ProtoDatabase<budget_service::Budget>::KeyEntryVector>
248 entries(new leveldb_proto::ProtoDatabase<
249 budget_service::Budget>::KeyEntryVector());
250 std::unique_ptr<std::vector<std::string>> keys_to_remove(
251 new std::vector<std::string>());
252
253 // Each operation can either update the existing budget or remove the origin's
254 // budget information.
255 if (IsCached(origin)) {
256 // Build the Budget proto object.
257 budget_service::Budget budget;
harkness03be8a1d2016-09-09 16:01:12258 const BudgetInfo& info = budget_map_[origin];
harknesse0e26092016-08-24 06:29:32259 for (const auto& chunk : info.chunks) {
harkness5186e6d822016-08-09 16:54:14260 budget_service::BudgetChunk* budget_chunk = budget.add_budget();
harknessbf8b3852016-08-10 18:42:02261 budget_chunk->set_amount(chunk.amount);
262 budget_chunk->set_expiration(chunk.expiration.ToInternalValue());
harkness5186e6d822016-08-09 16:54:14263 }
harknesse0e26092016-08-24 06:29:32264 budget.set_engagement_last_updated(
265 info.last_engagement_award.ToInternalValue());
harkness03be8a1d2016-09-09 16:01:12266 entries->push_back(std::make_pair(origin.Serialize(), budget));
harkness5186e6d822016-08-09 16:54:14267 } else {
268 // If the origin doesn't exist in the cache, this is a remove operation.
harkness03be8a1d2016-09-09 16:01:12269 keys_to_remove->push_back(origin.Serialize());
harkness5186e6d822016-08-09 16:54:14270 }
271
272 // Send the updates to the database.
tzikcf7bcd652017-06-15 04:19:30273 db_->UpdateEntries(std::move(entries), std::move(keys_to_remove),
274 std::move(callback));
harkness5186e6d822016-08-09 16:54:14275}
276
harkness03be8a1d2016-09-09 16:01:12277void BudgetDatabase::SyncCache(const url::Origin& origin,
tzikcf7bcd652017-06-15 04:19:30278 CacheCallback callback) {
harknessc3fb0452016-09-02 14:08:37279 // If the origin isn't already cached, add it to the cache.
280 if (!IsCached(origin)) {
tzikcf7bcd652017-06-15 04:19:30281 CacheCallback add_callback = base::BindOnce(
282 &BudgetDatabase::SyncLoadedCache, weak_ptr_factory_.GetWeakPtr(),
283 origin, std::move(callback));
tzika8668f8c2017-06-26 08:58:05284 db_->GetEntry(origin.Serialize(),
285 base::BindOnce(&BudgetDatabase::AddToCache,
286 weak_ptr_factory_.GetWeakPtr(), origin,
tzik6d3cd7562018-02-21 12:07:22287 std::move(add_callback)));
harkness5186e6d822016-08-09 16:54:14288 return;
harknessc3fb0452016-09-02 14:08:37289 }
tzikcf7bcd652017-06-15 04:19:30290 SyncLoadedCache(origin, std::move(callback), true /* success */);
harknessc3fb0452016-09-02 14:08:37291}
292
harkness03be8a1d2016-09-09 16:01:12293void BudgetDatabase::SyncLoadedCache(const url::Origin& origin,
tzikcf7bcd652017-06-15 04:19:30294 CacheCallback callback,
harknessc3fb0452016-09-02 14:08:37295 bool success) {
296 if (!success) {
tzikcf7bcd652017-06-15 04:19:30297 std::move(callback).Run(false /* success */);
harknessc3fb0452016-09-02 14:08:37298 return;
299 }
300
harknessc3fb0452016-09-02 14:08:37301 // Now, cleanup any expired budget chunks for the origin.
302 bool needs_write = CleanupExpiredBudget(origin);
303
harkness6f6b41432016-09-08 09:17:28304 // Get the SES score and add engagement budget for the site.
305 AddEngagementBudget(origin);
306
harknessc3fb0452016-09-02 14:08:37307 if (needs_write)
tzikcf7bcd652017-06-15 04:19:30308 WriteCachedValuesToDatabase(origin, std::move(callback));
harknessc3fb0452016-09-02 14:08:37309 else
tzikcf7bcd652017-06-15 04:19:30310 std::move(callback).Run(success);
harknessc3fb0452016-09-02 14:08:37311}
312
harkness03be8a1d2016-09-09 16:01:12313void BudgetDatabase::AddEngagementBudget(const url::Origin& origin) {
harknessccdc3f3a2017-01-16 12:42:17314 // Calculate how much budget should be awarded. The award depends on the
315 // time elapsed since the last award and the SES score.
316 // By default, give the origin kBudgetDurationInDays worth of budget, but
317 // reduce that if budget has already been given during that period.
318 base::TimeDelta elapsed = base::TimeDelta::FromDays(kBudgetDurationInDays);
harknessc3fb0452016-09-02 14:08:37319 if (IsCached(origin)) {
harknessccdc3f3a2017-01-16 12:42:17320 elapsed = clock_->Now() - budget_map_[origin].last_engagement_award;
harknessc3fb0452016-09-02 14:08:37321 // Don't give engagement awards for periods less than an hour.
harknessccdc3f3a2017-01-16 12:42:17322 if (elapsed.InHours() < 1)
harknessc3fb0452016-09-02 14:08:37323 return;
harknessccdc3f3a2017-01-16 12:42:17324 // Cap elapsed time to the budget duration.
325 if (elapsed.InDays() > kBudgetDurationInDays)
326 elapsed = base::TimeDelta::FromDays(kBudgetDurationInDays);
harknessc3fb0452016-09-02 14:08:37327 }
328
harknessccdc3f3a2017-01-16 12:42:17329 // Get the current SES score, and calculate the hourly budget for that score.
harknessccdc3f3a2017-01-16 12:42:17330 double hourly_budget = kMaximumHourlyBudget *
peter91882282017-05-25 17:57:33331 GetSiteEngagementScoreForOrigin(origin) /
332 SiteEngagementService::GetMaxPoints();
harknessccdc3f3a2017-01-16 12:42:17333
harknessc3fb0452016-09-02 14:08:37334 // Update the last_engagement_award to the current time. If the origin wasn't
335 // already in the map, this adds a new entry for it.
harkness03be8a1d2016-09-09 16:01:12336 budget_map_[origin].last_engagement_award = clock_->Now();
harknessc3fb0452016-09-02 14:08:37337
338 // Add a new chunk of budget for the origin at the default expiration time.
339 base::Time expiration =
harknessccdc3f3a2017-01-16 12:42:17340 clock_->Now() + base::TimeDelta::FromDays(kBudgetDurationInDays);
341 budget_map_[origin].chunks.emplace_back(elapsed.InHours() * hourly_budget,
342 expiration);
harkness6f6b41432016-09-08 09:17:28343
344 // Any time we award engagement budget, which is done at most once an hour
345 // whenever any budget action is taken, record the budget.
346 double budget = GetBudget(origin);
347 UMA_HISTOGRAM_COUNTS_100("PushMessaging.BackgroundBudget", budget);
harknessc3fb0452016-09-02 14:08:37348}
349
350// Cleans up budget in the cache. Relies on the caller eventually writing the
351// cache back to the database.
harkness03be8a1d2016-09-09 16:01:12352bool BudgetDatabase::CleanupExpiredBudget(const url::Origin& origin) {
harknessc3fb0452016-09-02 14:08:37353 if (!IsCached(origin))
354 return false;
harkness5186e6d822016-08-09 16:54:14355
356 base::Time now = clock_->Now();
harkness03be8a1d2016-09-09 16:01:12357 BudgetChunks& chunks = budget_map_[origin].chunks;
harkness5186e6d822016-08-09 16:54:14358 auto cleanup_iter = chunks.begin();
359
360 // This relies on the list of chunks being in timestamp order.
harknessbf8b3852016-08-10 18:42:02361 while (cleanup_iter != chunks.end() && cleanup_iter->expiration <= now)
harkness5186e6d822016-08-09 16:54:14362 cleanup_iter = chunks.erase(cleanup_iter);
363
harknesse0e26092016-08-24 06:29:32364 // If the entire budget is empty now AND there have been no engagements
harknessccdc3f3a2017-01-16 12:42:17365 // in the last kBudgetDurationInDays days, remove this from the cache.
harknesse0e26092016-08-24 06:29:32366 if (chunks.empty() &&
harkness03be8a1d2016-09-09 16:01:12367 budget_map_[origin].last_engagement_award <
harknessccdc3f3a2017-01-16 12:42:17368 clock_->Now() - base::TimeDelta::FromDays(kBudgetDurationInDays)) {
harkness03be8a1d2016-09-09 16:01:12369 budget_map_.erase(origin);
harknessc3fb0452016-09-02 14:08:37370 return true;
371 }
372
373 // Although some things may have expired, there are some chunks still valid.
374 // Don't write to the DB now, write either when all chunks expire or when the
375 // origin spends some budget.
376 return false;
harkness5186e6d822016-08-09 16:54:14377}
peter91882282017-05-25 17:57:33378
379double BudgetDatabase::GetSiteEngagementScoreForOrigin(
380 const url::Origin& origin) const {
381 if (profile_->IsOffTheRecord())
382 return 0;
383
384 return SiteEngagementService::Get(profile_)->GetScore(origin.GetURL());
385}