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

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: Integrated code review comments 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"
17 #include "chrome/browser/engagement/site_engagement_service.h" 18 #include "chrome/browser/engagement/site_engagement_service.h"
18 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
20 #include "components/pref_registry/pref_registry_syncable.h" 21 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "components/prefs/pref_service.h" 22 #include "components/prefs/pref_service.h"
22 #include "components/prefs/scoped_user_pref_update.h" 23 #include "components/prefs/scoped_user_pref_update.h"
23 24
24 namespace { 25 namespace {
25 26
26 constexpr char kSeparator = '#'; 27 constexpr char kSeparator = '#';
27 // We calculate the ratio of the different components of a budget with respect 28
29 // Calculate the ratio of the different components of a budget with respect
28 // to a maximum time period of 10 days = 864000.0 seconds. 30 // to a maximum time period of 10 days = 864000.0 seconds.
29 constexpr double kSecondsToAccumulate = 864000.0; 31 constexpr double kSecondsToAccumulate = 864000.0;
30 32
31 bool GetBudgetDataFromPrefs(Profile* profile, 33 bool GetBudgetDataFromPrefs(Profile* profile,
32 const GURL& origin, 34 const GURL& origin,
33 double* old_budget, 35 double* old_budget,
34 double* old_ses, 36 double* old_ses,
35 double* last_updated) { 37 double* last_updated) {
36 const base::DictionaryValue* map = 38 const base::DictionaryValue* map =
37 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap); 39 profile->GetPrefs()->GetDictionary(prefs::kBackgroundBudgetMap);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 } 90 }
89 91
90 BackgroundBudgetService::~BackgroundBudgetService() {} 92 BackgroundBudgetService::~BackgroundBudgetService() {}
91 93
92 // static 94 // static
93 void BackgroundBudgetService::RegisterProfilePrefs( 95 void BackgroundBudgetService::RegisterProfilePrefs(
94 user_prefs::PrefRegistrySyncable* registry) { 96 user_prefs::PrefRegistrySyncable* registry) {
95 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap); 97 registry->RegisterDictionaryPref(prefs::kBackgroundBudgetMap);
96 } 98 }
97 99
100 double BackgroundBudgetService::costs_[] = {
101 2.0, // SILENT_PUSH_COST
102 };
103
104 // static
105 double BackgroundBudgetService::GetBackgroundProcessingCost(CostType type) {
106 return costs_[type];
107 }
108
98 double BackgroundBudgetService::GetBudget(const GURL& origin) { 109 double BackgroundBudgetService::GetBudget(const GURL& origin) {
99 DCHECK_EQ(origin, origin.GetOrigin()); 110 DCHECK_EQ(origin, origin.GetOrigin());
100 111
101 // Get the current SES score, which we'll use to set a new budget. 112 // Get the current SES score, which we'll use to set a new budget.
102 SiteEngagementService* service = SiteEngagementService::Get(profile_); 113 SiteEngagementService* service = SiteEngagementService::Get(profile_);
103 double ses_score = service->GetScore(origin); 114 double ses_score = service->GetScore(origin);
104 115
105 // Get the last used budget data. This is a triple of last calculated time, 116 // 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. 117 // 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; 118 double old_budget = 0.0, old_ses = 0.0, last_updated_msec = 0.0;
(...skipping 21 matching lines...) Expand all
129 // total period. 140 // total period.
130 double ses_ratio = 141 double ses_ratio =
131 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate)); 142 std::min(1.0, (elapsed.InSeconds() / kSecondsToAccumulate));
132 double ses_component = (old_ses + ses_score) / 2 * ses_ratio; 143 double ses_component = (old_ses + ses_score) / 2 * ses_ratio;
133 144
134 // Budget recalculation consists of a budget carryover component, which 145 // Budget recalculation consists of a budget carryover component, which
135 // rewards sites that don't use all their budgets every day, and a ses 146 // 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. 147 // component, which gives extra budget to sites that have a high ses score.
137 double budget = budget_carryover + ses_component; 148 double budget = budget_carryover + ses_component;
138 DCHECK_GE(budget, 0.0); 149 DCHECK_GE(budget, 0.0);
150
151 // Generate histograms for the GetBudget calls which would return "no budget"
152 // or "low budget" if an API was available to app developers.
153 double cost = GetBackgroundProcessingCost(SILENT_PUSH_COST);
154 if (budget < cost)
155 UMA_HISTOGRAM_COUNTS("PushMessaging.SESForNoBudgetOrigin", ses_score);
156 else if (budget < 2.0 * cost)
157 UMA_HISTOGRAM_COUNTS("PushMessaging.SESForLowBudgetOrigin", ses_score);
Peter Beverloo 2016/05/17 13:51:01 To this and the other histograms: UMA_HISTOGRAM_C
Peter Beverloo 2016/05/17 14:03:23 Scratch this part, sorry! You're recording the *SE
harkness 2016/05/17 15:49:32 I updated to UMA_HISTOGRAM_COUNTS_100 for all thre
158
139 return budget; 159 return budget;
140 } 160 }
141 161
142 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) { 162 void BackgroundBudgetService::StoreBudget(const GURL& origin, double budget) {
143 DCHECK_EQ(origin, origin.GetOrigin()); 163 DCHECK_EQ(origin, origin.GetOrigin());
144 DCHECK_GE(budget, 0.0); 164 DCHECK_GE(budget, 0.0);
145 DCHECK_LE(budget, SiteEngagementScore::kMaxPoints); 165 DCHECK_LE(budget, SiteEngagementScore::kMaxPoints);
146 166
147 // Get the current SES score to write into the prefs with the new budget. 167 // Get the current SES score to write into the prefs with the new budget.
148 SiteEngagementService* service = SiteEngagementService::Get(profile_); 168 SiteEngagementService* service = SiteEngagementService::Get(profile_);
149 double ses_score = service->GetScore(origin); 169 double ses_score = service->GetScore(origin);
150 170
151 base::Time time = clock_->Now(); 171 base::Time time = clock_->Now();
152 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score); 172 SetBudgetDataInPrefs(profile_, origin, time.ToDoubleT(), budget, ses_score);
153 } 173 }
154 174
155 // Override the default clock with the specified clock. Only used for testing. 175 // Override the default clock with the specified clock. Only used for testing.
156 void BackgroundBudgetService::SetClockForTesting( 176 void BackgroundBudgetService::SetClockForTesting(
157 std::unique_ptr<base::Clock> clock) { 177 std::unique_ptr<base::Clock> clock) {
158 clock_ = std::move(clock); 178 clock_ = std::move(clock);
159 } 179 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698