| 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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 parse_json_callback_(parse_json_callback), | 162 parse_json_callback_(parse_json_callback), |
| 163 fetch_url_(GetFetchEndpoint()), | 163 fetch_url_(GetFetchEndpoint()), |
| 164 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) | 164 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) |
| 165 ? CHROME_CONTENT_SUGGESTIONS_API | 165 ? CHROME_CONTENT_SUGGESTIONS_API |
| 166 : CHROME_READER_API), | 166 : CHROME_READER_API), |
| 167 is_stable_channel_(is_stable_channel), | 167 is_stable_channel_(is_stable_channel), |
| 168 tick_clock_(new base::DefaultTickClock()), | 168 tick_clock_(new base::DefaultTickClock()), |
| 169 request_throttler_( | 169 request_throttler_( |
| 170 pref_service, | 170 pref_service, |
| 171 RequestThrottler::RequestType::CONTENT_SUGGESTION_FETCHER), | 171 RequestThrottler::RequestType::CONTENT_SUGGESTION_FETCHER), |
| 172 oauth_token_retried_(false), |
| 172 weak_ptr_factory_(this) { | 173 weak_ptr_factory_(this) { |
| 173 // Parse the variation parameters and set the defaults if missing. | 174 // Parse the variation parameters and set the defaults if missing. |
| 174 std::string personalization = variations::GetVariationParamValue( | 175 std::string personalization = variations::GetVariationParamValue( |
| 175 ntp_snippets::kStudyName, kPersonalizationName); | 176 ntp_snippets::kStudyName, kPersonalizationName); |
| 176 if (personalization == kPersonalizationNonPersonalString) { | 177 if (personalization == kPersonalizationNonPersonalString) { |
| 177 personalization_ = Personalization::kNonPersonal; | 178 personalization_ = Personalization::kNonPersonal; |
| 178 } else if (personalization == kPersonalizationPersonalString) { | 179 } else if (personalization == kPersonalizationPersonalString) { |
| 179 personalization_ = Personalization::kPersonal; | 180 personalization_ = Personalization::kPersonal; |
| 180 } else { | 181 } else { |
| 181 personalization_ = Personalization::kBoth; | 182 personalization_ = Personalization::kBoth; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 return; | 226 return; |
| 226 } | 227 } |
| 227 | 228 |
| 228 locale_ = PosixLocaleFromBCP47Language(language_code); | 229 locale_ = PosixLocaleFromBCP47Language(language_code); |
| 229 count_to_fetch_ = count; | 230 count_to_fetch_ = count; |
| 230 | 231 |
| 231 bool use_authentication = UsesAuthentication(); | 232 bool use_authentication = UsesAuthentication(); |
| 232 | 233 |
| 233 if (use_authentication && signin_manager_->IsAuthenticated()) { | 234 if (use_authentication && signin_manager_->IsAuthenticated()) { |
| 234 // Signed-in: get OAuth token --> fetch snippets. | 235 // Signed-in: get OAuth token --> fetch snippets. |
| 236 oauth_token_retried_ = false; |
| 235 StartTokenRequest(); | 237 StartTokenRequest(); |
| 236 } else if (use_authentication && signin_manager_->AuthInProgress()) { | 238 } else if (use_authentication && signin_manager_->AuthInProgress()) { |
| 237 // Currently signing in: wait for auth to finish (the refresh token) --> | 239 // Currently signing in: wait for auth to finish (the refresh token) --> |
| 238 // get OAuth token --> fetch snippets. | 240 // get OAuth token --> fetch snippets. |
| 239 if (!waiting_for_refresh_token_) { | 241 if (!waiting_for_refresh_token_) { |
| 240 // Wait until we get a refresh token. | 242 // Wait until we get a refresh token. |
| 241 waiting_for_refresh_token_ = true; | 243 waiting_for_refresh_token_ = true; |
| 242 token_service_->AddObserver(this); | 244 token_service_->AddObserver(this); |
| 243 } | 245 } |
| 244 } else { | 246 } else { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 const std::string& oauth_access_token) { | 397 const std::string& oauth_access_token) { |
| 396 RequestParams params; | 398 RequestParams params; |
| 397 params.fetch_api = fetch_api_; | 399 params.fetch_api = fetch_api_; |
| 398 params.obfuscated_gaia_id = account_id; | 400 params.obfuscated_gaia_id = account_id; |
| 399 params.only_return_personalized_results = | 401 params.only_return_personalized_results = |
| 400 personalization_ == Personalization::kPersonal; | 402 personalization_ == Personalization::kPersonal; |
| 401 params.user_locale = locale_; | 403 params.user_locale = locale_; |
| 402 params.host_restricts = | 404 params.host_restricts = |
| 403 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); | 405 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); |
| 404 params.count_to_fetch = count_to_fetch_; | 406 params.count_to_fetch = count_to_fetch_; |
| 407 // TODO(jkrcal, treib): Add unit-tests for authenticated fetches. |
| 405 FetchSnippetsImpl(fetch_url_, | 408 FetchSnippetsImpl(fetch_url_, |
| 406 base::StringPrintf(kAuthorizationRequestHeaderFormat, | 409 base::StringPrintf(kAuthorizationRequestHeaderFormat, |
| 407 oauth_access_token.c_str()), | 410 oauth_access_token.c_str()), |
| 408 params.BuildRequest()); | 411 params.BuildRequest()); |
| 409 } | 412 } |
| 410 | 413 |
| 411 void NTPSnippetsFetcher::StartTokenRequest() { | 414 void NTPSnippetsFetcher::StartTokenRequest() { |
| 412 OAuth2TokenService::ScopeSet scopes; | 415 OAuth2TokenService::ScopeSet scopes; |
| 413 scopes.insert(fetch_api_ == CHROME_CONTENT_SUGGESTIONS_API | 416 scopes.insert(fetch_api_ == CHROME_CONTENT_SUGGESTIONS_API |
| 414 ? kContentSuggestionsApiScope | 417 ? kContentSuggestionsApiScope |
| (...skipping 14 matching lines...) Expand all Loading... |
| 429 DCHECK_EQ(oauth_request.get(), request) | 432 DCHECK_EQ(oauth_request.get(), request) |
| 430 << "Got tokens from some previous request"; | 433 << "Got tokens from some previous request"; |
| 431 | 434 |
| 432 FetchSnippetsAuthenticated(oauth_request->GetAccountId(), access_token); | 435 FetchSnippetsAuthenticated(oauth_request->GetAccountId(), access_token); |
| 433 } | 436 } |
| 434 | 437 |
| 435 void NTPSnippetsFetcher::OnGetTokenFailure( | 438 void NTPSnippetsFetcher::OnGetTokenFailure( |
| 436 const OAuth2TokenService::Request* request, | 439 const OAuth2TokenService::Request* request, |
| 437 const GoogleServiceAuthError& error) { | 440 const GoogleServiceAuthError& error) { |
| 438 oauth_request_.reset(); | 441 oauth_request_.reset(); |
| 439 DLOG(ERROR) << "Unable to get token: " << error.ToString() | 442 |
| 440 << " - fetching the snippets without authentication."; | 443 if (!oauth_token_retried_ && |
| 444 error.state() == GoogleServiceAuthError::State::REQUEST_CANCELED) { |
| 445 // The request (especially on startup) can get reset by loading the refresh |
| 446 // token - do it one more time. |
| 447 oauth_token_retried_ = true; |
| 448 StartTokenRequest(); |
| 449 return; |
| 450 } |
| 451 |
| 452 DLOG(ERROR) << "Unable to get token: " << error.ToString(); |
| 441 FetchFinished( | 453 FetchFinished( |
| 442 OptionalSnippets(), FetchResult::OAUTH_TOKEN_ERROR, | 454 OptionalSnippets(), FetchResult::OAUTH_TOKEN_ERROR, |
| 443 /*extra_message=*/base::StringPrintf(" (%s)", error.ToString().c_str())); | 455 /*extra_message=*/base::StringPrintf(" (%s)", error.ToString().c_str())); |
| 444 } | 456 } |
| 445 | 457 |
| 446 //////////////////////////////////////////////////////////////////////////////// | 458 //////////////////////////////////////////////////////////////////////////////// |
| 447 // OAuth2TokenService::Observer overrides | 459 // OAuth2TokenService::Observer overrides |
| 448 void NTPSnippetsFetcher::OnRefreshTokenAvailable( | 460 void NTPSnippetsFetcher::OnRefreshTokenAvailable( |
| 449 const std::string& account_id) { | 461 const std::string& account_id) { |
| 450 // Only react on tokens for the account the user has signed in with. | 462 // Only react on tokens for the account the user has signed in with. |
| 451 if (account_id != signin_manager_->GetAuthenticatedAccountId()) | 463 if (account_id != signin_manager_->GetAuthenticatedAccountId()) |
| 452 return; | 464 return; |
| 453 | 465 |
| 454 token_service_->RemoveObserver(this); | 466 token_service_->RemoveObserver(this); |
| 455 waiting_for_refresh_token_ = false; | 467 waiting_for_refresh_token_ = false; |
| 468 oauth_token_retried_ = false; |
| 456 StartTokenRequest(); | 469 StartTokenRequest(); |
| 457 } | 470 } |
| 458 | 471 |
| 459 //////////////////////////////////////////////////////////////////////////////// | 472 //////////////////////////////////////////////////////////////////////////////// |
| 460 // URLFetcherDelegate overrides | 473 // URLFetcherDelegate overrides |
| 461 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { | 474 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { |
| 462 DCHECK_EQ(url_fetcher_.get(), source); | 475 DCHECK_EQ(url_fetcher_.get(), source); |
| 463 | 476 |
| 464 const URLRequestStatus& status = source->GetStatus(); | 477 const URLRequestStatus& status = source->GetStatus(); |
| 465 | 478 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 } | 582 } |
| 570 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 583 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
| 571 static_cast<int>(result), | 584 static_cast<int>(result), |
| 572 static_cast<int>(FetchResult::RESULT_MAX)); | 585 static_cast<int>(FetchResult::RESULT_MAX)); |
| 573 | 586 |
| 574 if (!snippets_available_callback_.is_null()) | 587 if (!snippets_available_callback_.is_null()) |
| 575 snippets_available_callback_.Run(std::move(snippets)); | 588 snippets_available_callback_.Run(std::move(snippets)); |
| 576 } | 589 } |
| 577 | 590 |
| 578 } // namespace ntp_snippets | 591 } // namespace ntp_snippets |
| OLD | NEW |