Commit 70a72281 authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

Add creation of PrimaryAccountAccessTokenFetcher via IdentityManager

This CL adds an interface on IdentityManager to create a
PrimaryAccountAccessTokenFetcher instance. In this way, clients of
IdentityManager can create PrimaryAccountAccessTokenFetchers without
needing to know about the core signin classes on which it depends. This
interface will facilitate moving clients of
PrimaryAccountAccessTokenFetcher to depend on IdentityManager rather
than depending on these core signin classes directly.

Bug: 654990
Change-Id: I15217dabf60a8dada17ce54f76406c011d011f5c
Reviewed-on: https://chromium-review.googlesource.com/832647Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Commit-Queue: Colin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#525303}
parent 7a534c36
......@@ -6,6 +6,7 @@
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/keyed_service.h"
......@@ -19,7 +20,9 @@
class IdentityManagerHolder : public KeyedService {
public:
explicit IdentityManagerHolder(Profile* profile)
: identity_manager_(SigninManagerFactory::GetForProfile(profile)) {}
: identity_manager_(
SigninManagerFactory::GetForProfile(profile),
ProfileOAuth2TokenServiceFactory::GetForProfile(profile)) {}
identity::IdentityManager* identity_manager() { return &identity_manager_; }
......@@ -31,6 +34,7 @@ IdentityManagerFactory::IdentityManagerFactory()
: BrowserContextKeyedServiceFactory(
"IdentityManager",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
DependsOn(SigninManagerFactory::GetInstance());
}
......
......@@ -3,12 +3,15 @@
// found in the LICENSE file.
#include "services/identity/public/cpp/identity_manager.h"
#include "base/memory/ptr_util.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
namespace identity {
IdentityManager::IdentityManager(SigninManagerBase* signin_manager)
: signin_manager_(signin_manager) {
IdentityManager::IdentityManager(SigninManagerBase* signin_manager,
ProfileOAuth2TokenService* token_service)
: signin_manager_(signin_manager), token_service_(token_service) {
#if defined(OS_CHROMEOS)
// On ChromeOS, the authenticated account is set very early in startup and
// never changed. No client should be accessing the IdentityManager before the
......@@ -27,6 +30,16 @@ AccountInfo IdentityManager::GetPrimaryAccountInfo() {
return primary_account_info_;
}
std::unique_ptr<PrimaryAccountAccessTokenFetcher>
IdentityManager::CreateAccessTokenFetcherForPrimaryAccount(
const std::string& oauth_consumer_name,
const OAuth2TokenService::ScopeSet& scopes,
PrimaryAccountAccessTokenFetcher::TokenCallback callback) {
return base::MakeUnique<PrimaryAccountAccessTokenFetcher>(
oauth_consumer_name, signin_manager_, token_service_, scopes,
std::move(callback));
}
void IdentityManager::AddObserver(Observer* observer) {
observer_list_.AddObserver(observer);
}
......
......@@ -8,6 +8,9 @@
#include "base/observer_list.h"
#include "components/signin/core/browser/account_info.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "services/identity/public/cpp/primary_account_access_token_fetcher.h"
class ProfileOAuth2TokenService;
namespace identity {
......@@ -34,13 +37,21 @@ class IdentityManager : public SigninManagerBase::Observer {
virtual ~Observer() {}
};
explicit IdentityManager(SigninManagerBase* signin_manager);
IdentityManager(SigninManagerBase* signin_manager,
ProfileOAuth2TokenService* token_service);
~IdentityManager() override;
// Provides access to the latest cached information of the user's primary
// account.
AccountInfo GetPrimaryAccountInfo();
// Creates a PrimaryAccountAccessTokenFetcher given the passed-in information.
std::unique_ptr<PrimaryAccountAccessTokenFetcher>
CreateAccessTokenFetcherForPrimaryAccount(
const std::string& oauth_consumer_name,
const OAuth2TokenService::ScopeSet& scopes,
PrimaryAccountAccessTokenFetcher::TokenCallback callback);
// Methods to register or remove observers.
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
......@@ -60,11 +71,12 @@ class IdentityManager : public SigninManagerBase::Observer {
// receiving this call asynchronously from the Identity Service.
void HandleGoogleSignedOut(const AccountInfo& account_info);
// Backing SigninManagerBase. NOTE: We strive to limit synchronous access to
// this class in the IdentityManager implementation, as all such synchronous
// access will become impossible when IdentityManager is backed by the
// Identity Service.
// Backing signin classes. NOTE: We strive to limit synchronous access to
// these classes in the IdentityManager implementation, as all such
// synchronous access will become impossible when IdentityManager is backed by
// the Identity Service.
SigninManagerBase* signin_manager_;
ProfileOAuth2TokenService* token_service_;
// The latest (cached) value of the primary account.
AccountInfo primary_account_info_;
......
......@@ -97,7 +97,8 @@ class IdentityManagerTest : public testing::Test {
signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
identity_manager_.reset(new IdentityManager(&signin_manager_));
identity_manager_.reset(
new IdentityManager(&signin_manager_, &token_service_));
identity_manager_observer_.reset(
new TestIdentityManagerObserver(identity_manager_.get()));
}
......@@ -187,4 +188,15 @@ TEST_F(IdentityManagerTest, PrimaryAccountInfoAfterSigninAndSignout) {
}
#endif // !defined(OS_CHROMEOS)
TEST_F(IdentityManagerTest, CreateAccessTokenFetcherForPrimaryAccount) {
std::set<std::string> scopes{"scope"};
PrimaryAccountAccessTokenFetcher::TokenCallback callback =
base::BindOnce([](const GoogleServiceAuthError& error,
const std::string& access_token) {});
std::unique_ptr<PrimaryAccountAccessTokenFetcher> token_fetcher =
identity_manager()->CreateAccessTokenFetcherForPrimaryAccount(
"dummy_consumer", scopes, std::move(callback));
EXPECT_TRUE(token_fetcher);
}
} // namespace identity
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment