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> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 return false; | 65 return false; |
| 66 | 66 |
| 67 if (!base::StringToDouble(parts[2], old_ses)) | 67 if (!base::StringToDouble(parts[2], old_ses)) |
| 68 return false; | 68 return false; |
| 69 | 69 |
| 70 return true; | 70 return true; |
| 71 } | 71 } |
| 72 | 72 |
| 73 } // namespace | 73 } // namespace |
| 74 | 74 |
| 75 BackgroundBudgetService::BackgroundBudgetService(Profile* profile) | |
| 76 : profile_(profile) { | |
| 77 DCHECK(profile); | |
| 78 } | |
| 79 | |
| 75 // static | 80 // static |
| 76 void BackgroundBudgetService::RegisterProfilePrefs( | 81 void BackgroundBudgetService::RegisterProfilePrefs( |
| 77 user_prefs::PrefRegistrySyncable* registry) { | 82 user_prefs::PrefRegistrySyncable* registry) { |
| 78 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); | 83 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); |
| 79 } | 84 } |
| 80 | 85 |
| 81 // static | 86 void BackgroundBudgetService::GetBudget(const GURL& origin, double& budget) { |
| 82 void BackgroundBudgetService::GetBudget(Profile* profile, | |
| 83 const GURL& origin, | |
| 84 double& budget) { | |
| 85 // Get the current SES score, which we'll use to set a new budget. | 87 // Get the current SES score, which we'll use to set a new budget. |
| 86 SiteEngagementService* service = SiteEngagementService::Get(profile); | 88 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 87 double ses_score = service->GetScore(origin); | 89 double ses_score = service->GetScore(origin); |
| 88 | 90 |
| 89 // Get the last used budget data. This is a triple of last calculated time, | 91 // 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. | 92 // budget at that time, and Site Engagement Score (ses) at that time. |
| 91 const std::string map_value = GetBudgetDataFromPrefs(profile, origin); | 93 const std::string map_value = GetBudgetDataFromPrefs(profile_, origin); |
| 92 | 94 |
| 93 // If there is no stored data, just return data based on the SES. | 95 // If there is no stored data, just return data based on the SES. |
| 94 if (map_value.empty()) { | 96 if (map_value.empty()) { |
| 95 budget = ses_score; | 97 budget = ses_score; |
| 96 return; | 98 return; |
| 97 } | 99 } |
| 98 | 100 |
| 99 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; | 101 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0; |
| 100 if (!GetBudgetDataFromPrefValue(map_value, &old_budget, &old_ses, | 102 if (!GetBudgetDataFromPrefValue(map_value, &old_budget, &old_ses, |
| 101 &last_updated_msec)) { | 103 &last_updated_msec)) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 112 double carryover_ratio = std::max( | 114 double carryover_ratio = std::max( |
| 113 0.0, | 115 0.0, |
| 114 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); | 116 ((kSecondsToAccumulate - elapsed.InSeconds()) / kSecondsToAccumulate)); |
| 115 double budget_carryover = old_budget * carryover_ratio; | 117 double budget_carryover = old_budget * carryover_ratio; |
| 116 double ses_ratio = | 118 double ses_ratio = |
| 117 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); | 119 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); |
| 118 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; | 120 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; |
| 119 budget = budget_carryover + ses_component; | 121 budget = budget_carryover + ses_component; |
| 120 } | 122 } |
| 121 | 123 |
| 122 // static | 124 // static |
|
Michael van Ouwerkerk
2016/04/14 14:36:31
nit: no longer
harkness
2016/04/14 17:44:19
Done.
| |
| 123 void BackgroundBudgetService::StoreBudget(Profile* profile, | 125 void BackgroundBudgetService::StoreBudget(const GURL& origin, |
| 124 const GURL& origin, | |
| 125 const double budget) { | 126 const double budget) { |
| 126 // Get the current SES score to write into the prefs with the new budget. | 127 // Get the current SES score to write into the prefs with the new budget. |
| 127 SiteEngagementService* service = SiteEngagementService::Get(profile); | 128 SiteEngagementService* service = SiteEngagementService::Get(profile_); |
| 128 double ses_score = service->GetScore(origin); | 129 double ses_score = service->GetScore(origin); |
| 129 | 130 |
| 130 std::string budget_string = MakePrefValueFromBudgetData(budget, ses_score); | 131 std::string budget_string = MakePrefValueFromBudgetData(budget, ses_score); |
| 131 SetBudgetDataInPrefs(profile, origin, budget_string); | 132 SetBudgetDataInPrefs(profile_, origin, budget_string); |
| 132 } | 133 } |
| OLD | NEW |