/ios/chrome/browser/signin/authentication_service_factory.mm

https://github.com/chromium/chromium · Objective C++ · 96 lines · 73 code · 16 blank · 7 comment · 3 complexity · 7f65b715b650a2cd2b72bc8fef53465a MD5 · raw file

  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "ios/chrome/browser/signin/authentication_service_factory.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/no_destructor.h"
  8. #include "components/keyed_service/ios/browser_state_dependency_manager.h"
  9. #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
  10. #import "ios/chrome/browser/signin/authentication_service.h"
  11. #import "ios/chrome/browser/signin/authentication_service_delegate.h"
  12. #import "ios/chrome/browser/signin/chrome_account_manager_service_factory.h"
  13. #include "ios/chrome/browser/signin/identity_manager_factory.h"
  14. #include "ios/chrome/browser/sync/sync_service_factory.h"
  15. #include "ios/chrome/browser/sync/sync_setup_service_factory.h"
  16. #if !defined(__has_feature) || !__has_feature(objc_arc)
  17. #error "This file requires ARC support."
  18. #endif
  19. namespace {
  20. std::unique_ptr<KeyedService> BuildAuthenticationService(
  21. web::BrowserState* context) {
  22. ChromeBrowserState* browser_state =
  23. ChromeBrowserState::FromBrowserState(context);
  24. return std::make_unique<AuthenticationService>(
  25. browser_state->GetPrefs(),
  26. SyncSetupServiceFactory::GetForBrowserState(browser_state),
  27. ChromeAccountManagerServiceFactory::GetForBrowserState(browser_state),
  28. IdentityManagerFactory::GetForBrowserState(browser_state),
  29. SyncServiceFactory::GetForBrowserState(browser_state));
  30. }
  31. } // namespace
  32. // static
  33. AuthenticationService* AuthenticationServiceFactory::GetForBrowserState(
  34. ChromeBrowserState* browser_state) {
  35. AuthenticationService* service = static_cast<AuthenticationService*>(
  36. GetInstance()->GetServiceForBrowserState(browser_state, true));
  37. CHECK(!service || service->initialized());
  38. return service;
  39. }
  40. // static
  41. AuthenticationServiceFactory* AuthenticationServiceFactory::GetInstance() {
  42. static base::NoDestructor<AuthenticationServiceFactory> instance;
  43. return instance.get();
  44. }
  45. // static
  46. void AuthenticationServiceFactory::CreateAndInitializeForBrowserState(
  47. ChromeBrowserState* browser_state,
  48. std::unique_ptr<AuthenticationServiceDelegate> delegate) {
  49. AuthenticationService* service = static_cast<AuthenticationService*>(
  50. GetInstance()->GetServiceForBrowserState(browser_state, true));
  51. CHECK(service && !service->initialized());
  52. service->Initialize(std::move(delegate));
  53. }
  54. // static
  55. AuthenticationServiceFactory::TestingFactory
  56. AuthenticationServiceFactory::GetDefaultFactory() {
  57. return base::BindRepeating(&BuildAuthenticationService);
  58. }
  59. AuthenticationServiceFactory::AuthenticationServiceFactory()
  60. : BrowserStateKeyedServiceFactory(
  61. "AuthenticationService",
  62. BrowserStateDependencyManager::GetInstance()) {
  63. DependsOn(ChromeAccountManagerServiceFactory::GetInstance());
  64. DependsOn(IdentityManagerFactory::GetInstance());
  65. DependsOn(SyncSetupServiceFactory::GetInstance());
  66. DependsOn(SyncServiceFactory::GetInstance());
  67. }
  68. AuthenticationServiceFactory::~AuthenticationServiceFactory() {}
  69. std::unique_ptr<KeyedService>
  70. AuthenticationServiceFactory::BuildServiceInstanceFor(
  71. web::BrowserState* context) const {
  72. return BuildAuthenticationService(context);
  73. }
  74. void AuthenticationServiceFactory::RegisterBrowserStatePrefs(
  75. user_prefs::PrefRegistrySyncable* registry) {
  76. AuthenticationService::RegisterPrefs(registry);
  77. }
  78. bool AuthenticationServiceFactory::ServiceIsNULLWhileTesting() const {
  79. return true;
  80. }