Update IdentityManager to own GaiaCookieManagerService.

With this change, GaiaCookieManagerService is now constructed only when
creating an IdentityManager, eliminating the need for
GaiaCookieManagerService factories.

Bug: 939372
Change-Id: I14463bc6b71b8bf135cceba0159d440fe7872dea
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1532226
Commit-Queue: Lowell Manners <lowell@chromium.org>
Reviewed-by: Colin Blundell <blundell@chromium.org>
Reviewed-by: David Roger <droger@chromium.org>
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#644232}
diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn
index 6563e87..d7dd6965 100644
--- a/chrome/browser/BUILD.gn
+++ b/chrome/browser/BUILD.gn
@@ -1533,8 +1533,6 @@
     "signin/chrome_signin_proxying_url_loader_factory.h",
     "signin/chrome_signin_url_loader_throttle.cc",
     "signin/chrome_signin_url_loader_throttle.h",
-    "signin/gaia_cookie_manager_service_factory.cc",
-    "signin/gaia_cookie_manager_service_factory.h",
     "signin/header_modification_delegate.h",
     "signin/header_modification_delegate_impl.cc",
     "signin/header_modification_delegate_impl.h",
diff --git a/chrome/browser/chromeos/login/signin/oauth2_login_manager.cc b/chrome/browser/chromeos/login/signin/oauth2_login_manager.cc
index 0f0b919..1e701e0 100644
--- a/chrome/browser/chromeos/login/signin/oauth2_login_manager.cc
+++ b/chrome/browser/chromeos/login/signin/oauth2_login_manager.cc
@@ -11,7 +11,6 @@
 #include "base/metrics/histogram_macros.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/signin/chrome_device_id_helper.h"
-#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
 #include "chrome/browser/signin/identity_manager_factory.h"
 #include "chromeos/constants/chromeos_switches.h"
 #include "components/account_id/account_id.h"
diff --git a/chrome/browser/signin/gaia_cookie_manager_service_factory.cc b/chrome/browser/signin/gaia_cookie_manager_service_factory.cc
deleted file mode 100644
index b67e07a..0000000
--- a/chrome/browser/signin/gaia_cookie_manager_service_factory.cc
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright 2013 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 "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
-
-#include "chrome/browser/profiles/profile.h"
-#include "chrome/browser/signin/chrome_signin_client_factory.h"
-#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
-#include "components/keyed_service/content/browser_context_dependency_manager.h"
-#include "components/signin/core/browser/gaia_cookie_manager_service.h"
-#include "components/signin/core/browser/profile_oauth2_token_service.h"
-
-GaiaCookieManagerServiceFactory::GaiaCookieManagerServiceFactory()
-    : BrowserContextKeyedServiceFactory(
-        "GaiaCookieManagerService",
-        BrowserContextDependencyManager::GetInstance()) {
-  DependsOn(ChromeSigninClientFactory::GetInstance());
-  DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
-}
-
-GaiaCookieManagerServiceFactory::~GaiaCookieManagerServiceFactory() {}
-
-// static
-GaiaCookieManagerService* GaiaCookieManagerServiceFactory::GetForProfile(
-    Profile* profile) {
-  return static_cast<GaiaCookieManagerService*>(
-      GetInstance()->GetServiceForBrowserContext(profile, true));
-}
-
-// static
-GaiaCookieManagerServiceFactory*
-GaiaCookieManagerServiceFactory::GetInstance() {
-  return base::Singleton<GaiaCookieManagerServiceFactory>::get();
-}
-
-KeyedService* GaiaCookieManagerServiceFactory::BuildServiceInstanceFor(
-    content::BrowserContext* context) const {
-  Profile* profile = Profile::FromBrowserContext(context);
-  GaiaCookieManagerService* cookie_service = new GaiaCookieManagerService(
-      ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
-      ChromeSigninClientFactory::GetForProfile(profile));
-  return cookie_service;
-}
-
-void GaiaCookieManagerServiceFactory::RegisterProfilePrefs(
-    user_prefs::PrefRegistrySyncable* registry) {
-}
diff --git a/chrome/browser/signin/gaia_cookie_manager_service_factory.h b/chrome/browser/signin/gaia_cookie_manager_service_factory.h
deleted file mode 100644
index e8b83db..0000000
--- a/chrome/browser/signin/gaia_cookie_manager_service_factory.h
+++ /dev/null
@@ -1,40 +0,0 @@
-// Copyright 2013 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.
-
-#ifndef CHROME_BROWSER_SIGNIN_GAIA_COOKIE_MANAGER_SERVICE_FACTORY_H_
-#define CHROME_BROWSER_SIGNIN_GAIA_COOKIE_MANAGER_SERVICE_FACTORY_H_
-
-#include "base/memory/singleton.h"
-#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
-
-class GaiaCookieManagerService;
-class Profile;
-
-// Singleton that owns the GaiaCookieManagerService(s) and associates them with
-// Profiles. Listens for the Profile's destruction notification and cleans up.
-class GaiaCookieManagerServiceFactory :
-    public BrowserContextKeyedServiceFactory {
- public:
-  // Returns the instance of GaiaCookieManagerService associated with this
-  // profile (creating one if none exists). Returns NULL if this profile cannot
-  // have an GaiaCookieManagerService (for example, if |profile| is incognito).
-  static GaiaCookieManagerService* GetForProfile(Profile* profile);
-
-  // Returns an instance of the factory singleton.
-  static GaiaCookieManagerServiceFactory* GetInstance();
-
- private:
-  friend struct base::DefaultSingletonTraits<GaiaCookieManagerServiceFactory>;
-
-  GaiaCookieManagerServiceFactory();
-  ~GaiaCookieManagerServiceFactory() override;
-
-  // BrowserContextKeyedServiceFactory:
-  KeyedService* BuildServiceInstanceFor(
-      content::BrowserContext* profile) const override;
-  void RegisterProfilePrefs(
-      user_prefs::PrefRegistrySyncable* registry) override;
-};
-
-#endif  // CHROME_BROWSER_SIGNIN_GAIA_COOKIE_MANAGER_SERVICE_FACTORY_H_
diff --git a/chrome/browser/signin/identity_manager_factory.cc b/chrome/browser/signin/identity_manager_factory.cc
index 271777e..e6d8ce3 100644
--- a/chrome/browser/signin/identity_manager_factory.cc
+++ b/chrome/browser/signin/identity_manager_factory.cc
@@ -13,7 +13,6 @@
 #include "chrome/browser/signin/account_fetcher_service_factory.h"
 #include "chrome/browser/signin/account_tracker_service_factory.h"
 #include "chrome/browser/signin/chrome_signin_client_factory.h"
-#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
 #include "components/keyed_service/content/browser_context_dependency_manager.h"
 #include "components/keyed_service/core/keyed_service.h"
@@ -73,7 +72,9 @@
 #endif
 }
 
-std::unique_ptr<ConcreteSigninManager> BuildSigninManager(Profile* profile) {
+std::unique_ptr<ConcreteSigninManager> BuildSigninManager(
+    Profile* profile,
+    GaiaCookieManagerService* gaia_cookie_manager_service) {
   std::unique_ptr<ConcreteSigninManager> signin_manager;
   SigninClient* client =
       ChromeSigninClientFactory::GetInstance()->GetForProfile(profile);
@@ -85,7 +86,7 @@
   signin_manager = std::make_unique<ConcreteSigninManager>(
       client, ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
       AccountTrackerServiceFactory::GetForProfile(profile),
-      GaiaCookieManagerServiceFactory::GetForProfile(profile),
+      gaia_cookie_manager_service,
       AccountConsistencyModeManager::GetMethodForProfile(profile));
 #endif
   signin_manager->Initialize(g_browser_process->local_state());
@@ -105,22 +106,26 @@
  public:
   explicit IdentityManagerWrapper(
       Profile* profile,
+      std::unique_ptr<GaiaCookieManagerService> gaia_cookie_manager_service,
       std::unique_ptr<SigninManagerBase> signin_manager,
       std::unique_ptr<identity::PrimaryAccountMutator> primary_account_mutator,
-      std::unique_ptr<identity::AccountsMutator> accounts_mutator)
+      std::unique_ptr<identity::AccountsMutator> accounts_mutator,
+      std::unique_ptr<identity::AccountsCookieMutatorImpl>
+          accounts_cookie_mutator,
+      std::unique_ptr<identity::DiagnosticsProviderImpl> diagnostics_provider)
       : identity::IdentityManager(
+            std::move(gaia_cookie_manager_service),
             std::move(signin_manager),
             ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
             AccountFetcherServiceFactory::GetForProfile(profile),
             AccountTrackerServiceFactory::GetForProfile(profile),
-            GaiaCookieManagerServiceFactory::GetForProfile(profile),
             std::move(primary_account_mutator),
             std::move(accounts_mutator),
-            std::make_unique<identity::AccountsCookieMutatorImpl>(
-                GaiaCookieManagerServiceFactory::GetForProfile(profile)),
-            std::make_unique<identity::DiagnosticsProviderImpl>(
-                ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
-                GaiaCookieManagerServiceFactory::GetForProfile(profile))) {}
+            std::move(accounts_cookie_mutator),
+            std::move(diagnostics_provider)) {}
+
+  // KeyedService overrides.
+  void Shutdown() override { IdentityManager::Shutdown(); }
 };
 
 void IdentityManagerFactory::RegisterProfilePrefs(
@@ -135,7 +140,6 @@
   DependsOn(AccountFetcherServiceFactory::GetInstance());
   DependsOn(AccountTrackerServiceFactory::GetInstance());
   DependsOn(ChromeSigninClientFactory::GetInstance());
-  DependsOn(GaiaCookieManagerServiceFactory::GetInstance());
   DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
 }
 
