Chromium Code Reviews| Index: chrome/browser/push_messaging/background_budget_service.cc |
| diff --git a/chrome/browser/push_messaging/background_budget_service.cc b/chrome/browser/push_messaging/background_budget_service.cc |
| index ef24a41a0ab4d659755f7f6ee30b635cff68b2f5..12fa3c8a5b78fa2d4da30ce95bdb4228bee72dbd 100644 |
| --- a/chrome/browser/push_messaging/background_budget_service.cc |
| +++ b/chrome/browser/push_messaging/background_budget_service.cc |
| @@ -4,7 +4,13 @@ |
| #include "chrome/browser/push_messaging/background_budget_service.h" |
| +#include <stdint.h> |
| + |
| #include "base/callback.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_split.h" |
| +#include "base/time/time.h" |
| +#include "chrome/browser/engagement/site_engagement_service.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/pref_names.h" |
| #include "components/pref_registry/pref_registry_syncable.h" |
| @@ -13,8 +19,10 @@ |
| namespace { |
| -std::string GetBudgetStringForOrigin(Profile* profile, const GURL& origin) { |
| - DCHECK_EQ(origin.GetOrigin(), origin); |
| +const char kSeparator = '#'; |
| +const double kSecondsToAccumulate = 864000.0; // 10 days of seconds. |
| + |
| +std::string GetBudgetDataFromPrefs(Profile* profile, const GURL& origin) { |
| const base::DictionaryValue* map = |
| profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); |
| @@ -23,6 +31,45 @@ std::string GetBudgetStringForOrigin(Profile* profile, const GURL& origin) { |
| return map_string; |
| } |
| +void SetBudgetDataInPrefs(Profile* profile, |
| + const GURL& origin, |
| + const std::string& data) { |
| + DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); |
| + base::DictionaryValue* map = update.Get(); |
| + |
| + map->SetStringWithoutPathExpansion(origin.spec(), data); |
| +} |
| + |
| +// The value stored in prefs is a concatenated string of last updated time, old |
| +// budget, and old ses. |
| +std::string MakePrefValueFromBudgetData(const double budget, const double ses) { |
| + base::Time t = base::Time::Now(); |
| + return base::DoubleToString(t.ToDoubleT()) + kSeparator + |
| + base::DoubleToString(budget) + kSeparator + base::DoubleToString(ses); |
|
Peter Beverloo
2016/04/14 18:02:09
Please check out base/strings/stringprintf.h. If a
harkness
2016/04/27 11:21:08
Done.
|
| +} |
| + |
| +bool GetBudgetDataFromPrefValue(const std::string& pref_value, |
|
Peter Beverloo
2016/04/14 18:02:09
Can we combine this with GetBudgetDataFromPrefs? I
harkness
2016/04/27 11:21:08
Done.
|
| + double* old_budget, |
| + double* old_ses, |
| + double* last_updated) { |
| + std::vector<std::string> parts = |
|
Peter Beverloo
2016/04/14 18:02:08
Please use base::SplitStringPiece() instead.
Stri
harkness
2016/04/27 11:21:08
As discussed in person, there's currently no Strin
|
| + base::SplitString(pref_value, std::string(1, kSeparator), |
| + base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| + if (parts.size() != 3) |
| + return false; |
| + |
| + if (!base::StringToDouble(parts[0], last_updated)) |
| + return false; |
| + |
| + if (!base::StringToDouble(parts[1], old_budget)) |
| + return false; |
| + |
| + if (!base::StringToDouble(parts[2], old_ses)) |
| + return false; |
| + |
| + return true; |
| +} |
| + |
| } // namespace |
| // static |
| @@ -32,17 +79,54 @@ void BackgroundBudgetService::RegisterProfilePrefs( |
| } |
| // static |
| -std::string BackgroundBudgetService::GetBudget(Profile* profile, |
| - const GURL& origin) { |
| - return GetBudgetStringForOrigin(profile, origin); |
| +void BackgroundBudgetService::GetBudget(Profile* profile, |
| + const GURL& origin, |
| + double& budget) { |
| + // Get the current SES score, which we'll use to set a new budget. |
| + SiteEngagementService* service = SiteEngagementService::Get(profile); |
| + double ses_score = service->GetScore(origin); |
| + |
| + // Get the last used budget data. This is a triple of last calculated time, |
| + // budget at that time, and Site Engagement Score (ses) at that time. |
| + const std::string map_value = GetBudgetDataFromPrefs(profile, origin); |
| + |
| + // If there is no stored data, just return data based on the SES. |
| + if (map_value.empty()) { |
| + budget = ses_score; |
| + return; |
| + } |
| + |
| + double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; |
| + if (!GetBudgetDataFromPrefValue(map_value, &old_budget, &old_ses, |
| + &last_updated_msec)) { |
| + budget = ses_score; |
| + return; |
| + } |
| + |
| + base::Time now = base::Time::Now(); |
| + base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec); |
| + |
| + // Budget recalculation consists of a budget carryover component, which |
| + // rewards sites that don't use all their budgets every day, and a ses |
| + // component, which gives extra budget to sites that have a high ses score. |
| + double carryover_ratio = std::max( |
| + 0.0, |
| + ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); |
| + double budget_carryover = old_budget * carryover_ratio; |
| + double ses_ratio = |
| + std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); |
| + double ses_component = (old_ses + ses_score) / 2 * ses_ratio; |
| + budget = budget_carryover + ses_component; |
|
Peter Beverloo
2016/04/14 18:02:09
So we have a linear decrease of budget over a peri
harkness
2016/04/27 11:21:08
The original spreadsheet assumed that everything w
|
| } |
| // static |
| -void BackgroundBudgetService::StoreBudget( |
| - Profile* profile, |
| - const GURL& origin, |
| - const std::string& notifications_shown) { |
| - DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); |
| - base::DictionaryValue* map = update.Get(); |
| - map->SetStringWithoutPathExpansion(origin.spec(), notifications_shown); |
| +void BackgroundBudgetService::StoreBudget(Profile* profile, |
| + const GURL& origin, |
| + const double budget) { |
| + // Get the current SES score to write into the prefs with the new budget. |
| + SiteEngagementService* service = SiteEngagementService::Get(profile); |
| + double ses_score = service->GetScore(origin); |
| + |
| + std::string budget_string = MakePrefValueFromBudgetData(budget, ses_score); |
| + SetBudgetDataInPrefs(profile, origin, budget_string); |
| } |