| 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/memory/ptr_util.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/time/clock.h" |
| 15 #include "base/time/default_clock.h" |
| 16 #include "base/time/time.h" |
| 17 #include "chrome/browser/engagement/site_engagement_service.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 10 #include "components/pref_registry/pref_registry_syncable.h" | 20 #include "components/pref_registry/pref_registry_syncable.h" |
| 11 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
| 12 #include "components/prefs/scoped_user_pref_update.h" | 22 #include "components/prefs/scoped_user_pref_update.h" |
| 13 | 23 |
| 14 namespace { | 24 namespace { |
| 15 | 25 |
| 16 std::string GetBudgetStringForOrigin(Profile* profile, const GURL& origin) { | 26 constexpr char kSeparator = '#'; |
| 17 DCHECK_EQ(origin.GetOrigin(), origin); | 27 // We calculate the ratio of the different components of a budget with respect |
| 28 // to a maximum time period of 10 days = 864000.0 seconds. |
| 29 constexpr double kSecondsToAccumulate = 864000.0; |
| 30 |
| 31 bool GetBudgetDataFromPrefs(Profile* profile, |
| 32 const GURL& origin, |
| 33 double* old_budget, |
| 34 double* old_ses, |
| 35 double* last_updated) { |
| 18 const base::DictionaryValue* map = | 36 const base::DictionaryValue* map = |
| 19 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); | 37 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); |
| 20 | 38 |
| 21 std::string map_string; | 39 std::string map_string; |
| 22 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); | 40 map->GetStringWithoutPathExpansion(origin.spec(), &map_string); |
| 23 return map_string; | 41 |
| 42 // There is no data for the preference, return false and let the caller |
| 43 // deal with that. |
| 44 if (map_string.empty()) |
| 45 return false; |
| 46 |
| 47 std::vector<base::StringPiece> parts = |
| 48 base::SplitStringPiece(map_string, base::StringPiece(&kSeparator, 1), |
| 49 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 50 if ((parts.size() != 3) || |
| 51 (!base::StringToDouble(parts[0].as_string(), last_updated)) || |
| 52 (!base::StringToDouble(parts[1].as_string(), old_budget)) || |
| 53 (!base::StringToDouble(parts[2].as_string(), old_ses))) { |
| 54 // Somehow the data stored in the preferences has become corrupted, log an |
| 55 // error and remove the invalid data. |
| 56 LOG(ERROR) << "Preferences data for background budget service is " |
| 57 << "invalid for origin " << origin.possibly_invalid_spec(); |
| 58 DictionaryPrefUpdate update(profile->GetPrefs(), |
| 59 prefs::kBackgroundBudgetMap); |
| 60 base::DictionaryValue* update_map = update.Get(); |
| 61 update_map->RemoveWithoutPathExpansion(origin.spec(), nullptr); |
| 62 return false; |
| 63 } |
| 64 |
| 65 return true; |
| 66 } |
| 67 |
| 68 // The value stored in prefs is a concatenated string of last updated time, old |
| 69 // budget, and old ses. |
| 70 void SetBudgetDataInPrefs(Profile* profile, |
| 71 const GURL& origin, |
| 72 double last_updated, |
| 73 double budget, |
| 74 double ses) { |
| 75 std::string s = base::StringPrintf("%f%c%f%c%f", last_updated, kSeparator, |
| 76 budget, kSeparator, ses); |
| 77 DictionaryPrefUpdate update(profile->GetPrefs(), prefs::kBackgroundBudgetMap); |
| 78 base::DictionaryValue* map = update.Get(); |
| 79 |
| 80 map->SetStringWithoutPathExpansion(origin.spec(), s); |
| 24 } | 81 } |
| 25 | 82 |
| 26 } // namespace | 83 } // namespace |
| 27 | 84 |
| 28 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) | 85 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) |
| 29 : profile_(profile) { | 86 : clock_(base::WrapUnique(new base::DefaultClock)), profile_(profile) { |
| 30 DCHECK(profile); | 87 DCHECK(profile); |
| 31 } | 88 } |
| 32 | 89 |
| 90 BackgroundBudgetService::~BackgroundBudgetService() {} |
| 91 |
| 33 // static | 92 // static |
| 34 void BackgroundBudgetService::RegisterProfilePrefs( | 93 void BackgroundBudgetService::RegisterProfilePrefs( |
| 35 user_prefs::PrefRegistrySyncable* registry) { | 94 user_prefs::PrefRegistrySyncable* registry) { |
| 36 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | 95 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); |
| 37 } | 96 } |
| 38 | 97 |
| 39 std::string BackgroundBudgetService::GetBudget(const GURL& origin) { | 98 double BackgroundBudgetService::GetBudget(const GURL& origin) { |
| 40 return GetBudgetStringForOrigin(profile_, origin); | 99 DCHECK_EQ(origin, origin.GetOrigin()); |
| 100 |
| 101 // Get the current SES score, which we'll use to set a new budget. |
| 102 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 103 double ses_score = service->GetScore(origin); |
| 104 |
| 105 // Get the last used budget data. This is a triple of last calculated time, |
| 106 // budget at that time, and Site Engagement Score (ses) at that time. |
| 107 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; |
| 108 if (!GetBudgetDataFromPrefs(profile_, origin, &old_budget, &old_ses, |
| 109 &last_updated_msec)) { |
| 110 // If there is no stored data or the data can't be parsed, just return the |
| 111 // SES. |
| 112 return ses_score; |
| 113 } |
| 114 |
| 115 base::Time now = clock_->Now(); |
| 116 base::TimeDelta elapsed = now - base::Time::FromDoubleT(last_updated_msec); |
| 117 |
| 118 // For each time period that elapses, calculate the carryover ratio as the |
| 119 // ratio of time remaining in our max period to the total period. |
| 120 // The carryover component is then the old budget multiplied by the ratio. |
| 121 double carryover_ratio = std::max( |
| 122 0.0, |
| 123 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); |
| 124 double budget_carryover = old_budget * carryover_ratio; |
| 125 |
| 126 // The ses component is an average of the last ses score used for budget |
| 127 // calculation and the current ses score. |
| 128 // The ses average is them multiplied by the ratio of time elapsed to the |
| 129 // total period. |
| 130 double ses_ratio = |
| 131 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); |
| 132 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; |
| 133 |
| 134 // Budget recalculation consists of a budget carryover component, which |
| 135 // rewards sites that don't use all their budgets every day, and a ses |
| 136 // component, which gives extra budget to sites that have a high ses score. |
| 137 double budget = budget_carryover + ses_component; |
| 138 DCHECK_GE(budget, 0.0); |
| 139 return budget; |
| 41 } | 140 } |
| 42 | 141 |
| 43 void BackgroundBudgetService::StoreBudget( | 142 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) { |
| 44 const GURL& origin, | 143 DCHECK_EQ(origin, origin.GetOrigin()); |
| 45 const std::string& notifications_shown) { | 144 DCHECK_GE(budget, 0.0); |
| 46 DictionaryPrefUpdate update(profile_->GetPrefs(), | 145 DCHECK_LE(budget, SiteEngagementScore::kMaxPoints); |
| 47 prefs::kBackgroundBudgetMap); | 146 |
| 48 base::DictionaryValue* map = update.Get(); | 147 // Get the current SES score to write into the prefs with the new budget. |
| 49 map->SetStringWithoutPathExpansion(origin.spec(), notifications_shown); | 148 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 149 double ses_score = service->GetScore(origin); |
| 150 |
| 151 base::Time time = clock_->Now(); |
| 152 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); |
| 50 } | 153 } |
| 154 |
| 155 // Override the default clock with the specified clock. Only used for testing. |
| 156 void BackgroundBudgetService::SetClockForTesting( |
| 157 std::unique_ptr<base::Clock> clock) { |
| 158 clock_ = std::move(clock); |
| 159 } |
| OLD | NEW |