blob: f0bd9dc9a93939adb27190c34c826076cbea7c5a [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
Colin Blundell3e0efae2018-06-05 07:11:499namespace invalidation {
10
Colin Blundell2a96a6912018-07-13 08:57:5111namespace {
12
13// ActiveAccountAccessTokenFetcher implementation that is backed by
14// IdentityManager and wraps an AccessTokenFetcher internally.
15class AccessTokenFetcherAdaptor : public ActiveAccountAccessTokenFetcher {
16 public:
17 AccessTokenFetcherAdaptor(const std::string& active_account_id,
18 const std::string& oauth_consumer_name,
19 identity::IdentityManager* identity_manager,
20 const OAuth2TokenService::ScopeSet& scopes,
21 ActiveAccountAccessTokenCallback callback);
22 ~AccessTokenFetcherAdaptor() override = default;
23
24 private:
Colin Blundell80bede82018-07-16 11:18:2725 // Invokes |callback_| with (|error|, |access_token_info.token|).
26 void HandleTokenRequestCompletion(
27 GoogleServiceAuthError error,
28 identity::AccessTokenInfo access_token_info);
Colin Blundell2a96a6912018-07-13 08:57:5129
30 ActiveAccountAccessTokenCallback callback_;
31 std::unique_ptr<identity::AccessTokenFetcher> access_token_fetcher_;
32
33 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherAdaptor);
34};
35
36AccessTokenFetcherAdaptor::AccessTokenFetcherAdaptor(
37 const std::string& active_account_id,
38 const std::string& oauth_consumer_name,
39 identity::IdentityManager* identity_manager,
40 const OAuth2TokenService::ScopeSet& scopes,
41 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)),
47 identity::AccessTokenFetcher::Mode::kImmediate);
Colin Blundell2a96a6912018-07-13 08:57:5148}
49
50void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion(
51 GoogleServiceAuthError error,
Colin Blundell80bede82018-07-16 11:18:2752 identity::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(
Colin Blundell2a96a6912018-07-13 08:57:5161 identity::IdentityManager* identity_manager)
62 : 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
[email protected]a263ff52014-04-23 19:46:5370std::string ProfileIdentityProvider::GetActiveAccountId() {
Colin Blundell2a96a6912018-07-13 08:57:5171 return identity_manager_->GetPrimaryAccountInfo().account_id;
[email protected]a263ff52014-04-23 19:46:5372}
73
Colin Blundella212f4f2018-06-05 14:13:1174bool ProfileIdentityProvider::IsActiveAccountAvailable() {
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
Colin Blundell7b50d332018-06-28 14:42:5282std::unique_ptr<ActiveAccountAccessTokenFetcher>
83ProfileIdentityProvider::FetchAccessToken(
84 const std::string& oauth_consumer_name,
85 const OAuth2TokenService::ScopeSet& scopes,
86 ActiveAccountAccessTokenCallback callback) {
Colin Blundell2a96a6912018-07-13 08:57:5187 return std::make_unique<AccessTokenFetcherAdaptor>(
88 GetActiveAccountId(), oauth_consumer_name, identity_manager_, scopes,
Colin Blundell7b50d332018-06-28 14:42:5289 std::move(callback));
90}
91
92void ProfileIdentityProvider::InvalidateAccessToken(
93 const OAuth2TokenService::ScopeSet& scopes,
94 const std::string& access_token) {
Colin Blundell2a96a6912018-07-13 08:57:5195 identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes,
96 access_token);
Colin Blundell7b50d332018-06-28 14:42:5297}
98
Colin Blundell2a96a6912018-07-13 08:57:5199void ProfileIdentityProvider::OnPrimaryAccountSet(
100 const AccountInfo& primary_account_info) {
[email protected]a263ff52014-04-23 19:46:53101 FireOnActiveAccountLogin();
102}
103
Colin Blundell2a96a6912018-07-13 08:57:51104void ProfileIdentityProvider::OnPrimaryAccountCleared(
105 const AccountInfo& previous_primary_account_info) {
[email protected]a263ff52014-04-23 19:46:53106 FireOnActiveAccountLogout();
107}
Colin Blundell3e0efae2018-06-05 07:11:49108
Colin Blundell2a96a6912018-07-13 08:57:51109void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount(
110 const AccountInfo& account_info,
111 bool is_valid) {
112 ProcessRefreshTokenUpdateForAccount(account_info.account_id);
Colin Blundelld0b89cd82018-06-29 13:39:00113}
114
Colin Blundell2a96a6912018-07-13 08:57:51115void ProfileIdentityProvider::OnRefreshTokenRemovedForAccount(
116 const AccountInfo& account_info) {
117 ProcessRefreshTokenRemovalForAccount(account_info.account_id);
Colin Blundelld0b89cd82018-06-29 13:39:00118}
119
Colin Blundell3e0efae2018-06-05 07:11:49120} // namespace invalidation