Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1603)

Side by Side Diff: chrome/browser/push_messaging/background_budget_service.cc

Issue 1977423002: Added UMA stats to track the budget for any service worker which receives a (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Post-rebase include fix Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/metrics/histogram_macros.h"
11 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
13 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
14 #include "base/time/clock.h" 15 #include "base/time/clock.h"
15 #include "base/time/default_clock.h" 16 #include "base/time/default_clock.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "chrome/browser/engagement/site_engagement_score.h"
17 #include "chrome/browser/engagement/site_engagement_service.h" 19 #include "chrome/browser/engagement/site_engagement_service.h"
18 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
20 #include "components/pref_registry/pref_registry_syncable.h" 22 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "components/prefs/pref_service.h" 23 #include "components/prefs/pref_service.h"
22 #include "components/prefs/scoped_user_pref_update.h" 24 #include "components/prefs/scoped_user_pref_update.h"
23 25
24 namespace { 26 namespace {
25 27
26 constexpr char kSeparator = '#'; 28 constexpr char kSeparator = '#';
27 // We calculate the ratio of the different components of a budget with respect 29
30 // Calculate the ratio of the different components of a budget with respect
28 // to a maximum time period of 10 days = 864000.0 seconds. 31 // to a maximum time period of 10 days = 864000.0 seconds.
29 constexpr double kSecondsToAccumulate = 864000.0; 32 constexpr double kSecondsToAccumulate = 864000.0;
30 33
31 bool GetBudgetDataFromPrefs(Profile* profile, 34 bool GetBudgetDataFromPrefs(Profile* profile,
32 const GURL& origin, 35 const GURL& origin,
33 double* old_budget, 36 double* old_budget,
34 double* old_ses, 37 double* old_ses,
35 double* last_updated) { 38 double* last_updated) {
36 const base::DictionaryValue* map = 39 const base::DictionaryValue* map =
37 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); 40 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 } 91 }
89 92
90 BackgroundBudgetService::~BackgroundBudgetService() {} 93 BackgroundBudgetService::~BackgroundBudgetService() {}
91 94
92 // static 95 // static
93 void BackgroundBudgetService::RegisterProfilePrefs( 96 void BackgroundBudgetService::RegisterProfilePrefs(
94 user_prefs::PrefRegistrySyncable* registry) { 97 user_prefs::PrefRegistrySyncable* registry) {
95 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); 98 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap);
96 } 99 }
97 100
101 // static
102 double BackgroundBudgetService::GetCost(CostType type) {
103 switch (type) {
104 case CostType::SILENT_PUSH:
105 return 2.0;
106 // No default case.
107 }
108 NOTREACHED();
109 return SiteEngagementScore::kMaxPoints + 1.0;
110 }
111
98 double BackgroundBudgetService::GetBudget(const GURL& origin) { 112 double BackgroundBudgetService::GetBudget(const GURL& origin) {
99 DCHECK_EQ(origin, origin.GetOrigin()); 113 DCHECK_EQ(origin, origin.GetOrigin());
100 114
101 // Get the current SES score, which we'll use to set a new budget. 115 // Get the current SES score, which we'll use to set a new budget.
102 SiteEngagementService* service = SiteEngagementService::Get(profile_); 116 SiteEngagementService* service = SiteEngagementService::Get(profile_);
103 double ses_score = service->GetScore(origin); 117 double ses_score = service->GetScore(origin);
104 118
105 // Get the last used budget data. This is a triple of last calculated time, 119 // 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. 120 // 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; 121 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0;
(...skipping 21 matching lines...) Expand all
129 // total period. 143 // total period.
130 double ses_ratio = 144 double ses_ratio =
131 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); 145 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate));
132 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; 146 double ses_component = (old_ses + ses_score) / 2 * ses_ratio;
133 147
134 // Budget recalculation consists of a budget carryover component, which 148 // Budget recalculation consists of a budget carryover component, which
135 // rewards sites that don't use all their budgets every day, and a ses 149 // 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. 150 // component, which gives extra budget to sites that have a high ses score.
137 double budget = budget_carryover + ses_component; 151 double budget = budget_carryover + ses_component;
138 DCHECK_GE(budget, 0.0); 152 DCHECK_GE(budget, 0.0);
153
139 return budget; 154 return budget;
140 } 155 }
141 156
142 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) { 157 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) {
143 DCHECK_EQ(origin, origin.GetOrigin()); 158 DCHECK_EQ(origin, origin.GetOrigin());
144 DCHECK_GE(budget, 0.0); 159 DCHECK_GE(budget, 0.0);
145 DCHECK_LE(budget, SiteEngagementService::GetMaxPoints()); 160 DCHECK_LE(budget, SiteEngagementService::GetMaxPoints());
146 161
147 // Get the current SES score to write into the prefs with the new budget. 162 // Get the current SES score to write into the prefs with the new budget.
148 SiteEngagementService* service = SiteEngagementService::Get(profile_); 163 SiteEngagementService* service = SiteEngagementService::Get(profile_);
149 double ses_score = service->GetScore(origin); 164 double ses_score = service->GetScore(origin);
150 165
151 base::Time time = clock_->Now(); 166 base::Time time = clock_->Now();
152 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); 167 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score);
153 } 168 }
154 169
155 // Override the default clock with the specified clock. Only used for testing. 170 // Override the default clock with the specified clock. Only used for testing.
156 void BackgroundBudgetService::SetClockForTesting( 171 void BackgroundBudgetService::SetClockForTesting(
157 std::unique_ptr<base::Clock> clock) { 172 std::unique_ptr<base::Clock> clock) {
158 clock_ = std::move(clock); 173 clock_ = std::move(clock);
159 } 174 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698