@@ -166,7 +170,6 @@
   IdentityManagerFactory::GetInstance();
   AccountTrackerServiceFactory::GetInstance();
   ChromeSigninClientFactory::GetInstance();
-  GaiaCookieManagerServiceFactory::GetInstance();
   ProfileOAuth2TokenServiceFactory::GetInstance();
 }
 
@@ -183,16 +186,28 @@
   Profile* profile = Profile::FromBrowserContext(context);
 
   // Construct the dependencies that IdentityManager will own.
+  auto gaia_cookie_manager_service = std::make_unique<GaiaCookieManagerService>(
+      ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
+      ChromeSigninClientFactory::GetForProfile(profile));
   std::unique_ptr<ConcreteSigninManager> signin_manager =
-      BuildSigninManager(profile);
+      BuildSigninManager(profile, gaia_cookie_manager_service.get());
   std::unique_ptr<identity::PrimaryAccountMutator> primary_account_mutator =
       BuildPrimaryAccountMutator(profile, signin_manager.get());
   std::unique_ptr<identity::AccountsMutator> accounts_mutator =
       BuildAccountsMutator(profile, signin_manager.get());
+  auto accounts_cookie_mutator =
+      std::make_unique<identity::AccountsCookieMutatorImpl>(
+          gaia_cookie_manager_service.get());
+  auto diagnostics_provider =
+      std::make_unique<identity::DiagnosticsProviderImpl>(
+          ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
+          gaia_cookie_manager_service.get());
 
   auto identity_manager = std::make_unique<IdentityManagerWrapper>(
-      profile, std::move(signin_manager), std::move(primary_account_mutator),
-      std::move(accounts_mutator));
+      profile, std::move(gaia_cookie_manager_service),
+      std::move(signin_manager), std::move(primary_account_mutator),
+      std::move(accounts_mutator), std::move(accounts_cookie_mutator),
+      std::move(diagnostics_provider));
   for (Observer& observer : observer_list_)
     observer.IdentityManagerCreated(identity_manager.get());
   return identity_manager.release();
diff --git a/chrome/browser/signin/identity_test_environment_profile_adaptor.cc b/chrome/browser/signin/identity_test_environment_profile_adaptor.cc
index d21c246..568f4f0f 100644
--- a/chrome/browser/signin/identity_test_environment_profile_adaptor.cc
+++ b/chrome/browser/signin/identity_test_environment_profile_adaptor.cc
@@ -8,7 +8,6 @@
 #include "chrome/browser/signin/account_fetcher_service_factory.h"
 #include "chrome/browser/signin/account_tracker_service_factory.h"
 #include "chrome/browser/signin/chrome_signin_client_factory.h"
-#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
 #include "chrome/browser/signin/identity_manager_factory.h"
 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
@@ -91,5 +90,4 @@
           AccountFetcherServiceFactory::GetForProfile(profile),
           static_cast<FakeProfileOAuth2TokenService*>(
               ProfileOAuth2TokenServiceFactory::GetForProfile(profile)),
-          GaiaCookieManagerServiceFactory::GetForProfile(profile),
           IdentityManagerFactory::GetForProfile(profile)) {}
diff --git a/components/signin/core/browser/gaia_cookie_manager_service.h b/components/signin/core/browser/gaia_cookie_manager_service.h
index ee2a8c5..336606b 100644
--- a/components/signin/core/browser/gaia_cookie_manager_service.h
+++ b/components/signin/core/browser/gaia_cookie_manager_service.h
@@ -69,8 +69,7 @@
 // Also checks the External CC result to ensure no services that consume the
 // GAIA cookie are blocked (such as youtube). This is executed once for the
 // lifetime of this object, when the first call is made to AddAccountToCookie.
-class GaiaCookieManagerService : public KeyedService,
-                                 public GaiaAuthConsumer,
+class GaiaCookieManagerService : public GaiaAuthConsumer,
                                  public network::mojom::CookieChangeListener,
                                  public OAuth2TokenService::Consumer {
  public:
@@ -245,7 +244,7 @@
   ~GaiaCookieManagerService() override;
 
   void InitCookieListener();
-  void Shutdown() override;
+  void Shutdown();
 
   void AddAccountToCookie(
       const std::string& account_id,
diff --git a/components/signin/core/browser/signin_manager_unittest.cc b/components/signin/core/browser/signin_manager_unittest.cc
index 16e61c1..b5f7b96 100644
--- a/components/signin/core/browser/signin_manager_unittest.cc
+++ b/components/signin/core/browser/signin_manager_unittest.cc
@@ -89,8 +89,8 @@
     }
     token_service_.Shutdown();
     test_signin_client_.Shutdown();
-    account_tracker_.Shutdown();
     cookie_manager_service_.Shutdown();
+    account_tracker_.Shutdown();
     account_fetcher_.Shutdown();
   }
 
diff --git a/ios/chrome/browser/signin/BUILD.gn b/ios/chrome/browser/signin/BUILD.gn
index e4e0eea..920854f 100644
--- a/ios/chrome/browser/signin/BUILD.gn
+++ b/ios/chrome/browser/signin/BUILD.gn
@@ -32,8 +32,6 @@
     "gaia_auth_fetcher_ios_ns_url_session_bridge.mm",
     "gaia_auth_fetcher_ios_wk_webview_bridge.h",
     "gaia_auth_fetcher_ios_wk_webview_bridge.mm",
-    "gaia_cookie_manager_service_factory.cc",
-    "gaia_cookie_manager_service_factory.h",
     "identity_manager_factory.cc",
     "identity_manager_factory.h",
     "identity_manager_factory_observer.h",
