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_(kRequestTypeInfo[static_cast<int>(type)]) { |
65 DCHECK(pref_service); | 77 DCHECK(pref_service); |
66 | 78 |
67 std::string quota = variations::GetVariationParamValue( | 79 std::string quota = variations::GetVariationParamValue( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); | 112 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
101 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). | 113 // Corresponds to UMA_HISTOGRAM_COUNTS_100(name, sample). |
102 histogram_per_day_interactive_ = base::Histogram::FactoryGet( | 114 histogram_per_day_interactive_ = base::Histogram::FactoryGet( |
103 base::StringPrintf("NewTabPage.RequestThrottler.PerDayInteractive_%s", | 115 base::StringPrintf("NewTabPage.RequestThrottler.PerDayInteractive_%s", |
104 GetRequestTypeName()), | 116 GetRequestTypeName()), |
105 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); | 117 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag); |
106 } | 118 } |
107 | 119 |
108 // static | 120 // static |
109 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { | 121 void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 122 // Collect all pref keys in a set to make sure we register each key exactly |
| 123 // once, even if they repeat. |
| 124 std::set<std::string> keys_to_register; |
110 for (const RequestTypeInfo& info : kRequestTypeInfo) { | 125 for (const RequestTypeInfo& info : kRequestTypeInfo) { |
111 registry->RegisterIntegerPref(info.count_pref, 0); | 126 keys_to_register.insert(info.day_pref); |
112 registry->RegisterIntegerPref(info.interactive_count_pref, 0); | 127 keys_to_register.insert(info.count_pref); |
113 registry->RegisterIntegerPref(info.day_pref, 0); | 128 keys_to_register.insert(info.interactive_count_pref); |
114 } | 129 } |
| 130 |
| 131 for (const std::string& key : keys_to_register) |
| 132 registry->RegisterIntegerPref(key, 0); |
115 } | 133 } |
116 | 134 |
117 bool RequestThrottler::DemandQuotaForRequest(bool interactive_request) { | 135 bool RequestThrottler::DemandQuotaForRequest(bool interactive_request) { |
118 ResetCounterIfDayChanged(); | 136 ResetCounterIfDayChanged(); |
119 | 137 |
120 int new_count = GetCount(interactive_request) + 1; | 138 int new_count = GetCount(interactive_request) + 1; |
121 SetCount(interactive_request, new_count); | 139 SetCount(interactive_request, new_count); |
122 bool available = (new_count <= GetQuota(interactive_request)); | 140 bool available = (new_count <= GetQuota(interactive_request)); |
123 | 141 |
124 if (interactive_request) { | 142 if (interactive_request) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 | 198 |
181 void RequestThrottler::SetDay(int day) { | 199 void RequestThrottler::SetDay(int day) { |
182 pref_service_->SetInteger(type_info_.day_pref, day); | 200 pref_service_->SetInteger(type_info_.day_pref, day); |
183 } | 201 } |
184 | 202 |
185 bool RequestThrottler::HasDay() const { | 203 bool RequestThrottler::HasDay() const { |
186 return pref_service_->HasPrefPath(type_info_.day_pref); | 204 return pref_service_->HasPrefPath(type_info_.day_pref); |
187 } | 205 } |
188 | 206 |
189 } // namespace ntp_snippets | 207 } // namespace ntp_snippets |
OLD | NEW |