Chromium Code Reviews| 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/push_messaging/background_budget_service.h" | 5 #include "chrome/browser/push_messaging/background_budget_service.h" |
| 6 | 6 |
| 7 #include <stdint.h> | |
| 8 | |
| 7 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_split.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "chrome/browser/engagement/site_engagement_service.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 10 #include "components/pref_registry/pref_registry_syncable.h" | 16 #include "components/pref_registry/pref_registry_syncable.h" |
| 11 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
| 12 #include "components/prefs/scoped_user_pref_update.h" | 18 #include "components/prefs/scoped_user_pref_update.h" |
| 13 | 19 |
| 14 namespace { | 20 namespace { |
| 15 | 21 |
| 16 std::string GetBudgetStringForOrigin(Profile* profile, const GURL& origin) { | 22 const char kSeparator = '#'; |
| 17 DCHECK_EQ(origin.GetOrigin(), origin); | 23 const double kSecondsToAccumulate = 864000.0; // 10 days of seconds. |
| 24 | |
| 25 std::string GetBudgetDataFromPrefs(Profile* profile, const GURL& origin) { | |
| 18 const base::DictionaryValue* map = | 26 const base::DictionaryValue* map = |
| 19 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); | 27 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); |
| 20 | 28 |
| 21 std::string map_string; | 29 std::string map_string; |
| 22 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); | 30 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); |
| 23 return map_string; | 31 return map_string; |
| 24 } | 32 } |
| 25 | 33 |
| 34 void SetBudgetDataInPrefs(Profile* profile, | |
| 35 const GURL& origin, | |
| 36 const std::string& data) { | |
| 37 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); | |
| 38 base::DictionaryValue* map = update.Get(); | |
| 39 | |
| 40 map->SetStringWithoutPathExpansion(origin.spec(), data); | |
| 41 } | |
| 42 | |
| 43 // The value stored in prefs is a concatenated string of last updated time, old | |
| 44 // budget, and old ses. | |
| 45 std::string MakePrefValueFromBudgetData(const double budget, const double ses) { | |
| 46 base::Time t = base::Time::Now(); | |
| 47 return base::DoubleToString(t.ToDoubleT()) + kSeparator + | |
| 48 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.
| |
| 49 } | |
| 50 | |
| 51 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.
| |
| 52 double* old_budget, | |
| 53 double* old_ses, | |
| 54 double* last_updated) { | |
| 55 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
| |
| 56 base::SplitString(pref_value, std::string(1, kSeparator), | |
| 57 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 58 if (parts.size() != 3) | |
| 59 return false; | |
| 60 | |
| 61 if (!base::StringToDouble(parts[0], last_updated)) | |
| 62 return false; | |
| 63 | |
| 64 if (!base::StringToDouble(parts[1], old_budget)) | |
| 65 return false; | |
| 66 | |
| 67 if (!base::StringToDouble(parts[2], old_ses)) | |
| 68 return false; | |
| 69 | |
| 70 return true; | |
| 71 } | |
| 72 | |
| 26 } // namespace | 73 } // namespace |
| 27 | 74 |
| 28 // static | 75 // static |
| 29 void BackgroundBudgetService::RegisterProfilePrefs( | 76 void BackgroundBudgetService::RegisterProfilePrefs( |
| 30 user_prefs::PrefRegistrySyncable* registry) { | 77 user_prefs::PrefRegistrySyncable* registry) { |
| 31 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | 78 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); |
| 32 } | 79 } |
| 33 | 80 |
| 34 // static | 81 // static |
| 35 std::string BackgroundBudgetService::GetBudget(Profile* profile, | 82 void BackgroundBudgetService::GetBudget(Profile* profile, |
| 36 const GURL& origin) { | 83 const GURL& origin, |
| 37 return GetBudgetStringForOrigin(profile, origin); | 84 double& budget) { |
| 85 // Get the current SES score, which we'll use to set a new budget. | |
| 86 SiteEngagementService* service = SiteEngagementService::Get(profile); | |
| 87 double ses_score = service->GetScore(origin); | |
| 88 | |
| 89 // Get the last used budget data. This is a triple of last calculated time, | |
| 90 // budget at that time, and Site Engagement Score (ses) at that time. | |
| 91 const std::string map_value = GetBudgetDataFromPrefs(profile, origin); | |
| 92 | |
| 93 // If there is no stored data, just return data based on the SES. | |
| 94 if (map_value.empty()) { | |
| 95 budget = ses_score; | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; | |
| 100 if (!GetBudgetDataFromPrefValue(map_value, &old_budget, &old_ses, | |
| 101 &last_updated_msec)) { | |
| 102 budget = ses_score; | |
| 103 return; | |
| 104 } | |
| 105 | |
| 106 base::Time now = base::Time::Now(); | |
| 107 base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec); | |
| 108 | |
| 109 // Budget recalculation consists of a budget carryover component, which | |
| 110 // rewards sites that don't use all their budgets every day, and a ses | |
| 111 // component, which gives extra budget to sites that have a high ses score. | |
| 112 double carryover_ratio = std::max( | |
| 113 0.0, | |
| 114 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); | |
| 115 double budget_carryover = old_budget * carryover_ratio; | |
| 116 double ses_ratio = | |
| 117 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); | |
| 118 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; | |
| 119 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
| |
| 38 } | 120 } |
| 39 | 121 |
| 40 // static | 122 // static |
| 41 void BackgroundBudgetService::StoreBudget( | 123 void BackgroundBudgetService::StoreBudget(Profile* profile, |
| 42 Profile* profile, | 124 const GURL& origin, |
| 43 const GURL& origin, | 125 const double budget) { |
| 44 const std::string& notifications_shown) { | 126 // Get the current SES score to write into the prefs with the new budget. |
| 45 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); | 127 SiteEngagementService* service = SiteEngagementService::Get(profile); |
| 46 base::DictionaryValue* map = update.Get(); | 128 double ses_score = service->GetScore(origin); |
| 47 map->SetStringWithoutPathExpansion(origin.spec(), notifications_shown); | 129 |
| 130 std::string budget_string = MakePrefValueFromBudgetData(budget, ses_score); | |
| 131 SetBudgetDataInPrefs(profile, origin, budget_string); | |
| 48 } | 132 } |
| OLD | NEW |