diff --git a/ios/chrome/browser/signin/gaia_cookie_manager_service_factory.cc b/ios/chrome/browser/signin/gaia_cookie_manager_service_factory.cc
deleted file mode 100644
index 93e7c18..0000000
--- a/ios/chrome/browser/signin/gaia_cookie_manager_service_factory.cc
+++ /dev/null
@@ -1,53 +0,0 @@
-// Copyright 2015 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 "ios/chrome/browser/signin/gaia_cookie_manager_service_factory.h"
-
-#include "base/no_destructor.h"
-#include "components/keyed_service/core/service_access_type.h"
-#include "components/keyed_service/ios/browser_state_dependency_manager.h"
-#include "components/signin/core/browser/gaia_cookie_manager_service.h"
-#include "components/signin/core/browser/profile_oauth2_token_service.h"
-#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
-#include "ios/chrome/browser/signin/profile_oauth2_token_service_factory.h"
-#include "ios/chrome/browser/signin/signin_client_factory.h"
-
-namespace ios {
-
-GaiaCookieManagerServiceFactory::GaiaCookieManagerServiceFactory()
-    : BrowserStateKeyedServiceFactory(
-          "GaiaCookieManagerService",
-          BrowserStateDependencyManager::GetInstance()) {
-  DependsOn(SigninClientFactory::GetInstance());
-  DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
-}
-
-GaiaCookieManagerServiceFactory::~GaiaCookieManagerServiceFactory() {}
-
-// static
-GaiaCookieManagerService* GaiaCookieManagerServiceFactory::GetForBrowserState(
-    ios::ChromeBrowserState* browser_state) {
-  return static_cast<GaiaCookieManagerService*>(
-      GetInstance()->GetServiceForBrowserState(browser_state, true));
-}
-
-// static
-GaiaCookieManagerServiceFactory*
-GaiaCookieManagerServiceFactory::GetInstance() {
-  static base::NoDestructor<GaiaCookieManagerServiceFactory> instance;
-  return instance.get();
-}
-
-std::unique_ptr<KeyedService>
-GaiaCookieManagerServiceFactory::BuildServiceInstanceFor(
-    web::BrowserState* context) const {
-  ios::ChromeBrowserState* chrome_browser_state =
-      ios::ChromeBrowserState::FromBrowserState(context);
-  return std::make_unique<GaiaCookieManagerService>(
-      ProfileOAuth2TokenServiceFactory::GetForBrowserState(
-          chrome_browser_state),
-      SigninClientFactory::GetForBrowserState(chrome_browser_state));
-}
-
-}  // namespace ios
diff --git a/ios/chrome/browser/signin/gaia_cookie_manager_service_factory.h b/ios/chrome/browser/signin/gaia_cookie_manager_service_factory.h
deleted file mode 100644
index e6795a9..0000000
--- a/ios/chrome/browser/signin/gaia_cookie_manager_service_factory.h
+++ /dev/null
@@ -1,49 +0,0 @@
-// Copyright 2015 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.
-
-#ifndef IOS_CHROME_BROWSER_SIGNIN_GAIA_COOKIE_MANAGER_SERVICE_FACTORY_H_
-#define IOS_CHROME_BROWSER_SIGNIN_GAIA_COOKIE_MANAGER_SERVICE_FACTORY_H_
-
-#include <memory>
-
-#include "base/macros.h"
-#include "base/no_destructor.h"
-#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
-
-class GaiaCookieManagerService;
-
-namespace ios {
-
-class ChromeBrowserState;
-
-// Singleton that creates the GaiaCookieManagerService(s) and associates those
-// services with browser states.
-class GaiaCookieManagerServiceFactory : public BrowserStateKeyedServiceFactory {
- public:
-  // Returns the instance of GaiaCookieManagerService associated with this
-  // browser state (creating one if none exists). Returns null if this browser
-  // state cannot have an GaiaCookieManagerService (for example, if it is
-  // incognito).
-  static GaiaCookieManagerService* GetForBrowserState(
-      ios::ChromeBrowserState* browser_state);
-
-  // Returns an instance of the factory singleton.
-  static GaiaCookieManagerServiceFactory* GetInstance();
-
- private:
-  friend class base::NoDestructor<GaiaCookieManagerServiceFactory>;
-
-  GaiaCookieManagerServiceFactory();
-  ~GaiaCookieManagerServiceFactory() override;
-
-  // BrowserStateKeyedServiceFactory:
-  std::unique_ptr<KeyedService> BuildServiceInstanceFor(
-      web::BrowserState* context) const override;
-
-  DISALLOW_COPY_AND_ASSIGN(GaiaCookieManagerServiceFactory);
-};
-
-}  // namespace ios
-
-#endif  // IOS_CHROME_BROWSER_SIGNIN_GAIA_COOKIE_MANAGER_SERVICE_FACTORY_H_
diff --git a/ios/chrome/browser/signin/identity_manager_factory.cc b/ios/chrome/browser/signin/identity_manager_factory.cc
index b1e1258..ee37a3ad 100644
--- a/ios/chrome/browser/signin/identity_manager_factory.cc
+++ b/ios/chrome/browser/signin/identity_manager_factory.cc
@@ -16,7 +16,6 @@
 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
 #include "ios/chrome/browser/signin/account_fetcher_service_factory.h"
 #include "ios/chrome/browser/signin/account_tracker_service_factory.h"
-#include "ios/chrome/browser/signin/gaia_cookie_manager_service_factory.h"
 #include "ios/chrome/browser/signin/identity_manager_factory_observer.h"
 #include "ios/chrome/browser/signin/profile_oauth2_token_service_factory.h"
 #include "ios/chrome/browser/signin/signin_client_factory.h"
@@ -28,16 +27,15 @@
 
 namespace {
 std::unique_ptr<SigninManager> BuildSigninManager(
-    ios::ChromeBrowserState* chrome_browser_state) {
+    ios::ChromeBrowserState* chrome_browser_state,
+    GaiaCookieManagerService* gaia_cookie_manager_service) {
   std::unique_ptr<SigninManager> service = std::make_unique<SigninManager>(
       SigninClientFactory::GetForBrowserState(chrome_browser_state),
       ProfileOAuth2TokenServiceFactory::GetForBrowserState(
           chrome_browser_state),
       ios::AccountTrackerServiceFactory::GetForBrowserState(
           chrome_browser_state),
-      ios::GaiaCookieManagerServiceFactory::GetForBrowserState(
-          chrome_browser_state),
-      signin::AccountConsistencyMethod::kMirror);
+      gaia_cookie_manager_service, signin::AccountConsistencyMethod::kMirror);
   service->Initialize(GetApplicationContext()->GetLocalState());
   return service;
 }
@@ -54,28 +52,28 @@
                                public identity::IdentityManager {
  public:
   IdentityManagerWrapper(
+      std::unique_ptr<GaiaCookieManagerService> gaia_cookie_manager_service,
       std::unique_ptr<SigninManagerBase> signin_manager,
       std::unique_ptr<identity::PrimaryAccountMutator> primary_account_mutator,
+      std::unique_ptr<identity::AccountsCookieMutatorImpl>
+          accounts_cookie_mutator,
+      std::unique_ptr<identity::DiagnosticsProviderImpl> diagnostics_provider,
       ios::ChromeBrowserState* browser_state)
       : identity::IdentityManager(
+            std::move(gaia_cookie_manager_service),
             std::move(signin_manager),
             ProfileOAuth2TokenServiceFactory::GetForBrowserState(browser_state),
             ios::AccountFetcherServiceFactory::GetForBrowserState(
                 browser_state),
             ios::AccountTrackerServiceFactory::GetForBrowserState(
                 browser_state),
-            ios::GaiaCookieManagerServiceFactory::GetForBrowserState(
-                browser_state),
             std::move(primary_account_mutator),
             /*accounts_mutator=*/nullptr,
-            std::make_unique<identity::AccountsCookieMutatorImpl>(
-                ios::GaiaCookieManagerServiceFactory::GetForBrowserState(
-                    browser_state)),
-            std::make_unique<identity::DiagnosticsProviderImpl>(
-                ProfileOAuth2TokenServiceFactory::GetForBrowserState(
-                    browser_state),
-                ios::GaiaCookieManagerServiceFactory::GetForBrowserState(
-                    browser_state))) {}
+            std::move(accounts_cookie_mutator),
+            std::move(diagnostics_provider)) {}
+
+  // KeyedService overrides.
+  void Shutdown() override { IdentityManager::Shutdown(); }
 };
 
 void IdentityManagerFactory::RegisterBrowserStatePrefs(
@@ -89,7 +87,6 @@
           BrowserStateDependencyManager::GetInstance()) {
   DependsOn(ios::AccountFetcherServiceFactory::GetInstance());
   DependsOn(ios::AccountTrackerServiceFactory::GetInstance());
-  DependsOn(ios::GaiaCookieManagerServiceFactory::GetInstance());
   DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
   DependsOn(SigninClientFactory::GetInstance());
 }
@@ -120,7 +117,6 @@
 void IdentityManagerFactory::EnsureFactoryAndDependeeFactoriesBuilt() {
   IdentityManagerFactory::GetInstance();
   ios::AccountTrackerServiceFactory::GetInstance();
-  ios::GaiaCookieManagerServiceFactory::GetInstance();
   ProfileOAuth2TokenServiceFactory::GetInstance();
   SigninClientFactory::GetInstance();
 }
@@ -136,21 +132,31 @@
 }
 
 std::unique_ptr<KeyedService> IdentityManagerFactory::BuildServiceInstanceFor(
-    web::BrowserState* browser_state) const {
-  ios::ChromeBrowserState* chrome_browser_state =
-      ios::ChromeBrowserState::FromBrowserState(browser_state);
+    web::BrowserState* context) const {
+  ios::ChromeBrowserState* browser_state =
+      ios::ChromeBrowserState::FromBrowserState(context);
 
   // Construct the dependencies that IdentityManager will own.
+  auto gaia_cookie_manager_service = std::make_unique<GaiaCookieManagerService>(
+      ProfileOAuth2TokenServiceFactory::GetForBrowserState(browser_state),
+      SigninClientFactory::GetForBrowserState(browser_state));
   std::unique_ptr<SigninManager> signin_manager =
-      BuildSigninManager(chrome_browser_state);
+      BuildSigninManager(browser_state, gaia_cookie_manager_service.get());
   auto primary_account_mutator =
       std::make_unique<identity::PrimaryAccountMutatorImpl>(
-          ios::AccountTrackerServiceFactory::GetForBrowserState(
-              chrome_browser_state),
+          ios::AccountTrackerServiceFactory::GetForBrowserState(browser_state),
           signin_manager.get());
+  auto accounts_cookie_mutator =
+      std::make_unique<identity::AccountsCookieMutatorImpl>(
+          gaia_cookie_manager_service.get());
+  auto diagnostics_provider =
+      std::make_unique<identity::DiagnosticsProviderImpl>(
+          ProfileOAuth2TokenServiceFactory::GetForBrowserState(browser_state),
+          gaia_cookie_manager_service.get());
   auto identity_manager = std::make_unique<IdentityManagerWrapper>(
-      std::move(signin_manager), std::move(primary_account_mutator),
-      chrome_browser_state);
+      std::move(gaia_cookie_manager_service), std::move(signin_manager),
+      std::move(primary_account_mutator), std::move(accounts_cookie_mutator),
+      std::move(diagnostics_provider), browser_state);
 
   for (auto& observer : observer_list_)
     observer.IdentityManagerCreated(identity_manager.get());
