| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/budget_service/budget_database.h" | 5 #include "chrome/browser/budget_service/budget_database.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/time/clock.h" | 8 #include "base/time/clock.h" |
| 9 #include "base/time/default_clock.h" | 9 #include "base/time/default_clock.h" |
| 10 #include "chrome/browser/budget_service/budget.pb.h" | 10 #include "chrome/browser/budget_service/budget.pb.h" |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 const SpendBudgetCallback& callback, | 174 const SpendBudgetCallback& callback, |
| 175 bool success) { | 175 bool success) { |
| 176 if (!success) { | 176 if (!success) { |
| 177 callback.Run(blink::mojom::BudgetServiceErrorType::DATABASE_ERROR, | 177 callback.Run(blink::mojom::BudgetServiceErrorType::DATABASE_ERROR, |
| 178 false /* success */); | 178 false /* success */); |
| 179 return; | 179 return; |
| 180 } | 180 } |
| 181 | 181 |
| 182 // Get the current SES score, to generate UMA. | 182 // Get the current SES score, to generate UMA. |
| 183 SiteEngagementService* service = SiteEngagementService::Get(profile_); | 183 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 184 double score = service->GetScore(GURL(origin.Serialize())); | 184 double score = service->GetScore(origin.GetURL()); |
| 185 | 185 |
| 186 // Walk the list of budget chunks to see if the origin has enough budget. | 186 // Walk the list of budget chunks to see if the origin has enough budget. |
| 187 double total = 0; | 187 double total = 0; |
| 188 BudgetInfo& info = budget_map_[origin]; | 188 BudgetInfo& info = budget_map_[origin]; |
| 189 for (const BudgetChunk& chunk : info.chunks) | 189 for (const BudgetChunk& chunk : info.chunks) |
| 190 total += chunk.amount; | 190 total += chunk.amount; |
| 191 | 191 |
| 192 if (total < amount) { | 192 if (total < amount) { |
| 193 UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForNoBudgetOrigin", score); | 193 UMA_HISTOGRAM_COUNTS_100("PushMessaging.SESForNoBudgetOrigin", score); |
| 194 callback.Run(blink::mojom::BudgetServiceErrorType::NONE, | 194 callback.Run(blink::mojom::BudgetServiceErrorType::NONE, |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 | 298 |
| 299 if (needs_write) | 299 if (needs_write) |
| 300 WriteCachedValuesToDatabase(origin, callback); | 300 WriteCachedValuesToDatabase(origin, callback); |
| 301 else | 301 else |
| 302 callback.Run(success); | 302 callback.Run(success); |
| 303 } | 303 } |
| 304 | 304 |
| 305 void BudgetDatabase::AddEngagementBudget(const url::Origin& origin) { | 305 void BudgetDatabase::AddEngagementBudget(const url::Origin& origin) { |
| 306 // Get the current SES score, which we'll use to set a new budget. | 306 // Get the current SES score, which we'll use to set a new budget. |
| 307 SiteEngagementService* service = SiteEngagementService::Get(profile_); | 307 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 308 double score = service->GetScore(GURL(origin.Serialize())); | 308 double score = service->GetScore(origin.GetURL()); |
| 309 | 309 |
| 310 // By default we award the "full" award. Then that ratio is decreased if | 310 // By default we award the "full" award. Then that ratio is decreased if |
| 311 // there have been other awards recently. | 311 // there have been other awards recently. |
| 312 double ratio = 1.0; | 312 double ratio = 1.0; |
| 313 | 313 |
| 314 // Calculate how much budget should be awarded. If there is no entry in the | 314 // Calculate how much budget should be awarded. If there is no entry in the |
| 315 // cache then we award a full amount. | 315 // cache then we award a full amount. |
| 316 if (IsCached(origin)) { | 316 if (IsCached(origin)) { |
| 317 base::TimeDelta elapsed = | 317 base::TimeDelta elapsed = |
| 318 clock_->Now() - budget_map_[origin].last_engagement_award; | 318 clock_->Now() - budget_map_[origin].last_engagement_award; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 clock_->Now() - base::TimeDelta::FromHours(kBudgetDurationInHours)) { | 360 clock_->Now() - base::TimeDelta::FromHours(kBudgetDurationInHours)) { |
| 361 budget_map_.erase(origin); | 361 budget_map_.erase(origin); |
| 362 return true; | 362 return true; |
| 363 } | 363 } |
| 364 | 364 |
| 365 // Although some things may have expired, there are some chunks still valid. | 365 // Although some things may have expired, there are some chunks still valid. |
| 366 // Don't write to the DB now, write either when all chunks expire or when the | 366 // Don't write to the DB now, write either when all chunks expire or when the |
| 367 // origin spends some budget. | 367 // origin spends some budget. |
| 368 return false; | 368 return false; |
| 369 } | 369 } |
| OLD | NEW |