Chromium Code Reviews| 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/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 | 42 |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 const char kApiScope[] = "https://www.googleapis.com/auth/webhistory"; | 45 const char kApiScope[] = "https://www.googleapis.com/auth/webhistory"; |
| 46 const char kSnippetsServer[] = | 46 const char kSnippetsServer[] = |
| 47 "https://chromereader-pa.googleapis.com/v1/fetch"; | 47 "https://chromereader-pa.googleapis.com/v1/fetch"; |
| 48 const char kSnippetsServerNonAuthorizedFormat[] = "%s?key=%s"; | 48 const char kSnippetsServerNonAuthorizedFormat[] = "%s?key=%s"; |
| 49 const char kAuthorizationRequestHeaderFormat[] = "Bearer %s"; | 49 const char kAuthorizationRequestHeaderFormat[] = "Bearer %s"; |
| 50 | 50 |
| 51 // Variation parameter for the variant of fetching to use. | 51 // Variation parameter for the variant of fetching to use. |
| 52 const char kVariantName[] = "fetching_variant"; | 52 const char kVariantName[] = "fetching_variant"; |
|
Marc Treib
2016/05/12 08:59:16
Also here: s/variant/personalization, or whatever
jkrcal
2016/05/12 09:57:29
Done.
| |
| 53 const char kHostRestrictionName[] = "fetching_host_restrict"; | |
| 53 | 54 |
| 54 // Constants listing possible values of the "fetching_variant" parameter. | 55 // Constants listing possible values of the "fetching_variant" parameter. |
| 55 const char kVariantRestrictedString[] = "restricted"; | |
| 56 const char kVariantPersonalizedString[] = "personalized"; | 56 const char kVariantPersonalizedString[] = "personalized"; |
| 57 const char kVariantRestrictedPersonalizedString[] = "restricted_personalized"; | 57 const char kVariantNonPersonalizedString[] = "non-personalized"; |
| 58 const char kVariantAllString[] = "all"; | |
| 59 | |
| 60 // Constants listing possible values of the "fetching_host_restrict" parameter. | |
| 61 const char kHostRestrictionOnString[] = "on"; | |
| 62 const char kHostRestrictionOffString[] = "off"; | |
| 58 | 63 |
| 59 const char kRequestParameterFormat[] = | 64 const char kRequestParameterFormat[] = |
| 60 "{" | 65 "{" |
| 61 " \"response_detail_level\": \"STANDARD\"," | 66 " \"response_detail_level\": \"STANDARD\"," |
| 62 "%s" // If authenticated - an obfuscated Gaia ID will be inserted here. | 67 "%s" // If authenticated - an obfuscated Gaia ID will be inserted here. |
| 63 " \"advanced_options\": {" | 68 " \"advanced_options\": {" |
| 64 " \"local_scoring_params\": {" | 69 " \"local_scoring_params\": {" |
| 65 " \"content_params\": {" | 70 " \"content_params\": {" |
| 66 " \"only_return_personalized_results\": false" | 71 " \"only_return_personalized_results\": %s" |
| 67 "%s" // If authenticated - user segment (lang code) will be inserted here. | 72 "%s" // If authenticated - user segment (lang code) will be inserted here. |
| 68 " }," | 73 " }," |
| 69 " \"content_restricts\": {" | 74 " \"content_restricts\": {" |
| 70 " \"type\": \"METADATA\"," | 75 " \"type\": \"METADATA\"," |
| 71 " \"value\": \"TITLE\"" | 76 " \"value\": \"TITLE\"" |
| 72 " }," | 77 " }," |
| 73 " \"content_restricts\": {" | 78 " \"content_restricts\": {" |
| 74 " \"type\": \"METADATA\"," | 79 " \"type\": \"METADATA\"," |
| 75 " \"value\": \"SNIPPET\"" | 80 " \"value\": \"SNIPPET\"" |
| 76 " }," | 81 " }," |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 87 " }" | 92 " }" |
| 88 "}"; | 93 "}"; |
| 89 | 94 |
| 90 const char kGaiaIdFormat[] = " \"obfuscated_gaia_id\": \"%s\","; | 95 const char kGaiaIdFormat[] = " \"obfuscated_gaia_id\": \"%s\","; |
| 91 const char kUserSegmentFormat[] = " ,\"user_segment\": \"%s\""; | 96 const char kUserSegmentFormat[] = " ,\"user_segment\": \"%s\""; |
| 92 const char kHostRestrictFormat[] = | 97 const char kHostRestrictFormat[] = |
| 93 " ,\"content_selectors\": {" | 98 " ,\"content_selectors\": {" |
| 94 " \"type\": \"HOST_RESTRICT\"," | 99 " \"type\": \"HOST_RESTRICT\"," |
| 95 " \"value\": \"%s\"" | 100 " \"value\": \"%s\"" |
| 96 " }"; | 101 " }"; |
| 102 const char kTrueString[] = "true"; | |
| 103 const char kFalseString[] = "false"; | |
| 97 | 104 |
| 98 std::string FetchResultToString(NTPSnippetsFetcher::FetchResult result) { | 105 std::string FetchResultToString(NTPSnippetsFetcher::FetchResult result) { |
| 99 switch (result) { | 106 switch (result) { |
| 100 case NTPSnippetsFetcher::FetchResult::SUCCESS: | 107 case NTPSnippetsFetcher::FetchResult::SUCCESS: |
| 101 return "OK"; | 108 return "OK"; |
| 102 case NTPSnippetsFetcher::FetchResult::EMPTY_HOSTS: | 109 case NTPSnippetsFetcher::FetchResult::EMPTY_HOSTS: |
| 103 return "Cannot fetch for empty hosts list."; | 110 return "Cannot fetch for empty hosts list."; |
| 104 case NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR: | 111 case NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR: |
| 105 return "URLRequestStatus error"; | 112 return "URLRequestStatus error"; |
| 106 case NTPSnippetsFetcher::FetchResult::HTTP_ERROR: | 113 case NTPSnippetsFetcher::FetchResult::HTTP_ERROR: |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 129 token_service_(token_service), | 136 token_service_(token_service), |
| 130 waiting_for_refresh_token_(false), | 137 waiting_for_refresh_token_(false), |
| 131 url_request_context_getter_(url_request_context_getter), | 138 url_request_context_getter_(url_request_context_getter), |
| 132 parse_json_callback_(parse_json_callback), | 139 parse_json_callback_(parse_json_callback), |
| 133 is_stable_channel_(is_stable_channel), | 140 is_stable_channel_(is_stable_channel), |
| 134 tick_clock_(new base::DefaultTickClock()), | 141 tick_clock_(new base::DefaultTickClock()), |
| 135 weak_ptr_factory_(this) { | 142 weak_ptr_factory_(this) { |
| 136 // Parse the variation parameters and set the defaults if missing. | 143 // Parse the variation parameters and set the defaults if missing. |
| 137 std::string variant = variations::GetVariationParamValue( | 144 std::string variant = variations::GetVariationParamValue( |
| 138 ntp_snippets::kStudyName, kVariantName); | 145 ntp_snippets::kStudyName, kVariantName); |
| 139 if (variant == kVariantRestrictedString) { | 146 if (variant == kVariantNonPersonalizedString) { |
| 140 variant_ = Variant::kRestricted; | 147 variant_ = Variant::kNonPersonalized; |
| 141 } else if (variant == kVariantPersonalizedString) { | 148 } else if (variant == kVariantPersonalizedString) { |
| 142 variant_ = Variant::kPersonalized; | 149 variant_ = Variant::kPersonalized; |
| 143 } else { | 150 } else { |
| 144 variant_ = Variant::kRestrictedPersonalized; | 151 variant_ = Variant::kAll; |
| 145 LOG_IF(WARNING, | 152 LOG_IF(WARNING, !variant.empty() && variant != kVariantAllString) |
| 146 !variant.empty() && variant != kVariantRestrictedPersonalizedString) | 153 << "Unknown value for fetching_variant: " << variant; |
| 147 << "Unknown fetching variant provided: " << variant; | 154 } |
| 155 | |
| 156 std::string host_restriction = variations::GetVariationParamValue( | |
| 157 ntp_snippets::kStudyName, kHostRestrictionName); | |
| 158 if (host_restriction == kHostRestrictionOffString) { | |
| 159 use_host_restriction_ = false; | |
| 160 } else { | |
| 161 use_host_restriction_ = true; | |
| 162 LOG_IF(WARNING, !host_restriction.empty() && | |
| 163 host_restriction != kHostRestrictionOnString) | |
| 164 << "Unknown value for fetching_host_restrict: " << host_restriction; | |
| 148 } | 165 } |
| 149 } | 166 } |
| 150 | 167 |
| 151 NTPSnippetsFetcher::~NTPSnippetsFetcher() { | 168 NTPSnippetsFetcher::~NTPSnippetsFetcher() { |
| 152 if (waiting_for_refresh_token_) | 169 if (waiting_for_refresh_token_) |
| 153 token_service_->RemoveObserver(this); | 170 token_service_->RemoveObserver(this); |
| 154 } | 171 } |
| 155 | 172 |
| 156 void NTPSnippetsFetcher::SetCallback( | 173 void NTPSnippetsFetcher::SetCallback( |
| 157 const SnippetsAvailableCallback& callback) { | 174 const SnippetsAvailableCallback& callback) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 std::string NTPSnippetsFetcher::GetHostRestricts() const { | 246 std::string NTPSnippetsFetcher::GetHostRestricts() const { |
| 230 std::string host_restricts; | 247 std::string host_restricts; |
| 231 if (UseHostRestriction()) { | 248 if (UseHostRestriction()) { |
| 232 for (const std::string& host : hosts_) | 249 for (const std::string& host : hosts_) |
| 233 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); | 250 host_restricts += base::StringPrintf(kHostRestrictFormat, host.c_str()); |
| 234 } | 251 } |
| 235 return host_restricts; | 252 return host_restricts; |
| 236 } | 253 } |
| 237 | 254 |
| 238 bool NTPSnippetsFetcher::UseHostRestriction() const { | 255 bool NTPSnippetsFetcher::UseHostRestriction() const { |
| 239 return (variant_ == Variant::kRestricted || | 256 return use_host_restriction_ && |
| 240 variant_ == Variant::kRestrictedPersonalized) && | |
| 241 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 257 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 242 switches::kDontRestrict); | 258 switches::kDontRestrict); |
| 243 } | 259 } |
| 244 | 260 |
| 245 bool NTPSnippetsFetcher::UseAuthentication() const { | 261 bool NTPSnippetsFetcher::UseAuthentication() const { |
| 246 return (variant_ == Variant::kPersonalized || | 262 return (variant_ == Variant::kPersonalized || variant_ == Variant::kAll); |
| 247 variant_ == Variant::kRestrictedPersonalized); | |
| 248 } | 263 } |
| 249 | 264 |
| 250 void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { | 265 void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { |
| 251 // When not providing OAuth token, we need to pass the Google API key. | 266 // When not providing OAuth token, we need to pass the Google API key. |
| 252 const std::string& key = is_stable_channel_ | 267 const std::string& key = is_stable_channel_ |
| 253 ? google_apis::GetAPIKey() | 268 ? google_apis::GetAPIKey() |
| 254 : google_apis::GetNonStableAPIKey(); | 269 : google_apis::GetNonStableAPIKey(); |
| 255 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, | 270 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, |
| 256 kSnippetsServer, key.c_str())); | 271 kSnippetsServer, key.c_str())); |
| 257 | 272 |
| 258 FetchSnippetsImpl( | 273 FetchSnippetsImpl( |
| 259 url, std::string(), | 274 url, std::string(), |
| 260 base::StringPrintf(kRequestParameterFormat, "", "", | 275 base::StringPrintf(kRequestParameterFormat, /*obfuscated_gaia_id=*/"", |
| 261 GetHostRestricts().c_str(), count_to_fetch_)); | 276 /* only_return_personalized_results=*/kFalseString, |
| 277 /*user_segment=*/"", GetHostRestricts().c_str(), | |
| 278 count_to_fetch_)); | |
| 262 } | 279 } |
| 263 | 280 |
| 264 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( | 281 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( |
| 265 const std::string& account_id, | 282 const std::string& account_id, |
| 266 const std::string& oauth_access_token) { | 283 const std::string& oauth_access_token) { |
| 267 std::string auth = base::StringPrintf(kGaiaIdFormat, account_id.c_str()); | 284 std::string auth = base::StringPrintf(kGaiaIdFormat, account_id.c_str()); |
|
Marc Treib
2016/05/12 08:59:16
nit: rename to gaia_id or something? It's not auth
jkrcal
2016/05/12 09:57:29
Done.
| |
| 268 std::string user_segment = | 285 std::string user_segment = |
| 269 base::StringPrintf(kUserSegmentFormat, locale_.c_str()); | 286 base::StringPrintf(kUserSegmentFormat, locale_.c_str()); |
| 270 | 287 |
| 271 FetchSnippetsImpl( | 288 FetchSnippetsImpl( |
| 272 GURL(kSnippetsServer), | 289 GURL(kSnippetsServer), |
| 273 base::StringPrintf(kAuthorizationRequestHeaderFormat, | 290 base::StringPrintf(kAuthorizationRequestHeaderFormat, |
| 274 oauth_access_token.c_str()), | 291 oauth_access_token.c_str()), |
| 275 base::StringPrintf(kRequestParameterFormat, auth.c_str(), | 292 base::StringPrintf(kRequestParameterFormat, auth.c_str(), |
|
Marc Treib
2016/05/12 08:59:16
Maybe make a BuildRequestParameter helper function
jkrcal
2016/05/12 09:57:28
Done.
| |
| 293 variant_ == Variant::kAll ? kFalseString : kTrueString, | |
|
Marc Treib
2016/05/12 08:59:16
Ah, so when we get here, the variant can't be kNon
jkrcal
2016/05/12 09:57:29
Done.
| |
| 276 user_segment.c_str(), GetHostRestricts().c_str(), | 294 user_segment.c_str(), GetHostRestricts().c_str(), |
| 277 count_to_fetch_)); | 295 count_to_fetch_)); |
| 278 } | 296 } |
| 279 | 297 |
| 280 void NTPSnippetsFetcher::StartTokenRequest() { | 298 void NTPSnippetsFetcher::StartTokenRequest() { |
| 281 OAuth2TokenService::ScopeSet scopes; | 299 OAuth2TokenService::ScopeSet scopes; |
| 282 scopes.insert(kApiScope); | 300 scopes.insert(kApiScope); |
| 283 oauth_request_ = token_service_->StartRequest( | 301 oauth_request_ = token_service_->StartRequest( |
| 284 signin_manager_->GetAuthenticatedAccountId(), scopes, this); | 302 signin_manager_->GetAuthenticatedAccountId(), scopes, this); |
| 285 } | 303 } |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 395 tick_clock_->NowTicks() - fetch_start_time_); | 413 tick_clock_->NowTicks() - fetch_start_time_); |
| 396 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 414 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
| 397 static_cast<int>(result), | 415 static_cast<int>(result), |
| 398 static_cast<int>(FetchResult::RESULT_MAX)); | 416 static_cast<int>(FetchResult::RESULT_MAX)); |
| 399 | 417 |
| 400 if (!snippets_available_callback_.is_null()) | 418 if (!snippets_available_callback_.is_null()) |
| 401 snippets_available_callback_.Run(std::move(snippets)); | 419 snippets_available_callback_.Run(std::move(snippets)); |
| 402 } | 420 } |
| 403 | 421 |
| 404 } // namespace ntp_snippets | 422 } // namespace ntp_snippets |
| OLD | NEW |