diff --git a/ios/chrome/browser/signin/identity_manager_factory.h b/ios/chrome/browser/signin/identity_manager_factory.h
index db94886..e5ee1d3 100644
--- a/ios/chrome/browser/signin/identity_manager_factory.h
+++ b/ios/chrome/browser/signin/identity_manager_factory.h
@@ -55,7 +55,7 @@
 
   // BrowserStateKeyedServiceFactory:
   std::unique_ptr<KeyedService> BuildServiceInstanceFor(
-      web::BrowserState* browser_state) const override;
+      web::BrowserState* context) const override;
   void BrowserStateShutdown(web::BrowserState* context) override;
   void RegisterBrowserStatePrefs(
       user_prefs::PrefRegistrySyncable* registry) override;
diff --git a/ios/chrome/browser/signin/identity_test_environment_chrome_browser_state_adaptor.cc b/ios/chrome/browser/signin/identity_test_environment_chrome_browser_state_adaptor.cc
index 49edd8e..ec64526 100644
--- a/ios/chrome/browser/signin/identity_test_environment_chrome_browser_state_adaptor.cc
+++ b/ios/chrome/browser/signin/identity_test_environment_chrome_browser_state_adaptor.cc
@@ -13,7 +13,6 @@
 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
 #include "ios/chrome/browser/signin/account_fetcher_service_factory.h"
 #include "ios/chrome/browser/signin/account_tracker_service_factory.h"
-#include "ios/chrome/browser/signin/gaia_cookie_manager_service_factory.h"
 #include "ios/chrome/browser/signin/identity_manager_factory.h"
 #include "ios/chrome/browser/signin/profile_oauth2_token_service_factory.h"
 #include "ios/chrome/browser/signin/profile_oauth2_token_service_ios_provider_impl.h"
@@ -127,6 +126,4 @@
           static_cast<FakeProfileOAuth2TokenService*>(
               ProfileOAuth2TokenServiceFactory::GetForBrowserState(
                   browser_state)),
-          ios::GaiaCookieManagerServiceFactory::GetForBrowserState(
-              browser_state),
           IdentityManagerFactory::GetForBrowserState(browser_state)) {}
diff --git a/ios/web_view/BUILD.gn b/ios/web_view/BUILD.gn
index 2c708ce..a4ae19d 100644
--- a/ios/web_view/BUILD.gn
+++ b/ios/web_view/BUILD.gn
@@ -166,8 +166,6 @@
   "internal/signin/web_view_account_fetcher_service_factory.mm",
   "internal/signin/web_view_account_tracker_service_factory.h",
   "internal/signin/web_view_account_tracker_service_factory.mm",
-  "internal/signin/web_view_gaia_cookie_manager_service_factory.h",
-  "internal/signin/web_view_gaia_cookie_manager_service_factory.mm",
   "internal/signin/web_view_identity_manager_factory.h",
   "internal/signin/web_view_identity_manager_factory.mm",
   "internal/signin/web_view_oauth2_token_service_factory.h",
diff --git a/ios/web_view/internal/signin/web_view_gaia_cookie_manager_service_factory.h b/ios/web_view/internal/signin/web_view_gaia_cookie_manager_service_factory.h
deleted file mode 100644
index f94cd60..0000000
--- a/ios/web_view/internal/signin/web_view_gaia_cookie_manager_service_factory.h
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2017 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.
-
-#ifndef IOS_WEB_VIEW_INTERNAL_SIGNIN_WEB_VIEW_GAIA_MANAGER_SERVICE_FACTORY_H_
-#define IOS_WEB_VIEW_INTERNAL_SIGNIN_WEB_VIEW_GAIA_MANAGER_SERVICE_FACTORY_H_
-
-#include <memory>
-
-#include "base/macros.h"
-#include "base/no_destructor.h"
-#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
-
-class GaiaCookieManagerService;
-
-namespace ios_web_view {
-
-class WebViewBrowserState;
-
-// Singleton that owns the GaiaCookieManagerService(s) and associates those
-// services  with browser states.
-class WebViewGaiaCookieManagerServiceFactory
-    : public BrowserStateKeyedServiceFactory {
- public:
-  // Returns the instance of GaiaCookieManagerService associated with this
-  // browser state (creating one if none exists). Returns null if this browser
-  // state cannot have an GaiaCookieManagerService (for example, if it is
-  // incognito).
-  static GaiaCookieManagerService* GetForBrowserState(
-      ios_web_view::WebViewBrowserState* browser_state);
-
-  // Returns an instance of the factory singleton.
-  static WebViewGaiaCookieManagerServiceFactory* GetInstance();
-
- private:
-  friend class base::NoDestructor<WebViewGaiaCookieManagerServiceFactory>;
-
-  WebViewGaiaCookieManagerServiceFactory();
-  ~WebViewGaiaCookieManagerServiceFactory() override = default;
-
-  // BrowserStateKeyedServiceFactory:
-  std::unique_ptr<KeyedService> BuildServiceInstanceFor(
-      web::BrowserState* context) const override;
-
-  DISALLOW_COPY_AND_ASSIGN(WebViewGaiaCookieManagerServiceFactory);
-};
-
-}  // namespace ios_web_view
-
-#endif  // IOS_WEB_VIEW_INTERNAL_SIGNIN_WEB_VIEW_GAIA_MANAGER_SERVICE_FACTORY_H_
diff --git a/ios/web_view/internal/signin/web_view_gaia_cookie_manager_service_factory.mm b/ios/web_view/internal/signin/web_view_gaia_cookie_manager_service_factory.mm
deleted file mode 100644
index 01d2ed3..0000000
--- a/ios/web_view/internal/signin/web_view_gaia_cookie_manager_service_factory.mm
+++ /dev/null
@@ -1,56 +0,0 @@
-// Copyright 2017 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 "ios/web_view/internal/signin/web_view_gaia_cookie_manager_service_factory.h"
-
-#include "base/no_destructor.h"
-#include "components/keyed_service/core/service_access_type.h"
-#include "components/keyed_service/ios/browser_state_dependency_manager.h"
-#include "components/signin/core/browser/gaia_cookie_manager_service.h"
-#include "components/signin/core/browser/profile_oauth2_token_service.h"
-#include "ios/web_view/internal/signin/ios_web_view_signin_client.h"
-#include "ios/web_view/internal/signin/web_view_oauth2_token_service_factory.h"
-#include "ios/web_view/internal/signin/web_view_signin_client_factory.h"
-#include "ios/web_view/internal/web_view_browser_state.h"
-
-#if !defined(__has_feature) || !__has_feature(objc_arc)
-#error "This file requires ARC support."
-#endif
-
-namespace ios_web_view {
-
-WebViewGaiaCookieManagerServiceFactory::WebViewGaiaCookieManagerServiceFactory()
-    : BrowserStateKeyedServiceFactory(
-          "GaiaCookieManagerService",
-          BrowserStateDependencyManager::GetInstance()) {
-  DependsOn(WebViewSigninClientFactory::GetInstance());
-  DependsOn(WebViewOAuth2TokenServiceFactory::GetInstance());
-}
-
-// static
-GaiaCookieManagerService*
-WebViewGaiaCookieManagerServiceFactory::GetForBrowserState(
-    ios_web_view::WebViewBrowserState* browser_state) {
-  return static_cast<GaiaCookieManagerService*>(
-      GetInstance()->GetServiceForBrowserState(browser_state, true));
-}
-
-// static
-WebViewGaiaCookieManagerServiceFactory*
-WebViewGaiaCookieManagerServiceFactory::GetInstance() {
-  static base::NoDestructor<WebViewGaiaCookieManagerServiceFactory> instance;
-  return instance.get();
-}
-
-std::unique_ptr<KeyedService>
-WebViewGaiaCookieManagerServiceFactory::BuildServiceInstanceFor(
-    web::BrowserState* context) const {
-  WebViewBrowserState* browser_state =
-      WebViewBrowserState::FromBrowserState(context);
-  return std::make_unique<GaiaCookieManagerService>(
-      WebViewOAuth2TokenServiceFactory::GetForBrowserState(browser_state),
-      WebViewSigninClientFactory::GetForBrowserState(browser_state));
-}
-
-}  // namespace ios_web_view
diff --git a/ios/web_view/internal/signin/web_view_identity_manager_factory.mm b/ios/web_view/internal/signin/web_view_identity_manager_factory.mm
index b4c352d..9b33f6d 100644
--- a/ios/web_view/internal/signin/web_view_identity_manager_factory.mm
+++ b/ios/web_view/internal/signin/web_view_identity_manager_factory.mm
@@ -17,7 +17,6 @@
 #include "ios/web_view/internal/signin/ios_web_view_signin_client.h"
 #include "ios/web_view/internal/signin/web_view_account_fetcher_service_factory.h"
 #include "ios/web_view/internal/signin/web_view_account_tracker_service_factory.h"
