| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "chrome/browser/signin/oauth2_token_service.h" | 5 #include "chrome/browser/signin/oauth2_token_service.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/signin/oauth2_token_service_factory.h" | 17 #include "chrome/browser/signin/oauth2_token_service_factory.h" |
| 18 #include "chrome/browser/signin/signin_manager.h" | |
| 19 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 20 #include "chrome/browser/signin/token_service.h" | 18 #include "chrome/browser/signin/token_service.h" |
| 21 #include "chrome/browser/signin/token_service_factory.h" | 19 #include "chrome/browser/signin/token_service_factory.h" |
| 22 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
| 23 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 25 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
| 26 #include "google_apis/gaia/gaia_constants.h" | 24 #include "google_apis/gaia/gaia_constants.h" |
| 27 #include "google_apis/gaia/gaia_urls.h" | 25 #include "google_apis/gaia/gaia_urls.h" |
| 28 #include "google_apis/gaia/google_service_auth_error.h" | 26 #include "google_apis/gaia/google_service_auth_error.h" |
| 29 #include "google_apis/gaia/oauth2_access_token_consumer.h" | 27 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 const OAuth2TokenService::ScopeSet& scopes, | 122 const OAuth2TokenService::ScopeSet& scopes, |
| 125 base::WeakPtr<RequestImpl> waiting_request); | 123 base::WeakPtr<RequestImpl> waiting_request); |
| 126 virtual ~Fetcher(); | 124 virtual ~Fetcher(); |
| 127 | 125 |
| 128 // Add a request that is waiting for the result of this Fetcher. | 126 // Add a request that is waiting for the result of this Fetcher. |
| 129 void AddWaitingRequest(base::WeakPtr<RequestImpl> waiting_request); | 127 void AddWaitingRequest(base::WeakPtr<RequestImpl> waiting_request); |
| 130 | 128 |
| 131 const OAuth2TokenService::ScopeSet& GetScopeSet() const; | 129 const OAuth2TokenService::ScopeSet& GetScopeSet() const; |
| 132 const std::string& GetRefreshToken() const; | 130 const std::string& GetRefreshToken() const; |
| 133 | 131 |
| 134 // The error result from this fetcher. | |
| 135 const GoogleServiceAuthError& error() const { return error_; } | |
| 136 | |
| 137 protected: | 132 protected: |
| 138 // OAuth2AccessTokenConsumer | 133 // OAuth2AccessTokenConsumer |
| 139 virtual void OnGetTokenSuccess(const std::string& access_token, | 134 virtual void OnGetTokenSuccess(const std::string& access_token, |
| 140 const base::Time& expiration_date) OVERRIDE; | 135 const base::Time& expiration_date) OVERRIDE; |
| 141 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | 136 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
| 142 | 137 |
| 143 private: | 138 private: |
| 144 Fetcher(Profile* profile, | 139 Fetcher(Profile* profile, |
| 145 net::URLRequestContextGetter* getter, | 140 net::URLRequestContextGetter* getter, |
| 146 const std::string& refresh_token, | 141 const std::string& refresh_token, |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 309 |
| 315 OAuth2TokenService::Request::~Request() { | 310 OAuth2TokenService::Request::~Request() { |
| 316 } | 311 } |
| 317 | 312 |
| 318 OAuth2TokenService::Consumer::Consumer() { | 313 OAuth2TokenService::Consumer::Consumer() { |
| 319 } | 314 } |
| 320 | 315 |
| 321 OAuth2TokenService::Consumer::~Consumer() { | 316 OAuth2TokenService::Consumer::~Consumer() { |
| 322 } | 317 } |
| 323 | 318 |
| 324 OAuth2TokenService::OAuth2TokenService() | 319 OAuth2TokenService::OAuth2TokenService() : profile_(NULL) { |
| 325 : profile_(NULL), | |
| 326 last_auth_error_(GoogleServiceAuthError::NONE) { | |
| 327 } | 320 } |
| 328 | 321 |
| 329 OAuth2TokenService::~OAuth2TokenService() { | 322 OAuth2TokenService::~OAuth2TokenService() { |
| 330 // Release all the pending fetchers. | 323 // Release all the pending fetchers. |
| 331 STLDeleteContainerPairSecondPointers( | 324 STLDeleteContainerPairSecondPointers( |
| 332 pending_fetchers_.begin(), pending_fetchers_.end()); | 325 pending_fetchers_.begin(), pending_fetchers_.end()); |
| 333 } | 326 } |
| 334 | 327 |
| 335 void OAuth2TokenService::Initialize(Profile* profile) { | 328 void OAuth2TokenService::Initialize(Profile* profile) { |
| 336 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 329 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 337 | 330 |
| 338 DCHECK(profile); | 331 DCHECK(profile); |
| 339 DCHECK(!profile_); | 332 DCHECK(!profile_); |
| 340 profile_ = profile; | 333 profile_ = profile; |
| 341 getter_ = profile->GetRequestContext(); | 334 getter_ = profile->GetRequestContext(); |
| 342 content::Source<TokenService> token_service_source( | 335 content::Source<TokenService> token_service_source( |
| 343 TokenServiceFactory::GetForProfile(profile)); | 336 TokenServiceFactory::GetForProfile(profile)); |
| 344 registrar_.Add(this, | 337 registrar_.Add(this, |
| 345 chrome::NOTIFICATION_TOKENS_CLEARED, | 338 chrome::NOTIFICATION_TOKENS_CLEARED, |
| 346 token_service_source); | 339 token_service_source); |
| 347 registrar_.Add(this, | 340 registrar_.Add(this, |
| 348 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 341 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| 349 token_service_source); | 342 token_service_source); |
| 350 SigninManagerFactory::GetForProfile(profile_)->signin_global_error()-> | |
| 351 AddProvider(this); | |
| 352 } | 343 } |
| 353 | 344 |
| 354 void OAuth2TokenService::Shutdown() { | |
| 355 if (profile_) { | |
| 356 SigninManagerFactory::GetForProfile(profile_)->signin_global_error()-> | |
| 357 RemoveProvider(this); | |
| 358 } | |
| 359 } | |
| 360 | |
| 361 | |
| 362 // static | 345 // static |
| 363 void OAuth2TokenService::InformConsumer( | 346 void OAuth2TokenService::InformConsumer( |
| 364 base::WeakPtr<OAuth2TokenService::RequestImpl> request, | 347 base::WeakPtr<OAuth2TokenService::RequestImpl> request, |
| 365 GoogleServiceAuthError error, | 348 GoogleServiceAuthError error, |
| 366 std::string access_token, | 349 std::string access_token, |
| 367 base::Time expiration_date) { | 350 base::Time expiration_date) { |
| 368 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 351 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 369 | 352 |
| 370 if (request) | 353 if (request) |
| 371 request->InformConsumer(error, access_token, expiration_date); | 354 request->InformConsumer(error, access_token, expiration_date); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 return request.PassAs<Request>(); | 406 return request.PassAs<Request>(); |
| 424 } | 407 } |
| 425 pending_fetchers_[fetch_parameters] = Fetcher::CreateAndStart( | 408 pending_fetchers_[fetch_parameters] = Fetcher::CreateAndStart( |
| 426 profile_, getter_, refresh_token, scopes, request->AsWeakPtr()); | 409 profile_, getter_, refresh_token, scopes, request->AsWeakPtr()); |
| 427 return request.PassAs<Request>(); | 410 return request.PassAs<Request>(); |
| 428 } | 411 } |
| 429 | 412 |
| 430 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { | 413 void OAuth2TokenService::OnFetchComplete(Fetcher* fetcher) { |
| 431 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 414 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 432 | 415 |
| 433 // Update the auth error state so auth errors are appropriately communicated | |
| 434 // to the user. | |
| 435 UpdateAuthError(fetcher->error()); | |
| 436 | |
| 437 // Note |fetcher| is recorded in |pending_fetcher_| mapped to its refresh | 416 // Note |fetcher| is recorded in |pending_fetcher_| mapped to its refresh |
| 438 // token and scope set. This is guaranteed as follows; here a Fetcher is said | 417 // token and scope set. This is guaranteed as follows; here a Fetcher is said |
| 439 // to be uncompleted if it has not finished calling back | 418 // to be uncompleted if it has not finished calling back |
| 440 // OAuth2TokenService::OnFetchComplete(). | 419 // OAuth2TokenService::OnFetchComplete(). |
| 441 // | 420 // |
| 442 // (1) All the live Fetchers are created by this service. | 421 // (1) All the live Fetchers are created by this service. |
| 443 // This is because (1) all the live Fetchers are created by a live | 422 // This is because (1) all the live Fetchers are created by a live |
| 444 // service, as all the fetchers created by a service are destructed in the | 423 // service, as all the fetchers created by a service are destructed in the |
| 445 // service's dtor, and (2) there is at most one live OAuth2TokenSevice for | 424 // service's dtor, and (2) there is at most one live OAuth2TokenSevice for |
| 446 // a given profile at a time. | 425 // a given profile at a time. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { | 492 if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) { |
| 514 TokenService::TokenAvailableDetails* tok_details = | 493 TokenService::TokenAvailableDetails* tok_details = |
| 515 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); | 494 content::Details<TokenService::TokenAvailableDetails>(details).ptr(); |
| 516 if (tok_details->service() != GaiaConstants::kGaiaOAuth2LoginRefreshToken) | 495 if (tok_details->service() != GaiaConstants::kGaiaOAuth2LoginRefreshToken) |
| 517 return; | 496 return; |
| 518 } | 497 } |
| 519 // The GaiaConstants::kGaiaOAuth2LoginRefreshToken token is used to create | 498 // The GaiaConstants::kGaiaOAuth2LoginRefreshToken token is used to create |
| 520 // OAuth2 access tokens. If this token either changes or is cleared, any | 499 // OAuth2 access tokens. If this token either changes or is cleared, any |
| 521 // available tokens must be invalidated. | 500 // available tokens must be invalidated. |
| 522 token_cache_.clear(); | 501 token_cache_.clear(); |
| 523 UpdateAuthError(GoogleServiceAuthError::None()); | |
| 524 } | 502 } |
| 525 | |
| 526 void OAuth2TokenService::UpdateAuthError(const GoogleServiceAuthError& error) { | |
| 527 // Do not report connection errors as these are not actually auth errors. | |
| 528 // We also want to avoid masking a "real" auth error just because we | |
| 529 // subsequently get a transient network error. | |
| 530 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) | |
| 531 return; | |
| 532 | |
| 533 if (error.state() != last_auth_error_.state()) { | |
| 534 last_auth_error_ = error; | |
| 535 SigninManagerFactory::GetForProfile(profile_)->signin_global_error()-> | |
| 536 AuthStatusChanged(); | |
| 537 } | |
| 538 } | |
| 539 | |
| 540 GoogleServiceAuthError OAuth2TokenService::GetAuthStatus() const { | |
| 541 return last_auth_error_; | |
| 542 } | |
| OLD | NEW |