Chromium Code Reviews| Index: components/ntp_snippets/ntp_snippets_fetcher.cc |
| diff --git a/components/ntp_snippets/ntp_snippets_fetcher.cc b/components/ntp_snippets/ntp_snippets_fetcher.cc |
| index 90c0815a07fc567c7c4715e068af69e5bfc0bd0a..4a50986c073e43a75b06fdda9009783eb0e534b2 100644 |
| --- a/components/ntp_snippets/ntp_snippets_fetcher.cc |
| +++ b/components/ntp_snippets/ntp_snippets_fetcher.cc |
| @@ -48,22 +48,27 @@ const char kSnippetsServer[] = |
| const char kSnippetsServerNonAuthorizedFormat[] = "%s?key=%s"; |
| const char kAuthorizationRequestHeaderFormat[] = "Bearer %s"; |
| -// Variation parameter for the variant of fetching to use. |
| -const char kVariantName[] = "fetching_variant"; |
| +// Variation parameter for personalizing fetching of snippets. |
| +const char kPersonalizationName[] = "fetching_personalization"; |
| +const char kHostRestrictionName[] = "fetching_host_restrict"; |
| -// Constants listing possible values of the "fetching_variant" parameter. |
| -const char kVariantRestrictedString[] = "restricted"; |
| -const char kVariantPersonalizedString[] = "personalized"; |
| -const char kVariantRestrictedPersonalizedString[] = "restricted_personalized"; |
| +// Constants for possible values of the "fetching_personalization" parameter. |
| +const char kPersonalizationPersonalString[] = "personal"; |
| +const char kPersonalizationNonPersonalString[] = "non_personal"; |
| +const char kPersonalizationBothString[] = "both"; // the default value |
| -const char kRequestParameterFormat[] = |
| +// Constants for possible values of the "fetching_host_restrict" parameter. |
| +const char kHostRestrictionOnString[] = "on"; // the default value |
| +const char kHostRestrictionOffString[] = "off"; |
| + |
| +const char kRequestFormat[] = |
| "{" |
| " \"response_detail_level\": \"STANDARD\"," |
| "%s" // If authenticated - an obfuscated Gaia ID will be inserted here. |
| " \"advanced_options\": {" |
| " \"local_scoring_params\": {" |
| " \"content_params\": {" |
| - " \"only_return_personalized_results\": false" |
| + " \"only_return_personalized_results\": %s" |
| "%s" // If authenticated - user segment (lang code) will be inserted here. |
| " }," |
| " \"content_restricts\": {" |
| @@ -94,6 +99,8 @@ const char kHostRestrictFormat[] = |
| " \"type\": \"HOST_RESTRICT\"," |
| " \"value\": \"%s\"" |
| " }"; |
| +const char kTrueString[] = "true"; |
| +const char kFalseString[] = "false"; |
| std::string FetchResultToString(NTPSnippetsFetcher::FetchResult result) { |
| switch (result) { |
| @@ -116,6 +123,17 @@ std::string FetchResultToString(NTPSnippetsFetcher::FetchResult result) { |
| return "Unknown error"; |
| } |
| +std::string BuildRequest(const std::string& obfuscated_gaia_id, |
| + bool only_return_personalized_results, |
| + const std::string& user_segment, |
| + const std::string& host_restricts, |
| + int count_to_fetch) { |
| + return base::StringPrintf( |
| + kRequestFormat, obfuscated_gaia_id.c_str(), |
| + only_return_personalized_results ? kTrueString : kFalseString, |
| + user_segment, host_restricts.c_str(), count_to_fetch); |
| +} |
| + |
| } // namespace |
| NTPSnippetsFetcher::NTPSnippetsFetcher( |
| @@ -135,16 +153,26 @@ NTPSnippetsFetcher::NTPSnippetsFetcher( |
| weak_ptr_factory_(this) { |
| // Parse the variation parameters and set the defaults if missing. |
| std::string variant = variations::GetVariationParamValue( |
| - ntp_snippets::kStudyName, kVariantName); |
| - if (variant == kVariantRestrictedString) { |
| - variant_ = Variant::kRestricted; |
| - } else if (variant == kVariantPersonalizedString) { |
| - variant_ = Variant::kPersonalized; |
| + ntp_snippets::kStudyName, kPersonalizationName); |
| + if (variant == kPersonalizationNonPersonalString) { |
| + personalization_ = Personalization::kNonPersonal; |
| + } else if (variant == kPersonalizationPersonalString) { |
| + personalization_ = Personalization::kPersonal; |
| + } else { |
| + personalization_ = Personalization::kBoth; |
| + LOG_IF(WARNING, !variant.empty() && variant != kPersonalizationBothString) |
| + << "Unknown value for fetching_variant: " << variant; |
|
Marc Treib
2016/05/12 10:06:15
"fetching_personalization" - just use kPersonaliza
jkrcal
2016/05/12 11:58:56
Done.
|
| + } |
| + |
| + std::string host_restriction = variations::GetVariationParamValue( |
| + ntp_snippets::kStudyName, kHostRestrictionName); |
| + if (host_restriction == kHostRestrictionOffString) { |
| + use_host_restriction_ = false; |
| } else { |
| - variant_ = Variant::kRestrictedPersonalized; |
| - LOG_IF(WARNING, |
| - !variant.empty() && variant != kVariantRestrictedPersonalizedString) |
| - << "Unknown fetching variant provided: " << variant; |
| + use_host_restriction_ = true; |
| + LOG_IF(WARNING, !host_restriction.empty() && |
| + host_restriction != kHostRestrictionOnString) |
| + << "Unknown value for fetching_host_restrict: " << host_restriction; |
|
Marc Treib
2016/05/12 10:06:15
Also here: kHostRestrictionName?
jkrcal
2016/05/12 11:58:56
Done.
|
| } |
| } |
| @@ -236,15 +264,14 @@ std::string NTPSnippetsFetcher::GetHostRestricts() const { |
| } |
| bool NTPSnippetsFetcher::UseHostRestriction() const { |
| - return (variant_ == Variant::kRestricted || |
| - variant_ == Variant::kRestrictedPersonalized) && |
| + return use_host_restriction_ && |
| !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| switches::kDontRestrict); |
| } |
| bool NTPSnippetsFetcher::UseAuthentication() const { |
| - return (variant_ == Variant::kPersonalized || |
| - variant_ == Variant::kRestrictedPersonalized); |
| + return (personalization_ == Personalization::kPersonal || |
| + personalization_ == Personalization::kBoth); |
| } |
| void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { |
| @@ -255,16 +282,17 @@ void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { |
| GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, |
| kSnippetsServer, key.c_str())); |
| - FetchSnippetsImpl( |
| - url, std::string(), |
| - base::StringPrintf(kRequestParameterFormat, "", "", |
| - GetHostRestricts().c_str(), count_to_fetch_)); |
| + FetchSnippetsImpl(url, std::string(), |
| + BuildRequest(/*obfuscated_gaia_id=*/std::string(), |
| + /*only_return_personalized_results=*/false, |
| + /*user_segment=*/std::string(), |
| + GetHostRestricts(), count_to_fetch_)); |
| } |
| void NTPSnippetsFetcher::FetchSnippetsAuthenticated( |
| const std::string& account_id, |
| const std::string& oauth_access_token) { |
| - std::string auth = base::StringPrintf(kGaiaIdFormat, account_id.c_str()); |
| + std::string gaia_id = base::StringPrintf(kGaiaIdFormat, account_id.c_str()); |
| std::string user_segment = |
| base::StringPrintf(kUserSegmentFormat, locale_.c_str()); |
| @@ -272,9 +300,8 @@ void NTPSnippetsFetcher::FetchSnippetsAuthenticated( |
| GURL(kSnippetsServer), |
| base::StringPrintf(kAuthorizationRequestHeaderFormat, |
| oauth_access_token.c_str()), |
| - base::StringPrintf(kRequestParameterFormat, auth.c_str(), |
| - user_segment.c_str(), GetHostRestricts().c_str(), |
| - count_to_fetch_)); |
| + BuildRequest(gaia_id, personalization_ == Personalization::kPersonal, |
| + user_segment, GetHostRestricts(), count_to_fetch_)); |
| } |
| void NTPSnippetsFetcher::StartTokenRequest() { |
| @@ -305,9 +332,6 @@ void NTPSnippetsFetcher::OnGetTokenFailure( |
| oauth_request_.reset(); |
| DLOG(ERROR) << "Unable to get token: " << error.ToString() |
| << " - fetching the snippets without authentication."; |
| - |
| - // Fallback to fetching non-authenticated tokens. |
| - FetchSnippetsNonAuthenticated(); |
|
Marc Treib
2016/05/12 10:06:15
If you remove this, you need to call FetchFinished
jkrcal
2016/05/12 11:58:56
Done.
Huh, thanks. I was too quick to send it out
|
| } |
| //////////////////////////////////////////////////////////////////////////////// |