-#include "ios/web_view/internal/signin/web_view_gaia_cookie_manager_service_factory.h"
 #include "ios/web_view/internal/signin/web_view_oauth2_token_service_factory.h"
 #include "ios/web_view/internal/signin/web_view_signin_client_factory.h"
 #include "ios/web_view/internal/web_view_browser_state.h"
@@ -35,7 +34,8 @@
 
 namespace {
 std::unique_ptr<SigninManager> BuildSigninManager(
-    WebViewBrowserState* browser_state) {
+    WebViewBrowserState* browser_state,
+    GaiaCookieManagerService* gaia_cookie_manager_service) {
   // Clearing the sign in state on start up greatly simplifies the management of
   // ChromeWebView's signin state.
   PrefService* pref_service = browser_state->GetPrefs();
@@ -47,8 +47,7 @@
       WebViewSigninClientFactory::GetForBrowserState(browser_state),
       WebViewOAuth2TokenServiceFactory::GetForBrowserState(browser_state),
       WebViewAccountTrackerServiceFactory::GetForBrowserState(browser_state),
-      WebViewGaiaCookieManagerServiceFactory::GetForBrowserState(browser_state),
-      signin::AccountConsistencyMethod::kDisabled);
+      gaia_cookie_manager_service, signin::AccountConsistencyMethod::kDisabled);
   service->Initialize(ApplicationContext::GetInstance()->GetLocalState());
   return service;
 }
@@ -65,28 +64,28 @@
                                public identity::IdentityManager {
  public:
   explicit IdentityManagerWrapper(
+      std::unique_ptr<GaiaCookieManagerService> gaia_cookie_manager_service,
       std::unique_ptr<SigninManagerBase> signin_manager,
       std::unique_ptr<identity::PrimaryAccountMutator> primary_account_mutator,
+      std::unique_ptr<identity::AccountsCookieMutatorImpl>
+          accounts_cookie_mutator,
+      std::unique_ptr<identity::DiagnosticsProviderImpl> diagnostics_provider,
       WebViewBrowserState* browser_state)
       : identity::IdentityManager(
+            std::move(gaia_cookie_manager_service),
             std::move(signin_manager),
             WebViewOAuth2TokenServiceFactory::GetForBrowserState(browser_state),
             WebViewAccountFetcherServiceFactory::GetForBrowserState(
                 browser_state),
             WebViewAccountTrackerServiceFactory::GetForBrowserState(
                 browser_state),
-            WebViewGaiaCookieManagerServiceFactory::GetForBrowserState(
-                browser_state),
             std::move(primary_account_mutator),
             /*accounts_mutator=*/nullptr,
-            std::make_unique<identity::AccountsCookieMutatorImpl>(
-                WebViewGaiaCookieManagerServiceFactory::GetForBrowserState(
-                    browser_state)),
-            std::make_unique<identity::DiagnosticsProviderImpl>(
-                WebViewOAuth2TokenServiceFactory::GetForBrowserState(
-                    browser_state),
-                WebViewGaiaCookieManagerServiceFactory::GetForBrowserState(
-                    browser_state))) {}
+            std::move(accounts_cookie_mutator),
+            std::move(diagnostics_provider)) {}
+
+  // KeyedService overrides.
+  void Shutdown() override { IdentityManager::Shutdown(); }
 };
 
 void WebViewIdentityManagerFactory::RegisterBrowserStatePrefs(
@@ -100,7 +99,6 @@
           BrowserStateDependencyManager::GetInstance()) {
   DependsOn(WebViewAccountFetcherServiceFactory::GetInstance());
   DependsOn(WebViewAccountTrackerServiceFactory::GetInstance());
-  DependsOn(WebViewGaiaCookieManagerServiceFactory::GetInstance());
   DependsOn(WebViewOAuth2TokenServiceFactory::GetInstance());
   DependsOn(WebViewSigninClientFactory::GetInstance());
 }
@@ -124,7 +122,6 @@
 void WebViewIdentityManagerFactory::EnsureFactoryAndDependeeFactoriesBuilt() {
   WebViewIdentityManagerFactory::GetInstance();
   WebViewAccountTrackerServiceFactory::GetInstance();
-  WebViewGaiaCookieManagerServiceFactory::GetInstance();
   WebViewOAuth2TokenServiceFactory::GetInstance();
   WebViewSigninClientFactory::GetInstance();
 }
@@ -136,16 +133,27 @@
       WebViewBrowserState::FromBrowserState(context);
 
   // Construct the dependencies that IdentityManager will own.
+  auto gaia_cookie_manager_service = std::make_unique<GaiaCookieManagerService>(
+      WebViewOAuth2TokenServiceFactory::GetForBrowserState(browser_state),
+      WebViewSigninClientFactory::GetForBrowserState(browser_state));
   std::unique_ptr<SigninManager> signin_manager =
-      BuildSigninManager(browser_state);
+      BuildSigninManager(browser_state, gaia_cookie_manager_service.get());
   auto primary_account_mutator =
       std::make_unique<identity::PrimaryAccountMutatorImpl>(
           WebViewAccountTrackerServiceFactory::GetForBrowserState(
               browser_state),
           signin_manager.get());
+  auto accounts_cookie_mutator =
+      std::make_unique<identity::AccountsCookieMutatorImpl>(
+          gaia_cookie_manager_service.get());
+  auto diagnostics_provider =
+      std::make_unique<identity::DiagnosticsProviderImpl>(
+          WebViewOAuth2TokenServiceFactory::GetForBrowserState(browser_state),
+          gaia_cookie_manager_service.get());
   auto identity_manager = std::make_unique<IdentityManagerWrapper>(
-      std::move(signin_manager), std::move(primary_account_mutator),
-      browser_state);
+      std::move(gaia_cookie_manager_service), std::move(signin_manager),
+      std::move(primary_account_mutator), std::move(accounts_cookie_mutator),
+      std::move(diagnostics_provider), browser_state);
   return identity_manager;
 }
 
diff --git a/services/identity/public/cpp/identity_manager.cc b/services/identity/public/cpp/identity_manager.cc
index 600073f..860f396 100644
--- a/services/identity/public/cpp/identity_manager.cc
+++ b/services/identity/public/cpp/identity_manager.cc
@@ -40,20 +40,20 @@
 }  // namespace
 
 IdentityManager::IdentityManager(
+    std::unique_ptr<GaiaCookieManagerService> gaia_cookie_manager_service,
     std::unique_ptr<SigninManagerBase> signin_manager,
     ProfileOAuth2TokenService* token_service,
     AccountFetcherService* account_fetcher_service,
     AccountTrackerService* account_tracker_service,
-    GaiaCookieManagerService* gaia_cookie_manager_service,
     std::unique_ptr<PrimaryAccountMutator> primary_account_mutator,
     std::unique_ptr<AccountsMutator> accounts_mutator,
     std::unique_ptr<AccountsCookieMutator> accounts_cookie_mutator,
     std::unique_ptr<DiagnosticsProvider> diagnostics_provider)
-    : signin_manager_(std::move(signin_manager)),
+    : gaia_cookie_manager_service_(std::move(gaia_cookie_manager_service)),
+      signin_manager_(std::move(signin_manager)),
       token_service_(token_service),
       account_fetcher_service_(account_fetcher_service),
       account_tracker_service_(account_tracker_service),
-      gaia_cookie_manager_service_(gaia_cookie_manager_service),
       primary_account_mutator_(std::move(primary_account_mutator)),
       accounts_mutator_(std::move(accounts_mutator)),
       accounts_cookie_mutator_(std::move(accounts_cookie_mutator)),
@@ -386,6 +386,10 @@
   diagnostics_observer_list_.RemoveObserver(observer);
 }
 
