blob: 6fa8f96ddb023f5608d830a297eede9c751097fa [file] [log] [blame]
[email protected]a263ff52014-04-23 19:46:531// 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 Blundell3e0efae2018-06-05 07:11:495#include "components/invalidation/impl/profile_identity_provider.h"
[email protected]a263ff52014-04-23 19:46:536
Colin Blundell7b50d332018-06-28 14:42:527#include "components/invalidation/public/active_account_access_token_fetcher_impl.h"
[email protected]a263ff52014-04-23 19:46:538#include "components/signin/core/browser/profile_oauth2_token_service.h"
9
Colin Blundell3e0efae2018-06-05 07:11:4910namespace invalidation {
11
[email protected]a263ff52014-04-23 19:46:5312ProfileIdentityProvider::ProfileIdentityProvider(
13 SigninManagerBase* signin_manager,
Colin Blundell12b411d22018-05-23 09:24:2614 ProfileOAuth2TokenService* token_service)
15 : signin_manager_(signin_manager), token_service_(token_service) {
Colin Blundelld0b89cd82018-06-29 13:39:0016 // TODO(blundell): Can |token_service_| ever actually be non-null?
17 if (token_service_)
18 token_service_->AddObserver(this);
[email protected]a263ff52014-04-23 19:46:5319 signin_manager_->AddObserver(this);
20}
21
22ProfileIdentityProvider::~ProfileIdentityProvider() {
Colin Blundelld0b89cd82018-06-29 13:39:0023 // TODO(blundell): Can |token_service_| ever actually be non-null?
24 if (token_service_)
25 token_service_->RemoveObserver(this);
26
Colin Blundell3b37c4e2018-06-06 11:35:4327 // 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 Blundelld0b89cd82018-06-29 13:39:0031 if (signin_manager_)
32 signin_manager_->RemoveObserver(this);
[email protected]a263ff52014-04-23 19:46:5333}
34
[email protected]a263ff52014-04-23 19:46:5335std::string ProfileIdentityProvider::GetActiveAccountId() {
Colin Blundell3b37c4e2018-06-06 11:35:4336 // 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]a263ff52014-04-23 19:46:5343 return signin_manager_->GetAuthenticatedAccountId();
44}
45
Colin Blundella212f4f2018-06-05 14:13:1146bool ProfileIdentityProvider::IsActiveAccountAvailable() {
47 if (GetActiveAccountId().empty() || !token_service_ ||
48 !token_service_->RefreshTokenIsAvailable(GetActiveAccountId()))
49 return false;
50
51 return true;
52}
53
Colin Blundell7b50d332018-06-28 14:42:5254std::unique_ptr<ActiveAccountAccessTokenFetcher>
55ProfileIdentityProvider::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
64void 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]a263ff52014-04-23 19:46:5371void ProfileIdentityProvider::GoogleSigninSucceeded(
rogerta22aa58e2014-08-29 15:15:5772 const std::string& account_id,
Mihai Sardarescub4b697e2017-07-04 16:41:4673 const std::string& username) {
[email protected]a263ff52014-04-23 19:46:5374 FireOnActiveAccountLogin();
75}
76
rogerta22aa58e2014-08-29 15:15:5777void ProfileIdentityProvider::GoogleSignedOut(const std::string& account_id,
78 const std::string& username) {
[email protected]a263ff52014-04-23 19:46:5379 FireOnActiveAccountLogout();
80}
Colin Blundell3e0efae2018-06-05 07:11:4981
Colin Blundelld0b89cd82018-06-29 13:39:0082void ProfileIdentityProvider::OnRefreshTokenAvailable(
83 const std::string& account_id) {
84 ProcessRefreshTokenUpdateForAccount(account_id);
85}
86
87void ProfileIdentityProvider::OnRefreshTokenRevoked(
88 const std::string& account_id) {
89 ProcessRefreshTokenRemovalForAccount(account_id);
90}
91
Colin Blundell3e0efae2018-06-05 07:11:4992} // namespace invalidation