blob: f0bd9dc9a93939adb27190c34c826076cbea7c5a [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// 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 "components/invalidation/public/active_account_access_token_fetcher_impl.h"
namespace invalidation {
namespace {
// ActiveAccountAccessTokenFetcher implementation that is backed by
// IdentityManager and wraps an AccessTokenFetcher internally.
class AccessTokenFetcherAdaptor : public ActiveAccountAccessTokenFetcher {
public:
AccessTokenFetcherAdaptor(const std::string& active_account_id,
const std::string& oauth_consumer_name,
identity::IdentityManager* identity_manager,
const OAuth2TokenService::ScopeSet& scopes,
ActiveAccountAccessTokenCallback callback);
~AccessTokenFetcherAdaptor() override = default;
private:
// Invokes |callback_| with (|error|, |access_token_info.token|).
void HandleTokenRequestCompletion(
GoogleServiceAuthError error,
identity::AccessTokenInfo access_token_info);
ActiveAccountAccessTokenCallback callback_;
std::unique_ptr<identity::AccessTokenFetcher> access_token_fetcher_;
DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcherAdaptor);
};
AccessTokenFetcherAdaptor::AccessTokenFetcherAdaptor(
const std::string& active_account_id,
const std::string& oauth_consumer_name,
identity::IdentityManager* identity_manager,
const OAuth2TokenService::ScopeSet& scopes,
ActiveAccountAccessTokenCallback callback)
: callback_(std::move(callback)) {
access_token_fetcher_ = identity_manager->CreateAccessTokenFetcherForAccount(
active_account_id, oauth_consumer_name, scopes,
base::BindOnce(&AccessTokenFetcherAdaptor::HandleTokenRequestCompletion,
base::Unretained(this)),
identity::AccessTokenFetcher::Mode::kImmediate);
}
void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion(
GoogleServiceAuthError error,
identity::AccessTokenInfo access_token_info) {
access_token_fetcher_.reset();
std::move(callback_).Run(error, access_token_info.token);
}
} // namespace
ProfileIdentityProvider::ProfileIdentityProvider(
identity::IdentityManager* identity_manager)
: identity_manager_(identity_manager) {
identity_manager_->AddObserver(this);
}
ProfileIdentityProvider::~ProfileIdentityProvider() {
identity_manager_->RemoveObserver(this);
}
std::string ProfileIdentityProvider::GetActiveAccountId() {
return identity_manager_->GetPrimaryAccountInfo().account_id;
}
bool ProfileIdentityProvider::IsActiveAccountAvailable() {
if (GetActiveAccountId().empty() || !identity_manager_ ||
!identity_manager_->HasAccountWithRefreshToken(GetActiveAccountId()))
return false;
return true;
}
std::unique_ptr<ActiveAccountAccessTokenFetcher>
ProfileIdentityProvider::FetchAccessToken(
const std::string& oauth_consumer_name,
const OAuth2TokenService::ScopeSet& scopes,
ActiveAccountAccessTokenCallback callback) {
return std::make_unique<AccessTokenFetcherAdaptor>(
GetActiveAccountId(), oauth_consumer_name, identity_manager_, scopes,
std::move(callback));
}
void ProfileIdentityProvider::InvalidateAccessToken(
const OAuth2TokenService::ScopeSet& scopes,
const std::string& access_token) {
identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes,
access_token);
}
void ProfileIdentityProvider::OnPrimaryAccountSet(
const AccountInfo& primary_account_info) {
FireOnActiveAccountLogin();
}
void ProfileIdentityProvider::OnPrimaryAccountCleared(
const AccountInfo& previous_primary_account_info) {
FireOnActiveAccountLogout();
}
void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount(
const AccountInfo& account_info,
bool is_valid) {
ProcessRefreshTokenUpdateForAccount(account_info.account_id);
}
void ProfileIdentityProvider::OnRefreshTokenRemovedForAccount(
const AccountInfo& account_info) {
ProcessRefreshTokenRemovalForAccount(account_info.account_id);
}
} // namespace invalidation