+void IdentityManager::Shutdown() {
+  gaia_cookie_manager_service_->Shutdown();
+}
+
 SigninManagerBase* IdentityManager::GetSigninManager() {
   return signin_manager_.get();
 }
@@ -403,7 +407,7 @@
 }
 
 GaiaCookieManagerService* IdentityManager::GetGaiaCookieManagerService() {
-  return gaia_cookie_manager_service_;
+  return gaia_cookie_manager_service_.get();
 }
 
 AccountInfo IdentityManager::GetAccountInfoForAccountWithRefreshToken(
diff --git a/services/identity/public/cpp/identity_manager.h b/services/identity/public/cpp/identity_manager.h
index 51873c3..96576c9 100644
--- a/services/identity/public/cpp/identity_manager.h
+++ b/services/identity/public/cpp/identity_manager.h
@@ -198,11 +198,11 @@
   };
 
   IdentityManager(
+      std::unique_ptr<GaiaCookieManagerService> gaia_cookie_manager_service,
       std::unique_ptr<SigninManagerBase> signin_manager,
       ProfileOAuth2TokenService* token_service,
       AccountFetcherService* account_fetcher_service,
       AccountTrackerService* account_tracker_service,
-      GaiaCookieManagerService* gaia_cookie_manager_service,
       std::unique_ptr<PrimaryAccountMutator> primary_account_mutator,
       std::unique_ptr<AccountsMutator> accounts_mutator,
       std::unique_ptr<AccountsCookieMutator> accounts_cookie_mutator,
@@ -469,6 +469,9 @@
   void AddDiagnosticsObserver(DiagnosticsObserver* observer);
   void RemoveDiagnosticsObserver(DiagnosticsObserver* observer);
 
+ protected:
+  void Shutdown();
+
  private:
   // These test helpers need to use some of the private methods below.
   friend CoreAccountInfo SetPrimaryAccount(IdentityManager* identity_manager,
@@ -549,6 +552,31 @@
   FRIEND_TEST_ALL_PREFIXES(IdentityManagerTest, RemoveAccessTokenFromCache);
   FRIEND_TEST_ALL_PREFIXES(IdentityManagerTest,
                            CreateAccessTokenFetcherWithCustomURLLoaderFactory);
+  FRIEND_TEST_ALL_PREFIXES(
+      IdentityManagerTest,
+      CallbackSentOnUpdateToAccountsInCookieWithNoAccounts);
+  FRIEND_TEST_ALL_PREFIXES(
+      IdentityManagerTest,
+      CallbackSentOnUpdateToAccountsInCookieWithOneAccount);
+  FRIEND_TEST_ALL_PREFIXES(
+      IdentityManagerTest,
+      CallbackSentOnUpdateToAccountsInCookieWithTwoAccounts);
+  FRIEND_TEST_ALL_PREFIXES(IdentityManagerTest,
+                           CallbackSentOnUpdateToSignOutAccountsInCookie);
+  FRIEND_TEST_ALL_PREFIXES(
+      IdentityManagerTest,
+      CallbackSentOnUpdateToAccountsInCookieWithStaleAccounts);
+  FRIEND_TEST_ALL_PREFIXES(IdentityManagerTest,
+                           CallbackSentOnSuccessfulAdditionOfAccountToCookie);
+  FRIEND_TEST_ALL_PREFIXES(IdentityManagerTest,
+                           CallbackSentOnFailureAdditionOfAccountToCookie);
+  FRIEND_TEST_ALL_PREFIXES(IdentityManagerTest,
+                           CallbackSentOnSetAccountsInCookieCompleted_Success);
+  FRIEND_TEST_ALL_PREFIXES(IdentityManagerTest,
+                           CallbackSentOnSetAccountsInCookieCompleted_Failure);
+  FRIEND_TEST_ALL_PREFIXES(IdentityManagerTest,
+                           CallbackSentOnAccountsCookieDeletedByUserAction);
+  FRIEND_TEST_ALL_PREFIXES(IdentityManagerTest, OnNetworkInitialized);
 
   // Private getters used for testing only (i.e. see identity_test_utils.h).
   SigninManagerBase* GetSigninManager();
@@ -616,11 +644,11 @@
   // these classes in the IdentityManager implementation, as all such
   // synchronous access will become impossible when IdentityManager is
   // backed by the Identity Service.
+  std::unique_ptr<GaiaCookieManagerService> gaia_cookie_manager_service_;
   std::unique_ptr<SigninManagerBase> signin_manager_;
   ProfileOAuth2TokenService* token_service_;
   AccountFetcherService* account_fetcher_service_;
   AccountTrackerService* account_tracker_service_;
-  GaiaCookieManagerService* gaia_cookie_manager_service_;
 
   // PrimaryAccountMutator instance. May be null if mutation of the primary
   // account state is not supported on the current platform.
diff --git a/services/identity/public/cpp/identity_manager_unittest.cc b/services/identity/public/cpp/identity_manager_unittest.cc
index 7015c15..61ea627 100644
--- a/services/identity/public/cpp/identity_manager_unittest.cc
+++ b/services/identity/public/cpp/identity_manager_unittest.cc
@@ -255,17 +255,7 @@
 class IdentityManagerTest : public testing::Test {
  protected:
   IdentityManagerTest()
-      : signin_client_(&pref_service_),
-        token_service_(&pref_service_),
-        gaia_cookie_manager_service_(
-            &token_service_,
-            &signin_client_,
-            base::BindRepeating(
-                [](network::TestURLLoaderFactory* test_url_loader_factory)
-                    -> scoped_refptr<network::SharedURLLoaderFactory> {
-                  return test_url_loader_factory->GetSafeWeakWrapper();
-                },
-                test_url_loader_factory())) {
+      : signin_client_(&pref_service_), token_service_(&pref_service_) {
     AccountTrackerService::RegisterPrefs(pref_service_.registry());
     AccountFetcherService::RegisterPrefs(pref_service_.registry());
     ProfileOAuth2TokenService::RegisterProfilePrefs(pref_service_.registry());
@@ -284,7 +274,6 @@
   ~IdentityManagerTest() override {
     signin_client_.Shutdown();
     token_service_.Shutdown();
-    gaia_cookie_manager_service_.Shutdown();
     account_tracker_.Shutdown();
     account_fetcher_.Shutdown();
   }
@@ -295,21 +284,22 @@
   }
 
   IdentityManager* identity_manager() { return identity_manager_.get(); }
+
   TestIdentityManagerObserver* identity_manager_observer() {
     return identity_manager_observer_.get();
   }
+
   TestIdentityManagerDiagnosticsObserver*
   identity_manager_diagnostics_observer() {
     return identity_manager_diagnostics_observer_.get();
   }
+
   AccountTrackerService* account_tracker() { return &account_tracker_; }
+
   AccountFetcherService* account_fetcher() { return &account_fetcher_; }
   CustomFakeProfileOAuth2TokenService* token_service() {
     return &token_service_;
   }
-  GaiaCookieManagerService* gaia_cookie_manager_service() {
-    return &gaia_cookie_manager_service_;
-  }
 
   // See RecreateIdentityManager.
   enum class SigninManagerSetup {
@@ -338,6 +328,16 @@
     identity_manager_diagnostics_observer_.reset();
     identity_manager_.reset();
 
+    auto gaia_cookie_manager_service =
+        std::make_unique<GaiaCookieManagerService>(
+            &token_service_, &signin_client_,
+            base::BindRepeating(
+                [](network::TestURLLoaderFactory* test_url_loader_factory)
+                    -> scoped_refptr<network::SharedURLLoaderFactory> {
+                  return test_url_loader_factory->GetSafeWeakWrapper();
+                },
+                test_url_loader_factory()));
+
 #if defined(OS_CHROMEOS)
     DCHECK_EQ(account_consistency, signin::AccountConsistencyMethod::kDisabled)
         << "AccountConsistency is not used by SigninManagerBase";
@@ -346,7 +346,7 @@
 #else
     auto signin_manager = std::make_unique<SigninManager>(
         &signin_client_, &token_service_, &account_tracker_,
-        &gaia_cookie_manager_service_, account_consistency);
+        gaia_cookie_manager_service.get(), account_consistency);
 #endif
 
     // Passing this switch ensures that the new SigninManager starts with a
@@ -361,13 +361,16 @@
       signin_manager->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
     }
 
+    auto accounts_cookie_mutator = std::make_unique<AccountsCookieMutatorImpl>(
+        gaia_cookie_manager_service.get());
+
+    auto diagnostics_provider = std::make_unique<DiagnosticsProviderImpl>(
+        &token_service_, gaia_cookie_manager_service.get());
+
     identity_manager_.reset(new IdentityManager(
-        std::move(signin_manager), &token_service_, &account_fetcher_,
-        &account_tracker_, &gaia_cookie_manager_service_, nullptr, nullptr,
-        std::make_unique<AccountsCookieMutatorImpl>(
-            &gaia_cookie_manager_service_),
-        std::make_unique<DiagnosticsProviderImpl>(
-            &token_service_, &gaia_cookie_manager_service_)));
+        std::move(gaia_cookie_manager_service), std::move(signin_manager),
+        &token_service_, &account_fetcher_, &account_tracker_, nullptr, nullptr,
+        std::move(accounts_cookie_mutator), std::move(diagnostics_provider)));
     identity_manager_observer_.reset(
         new TestIdentityManagerObserver(identity_manager_.get()));
     identity_manager_diagnostics_observer_.reset(
@@ -413,7 +416,6 @@
   TestSigninClient signin_client_;
   CustomFakeProfileOAuth2TokenService token_service_;
   network::TestURLLoaderFactory test_url_loader_factory_;
-  GaiaCookieManagerService gaia_cookie_manager_service_;
   std::unique_ptr<IdentityManager> identity_manager_;
   std::unique_ptr<TestIdentityManagerObserver> identity_manager_observer_;
   std::unique_ptr<TestIdentityManagerDiagnosticsObserver>
@@ -1617,7 +1619,7 @@
       run_loop.QuitClosure());
 
   signin::SetListAccountsResponseNoAccounts(test_url_loader_factory());
-  gaia_cookie_manager_service()->TriggerListAccounts();
+  identity_manager()->GetGaiaCookieManagerService()->TriggerListAccounts();
   run_loop.Run();
 
   const AccountsInCookieJarInfo& accounts_in_cookie_jar_info =
@@ -1635,7 +1637,7 @@
 
   signin::SetListAccountsResponseOneAccount(kTestEmail, kTestGaiaId,
                                             test_url_loader_factory());
-  gaia_cookie_manager_service()->TriggerListAccounts();
+  identity_manager()->GetGaiaCookieManagerService()->TriggerListAccounts();
   run_loop.Run();
 
   const AccountsInCookieJarInfo& accounts_in_cookie_jar_info =
@@ -1663,7 +1665,7 @@
   signin::SetListAccountsResponseTwoAccounts(kTestEmail, kTestGaiaId,
                                              kTestEmail2, kTestGaiaId2,
                                              test_url_loader_factory());
-  gaia_cookie_manager_service()->TriggerListAccounts();
+  identity_manager()->GetGaiaCookieManagerService()->TriggerListAccounts();
   run_loop.Run();
 
   const AccountsInCookieJarInfo& accounts_in_cookie_jar_info =
@@ -1710,7 +1712,7 @@
           signed_out_status.account_2 /* signed_out */, true /* verified */}},
         test_url_loader_factory());
 
