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

Side by Side Diff: components/ntp_snippets/ntp_snippets_fetcher.cc

Issue 2239653002: Add robustness against OAuth2 Token requests getting canceled. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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 unified diff | Download patch
OLDNEW
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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 return; 225 return;
226 } 226 }
227 227
228 locale_ = PosixLocaleFromBCP47Language(language_code); 228 locale_ = PosixLocaleFromBCP47Language(language_code);
229 count_to_fetch_ = count; 229 count_to_fetch_ = count;
230 230
231 bool use_authentication = UsesAuthentication(); 231 bool use_authentication = UsesAuthentication();
232 232
233 if (use_authentication && signin_manager_->IsAuthenticated()) { 233 if (use_authentication && signin_manager_->IsAuthenticated()) {
234 // Signed-in: get OAuth token --> fetch snippets. 234 // Signed-in: get OAuth token --> fetch snippets.
235 oauth_token_retried_ = false;
Marc Treib 2016/08/11 09:35:17 You should also initialize this in the ctor
jkrcal 2016/08/11 10:03:56 Done. I was wondering about it. In the current c
Marc Treib 2016/08/11 10:11:03 Yes. I'm not sure if the style guide has a rule ab
jkrcal 2016/08/11 12:31:48 Makes sense. Thanks.
235 StartTokenRequest(); 236 StartTokenRequest();
236 } else if (use_authentication && signin_manager_->AuthInProgress()) { 237 } else if (use_authentication && signin_manager_->AuthInProgress()) {
237 // Currently signing in: wait for auth to finish (the refresh token) --> 238 // Currently signing in: wait for auth to finish (the refresh token) -->
238 // get OAuth token --> fetch snippets. 239 // get OAuth token --> fetch snippets.
239 if (!waiting_for_refresh_token_) { 240 if (!waiting_for_refresh_token_) {
240 // Wait until we get a refresh token. 241 // Wait until we get a refresh token.
241 waiting_for_refresh_token_ = true; 242 waiting_for_refresh_token_ = true;
242 token_service_->AddObserver(this); 243 token_service_->AddObserver(this);
243 } 244 }
244 } else { 245 } else {
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 DCHECK_EQ(oauth_request.get(), request) 430 DCHECK_EQ(oauth_request.get(), request)
430 << "Got tokens from some previous request"; 431 << "Got tokens from some previous request";
431 432
432 FetchSnippetsAuthenticated(oauth_request->GetAccountId(), access_token); 433 FetchSnippetsAuthenticated(oauth_request->GetAccountId(), access_token);
433 } 434 }
434 435
435 void NTPSnippetsFetcher::OnGetTokenFailure( 436 void NTPSnippetsFetcher::OnGetTokenFailure(
436 const OAuth2TokenService::Request* request, 437 const OAuth2TokenService::Request* request,
437 const GoogleServiceAuthError& error) { 438 const GoogleServiceAuthError& error) {
438 oauth_request_.reset(); 439 oauth_request_.reset();
439 DLOG(ERROR) << "Unable to get token: " << error.ToString() 440
440 << " - fetching the snippets without authentication."; 441 if (!oauth_token_retried_ &&
Marc Treib 2016/08/11 09:35:17 Outdated documentation ftl :D
jkrcal 2016/08/11 10:03:56 :)
442 error.state() == GoogleServiceAuthError::State::REQUEST_CANCELED) {
443 // The request (especially on startup) can get reset by loading the refresh
444 // token - do it one more time.
445 oauth_token_retried_ = true;
446 StartTokenRequest();
447 return;
448 }
449
450 DLOG(ERROR) << "Unable to get token: " << error.ToString();
441 FetchFinished( 451 FetchFinished(
442 OptionalSnippets(), FetchResult::OAUTH_TOKEN_ERROR, 452 OptionalSnippets(), FetchResult::OAUTH_TOKEN_ERROR,
443 /*extra_message=*/base::StringPrintf(" (%s)", error.ToString().c_str())); 453 /*extra_message=*/base::StringPrintf(" (%s)", error.ToString().c_str()));
444 } 454 }
445 455
446 //////////////////////////////////////////////////////////////////////////////// 456 ////////////////////////////////////////////////////////////////////////////////
447 // OAuth2TokenService::Observer overrides 457 // OAuth2TokenService::Observer overrides
448 void NTPSnippetsFetcher::OnRefreshTokenAvailable( 458 void NTPSnippetsFetcher::OnRefreshTokenAvailable(
449 const std::string& account_id) { 459 const std::string& account_id) {
450 // Only react on tokens for the account the user has signed in with. 460 // Only react on tokens for the account the user has signed in with.
451 if (account_id != signin_manager_->GetAuthenticatedAccountId()) 461 if (account_id != signin_manager_->GetAuthenticatedAccountId())
452 return; 462 return;
453 463
454 token_service_->RemoveObserver(this); 464 token_service_->RemoveObserver(this);
455 waiting_for_refresh_token_ = false; 465 waiting_for_refresh_token_ = false;
466 oauth_token_retried_ = false;
456 StartTokenRequest(); 467 StartTokenRequest();
457 } 468 }
458 469
459 //////////////////////////////////////////////////////////////////////////////// 470 ////////////////////////////////////////////////////////////////////////////////
460 // URLFetcherDelegate overrides 471 // URLFetcherDelegate overrides
461 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { 472 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) {
462 DCHECK_EQ(url_fetcher_.get(), source); 473 DCHECK_EQ(url_fetcher_.get(), source);
463 474
464 const URLRequestStatus& status = source->GetStatus(); 475 const URLRequestStatus& status = source->GetStatus();
465 476
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 } 580 }
570 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", 581 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult",
571 static_cast<int>(result), 582 static_cast<int>(result),
572 static_cast<int>(FetchResult::RESULT_MAX)); 583 static_cast<int>(FetchResult::RESULT_MAX));
573 584
574 if (!snippets_available_callback_.is_null()) 585 if (!snippets_available_callback_.is_null())
575 snippets_available_callback_.Run(std::move(snippets)); 586 snippets_available_callback_.Run(std::move(snippets));
576 } 587 }
577 588
578 } // namespace ntp_snippets 589 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698