[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 5 | #include "components/invalidation/impl/profile_identity_provider.h" |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 6 | |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 7 | #include "components/invalidation/public/active_account_access_token_fetcher_impl.h" |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 8 | #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 9 | |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 10 | namespace invalidation { |
| 11 | |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 12 | ProfileIdentityProvider::ProfileIdentityProvider( |
| 13 | SigninManagerBase* signin_manager, |
Colin Blundell | 12b411d2 | 2018-05-23 09:24:26 | [diff] [blame] | 14 | ProfileOAuth2TokenService* token_service) |
| 15 | : signin_manager_(signin_manager), token_service_(token_service) { |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame^] | 16 | // TODO(blundell): Can |token_service_| ever actually be non-null? |
| 17 | if (token_service_) |
| 18 | token_service_->AddObserver(this); |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 19 | signin_manager_->AddObserver(this); |
| 20 | } |
| 21 | |
| 22 | ProfileIdentityProvider::~ProfileIdentityProvider() { |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame^] | 23 | // TODO(blundell): Can |token_service_| ever actually be non-null? |
| 24 | if (token_service_) |
| 25 | token_service_->RemoveObserver(this); |
| 26 | |
Colin Blundell | 3b37c4e | 2018-06-06 11:35:43 | [diff] [blame] | 27 | // In unittests |signin_manager_| is allowed to be null. |
| 28 | // TODO(809452): Eliminate this short-circuit when this class is converted to |
| 29 | // take in IdentityManager, at which point the tests can use |
| 30 | // IdentityTestEnvironment. |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame^] | 31 | if (signin_manager_) |
| 32 | signin_manager_->RemoveObserver(this); |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 33 | } |
| 34 | |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 35 | std::string ProfileIdentityProvider::GetActiveAccountId() { |
Colin Blundell | 3b37c4e | 2018-06-06 11:35:43 | [diff] [blame] | 36 | // In unittests |signin_manager_| is allowed to be null. |
| 37 | // TODO(809452): Eliminate this short-circuit when this class is converted to |
| 38 | // take in IdentityManager, at which point the tests can use |
| 39 | // IdentityTestEnvironment. |
| 40 | if (!signin_manager_) |
| 41 | return std::string(); |
| 42 | |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 43 | return signin_manager_->GetAuthenticatedAccountId(); |
| 44 | } |
| 45 | |
Colin Blundell | a212f4f | 2018-06-05 14:13:11 | [diff] [blame] | 46 | bool ProfileIdentityProvider::IsActiveAccountAvailable() { |
| 47 | if (GetActiveAccountId().empty() || !token_service_ || |
| 48 | !token_service_->RefreshTokenIsAvailable(GetActiveAccountId())) |
| 49 | return false; |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
Colin Blundell | 7b50d33 | 2018-06-28 14:42:52 | [diff] [blame] | 54 | std::unique_ptr<ActiveAccountAccessTokenFetcher> |
| 55 | ProfileIdentityProvider::FetchAccessToken( |
| 56 | const std::string& oauth_consumer_name, |
| 57 | const OAuth2TokenService::ScopeSet& scopes, |
| 58 | ActiveAccountAccessTokenCallback callback) { |
| 59 | return std::make_unique<ActiveAccountAccessTokenFetcherImpl>( |
| 60 | GetActiveAccountId(), oauth_consumer_name, token_service_, scopes, |
| 61 | std::move(callback)); |
| 62 | } |
| 63 | |
| 64 | void ProfileIdentityProvider::InvalidateAccessToken( |
| 65 | const OAuth2TokenService::ScopeSet& scopes, |
| 66 | const std::string& access_token) { |
| 67 | token_service_->InvalidateAccessToken(GetActiveAccountId(), scopes, |
| 68 | access_token); |
| 69 | } |
| 70 | |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 71 | void ProfileIdentityProvider::GoogleSigninSucceeded( |
rogerta | 22aa58e | 2014-08-29 15:15:57 | [diff] [blame] | 72 | const std::string& account_id, |
Mihai Sardarescu | b4b697e | 2017-07-04 16:41:46 | [diff] [blame] | 73 | const std::string& username) { |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 74 | FireOnActiveAccountLogin(); |
| 75 | } |
| 76 | |
rogerta | 22aa58e | 2014-08-29 15:15:57 | [diff] [blame] | 77 | void ProfileIdentityProvider::GoogleSignedOut(const std::string& account_id, |
| 78 | const std::string& username) { |
[email protected] | a263ff5 | 2014-04-23 19:46:53 | [diff] [blame] | 79 | FireOnActiveAccountLogout(); |
| 80 | } |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 81 | |
Colin Blundell | d0b89cd8 | 2018-06-29 13:39:00 | [diff] [blame^] | 82 | void ProfileIdentityProvider::OnRefreshTokenAvailable( |
| 83 | const std::string& account_id) { |
| 84 | ProcessRefreshTokenUpdateForAccount(account_id); |
| 85 | } |
| 86 | |
| 87 | void ProfileIdentityProvider::OnRefreshTokenRevoked( |
| 88 | const std::string& account_id) { |
| 89 | ProcessRefreshTokenRemovalForAccount(account_id); |
| 90 | } |
| 91 | |
Colin Blundell | 3e0efae | 2018-06-05 07:11:49 | [diff] [blame] | 92 | } // namespace invalidation |