-    gaia_cookie_manager_service()->TriggerListAccounts();
+    identity_manager()->GetGaiaCookieManagerService()->TriggerListAccounts();
     run_loop.Run();
 
     unsigned int accounts_signed_out =
@@ -1762,7 +1764,7 @@
 
   // Configure list accounts to return a permanent Gaia auth error.
   signin::SetListAccountsResponseWebLoginRequired(test_url_loader_factory());
-  gaia_cookie_manager_service()->TriggerListAccounts();
+  identity_manager()->GetGaiaCookieManagerService()->TriggerListAccounts();
   run_loop.Run();
 
   const AccountsInCookieJarInfo& accounts_in_cookie_jar_info =
@@ -1897,11 +1899,11 @@
         error_from_add_account_to_cookie_completed_callback = error;
       });
 
-  gaia_cookie_manager_service()->AddAccountToCookie(
+  identity_manager()->GetGaiaCookieManagerService()->AddAccountToCookie(
       kTestAccountId, gaia::GaiaSource::kChrome,
       std::move(completion_callback));
-  SimulateAdditionOfAccountToCookieSuccess(gaia_cookie_manager_service(),
-                                           "token");
+  SimulateAdditionOfAccountToCookieSuccess(
+      identity_manager()->GetGaiaCookieManagerService(), "token");
   EXPECT_EQ(account_from_add_account_to_cookie_completed_callback,
             kTestAccountId);
   EXPECT_EQ(error_from_add_account_to_cookie_completed_callback,
@@ -1919,13 +1921,13 @@
         error_from_add_account_to_cookie_completed_callback = error;
       });
 
-  gaia_cookie_manager_service()->AddAccountToCookie(
+  identity_manager()->GetGaiaCookieManagerService()->AddAccountToCookie(
       kTestAccountId, gaia::GaiaSource::kChrome,
       std::move(completion_callback));
 
   GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_ERROR);
-  SimulateAdditionOfAccountToCookieSuccessFailure(gaia_cookie_manager_service(),
-                                                  error);
+  SimulateAdditionOfAccountToCookieSuccessFailure(
+      identity_manager()->GetGaiaCookieManagerService(), error);
 
   EXPECT_EQ(account_from_add_account_to_cookie_completed_callback,
             kTestAccountId);
@@ -1947,7 +1949,7 @@
       });
 
   // Needed to insert request in the queue.
-  gaia_cookie_manager_service()->SetAccountsInCookie(
+  identity_manager()->GetGaiaCookieManagerService()->SetAccountsInCookie(
       account_ids, gaia::GaiaSource::kChrome, std::move(completion_callback));
 
   // Sample success cookie response.
@@ -1971,7 +1973,8 @@
     )";
   OAuthMultiloginResult result(data);
 
-  SimulateOAuthMultiloginFinished(gaia_cookie_manager_service(), result);
+  SimulateOAuthMultiloginFinished(
+      identity_manager()->GetGaiaCookieManagerService(), result);
 
   EXPECT_EQ(error_from_set_accounts_in_cookie_completed_callback,
             GoogleServiceAuthError::AuthErrorNone());
@@ -1992,14 +1995,15 @@
       });
 
   // Needed to insert request in the queue.
-  gaia_cookie_manager_service()->SetAccountsInCookie(
+  identity_manager()->GetGaiaCookieManagerService()->SetAccountsInCookie(
       account_ids, gaia::GaiaSource::kChrome, std::move(completion_callback));
 
   // Sample an erroneous response.
   GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_ERROR);
   OAuthMultiloginResult result(error);
 
-  SimulateOAuthMultiloginFinished(gaia_cookie_manager_service(), result);
+  SimulateOAuthMultiloginFinished(
+      identity_manager()->GetGaiaCookieManagerService(), result);
 
   EXPECT_EQ(error_from_set_accounts_in_cookie_completed_callback, error);
 }
@@ -2011,7 +2015,7 @@
                                                 kTestAccountId2};
 
   // Needed to insert request in the queue.
-  gaia_cookie_manager_service()->SetAccountsInCookie(
+  identity_manager()->GetGaiaCookieManagerService()->SetAccountsInCookie(
       account_ids, gaia::GaiaSource::kChrome,
       GaiaCookieManagerService::SetAccountsInCookieCompletedCallback());
 
@@ -2036,7 +2040,8 @@
     )";
   OAuthMultiloginResult result(data);
 
-  SimulateOAuthMultiloginFinished(gaia_cookie_manager_service(), result);
+  SimulateOAuthMultiloginFinished(
+      identity_manager()->GetGaiaCookieManagerService(), result);
   base::RunLoop().RunUntilIdle();
 
   base::RunLoop run_loop;
@@ -2044,7 +2049,8 @@
       run_loop.QuitClosure());
 
   const std::vector<net::CanonicalCookie>& cookies = result.cookies();
-  SimulateCookieDeletedByUser(gaia_cookie_manager_service(), cookies[0]);
+  SimulateCookieDeletedByUser(identity_manager()->GetGaiaCookieManagerService(),
+                              cookies[0]);
   run_loop.Run();
 }
 
@@ -2062,7 +2068,7 @@
   identity_manager()->OnNetworkInitialized();
 
   // Needed to insert request in the queue.
-  gaia_cookie_manager_service()->SetAccountsInCookie(
+  identity_manager()->GetGaiaCookieManagerService()->SetAccountsInCookie(
       account_ids, gaia::GaiaSource::kChrome,
       GaiaCookieManagerService::SetAccountsInCookieCompletedCallback());
 
@@ -2087,7 +2093,8 @@
     )";
   OAuthMultiloginResult result(data);
 
-  SimulateOAuthMultiloginFinished(gaia_cookie_manager_service(), result);
+  SimulateOAuthMultiloginFinished(
+      identity_manager()->GetGaiaCookieManagerService(), result);
   base::RunLoop().RunUntilIdle();
 
   base::RunLoop run_loop;
