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

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

Issue 2395123002: Connecting UserClassifier to NtpSnippetsFetcher (Closed)
Patch Set: A minor 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/ntp_snippets_fetcher.cc
diff --git a/components/ntp_snippets/remote/ntp_snippets_fetcher.cc b/components/ntp_snippets/remote/ntp_snippets_fetcher.cc
index b447572fe8b65433991268f54ec99c0a0593be8e..6d7123b5489fc57aaed9aae2e36dded7b943e1cd 100644
--- a/components/ntp_snippets/remote/ntp_snippets_fetcher.cc
+++ b/components/ntp_snippets/remote/ntp_snippets_fetcher.cc
@@ -24,6 +24,7 @@
#include "components/ntp_snippets/category_factory.h"
#include "components/ntp_snippets/ntp_snippets_constants.h"
#include "components/ntp_snippets/switches.h"
+#include "components/ntp_snippets/user_classifier.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/signin/core/browser/signin_manager_base.h"
@@ -173,6 +174,19 @@ std::string PosixLocaleFromBCP47Language(const std::string& language_code) {
return locale;
}
+std::string GetUserClassString(UserClassifier::UserClass user_class) {
+ switch (user_class) {
+ case UserClassifier::UserClass::RARE_NTP_USER:
+ return "RARE_NTP_USER";
+ case UserClassifier::UserClass::ACTIVE_NTP_USER:
+ return "ACTIVE_NTP_USER";
+ case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER:
+ return "ACTIVE_SUGGESTIONS_CONSUMER";
+ }
+ NOTREACHED();
+ return std::string();
+}
+
} // namespace
NTPSnippetsFetcher::FetchedCategory::FetchedCategory(Category c)
@@ -191,7 +205,8 @@ NTPSnippetsFetcher::NTPSnippetsFetcher(
PrefService* pref_service,
CategoryFactory* category_factory,
const ParseJSONCallback& parse_json_callback,
- const std::string& api_key)
+ const std::string& api_key,
+ const UserClassifier* user_classifier)
: OAuth2TokenService::Consumer("ntp_snippets"),
signin_manager_(signin_manager),
token_service_(token_service),
@@ -207,9 +222,19 @@ NTPSnippetsFetcher::NTPSnippetsFetcher(
api_key_(api_key),
interactive_request_(false),
tick_clock_(new base::DefaultTickClock()),
- request_throttler_(
+ user_classifier_(user_classifier),
+ request_throttler_rare_ntp_user_(
+ pref_service,
+ RequestThrottler::RequestType::
+ CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER),
+ request_throttler_active_ntp_user_(
+ pref_service,
+ RequestThrottler::RequestType::
+ CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER),
+ request_throttler_active_suggestions_consumer_(
pref_service,
- RequestThrottler::RequestType::CONTENT_SUGGESTION_FETCHER),
+ RequestThrottler::RequestType::
+ CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER),
oauth_token_retried_(false),
weak_ptr_factory_(this) {
// Parse the variation parameters and set the defaults if missing.
@@ -256,7 +281,7 @@ void NTPSnippetsFetcher::FetchSnippetsFromHosts(
const std::set<std::string>& excluded_ids,
int count,
bool interactive_request) {
- if (!request_throttler_.DemandQuotaForRequest(interactive_request)) {
+ if (!DemandQuotaForRequest(interactive_request)) {
FetchFinished(OptionalSnippets(),
interactive_request
? FetchResult::INTERACTIVE_QUOTA_ERROR
@@ -306,7 +331,8 @@ NTPSnippetsFetcher::RequestParams::RequestParams()
user_locale(),
host_restricts(),
count_to_fetch(),
- interactive_request() {}
+ interactive_request(),
+ user_class() {}
NTPSnippetsFetcher::RequestParams::~RequestParams() = default;
@@ -381,6 +407,7 @@ std::string NTPSnippetsFetcher::RequestParams::BuildRequest() {
break;
}
request->Set("excludedSuggestionIds", std::move(excluded));
+ request->SetString("user_activeness_class", user_class);
// TODO(sfiera): support authentication and personalization
// TODO(sfiera): support count_to_fetch
@@ -453,6 +480,7 @@ void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() {
params.count_to_fetch = count_to_fetch_;
params.interactive_request = interactive_request_;
params.user_locale = locale_;
+ params.user_class = GetUserClassString(user_classifier_->GetUserClass());
FetchSnippetsImpl(url, std::string(), params.BuildRequest());
}
@@ -470,6 +498,7 @@ void NTPSnippetsFetcher::FetchSnippetsAuthenticated(
params.excluded_ids = excluded_ids_;
params.count_to_fetch = count_to_fetch_;
params.interactive_request = interactive_request_;
+ params.user_class = GetUserClassString(user_classifier_->GetUserClass());
// TODO(jkrcal, treib): Add unit-tests for authenticated fetches.
FetchSnippetsImpl(fetch_url_,
base::StringPrintf(kAuthorizationRequestHeaderFormat,
@@ -670,4 +699,20 @@ void NTPSnippetsFetcher::FetchFinished(OptionalSnippets snippets,
snippets_available_callback_.Run(std::move(snippets));
}
+bool NTPSnippetsFetcher::DemandQuotaForRequest(bool interactive_request) {
+ switch (user_classifier_->GetUserClass()) {
+ case UserClassifier::UserClass::RARE_NTP_USER:
+ return request_throttler_rare_ntp_user_.DemandQuotaForRequest(
+ interactive_request);
+ case UserClassifier::UserClass::ACTIVE_NTP_USER:
+ return request_throttler_active_ntp_user_.DemandQuotaForRequest(
+ interactive_request);
+ case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER:
+ return request_throttler_active_suggestions_consumer_
+ .DemandQuotaForRequest(interactive_request);
+ }
+ NOTREACHED();
+ return false;
+}
+
} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698