blob: 9777dcb572a350dcfc3541da61e13e81b2fe6818 [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
Sebastien Marchand53801a32019-01-25 16:26:117#include "base/bind.h"
Henrique Ferreiro960e6f2e2019-07-04 12:33:158#include "components/signin/public/identity_manager/access_token_info.h"
[email protected]a263ff52014-04-23 19:46:539
Colin Blundell3e0efae2018-06-05 07:11:4910namespace invalidation {
11
Colin Blundell2a96a6912018-07-13 08:57:5112namespace {
13
14// ActiveAccountAccessTokenFetcher implementation that is backed by
15// IdentityManager and wraps an AccessTokenFetcher internally.
16class AccessTokenFetcherAdaptor : public ActiveAccountAccessTokenFetcher {
17 public:
Tanmoy Mollikf95f6182019-06-24 17:40:3418 AccessTokenFetcherAdaptor(const CoreAccountId& active_account_id,
Colin Blundell2a96a6912018-07-13 08:57:5119 const std::string& oauth_consumer_name,
Miyoung Shin23737f62019-07-23 15:43:3120 signin::IdentityManager* identity_manager,
James Cookfaf1c102020-02-27 15:23:2021 const signin::ScopeSet& scopes,
Colin Blundell2a96a6912018-07-13 08:57:5122 ActiveAccountAccessTokenCallback callback);
23 ~AccessTokenFetcherAdaptor() override = default;
24
25 private:
Colin Blundell80bede82018-07-16 11:18:2726 // Invokes |callback_| with (|error|, |access_token_info.token|).
Miyoung Shin23737f62019-07-23 15:43:3127 void HandleTokenRequestCompletion(GoogleServiceAuthError error,
28 signin::AccessTokenInfo access_token_info);
Colin Blundell2a96a6912018-07-13 08:57:5129
30 ActiveAccountAccessTokenCallback callback_;
Miyoung Shin23737f62019-07-23 15:43:3131 std::unique_ptr<signin::AccessTokenFetcher> access_token_fetcher_;
Colin Blundell2a96a6912018-07-13 08:57:5132
33 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherAdaptor);
34};
35
36AccessTokenFetcherAdaptor::AccessTokenFetcherAdaptor(
Tanmoy Mollikf95f6182019-06-24 17:40:3437 const CoreAccountId& active_account_id,
Colin Blundell2a96a6912018-07-13 08:57:5138 const std::string& oauth_consumer_name,
Miyoung Shin23737f62019-07-23 15:43:3139 signin::IdentityManager* identity_manager,
James Cookfaf1c102020-02-27 15:23:2040 const signin::ScopeSet& scopes,
Colin Blundell2a96a6912018-07-13 08:57:5141 ActiveAccountAccessTokenCallback callback)
42 : callback_(std::move(callback)) {
43 access_token_fetcher_ = identity_manager->CreateAccessTokenFetcherForAccount(
44 active_account_id, oauth_consumer_name, scopes,
45 base::BindOnce(&AccessTokenFetcherAdaptor::HandleTokenRequestCompletion,
Marc Treib46f3a042018-08-13 09:12:4046 base::Unretained(this)),
Miyoung Shin23737f62019-07-23 15:43:3147 signin::AccessTokenFetcher::Mode::kImmediate);
Colin Blundell2a96a6912018-07-13 08:57:5148}
49
50void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion(
51 GoogleServiceAuthError error,
Miyoung Shin23737f62019-07-23 15:43:3152 signin::AccessTokenInfo access_token_info) {
Colin Blundell2a96a6912018-07-13 08:57:5153 access_token_fetcher_.reset();
54
Colin Blundell80bede82018-07-16 11:18:2755 std::move(callback_).Run(error, access_token_info.token);
Colin Blundell2a96a6912018-07-13 08:57:5156}
57
58} // namespace
59
[email protected]a263ff52014-04-23 19:46:5360ProfileIdentityProvider::ProfileIdentityProvider(
Miyoung Shin23737f62019-07-23 15:43:3161 signin::IdentityManager* identity_manager)
Colin Blundell2a96a6912018-07-13 08:57:5162 : identity_manager_(identity_manager) {
63 identity_manager_->AddObserver(this);
[email protected]a263ff52014-04-23 19:46:5364}
65
66ProfileIdentityProvider::~ProfileIdentityProvider() {
Colin Blundell2a96a6912018-07-13 08:57:5167 identity_manager_->RemoveObserver(this);
[email protected]a263ff52014-04-23 19:46:5368}
69
Tanmoy Mollikf95f6182019-06-24 17:40:3470CoreAccountId ProfileIdentityProvider::GetActiveAccountId() {
Tanja Gornak32e7d7f42018-08-31 10:33:2671 return active_account_id_;
[email protected]a263ff52014-04-23 19:46:5372}
73
Tanja Gornak16e197e2019-02-25 12:50:5674bool ProfileIdentityProvider::IsActiveAccountWithRefreshToken() {
Colin Blundell2a96a6912018-07-13 08:57:5175 if (GetActiveAccountId().empty() || !identity_manager_ ||
76 !identity_manager_->HasAccountWithRefreshToken(GetActiveAccountId()))
Colin Blundella212f4f2018-06-05 14:13:1177 return false;
78
79 return true;
80}
81
Tanja Gornak32e7d7f42018-08-31 10:33:2682void ProfileIdentityProvider::SetActiveAccountId(
Tanmoy Mollikf95f6182019-06-24 17:40:3483 const CoreAccountId& account_id) {
Tanja Gornak32e7d7f42018-08-31 10:33:2684 if (account_id == active_account_id_)
85 return;
86
87 if (!active_account_id_.empty())
88 FireOnActiveAccountLogout();
89
90 active_account_id_ = account_id;
91 if (!active_account_id_.empty())
92 FireOnActiveAccountLogin();
93}
94
Colin Blundell7b50d332018-06-28 14:42:5295std::unique_ptr<ActiveAccountAccessTokenFetcher>
96ProfileIdentityProvider::FetchAccessToken(
97 const std::string& oauth_consumer_name,
James Cookfaf1c102020-02-27 15:23:2098 const signin::ScopeSet& scopes,
Colin Blundell7b50d332018-06-28 14:42:5299 ActiveAccountAccessTokenCallback callback) {
Colin Blundell2a96a6912018-07-13 08:57:51100 return std::make_unique<AccessTokenFetcherAdaptor>(
101 GetActiveAccountId(), oauth_consumer_name, identity_manager_, scopes,
Colin Blundell7b50d332018-06-28 14:42:52102 std::move(callback));
103}
104
105void ProfileIdentityProvider::InvalidateAccessToken(
James Cookfaf1c102020-02-27 15:23:20106 const signin::ScopeSet& scopes,
Colin Blundell7b50d332018-06-28 14:42:52107 const std::string& access_token) {
Colin Blundell2a96a6912018-07-13 08:57:51108 identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes,
109 access_token);
Colin Blundell7b50d332018-06-28 14:42:52110}
111
Colin Blundell2a96a6912018-07-13 08:57:51112void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount(
Gyuyoung Kimb70d3c72019-02-12 01:45:43113 const CoreAccountInfo& account_info) {
Colin Blundell2a96a6912018-07-13 08:57:51114 ProcessRefreshTokenUpdateForAccount(account_info.account_id);
Colin Blundelld0b89cd82018-06-29 13:39:00115}
116
Colin Blundell2a96a6912018-07-13 08:57:51117void ProfileIdentityProvider::OnRefreshTokenRemovedForAccount(
Tanmoy Mollik66a47f782019-05-20 15:05:18118 const CoreAccountId& account_id) {
Colin Blundell63d23fa2018-09-20 09:01:55119 ProcessRefreshTokenRemovalForAccount(account_id);
Colin Blundelld0b89cd82018-06-29 13:39:00120}
121
Colin Blundell3e0efae2018-06-05 07:11:49122} // namespace invalidation