1 // Copyright 2015 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.
5 #include "ios/chrome/browser/signin/signin_manager_factory.h"
7 #include "base/memory/singleton.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/time/time.h"
10 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
11 #include "components/pref_registry/pref_registry_syncable.h"
12 #include "components/signin/core/browser/signin_manager.h"
13 #include "components/signin/core/common/signin_pref_names.h"
14 #include "ios/chrome/browser/application_context.h"
15 #include "ios/chrome/browser/signin/account_tracker_service_factory.h"
16 #include "ios/chrome/browser/signin/gaia_cookie_manager_service_factory.h"
17 #include "ios/chrome/browser/signin/oauth2_token_service_factory.h"
18 #include "ios/chrome/browser/signin/signin_client_factory.h"
19 #include "ios/chrome/browser/signin/signin_manager_factory_observer.h"
20 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
24 SigninManagerFactory::SigninManagerFactory()
25 : BrowserStateKeyedServiceFactory(
27 BrowserStateDependencyManager::GetInstance()) {
28 DependsOn(SigninClientFactory::GetInstance());
29 DependsOn(ios::GaiaCookieManagerServiceFactory::GetInstance());
30 DependsOn(OAuth2TokenServiceFactory::GetInstance());
31 DependsOn(ios::AccountTrackerServiceFactory::GetInstance());
34 SigninManagerFactory::~SigninManagerFactory() {}
37 SigninManager
* SigninManagerFactory::GetForBrowserState(
38 ios::ChromeBrowserState
* browser_state
) {
39 return static_cast<SigninManager
*>(
40 GetInstance()->GetServiceForBrowserState(browser_state
, true));
44 SigninManager
* SigninManagerFactory::GetForBrowserStateIfExists(
45 ios::ChromeBrowserState
* browser_state
) {
46 return static_cast<SigninManager
*>(
47 GetInstance()->GetServiceForBrowserState(browser_state
, false));
51 SigninManagerFactory
* SigninManagerFactory::GetInstance() {
52 return Singleton
<SigninManagerFactory
>::get();
55 void SigninManagerFactory::RegisterBrowserStatePrefs(
56 user_prefs::PrefRegistrySyncable
* registry
) {
57 SigninManagerBase::RegisterProfilePrefs(registry
);
61 void SigninManagerFactory::RegisterPrefs(PrefRegistrySimple
* registry
) {
62 SigninManagerBase::RegisterPrefs(registry
);
65 void SigninManagerFactory::AddObserver(SigninManagerFactoryObserver
* observer
) {
66 observer_list_
.AddObserver(observer
);
69 void SigninManagerFactory::RemoveObserver(
70 SigninManagerFactoryObserver
* observer
) {
71 observer_list_
.RemoveObserver(observer
);
74 void SigninManagerFactory::NotifyObserversOfSigninManagerCreationForTesting(
75 SigninManager
* manager
) {
76 FOR_EACH_OBSERVER(SigninManagerFactoryObserver
, observer_list_
,
77 SigninManagerCreated(manager
));
80 scoped_ptr
<KeyedService
> SigninManagerFactory::BuildServiceInstanceFor(
81 web::BrowserState
* context
) const {
82 ios::ChromeBrowserState
* chrome_browser_state
=
83 ios::ChromeBrowserState::FromBrowserState(context
);
84 scoped_ptr
<SigninManager
> service(new SigninManager(
85 SigninClientFactory::GetForBrowserState(chrome_browser_state
),
86 OAuth2TokenServiceFactory::GetForBrowserState(chrome_browser_state
),
87 ios::AccountTrackerServiceFactory::GetForBrowserState(
88 chrome_browser_state
),
89 ios::GaiaCookieManagerServiceFactory::GetForBrowserState(
90 chrome_browser_state
)));
91 service
->Initialize(GetApplicationContext()->GetLocalState());
92 FOR_EACH_OBSERVER(SigninManagerFactoryObserver
, observer_list_
,
93 SigninManagerCreated(service
.get()));
94 return service
.Pass();
97 void SigninManagerFactory::BrowserStateShutdown(web::BrowserState
* context
) {
98 SigninManager
* manager
=
99 static_cast<SigninManager
*>(GetServiceForBrowserState(context
, false));
101 FOR_EACH_OBSERVER(SigninManagerFactoryObserver
, observer_list_
,
102 SigninManagerShutdown(manager
));
103 BrowserStateKeyedServiceFactory::BrowserStateShutdown(context
);