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 "components/ntp_snippets/request_throttler.h" | 5 #include "components/ntp_snippets/request_throttler.h" |
6 | 6 |
| 7 #include <limits.h> |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
14 #include "components/ntp_snippets/ntp_snippets_constants.h" | 15 #include "components/ntp_snippets/ntp_snippets_constants.h" |
15 #include "components/ntp_snippets/pref_names.h" | 16 #include "components/ntp_snippets/pref_names.h" |
16 #include "components/prefs/pref_registry_simple.h" | 17 #include "components/prefs/pref_registry_simple.h" |
17 #include "components/prefs/pref_service.h" | 18 #include "components/prefs/pref_service.h" |
18 #include "components/variations/variations_associated_data.h" | 19 #include "components/variations/variations_associated_data.h" |
19 | 20 |
20 namespace ntp_snippets { | 21 namespace ntp_snippets { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA | 25 // Enumeration listing all possible outcomes for fetch attempts. Used for UMA |
25 // histogram, so do not change existing values. Insert new values at the end, | 26 // histogram, so do not change existing values. Insert new values at the end, |
26 // and update the histogram definition. | 27 // and update the histogram definition. |
27 enum class RequestStatus { | 28 enum class RequestStatus { |
28 FORCED, | 29 INTERACTIVE_QUOTA_GRANTED, |
29 QUOTA_GRANTED, | 30 BACKGROUND_QUOTA_GRANTED, |
30 QUOTA_EXCEEDED, | 31 BACKGROUND_QUOTA_EXCEEDED, |
| 32 INTERACTIVE_QUOTA_EXCEEDED, |
31 REQUEST_STATUS_COUNT | 33 REQUEST_STATUS_COUNT |
32 }; | 34 }; |
33 | 35 |
| 36 // Quota value to use if no quota should be applied (by default). |
| 37 const int kUnlimitedQuota = INT_MAX; |
| 38 |
34 } // namespace | 39 } // namespace |
35 | 40 |
36 struct RequestThrottler::RequestTypeInfo { | 41 struct RequestThrottler::RequestTypeInfo { |
37 const char* name; | 42 const char* name; |
38 const char* count_pref; | 43 const char* count_pref; |
| 44 const char* interactive_count_pref; |
39 const char* day_pref; | 45 const char* day_pref; |
40 const int default_quota; | 46 const int default_quota; |
| 47 const int default_interactive_quota; |
41 }; | 48 }; |
42 | 49 |
43 // When adding a new type here, extend also the "RequestCounterTypes" | 50 // When adding a new type here, extend also the "RequestCounterTypes" |
44 // <histogram_suffixes> in histograms.xml with the |name| string. | 51 // <histogram_suffixes> in histograms.xml with the |name| string. |
45 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { | 52 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { |
46 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, | 53 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, |
47 {"SuggestionFetcher", prefs::kSnippetFetcherQuotaCount, | 54 {"SuggestionFetcher", prefs::kSnippetFetcherRequestCount, |
48 prefs::kSnippetFetcherQuotaDay, 50}}; | 55 prefs::kSnippetFetcherInteractiveRequestCount, |
| 56 prefs::kSnippetFetcherRequestsDay, 50, kUnlimitedQuota}, |
| 57 {"SuggestionThumbnailFetcher", prefs::kSnippetThumbnailsRequestCount, |
| 58 prefs::kSnippetThumbnailsInteractiveRequestCount, |
| 59 prefs::kSnippetThumbnailsRequestsDay, kUnlimitedQuota, kUnlimitedQuota}}; |
49 | 60 |
50 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) | 61 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) |
51 : pref_service_(pref_service), | 62 : pref_service_(pref_service), |
52 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { | 63 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { |
53 DCHECK(pref_service); | 64 DCHECK(pref_service); |
54 | 65 |
55 std::string quota = variations::GetVariationParamValue( | 66 std::string quota = variations::GetVariationParamValue( |
56 ntp_snippets::kStudyName, | 67 ntp_snippets::kStudyName, |
57 base::StringPrintf("quota_%s", GetRequestTypeAsString())); | 68 base::StringPrintf("quota_%s", GetRequestTypeAsString())); |
58 if (!base::StringToInt(quota, "a_)) { | 69 if (!base::StringToInt(quota, "a_)) { |
59 LOG_IF(WARNING, !quota.empty()) | 70 LOG_IF(WARNING, !quota.empty()) |
60 << "Invalid variation parameter for quota for " | 71 << "Invalid variation parameter for quota for " |
61 << GetRequestTypeAsString(); | 72 << GetRequestTypeAsString(); |
62 quota_ = type_info_.default_quota; | 73 quota_ = type_info_.default_quota; |
63 } | 74 } |
64 | 75 |
| 76 std::string interactive_quota = variations::GetVariationParamValue( |
| 77 ntp_snippets::kStudyName, |
| 78 base::StringPrintf("interactive_quota_%s", GetRequestTypeAsString())); |
| 79 if (!base::StringToInt(interactive_quota, &interactive_quota_)) { |
| 80 LOG_IF(WARNING, !interactive_quota.empty()) |
| 81 << "Invalid variation parameter for interactive quota for " |
| 82 << GetRequestTypeAsString(); |
| 83 interactive_quota_ = type_info_.default_interactive_quota; |
| 84 } |
| 85 |
65 // Since the histogram names are dynamic, we cannot use the standard macros | 86 // Since the histogram names are dynamic, we cannot use the standard macros |
66 // and we need to lookup the histograms, instead. | 87 // and we need to lookup the histograms, instead. |
67 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); | 88 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); |
68 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). | 89 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). |
69 histogram_request_status_ = base::LinearHistogram::FactoryGet( | 90 histogram_request_status_ = base::LinearHistogram::FactoryGet( |
70 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", | 91 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", |
71 GetRequestTypeAsString()), | 92 GetRequestTypeAsString()), |
72 1, status_count, status_count + 1, | 93 1, status_count, status_count + 1, |
73 base::HistogramBase::kUmaTargetedHistogramFlag); | 94 base::HistogramBase::kUmaTargetedHistogramFlag); |
74 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). | 95 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). |
75 histogram_per_day_ = base::Histogram::FactoryGet( | 96 histogram_per_day_ = base::Histogram::FactoryGet( |
76 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s", | 97 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s", |
77 GetRequestTypeAsString()), | 98 GetRequestTypeAsString()), |
78 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); | 99 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
79 } | 100 } |
80 | 101 |
81 // static | 102 // static |
82 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 103 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
83 for (const RequestTypeInfo& info : kRequestTypeInfo) { | 104 for (const RequestTypeInfo& info : kRequestTypeInfo) { |
84 registry->RegisterIntegerPref(info.count_pref, 0); | 105 registry->RegisterIntegerPref(info.count_pref, 0); |
| 106 registry->RegisterIntegerPref(info.interactive_count_pref, 0); |
85 registry->RegisterIntegerPref(info.day_pref, 0); | 107 registry->RegisterIntegerPref(info.day_pref, 0); |
86 } | 108 } |
87 } | 109 } |
88 | 110 |
89 bool RequestThrottler::DemandQuotaForRequest(bool forced_request) { | 111 bool RequestThrottler::DemandQuotaForRequest(bool interactive_request) { |
90 ResetCounterIfDayChanged(); | 112 ResetCounterIfDayChanged(); |
91 | 113 |
92 if (forced_request) { | 114 int new_count = GetCount(interactive_request) + 1; |
93 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED)); | 115 SetCount(interactive_request, new_count); |
94 return true; | 116 bool available = (new_count <= GetQuota(interactive_request)); |
| 117 |
| 118 if (interactive_request) { |
| 119 histogram_request_status_->Add(static_cast<int>( |
| 120 available ? RequestStatus::INTERACTIVE_QUOTA_GRANTED |
| 121 : RequestStatus::INTERACTIVE_QUOTA_EXCEEDED)); |
| 122 } else { |
| 123 histogram_request_status_->Add( |
| 124 static_cast<int>(available ? RequestStatus::BACKGROUND_QUOTA_GRANTED |
| 125 : RequestStatus::BACKGROUND_QUOTA_EXCEEDED)); |
95 } | 126 } |
96 | |
97 int new_count = GetCount() + 1; | |
98 SetCount(new_count); | |
99 bool available = (new_count <= quota_); | |
100 | |
101 histogram_request_status_->Add( | |
102 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED | |
103 : RequestStatus::QUOTA_EXCEEDED)); | |
104 return available; | 127 return available; |
105 } | 128 } |
106 | 129 |
107 void RequestThrottler::ResetCounterIfDayChanged() { | 130 void RequestThrottler::ResetCounterIfDayChanged() { |
108 // The count of days since a fixed reference (the Unix epoch). | 131 // The count of days since a fixed reference (the Unix epoch). |
109 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); | 132 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); |
110 | 133 |
111 if (!HasDay()) { | 134 if (!HasDay()) { |
112 // The counter is used for the first time in this profile. | 135 // The counter is used for the first time in this profile. |
113 SetDay(now_day); | 136 SetDay(now_day); |
114 } else if (now_day != GetDay()) { | 137 } else if (now_day != GetDay()) { |
115 // Day has changed - report the number of requests from the previous day. | 138 // Day has changed - report the number of background requests from the |
116 histogram_per_day_->Add(GetCount()); | 139 // previous day. |
| 140 histogram_per_day_->Add(GetCount(/*interactive_request=*/false)); |
117 // Reset the counter. | 141 // Reset the counter. |
118 SetCount(0); | 142 SetCount(/*interactive_request=*/false, 0); |
| 143 SetCount(/*interactive_request=*/true, 0); |
119 SetDay(now_day); | 144 SetDay(now_day); |
120 } | 145 } |
121 } | 146 } |
122 | 147 |
123 const char* RequestThrottler::GetRequestTypeAsString() const { | 148 const char* RequestThrottler::GetRequestTypeAsString() const { |
124 return type_info_.name; | 149 return type_info_.name; |
125 } | 150 } |
126 | 151 |
127 int RequestThrottler::GetCount() const { | 152 int RequestThrottler::GetQuota(bool interactive_request) const { |
128 return pref_service_->GetInteger(type_info_.count_pref); | 153 return interactive_request ? interactive_quota_ : quota_; |
129 } | 154 } |
130 | 155 |
131 void RequestThrottler::SetCount(int count) { | 156 int RequestThrottler::GetCount(bool interactive_request) const { |
132 pref_service_->SetInteger(type_info_.count_pref, count); | 157 return pref_service_->GetInteger(interactive_request |
| 158 ? type_info_.interactive_count_pref |
| 159 : type_info_.count_pref); |
| 160 } |
| 161 |
| 162 void RequestThrottler::SetCount(bool interactive_request, int count) { |
| 163 pref_service_->SetInteger(interactive_request |
| 164 ? type_info_.interactive_count_pref |
| 165 : type_info_.count_pref, |
| 166 count); |
133 } | 167 } |
134 | 168 |
135 int RequestThrottler::GetDay() const { | 169 int RequestThrottler::GetDay() const { |
136 return pref_service_->GetInteger(type_info_.day_pref); | 170 return pref_service_->GetInteger(type_info_.day_pref); |
137 } | 171 } |
138 | 172 |
139 void RequestThrottler::SetDay(int day) { | 173 void RequestThrottler::SetDay(int day) { |
140 pref_service_->SetInteger(type_info_.day_pref, day); | 174 pref_service_->SetInteger(type_info_.day_pref, day); |
141 } | 175 } |
142 | 176 |
143 bool RequestThrottler::HasDay() const { | 177 bool RequestThrottler::HasDay() const { |
144 return pref_service_->HasPrefPath(type_info_.day_pref); | 178 return pref_service_->HasPrefPath(type_info_.day_pref); |
145 } | 179 } |
146 | 180 |
147 } // namespace ntp_snippets | 181 } // namespace ntp_snippets |
OLD | NEW |