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