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

Side by Side Diff: components/ntp_snippets/remote/request_throttler.cc

Issue 2804633003: Add base::FeatureParam<> struct (Closed)
Patch Set: Remove windows-incompatible constexpr Created 3 years, 8 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 "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 <set>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 27 matching lines...) Expand all
38 // Quota value to use if no quota should be applied (by default). 38 // Quota value to use if no quota should be applied (by default).
39 const int kUnlimitedQuota = INT_MAX; 39 const int kUnlimitedQuota = INT_MAX;
40 40
41 } // namespace 41 } // namespace
42 42
43 struct RequestThrottler::RequestTypeInfo { 43 struct RequestThrottler::RequestTypeInfo {
44 const char* name; 44 const char* name;
45 const char* count_pref; 45 const char* count_pref;
46 const char* interactive_count_pref; 46 const char* interactive_count_pref;
47 const char* day_pref; 47 const char* day_pref;
48 const int default_quota; 48 const base::FeatureParam<int> quota;
49 const int default_interactive_quota; 49 const base::FeatureParam<int> interactive_quota;
50 }; 50 };
51 51
52 // When adding a new type here, extend also the "RequestThrottlerTypes" 52 // When adding a new type here, extend also the "RequestThrottlerTypes"
53 // <histogram_suffixes> in histograms.xml with the |name| string. 53 // <histogram_suffixes> in histograms.xml with the |name| string.
54 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = { 54 const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = {
55 // The following three types share the same prefs. They differ in quota 55 // The following three types share the same prefs. They differ in quota
56 // values (and UMA histograms). 56 // values (and UMA histograms).
57 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER, 57 // RequestCounter::RequestType::
58 {"SuggestionFetcherRareNTPUser", prefs::kSnippetFetcherRequestCount, 58 // CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER,
59 {"SuggestionFetcherRareNTPUser",
60 prefs::kSnippetFetcherRequestCount,
59 prefs::kSnippetFetcherInteractiveRequestCount, 61 prefs::kSnippetFetcherInteractiveRequestCount,
60 prefs::kSnippetFetcherRequestsDay, 5, kUnlimitedQuota}, 62 prefs::kSnippetFetcherRequestsDay,
61 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER, 63 {&kArticleSuggestionsFeature, "quota_SuggestionFetcherRareNTPUser", 5},
62 {"SuggestionFetcherActiveNTPUser", prefs::kSnippetFetcherRequestCount, 64 {&kArticleSuggestionsFeature,
65 "interactive_quota_SuggestionFetcherRareNTPUser", kUnlimitedQuota}},
66 // RequestCounter::RequestType::
67 // CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER,
68 {"SuggestionFetcherActiveNTPUser",
69 prefs::kSnippetFetcherRequestCount,
63 prefs::kSnippetFetcherInteractiveRequestCount, 70 prefs::kSnippetFetcherInteractiveRequestCount,
64 prefs::kSnippetFetcherRequestsDay, 20, kUnlimitedQuota}, 71 prefs::kSnippetFetcherRequestsDay,
65 // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTION S_CONSUMER, 72 {&kArticleSuggestionsFeature, "quota_SuggestionFetcherActiveNTPUser", 20},
73 {&kArticleSuggestionsFeature,
74 "interactive_quota_SuggestionFetcherActiveNTPUser", kUnlimitedQuota}},
75 // RequestCounter::RequestType::
76 // CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER,
66 {"SuggestionFetcherActiveSuggestionsConsumer", 77 {"SuggestionFetcherActiveSuggestionsConsumer",
67 prefs::kSnippetFetcherRequestCount, 78 prefs::kSnippetFetcherRequestCount,
68 prefs::kSnippetFetcherInteractiveRequestCount, 79 prefs::kSnippetFetcherInteractiveRequestCount,
69 prefs::kSnippetFetcherRequestsDay, 20, kUnlimitedQuota}, 80 prefs::kSnippetFetcherRequestsDay,
81 {&kArticleSuggestionsFeature,
82 "quota_SuggestionFetcherActiveSuggestionsConsumer", 20},
83 {&kArticleSuggestionsFeature,
84 "interactive_quota_SuggestionFetcherActiveSuggestionsConsumer",
85 kUnlimitedQuota}},
70 // RequestCounter::RequestType::CONTENT_SUGGESTION_THUMBNAIL, 86 // RequestCounter::RequestType::CONTENT_SUGGESTION_THUMBNAIL,
71 {"SuggestionThumbnailFetcher", prefs::kSnippetThumbnailsRequestCount, 87 {"SuggestionThumbnailFetcher",
88 prefs::kSnippetThumbnailsRequestCount,
72 prefs::kSnippetThumbnailsInteractiveRequestCount, 89 prefs::kSnippetThumbnailsInteractiveRequestCount,
73 prefs::kSnippetThumbnailsRequestsDay, kUnlimitedQuota, kUnlimitedQuota}}; 90 prefs::kSnippetThumbnailsRequestsDay,
91 {&kArticleSuggestionsFeature, "quota_SuggestionThumbnailFetcher",
92 kUnlimitedQuota},
93 {&kArticleSuggestionsFeature,
94 "interactive_quota_SuggestionThumbnailFetcher", kUnlimitedQuota}}};
74 95
75 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type) 96 RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type)
76 : pref_service_(pref_service), 97 : pref_service_(pref_service),
77 type_info_(kRequestTypeInfo[static_cast<int>(type)]) { 98 type_info_(kRequestTypeInfo[static_cast<int>(type)]) {
78 DCHECK(pref_service); 99 DCHECK(pref_service);
79 100
80 std::string quota = variations::GetVariationParamValueByFeature( 101 quota_ = type_info_.quota.Get();
81 ntp_snippets::kArticleSuggestionsFeature, 102 interactive_quota_ = type_info_.interactive_quota.Get();
82 base::StringPrintf("quota_%s", GetRequestTypeName()));
83 if (!base::StringToInt(quota, &quota_)) {
84 LOG_IF(WARNING, !quota.empty())
85 << "Invalid variation parameter for quota for " << GetRequestTypeName();
86 quota_ = type_info_.default_quota;
87 }
88
89 std::string interactive_quota = variations::GetVariationParamValueByFeature(
90 ntp_snippets::kArticleSuggestionsFeature,
91 base::StringPrintf("interactive_quota_%s", GetRequestTypeName()));
92 if (!base::StringToInt(interactive_quota, &interactive_quota_)) {
93 LOG_IF(WARNING, !interactive_quota.empty())
94 << "Invalid variation parameter for interactive quota for "
95 << GetRequestTypeName();
96 interactive_quota_ = type_info_.default_interactive_quota;
97 }
98 103
99 // Since the histogram names are dynamic, we cannot use the standard macros 104 // Since the histogram names are dynamic, we cannot use the standard macros
100 // and we need to lookup the histograms, instead. 105 // and we need to lookup the histograms, instead.
101 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT); 106 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT);
102 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|). 107 // Corresponds to UMA_HISTOGRAM_ENUMERATION(name, sample, |status_count|).
103 histogram_request_status_ = base::LinearHistogram::FactoryGet( 108 histogram_request_status_ = base::LinearHistogram::FactoryGet(
104 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s", 109 base::StringPrintf("NewTabPage.RequestThrottler.RequestStatus_%s",
105 GetRequestTypeName()), 110 GetRequestTypeName()),
106 1, status_count, status_count + 1, 111 1, status_count, status_count + 1,
107 base::HistogramBase::kUmaTargetedHistogramFlag); 112 base::HistogramBase::kUmaTargetedHistogramFlag);
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 207
203 void RequestThrottler::SetDay(int day) { 208 void RequestThrottler::SetDay(int day) {
204 pref_service_->SetInteger(type_info_.day_pref, day); 209 pref_service_->SetInteger(type_info_.day_pref, day);
205 } 210 }
206 211
207 bool RequestThrottler::HasDay() const { 212 bool RequestThrottler::HasDay() const {
208 return pref_service_->HasPrefPath(type_info_.day_pref); 213 return pref_service_->HasPrefPath(type_info_.day_pref);
209 } 214 }
210 215
211 } // namespace ntp_snippets 216 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698