diff --git a/services/identity/public/cpp/identity_test_environment.cc b/services/identity/public/cpp/identity_test_environment.cc
index 1247dd2..6c67c4b 100644
--- a/services/identity/public/cpp/identity_test_environment.cc
+++ b/services/identity/public/cpp/identity_test_environment.cc
@@ -106,7 +106,6 @@
           /*account_tracker_service=*/nullptr,
           /*account_fetcher_service=*/nullptr,
           /*token_service=*/nullptr,
-          /*gaia_cookie_manager_service=*/nullptr,
           test_url_loader_factory,
           account_consistency,
           std::make_unique<IdentityManagerDependenciesOwner>(
@@ -119,7 +118,6 @@
     AccountTrackerService* account_tracker_service,
     AccountFetcherService* account_fetcher_service,
     FakeProfileOAuth2TokenService* token_service,
-    GaiaCookieManagerService* gaia_cookie_manager_service,
     IdentityManager* identity_manager,
     network::TestURLLoaderFactory* test_url_loader_factory,
     signin::AccountConsistencyMethod account_consistency)
@@ -127,7 +125,6 @@
                               account_tracker_service,
                               account_fetcher_service,
                               token_service,
-                              gaia_cookie_manager_service,
                               test_url_loader_factory,
                               account_consistency,
                               /*dependency_owner=*/nullptr,
@@ -138,7 +135,6 @@
     AccountTrackerService* account_tracker_service,
     AccountFetcherService* account_fetcher_service,
     FakeProfileOAuth2TokenService* token_service,
-    GaiaCookieManagerService* gaia_cookie_manager_service,
     network::TestURLLoaderFactory* test_url_loader_factory,
     signin::AccountConsistencyMethod account_consistency,
     std::unique_ptr<IdentityManagerDependenciesOwner> dependencies_owner,
@@ -154,7 +150,7 @@
   TestSigninClient* test_signin_client = nullptr;
   if (dependencies_owner) {
     DCHECK(!(account_tracker_service || account_fetcher_service ||
-             token_service || gaia_cookie_manager_service || identity_manager));
+             token_service || identity_manager));
 
     dependencies_owner_ = std::move(dependencies_owner);
     test_signin_client = dependencies_owner_->signin_client();
@@ -184,22 +180,6 @@
         std::make_unique<image_fetcher::FakeImageDecoder>());
     account_fetcher_service = owned_account_fetcher_service_.get();
 
-    if (test_url_loader_factory != nullptr) {
-      owned_gaia_cookie_manager_service_ =
-          std::make_unique<GaiaCookieManagerService>(
-              token_service, test_signin_client,
-              base::BindRepeating(
-                  [](network::TestURLLoaderFactory* test_url_loader_factory)
-                      -> scoped_refptr<network::SharedURLLoaderFactory> {
-                    return test_url_loader_factory->GetSafeWeakWrapper();
-                  },
-                  test_url_loader_factory));
-    } else {
-      owned_gaia_cookie_manager_service_ =
-          std::make_unique<GaiaCookieManagerService>(token_service,
-                                                     test_signin_client);
-    }
-    gaia_cookie_manager_service = owned_gaia_cookie_manager_service_.get();
   } else {
     owned_signin_client_ = std::make_unique<TestSigninClient>(pref_service);
     test_signin_client = owned_signin_client_.get();
@@ -218,7 +198,7 @@
     raw_identity_manager_ = identity_manager;
   } else {
     DCHECK(pref_service && account_tracker_service && account_fetcher_service &&
-           token_service && gaia_cookie_manager_service);
+           token_service);
 #if defined(OS_CHROMEOS)
     std::unique_ptr<SigninManagerBase> signin_manager =
         std::make_unique<SigninManagerBase>(test_signin_client, token_service,
@@ -230,6 +210,22 @@
                                         account_consistency);
 #endif
     signin_manager->Initialize(pref_service);
+
+    std::unique_ptr<GaiaCookieManagerService> gaia_cookie_manager_service;
+    if (test_url_loader_factory != nullptr) {
+      gaia_cookie_manager_service = std::make_unique<GaiaCookieManagerService>(
+          token_service, test_signin_client,
+          base::BindRepeating(
+              [](network::TestURLLoaderFactory* test_url_loader_factory)
+                  -> scoped_refptr<network::SharedURLLoaderFactory> {
+                return test_url_loader_factory->GetSafeWeakWrapper();
+              },
+              test_url_loader_factory));
+    } else {
+      gaia_cookie_manager_service = std::make_unique<GaiaCookieManagerService>(
+          token_service, test_signin_client);
+    }
+
     std::unique_ptr<PrimaryAccountMutator> primary_account_mutator;
     std::unique_ptr<AccountsMutator> accounts_mutator;
 
@@ -246,16 +242,16 @@
 #endif
 
     std::unique_ptr<DiagnosticsProvider> diagnostics_provider =
-        std::make_unique<DiagnosticsProviderImpl>(token_service,
-                                                  gaia_cookie_manager_service);
+        std::make_unique<DiagnosticsProviderImpl>(
+            token_service, gaia_cookie_manager_service.get());
 
     std::unique_ptr<AccountsCookieMutator> accounts_cookie_mutator =
         std::make_unique<AccountsCookieMutatorImpl>(
-            gaia_cookie_manager_service);
+            gaia_cookie_manager_service.get());
 
     owned_identity_manager_ = std::make_unique<IdentityManager>(
-        std::move(signin_manager), token_service, account_fetcher_service,
-        account_tracker_service, gaia_cookie_manager_service,
+        std::move(gaia_cookie_manager_service), std::move(signin_manager),
+        token_service, account_fetcher_service, account_tracker_service,
         std::move(primary_account_mutator), std::move(accounts_mutator),
         std::move(accounts_cookie_mutator), std::move(diagnostics_provider));
   }
@@ -276,6 +272,7 @@
   }
 
   identity_manager()->RemoveDiagnosticsObserver(this);
+  identity_manager()->Shutdown();
 }
 
 IdentityManager* IdentityTestEnvironment::identity_manager() {
diff --git a/services/identity/public/cpp/identity_test_environment.h b/services/identity/public/cpp/identity_test_environment.h
index 90ef9f0..9f7c612 100644
--- a/services/identity/public/cpp/identity_test_environment.h
+++ b/services/identity/public/cpp/identity_test_environment.h
@@ -13,11 +13,9 @@
 class AccountFetcherService;
 class AccountTrackerService;
 class FakeProfileOAuth2TokenService;
-class GaiaCookieManagerService;
 class IdentityTestEnvironmentChromeBrowserStateAdaptor;
 class IdentityTestEnvironmentProfileAdaptor;
 class PrefService;
-class SigninManager;
 class TestSigninClient;
 
 namespace sync_preferences {
@@ -57,9 +55,8 @@
   //
   // This constructor also takes an optional PrefService instance as parameter,
   // which allows tests to move away from referencing IdentityManager's
-  // dependencies directly (namely AccountTrackerService, PO2TS, SigninManager
-  // and GaiaCookieManagerService), but still be able to tweak preferences on
-  // demand.
+  // dependencies directly (namely AccountTrackerService, PO2TS), but still be
+  // able to tweak preferences on demand.
   //
   // Last, this constructor can take an optional parameter |account_consistency|
   // as parameter, to specify the account consistency policy that will be used.
@@ -309,7 +306,6 @@
       AccountTrackerService* account_tracker_service,
       AccountFetcherService* account_fetcher_service,
       FakeProfileOAuth2TokenService* token_service,
-      GaiaCookieManagerService* gaia_cookie_manager_service,
       IdentityManager* identity_manager,
       network::TestURLLoaderFactory* test_url_loader_factory = nullptr,
       signin::AccountConsistencyMethod account_consistency =
@@ -331,7 +327,6 @@
       AccountTrackerService* account_tracker_service,
       AccountFetcherService* account_fetcher_service,
       FakeProfileOAuth2TokenService* token_service,
-      GaiaCookieManagerService* gaia_cookie_manager_service,
       network::TestURLLoaderFactory* test_url_loader_factory,
       signin::AccountConsistencyMethod account_consistency,
       std::unique_ptr<IdentityManagerDependenciesOwner> dependencies_owner,
@@ -378,10 +373,6 @@
   // IdentityTestEnvironment's constructor.
   std::unique_ptr<FakeProfileOAuth2TokenService> owned_token_service_;
 
-  // This will be null if a GaiaCookieManagerService was provided to
-  // IdentityTestEnvironment's constructor.
-  std::unique_ptr<GaiaCookieManagerService> owned_gaia_cookie_manager_service_;
-
   // Depending on which constructor is used, exactly one of these will be
   // non-null. See the documentation on the constructor wherein IdentityManager
   // is passed in for required lifetime invariants in that case.