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/remote/request_throttler.h" | 5 #include "components/ntp_snippets/remote/request_throttler.h" |
6 | 6 |
7 #include <climits> | 7 #include <climits> |
| 8 #include <set> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.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/time.h" | 15 #include "base/time/time.h" |
15 #include "components/ntp_snippets/ntp_snippets_constants.h" | 16 #include "components/ntp_snippets/ntp_snippets_constants.h" |
16 #include "components/ntp_snippets/pref_names.h" | 17 #include "components/ntp_snippets/pref_names.h" |
17 #include "components/prefs/pref_registry_simple.h" | 18 #include "components/prefs/pref_registry_simple.h" |
(...skipping 25 matching lines...) Expand all Loading... |
43 const char* count_pref; | 44 const char* count_pref; |
44 const char* interactive_count_pref; | 45 const char* interactive_count_pref; |
45 const char* day_pref; | 46 const char* day_pref; |
46 const int default_quota; | 47 const int default_quota; |
47 const int default_interactive_quota; | 48 const int default_interactive_quota; |
48 }; | 49 }; |
49 | 50 |
50 // When adding a new type here, extend also the "RequestThrottlerTypes" | 51 // When adding a new type here, extend also the "RequestThrottlerTypes" |
51 // <histogram_suffixes> in histograms.xml with the |name| string. | 52 // <histogram_suffixes> in histograms.xml with the |name| string. |
52 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { | 53 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { |
53 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER, | 54 // The following three types share the same prefs. They differ in quota |
54 {"SuggestionFetcher", prefs::kSnippetFetcherRequestCount, | 55 // values (and UMA histograms). |
| 56 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER, |
| 57 {"SuggestionFetcherRareNTPUser", prefs::kSnippetFetcherRequestCount, |
55 prefs::kSnippetFetcherInteractiveRequestCount, | 58 prefs::kSnippetFetcherInteractiveRequestCount, |
56 prefs::kSnippetFetcherRequestsDay, 50, kUnlimitedQuota}, | 59 prefs::kSnippetFetcherRequestsDay, 5, kUnlimitedQuota}, |
57 // RequestCounter::RequestType::CONTENT_SUGGESTION_THUMBNAIL, | 60 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER, |
| 61 {"SuggestionFetcherActiveNTPUser", prefs::kSnippetFetcherRequestCount, |
| 62 prefs::kSnippetFetcherInteractiveRequestCount, |
| 63 prefs::kSnippetFetcherRequestsDay, 20, kUnlimitedQuota}, |
| 64 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTION
S_CONSUMER, |
| 65 {"SuggestionFetcherActiveSuggestionsConsumer", |
| 66 prefs::kSnippetFetcherRequestCount, |
| 67 prefs::kSnippetFetcherInteractiveRequestCount, |
| 68 prefs::kSnippetFetcherRequestsDay, 20, kUnlimitedQuota}, |
| 69 // RequestCounter::RequestType::CONTENT_SUGGESTION_THUMBNAIL, |
58 {"SuggestionThumbnailFetcher", prefs::kSnippetThumbnailsRequestCount, | 70 {"SuggestionThumbnailFetcher", prefs::kSnippetThumbnailsRequestCount, |
59 prefs::kSnippetThumbnailsInteractiveRequestCount, | 71 prefs::kSnippetThumbnailsInteractiveRequestCount, |
60 prefs::kSnippetThumbnailsRequestsDay, kUnlimitedQuota, kUnlimitedQuota}}; | 72 prefs::kSnippetThumbnailsRequestsDay, kUnlimitedQuota, kUnlimitedQuota}}; |
61 | 73 |
62 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) | 74 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) |
63 : pref_service_(pref_service), | 75 : pref_service_(pref_service), |
64 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { | 76 type_info_(nullptr) { |
65 DCHECK(pref_service); | 77 DCHECK(pref_service); |
| 78 ChangeRequestType(type); |
| 79 } |
| 80 |
| 81 void RequestThrottler::ChangeRequestType(RequestType new_type) { |
| 82 // Do the changes only if the new type differs from the previous one. |
| 83 if (type_info_ == &kRequestTypeInfo[static_cast<int>(new_type)]) |
| 84 return; |
| 85 |
| 86 type_info_ = &kRequestTypeInfo[static_cast<int>(new_type)]; |
66 | 87 |
67 std::string quota = variations::GetVariationParamValue( | 88 std::string quota = variations::GetVariationParamValue( |
68 ntp_snippets::kStudyName, | 89 ntp_snippets::kStudyName, |
69 base::StringPrintf("quota_%s", GetRequestTypeName())); | 90 base::StringPrintf("quota_%s", GetRequestTypeName())); |
70 if (!base::StringToInt(quota, "a_)) { | 91 if (!base::StringToInt(quota, "a_)) { |
71 LOG_IF(WARNING, !quota.empty()) | 92 LOG_IF(WARNING, !quota.empty()) |
72 << "Invalid variation parameter for quota for " | 93 << "Invalid variation parameter for quota for " |
73 << GetRequestTypeName(); | 94 << GetRequestTypeName(); |
74 quota_ = type_info_.default_quota; | 95 quota_ = type_info_->default_quota; |
75 } | 96 } |
76 | 97 |
77 std::string interactive_quota = variations::GetVariationParamValue( | 98 std::string interactive_quota = variations::GetVariationParamValue( |
78 ntp_snippets::kStudyName, | 99 ntp_snippets::kStudyName, |
79 base::StringPrintf("interactive_quota_%s", GetRequestTypeName())); | 100 base::StringPrintf("interactive_quota_%s", GetRequestTypeName())); |
80 if (!base::StringToInt(interactive_quota, &interactive_quota_)) { | 101 if (!base::StringToInt(interactive_quota, &interactive_quota_)) { |
81 LOG_IF(WARNING, !interactive_quota.empty()) | 102 LOG_IF(WARNING, !interactive_quota.empty()) |
82 << "Invalid variation parameter for interactive quota for " | 103 << "Invalid variation parameter for interactive quota for " |
83 << GetRequestTypeName(); | 104 << GetRequestTypeName(); |
84 interactive_quota_ = type_info_.default_interactive_quota; | 105 interactive_quota_ = type_info_->default_interactive_quota; |
85 } | 106 } |
86 | 107 |
87 // Since the histogram names are dynamic, we cannot use the standard macros | 108 // Since the histogram names are dynamic, we cannot use the standard macros |
88 // and we need to lookup the histograms, instead. | 109 // and we need to lookup the histograms, instead. |
89 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); | 110 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); |
90 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). | 111 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). |
91 histogram_request_status_ = base::LinearHistogram::FactoryGet( | 112 histogram_request_status_ = base::LinearHistogram::FactoryGet( |
92 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", | 113 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", |
93 GetRequestTypeName()), | 114 GetRequestTypeName()), |
94 1, status_count, status_count + 1, | 115 1, status_count, status_count + 1, |
95 base::HistogramBase::kUmaTargetedHistogramFlag); | 116 base::HistogramBase::kUmaTargetedHistogramFlag); |
96 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). | 117 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). |
97 histogram_per_day_background_ = base::Histogram::FactoryGet( | 118 histogram_per_day_background_ = base::Histogram::FactoryGet( |
98 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s", | 119 base::StringPrintf("NewTabPage.RequestThrottler.PerDay_%s", |
99 GetRequestTypeName()), | 120 GetRequestTypeName()), |
100 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); | 121 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
101 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). | 122 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). |
102 histogram_per_day_interactive_ = base::Histogram::FactoryGet( | 123 histogram_per_day_interactive_ = base::Histogram::FactoryGet( |
103 base::StringPrintf("NewTabPage.RequestThrottler.PerDayInteractive_%s", | 124 base::StringPrintf("NewTabPage.RequestThrottler.PerDayInteractive_%s", |
104 GetRequestTypeName()), | 125 GetRequestTypeName()), |
105 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); | 126 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
106 } | 127 } |
107 | 128 |
108 // static | 129 // static |
109 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 130 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 131 std::set<std::string> registered; |
110 for (const RequestTypeInfo& info : kRequestTypeInfo) { | 132 for (const RequestTypeInfo& info : kRequestTypeInfo) { |
| 133 // Make sure we do not register the same prefs twice. Assuming that if any |
| 134 // types share some prefs, they share all the prefs. Thus, checking, e.g., |
| 135 // day_pref is enough. |
| 136 if (registered.count(info.day_pref) > 0) |
| 137 continue; |
| 138 |
| 139 registered.insert(info.day_pref); |
| 140 registry->RegisterIntegerPref(info.day_pref, 0); |
111 registry->RegisterIntegerPref(info.count_pref, 0); | 141 registry->RegisterIntegerPref(info.count_pref, 0); |
112 registry->RegisterIntegerPref(info.interactive_count_pref, 0); | 142 registry->RegisterIntegerPref(info.interactive_count_pref, 0); |
113 registry->RegisterIntegerPref(info.day_pref, 0); | |
114 } | 143 } |
115 } | 144 } |
116 | 145 |
117 bool RequestThrottler::DemandQuotaForRequest(bool interactive_request) { | 146 bool RequestThrottler::DemandQuotaForRequest(bool interactive_request) { |
118 ResetCounterIfDayChanged(); | 147 ResetCounterIfDayChanged(); |
119 | 148 |
120 int new_count = GetCount(interactive_request) + 1; | 149 int new_count = GetCount(interactive_request) + 1; |
121 SetCount(interactive_request, new_count); | 150 SetCount(interactive_request, new_count); |
122 bool available = (new_count <= GetQuota(interactive_request)); | 151 bool available = (new_count <= GetQuota(interactive_request)); |
123 | 152 |
(...skipping 21 matching lines...) Expand all Loading... |
145 histogram_per_day_background_->Add(GetCount(/*interactive_request=*/false)); | 174 histogram_per_day_background_->Add(GetCount(/*interactive_request=*/false)); |
146 histogram_per_day_interactive_->Add(GetCount(/*interactive_request=*/true)); | 175 histogram_per_day_interactive_->Add(GetCount(/*interactive_request=*/true)); |
147 // Reset the counters. | 176 // Reset the counters. |
148 SetCount(/*interactive_request=*/false, 0); | 177 SetCount(/*interactive_request=*/false, 0); |
149 SetCount(/*interactive_request=*/true, 0); | 178 SetCount(/*interactive_request=*/true, 0); |
150 SetDay(now_day); | 179 SetDay(now_day); |
151 } | 180 } |
152 } | 181 } |
153 | 182 |
154 const char* RequestThrottler::GetRequestTypeName() const { | 183 const char* RequestThrottler::GetRequestTypeName() const { |
155 return type_info_.name; | 184 return type_info_->name; |
156 } | 185 } |
157 | 186 |
158 // TODO(jkrcal): turn RequestTypeInfo into a proper class, move those methods | 187 // TODO(jkrcal): turn RequestTypeInfo into a proper class, move those methods |
159 // onto the class and hide the members. | 188 // onto the class and hide the members. |
160 int RequestThrottler::GetQuota(bool interactive_request) const { | 189 int RequestThrottler::GetQuota(bool interactive_request) const { |
161 return interactive_request ? interactive_quota_ : quota_; | 190 return interactive_request ? interactive_quota_ : quota_; |
162 } | 191 } |
163 | 192 |
164 int RequestThrottler::GetCount(bool interactive_request) const { | 193 int RequestThrottler::GetCount(bool interactive_request) const { |
165 return pref_service_->GetInteger(interactive_request | 194 return pref_service_->GetInteger(interactive_request |
166 ? type_info_.interactive_count_pref | 195 ? type_info_->interactive_count_pref |
167 : type_info_.count_pref); | 196 : type_info_->count_pref); |
168 } | 197 } |
169 | 198 |
170 void RequestThrottler::SetCount(bool interactive_request, int count) { | 199 void RequestThrottler::SetCount(bool interactive_request, int count) { |
171 pref_service_->SetInteger(interactive_request | 200 pref_service_->SetInteger(interactive_request |
172 ? type_info_.interactive_count_pref | 201 ? type_info_->interactive_count_pref |
173 : type_info_.count_pref, | 202 : type_info_->count_pref, |
174 count); | 203 count); |
175 } | 204 } |
176 | 205 |
177 int RequestThrottler::GetDay() const { | 206 int RequestThrottler::GetDay() const { |
178 return pref_service_->GetInteger(type_info_.day_pref); | 207 return pref_service_->GetInteger(type_info_->day_pref); |
179 } | 208 } |
180 | 209 |
181 void RequestThrottler::SetDay(int day) { | 210 void RequestThrottler::SetDay(int day) { |
182 pref_service_->SetInteger(type_info_.day_pref, day); | 211 pref_service_->SetInteger(type_info_->day_pref, day); |
183 } | 212 } |
184 | 213 |
185 bool RequestThrottler::HasDay() const { | 214 bool RequestThrottler::HasDay() const { |
186 return pref_service_->HasPrefPath(type_info_.day_pref); | 215 return pref_service_->HasPrefPath(type_info_->day_pref); |
187 } | 216 } |
188 | 217 |
189 } // namespace ntp_snippets | 218 } // namespace ntp_snippets |
OLD | NEW |