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

Unified Diff: components/ntp_snippets/remote/request_throttler.cc

Issue 2395123002: Connecting UserClassifier to NtpSnippetsFetcher (Closed)
Patch Set: A small fix Created 4 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/remote/request_throttler.cc
diff --git a/components/ntp_snippets/remote/request_throttler.cc b/components/ntp_snippets/remote/request_throttler.cc
index 4777f38156a0108c3866c439c373feed27d99df0..f9a2b5e73880c3600307e6754ca1f7a707c03dc2 100644
--- a/components/ntp_snippets/remote/request_throttler.cc
+++ b/components/ntp_snippets/remote/request_throttler.cc
@@ -5,6 +5,7 @@
#include "components/ntp_snippets/remote/request_throttler.h"
#include <climits>
+#include <set>
#include <vector>
#include "base/metrics/histogram.h"
@@ -50,19 +51,39 @@ struct RequestThrottler::RequestTypeInfo {
// When adding a new type here, extend also the "RequestThrottlerTypes"
// <histogram_suffixes> in histograms.xml with the |name| string.
const RequestThrottler::RequestTypeInfo RequestThrottler::kRequestTypeInfo[] = {
- // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER,
- {"SuggestionFetcher", prefs::kSnippetFetcherRequestCount,
+ // The following three types share the same prefs. They differ in quota
+ // values (and UMA histograms).
+ // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER,
+ {"SuggestionFetcherRareNTPUser", prefs::kSnippetFetcherRequestCount,
prefs::kSnippetFetcherInteractiveRequestCount,
- prefs::kSnippetFetcherRequestsDay, 50, kUnlimitedQuota},
- // RequestCounter::RequestType::CONTENT_SUGGESTION_THUMBNAIL,
+ prefs::kSnippetFetcherRequestsDay, 5, kUnlimitedQuota},
+ // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER,
+ {"SuggestionFetcherActiveNTPUser", prefs::kSnippetFetcherRequestCount,
+ prefs::kSnippetFetcherInteractiveRequestCount,
+ prefs::kSnippetFetcherRequestsDay, 20, kUnlimitedQuota},
+ // RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER,
+ {"SuggestionFetcherActiveSuggestionsConsumer",
+ prefs::kSnippetFetcherRequestCount,
+ prefs::kSnippetFetcherInteractiveRequestCount,
+ prefs::kSnippetFetcherRequestsDay, 20, kUnlimitedQuota},
+ // RequestCounter::RequestType::CONTENT_SUGGESTION_THUMBNAIL,
{"SuggestionThumbnailFetcher", prefs::kSnippetThumbnailsRequestCount,
prefs::kSnippetThumbnailsInteractiveRequestCount,
prefs::kSnippetThumbnailsRequestsDay, kUnlimitedQuota, kUnlimitedQuota}};
RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type)
: pref_service_(pref_service),
- type_info_(kRequestTypeInfo[static_cast<int>(type)]) {
+ type_info_(nullptr) {
DCHECK(pref_service);
+ ChangeRequestType(type);
+}
+
+void RequestThrottler::ChangeRequestType(RequestType new_type) {
+ // Do the changes only if the new type differs from the previous one.
+ if (type_info_ == &kRequestTypeInfo[static_cast<int>(new_type)])
+ return;
+
+ type_info_ = &kRequestTypeInfo[static_cast<int>(new_type)];
std::string quota = variations::GetVariationParamValue(
ntp_snippets::kStudyName,
@@ -71,7 +92,7 @@ RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type)
LOG_IF(WARNING, !quota.empty())
<< "Invalid variation parameter for quota for "
<< GetRequestTypeName();
- quota_ = type_info_.default_quota;
+ quota_ = type_info_->default_quota;
}
std::string interactive_quota = variations::GetVariationParamValue(
@@ -81,7 +102,7 @@ RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type)
LOG_IF(WARNING, !interactive_quota.empty())
<< "Invalid variation parameter for interactive quota for "
<< GetRequestTypeName();
- interactive_quota_ = type_info_.default_interactive_quota;
+ interactive_quota_ = type_info_->default_interactive_quota;
}
// Since the histogram names are dynamic, we cannot use the standard macros
@@ -107,10 +128,18 @@ RequestThrottler::RequestThrottler(PrefService* pref_service, RequestType type)
// static
void RequestThrottler::RegisterProfilePrefs(PrefRegistrySimple* registry) {
+ std::set<std::string> registered;
for (const RequestTypeInfo& info : kRequestTypeInfo) {
+ // Make sure we do not register the same prefs twice. Assuming that if any
+ // types share some prefs, they share all the prefs. Thus, checking, e.g.,
+ // day_pref is enough.
+ if (registered.count(info.day_pref) > 0)
+ continue;
+
+ registered.insert(info.day_pref);
+ registry->RegisterIntegerPref(info.day_pref, 0);
registry->RegisterIntegerPref(info.count_pref, 0);
registry->RegisterIntegerPref(info.interactive_count_pref, 0);
- registry->RegisterIntegerPref(info.day_pref, 0);
}
}
@@ -152,7 +181,7 @@ void RequestThrottler::ResetCounterIfDayChanged() {
}
const char* RequestThrottler::GetRequestTypeName() const {
- return type_info_.name;
+ return type_info_->name;
}
// TODO(jkrcal): turn RequestTypeInfo into a proper class, move those methods
@@ -163,27 +192,27 @@ int RequestThrottler::GetQuota(bool interactive_request) const {
int RequestThrottler::GetCount(bool interactive_request) const {
return pref_service_->GetInteger(interactive_request
- ? type_info_.interactive_count_pref
- : type_info_.count_pref);
+ ? type_info_->interactive_count_pref
+ : type_info_->count_pref);
}
void RequestThrottler::SetCount(bool interactive_request, int count) {
pref_service_->SetInteger(interactive_request
- ? type_info_.interactive_count_pref
- : type_info_.count_pref,
+ ? type_info_->interactive_count_pref
+ : type_info_->count_pref,
count);
}
int RequestThrottler::GetDay() const {
- return pref_service_->GetInteger(type_info_.day_pref);
+ return pref_service_->GetInteger(type_info_->day_pref);
}
void RequestThrottler::SetDay(int day) {
- pref_service_->SetInteger(type_info_.day_pref, day);
+ pref_service_->SetInteger(type_info_->day_pref, day);
}
bool RequestThrottler::HasDay() const {
- return pref_service_->HasPrefPath(type_info_.day_pref);
+ return pref_service_->HasPrefPath(type_info_->day_pref);
}
} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698