1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/invalidation/impl/profile_identity_provider.h"
#include "base/functional/bind.h"
#include "components/signin/public/base/consent_level.h"
#include "components/signin/public/identity_manager/access_token_info.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/primary_account_access_token_fetcher.h"
namespace invalidation {
namespace {
// `ActiveAccountAccessTokenFetcher` implementation that is backed by
// `IdentityManager` and wraps an `PrimaryAccountAccessTokenFetcher` internally.
class AccessTokenFetcherAdaptor : public ActiveAccountAccessTokenFetcher {
public:
AccessTokenFetcherAdaptor(const std::string& oauth_consumer_name,
signin::IdentityManager* identity_manager,
const signin::ScopeSet& scopes,
ActiveAccountAccessTokenCallback callback);
AccessTokenFetcherAdaptor(const AccessTokenFetcherAdaptor& other) = delete;
AccessTokenFetcherAdaptor& operator=(const AccessTokenFetcherAdaptor& other) =
delete;
~AccessTokenFetcherAdaptor() override = default;
private:
// Invokes |callback_| with (|error|, |access_token_info.token|).
void HandleTokenRequestCompletion(GoogleServiceAuthError error,
signin::AccessTokenInfo access_token_info);
ActiveAccountAccessTokenCallback callback_;
std::unique_ptr<signin::PrimaryAccountAccessTokenFetcher>
primary_account_access_token_fetcher_;
};
AccessTokenFetcherAdaptor::AccessTokenFetcherAdaptor(
const std::string& oauth_consumer_name,
signin::IdentityManager* identity_manager,
const signin::ScopeSet& scopes,
ActiveAccountAccessTokenCallback callback)
: callback_(std::move(callback)) {
primary_account_access_token_fetcher_ =
std::make_unique<signin::PrimaryAccountAccessTokenFetcher>(
oauth_consumer_name, identity_manager, scopes,
base::BindOnce(
&AccessTokenFetcherAdaptor::HandleTokenRequestCompletion,
// It is safe to use base::Unretained as
// |this| owns |access_token_fetcher_|.
base::Unretained(this)),
signin::PrimaryAccountAccessTokenFetcher::Mode::kImmediate,
signin::ConsentLevel::kSignin);
}
void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion(
GoogleServiceAuthError error,
signin::AccessTokenInfo access_token_info) {
primary_account_access_token_fetcher_.reset();
std::move(callback_).Run(error, access_token_info.token);
}
} // namespace
ProfileIdentityProvider::ProfileIdentityProvider(
signin::IdentityManager* identity_manager)
: identity_manager_(identity_manager) {
identity_manager_observation_.Observe(identity_manager_);
}
ProfileIdentityProvider::~ProfileIdentityProvider() = default;
CoreAccountId ProfileIdentityProvider::GetActiveAccountId() {
return identity_manager_->GetPrimaryAccountId(signin::ConsentLevel::kSignin);
}
bool ProfileIdentityProvider::IsActiveAccountWithRefreshToken() {
if (GetActiveAccountId().empty() ||
!identity_manager_->HasPrimaryAccountWithRefreshToken(
signin::ConsentLevel::kSignin)) {
return false;
}
return true;
}
std::unique_ptr<ActiveAccountAccessTokenFetcher>
ProfileIdentityProvider::FetchAccessToken(
const std::string& oauth_consumer_name,
const signin::ScopeSet& scopes,
ActiveAccountAccessTokenCallback callback) {
return std::make_unique<AccessTokenFetcherAdaptor>(
oauth_consumer_name, identity_manager_, scopes, std::move(callback));
}
void ProfileIdentityProvider::InvalidateAccessToken(
const signin::ScopeSet& scopes,
const std::string& access_token) {
identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes,
access_token);
}
void ProfileIdentityProvider::OnPrimaryAccountChanged(
const signin::PrimaryAccountChangeEvent& event_details) {
if (event_details.GetEventTypeFor(signin::ConsentLevel::kSignin) ==
signin::PrimaryAccountChangeEvent::Type::kNone) {
return;
}
const CoreAccountId& previous_account_id =
event_details.GetPreviousState().primary_account.account_id;
const CoreAccountId& current_account_id =
event_details.GetCurrentState().primary_account.account_id;
if (!previous_account_id.empty()) {
FireOnActiveAccountLogout();
}
if (!current_account_id.empty()) {
FireOnActiveAccountLogin();
}
}
void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount(
const CoreAccountInfo& account_info) {
ProcessRefreshTokenUpdateForAccount(account_info.account_id);
}
void ProfileIdentityProvider::OnIdentityManagerShutdown(
signin::IdentityManager* identity_manager) {
CHECK_EQ(identity_manager, identity_manager_);
identity_manager_observation_.Reset();
}
} // namespace invalidation
|