[invalidation] Port ProfileIdentityProvider to use IdentityManager

This CL ports ProfileIdentityProvider to talk to IdentityManager rather
than SigninManager and ProfileOAuth2TokenService. The conversion is
straightforward after a long line of preparatory work.

Bug: 809452
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: Ifa638f1b7a9c77089bb87fd7215e60041c762d3a
Reviewed-on: https://chromium-review.googlesource.com/1118163
Commit-Queue: Colin Blundell <[email protected]>
Reviewed-by: Pavel Yatsuk <[email protected]>
Cr-Commit-Position: refs/heads/master@{#574865}
diff --git a/components/invalidation/impl/profile_identity_provider.cc b/components/invalidation/impl/profile_identity_provider.cc
index 6fa8f96..fcfbf75 100644
--- a/components/invalidation/impl/profile_identity_provider.cc
+++ b/components/invalidation/impl/profile_identity_provider.cc
@@ -5,47 +5,73 @@
 #include "components/invalidation/impl/profile_identity_provider.h"
 
 #include "components/invalidation/public/active_account_access_token_fetcher_impl.h"
-#include "components/signin/core/browser/profile_oauth2_token_service.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 (|access_token|, |error|).
+  void HandleTokenRequestCompletion(GoogleServiceAuthError error,
+                                    std::string access_token);
+
+  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)));
+}
+
+void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion(
+    GoogleServiceAuthError error,
+    std::string access_token) {
+  access_token_fetcher_.reset();
+
+  std::move(callback_).Run(error, access_token);
+}
+
+}  // namespace
+
 ProfileIdentityProvider::ProfileIdentityProvider(
-    SigninManagerBase* signin_manager,
-    ProfileOAuth2TokenService* token_service)
-    : signin_manager_(signin_manager), token_service_(token_service) {
-  // TODO(blundell): Can |token_service_| ever actually be non-null?
-  if (token_service_)
-    token_service_->AddObserver(this);
-  signin_manager_->AddObserver(this);
+    identity::IdentityManager* identity_manager)
+    : identity_manager_(identity_manager) {
+  identity_manager_->AddObserver(this);
 }
 
 ProfileIdentityProvider::~ProfileIdentityProvider() {
-  // TODO(blundell): Can |token_service_| ever actually be non-null?
-  if (token_service_)
-    token_service_->RemoveObserver(this);
-
-  // In unittests |signin_manager_| is allowed to be null.
-  // TODO(809452): Eliminate this short-circuit when this class is converted to
-  // take in IdentityManager, at which point the tests can use
-  // IdentityTestEnvironment.
-  if (signin_manager_)
-    signin_manager_->RemoveObserver(this);
+  identity_manager_->RemoveObserver(this);
 }
 
 std::string ProfileIdentityProvider::GetActiveAccountId() {
-  // In unittests |signin_manager_| is allowed to be null.
-  // TODO(809452): Eliminate this short-circuit when this class is converted to
-  // take in IdentityManager, at which point the tests can use
-  // IdentityTestEnvironment.
-  if (!signin_manager_)
-    return std::string();
-
-  return signin_manager_->GetAuthenticatedAccountId();
+  return identity_manager_->GetPrimaryAccountInfo().account_id;
 }
 
 bool ProfileIdentityProvider::IsActiveAccountAvailable() {
-  if (GetActiveAccountId().empty() || !token_service_ ||
-      !token_service_->RefreshTokenIsAvailable(GetActiveAccountId()))
+  if (GetActiveAccountId().empty() || !identity_manager_ ||
+      !identity_manager_->HasAccountWithRefreshToken(GetActiveAccountId()))
     return false;
 
   return true;
@@ -56,37 +82,37 @@
     const std::string& oauth_consumer_name,
     const OAuth2TokenService::ScopeSet& scopes,
     ActiveAccountAccessTokenCallback callback) {
-  return std::make_unique<ActiveAccountAccessTokenFetcherImpl>(
-      GetActiveAccountId(), oauth_consumer_name, token_service_, scopes,
+  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) {
-  token_service_->InvalidateAccessToken(GetActiveAccountId(), scopes,
-                                        access_token);
+  identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes,
+                                                access_token);
 }
 
-void ProfileIdentityProvider::GoogleSigninSucceeded(
-    const std::string& account_id,
-    const std::string& username) {
+void ProfileIdentityProvider::OnPrimaryAccountSet(
+    const AccountInfo& primary_account_info) {
   FireOnActiveAccountLogin();
 }
 
-void ProfileIdentityProvider::GoogleSignedOut(const std::string& account_id,
-                                              const std::string& username) {
+void ProfileIdentityProvider::OnPrimaryAccountCleared(
+    const AccountInfo& previous_primary_account_info) {
   FireOnActiveAccountLogout();
 }
 
-void ProfileIdentityProvider::OnRefreshTokenAvailable(
-    const std::string& account_id) {
-  ProcessRefreshTokenUpdateForAccount(account_id);
+void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount(
+    const AccountInfo& account_info,
+    bool is_valid) {
+  ProcessRefreshTokenUpdateForAccount(account_info.account_id);
 }
 
-void ProfileIdentityProvider::OnRefreshTokenRevoked(
-    const std::string& account_id) {
-  ProcessRefreshTokenRemovalForAccount(account_id);
+void ProfileIdentityProvider::OnRefreshTokenRemovedForAccount(
+    const AccountInfo& account_info) {
+  ProcessRefreshTokenRemovalForAccount(account_info.account_id);
 }
 
 }  // namespace invalidation