1 // Copyright (c) 2012 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 "chrome/browser/signin/signin_manager_factory.h"
7 #include "base/prefs/pref_registry_simple.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/chrome_signin_client_factory.h"
11 #include "chrome/browser/signin/local_auth.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/keyed_service/content/browser_context_dependency_manager.h"
15 #include "components/signin/core/browser/signin_manager.h"
16 #include "components/user_prefs/pref_registry_syncable.h"
18 SigninManagerFactory::SigninManagerFactory()
19 : BrowserContextKeyedServiceFactory(
21 BrowserContextDependencyManager::GetInstance()) {
22 DependsOn(ChromeSigninClientFactory::GetInstance());
23 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
26 SigninManagerFactory::~SigninManagerFactory() {
27 #if defined(OS_MACOSX)
28 // Check that the number of remaining observers is as expected. Mac has a
29 // known issue wherein there might be a remaining observer
30 // (UIAppListViewDelegate).
31 int num_observers
= 0;
32 if (observer_list_
.might_have_observers()) {
33 ObserverListBase
<SigninManagerFactory::Observer
>::Iterator
it(
35 while (it
.GetNext()) {
39 DCHECK_LE(num_observers
, 1);
40 #endif // defined(OS_MACOSX)
43 #if defined(OS_CHROMEOS)
45 SigninManagerBase
* SigninManagerFactory::GetForProfileIfExists(
47 return static_cast<SigninManagerBase
*>(
48 GetInstance()->GetServiceForBrowserContext(profile
, false));
52 SigninManagerBase
* SigninManagerFactory::GetForProfile(
54 return static_cast<SigninManagerBase
*>(
55 GetInstance()->GetServiceForBrowserContext(profile
, true));
60 SigninManager
* SigninManagerFactory::GetForProfile(Profile
* profile
) {
61 return static_cast<SigninManager
*>(
62 GetInstance()->GetServiceForBrowserContext(profile
, true));
66 SigninManager
* SigninManagerFactory::GetForProfileIfExists(Profile
* profile
) {
67 return static_cast<SigninManager
*>(
68 GetInstance()->GetServiceForBrowserContext(profile
, false));
73 SigninManagerFactory
* SigninManagerFactory::GetInstance() {
74 return Singleton
<SigninManagerFactory
>::get();
77 void SigninManagerFactory::RegisterProfilePrefs(
78 user_prefs::PrefRegistrySyncable
* registry
) {
79 registry
->RegisterStringPref(
80 prefs::kGoogleServicesLastUsername
,
82 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
83 registry
->RegisterStringPref(
84 prefs::kGoogleServicesUserAccountId
,
86 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
87 registry
->RegisterStringPref(
88 prefs::kGoogleServicesUsername
,
90 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
91 registry
->RegisterBooleanPref(
92 prefs::kAutologinEnabled
,
94 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
95 registry
->RegisterBooleanPref(
96 prefs::kReverseAutologinEnabled
,
98 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
99 registry
->RegisterListPref(prefs::kReverseAutologinRejectedEmailList
,
101 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
102 chrome::RegisterLocalAuthPrefs(registry
);
106 void SigninManagerFactory::RegisterPrefs(PrefRegistrySimple
* registry
) {
107 registry
->RegisterStringPref(prefs::kGoogleServicesUsernamePattern
,
111 void SigninManagerFactory::AddObserver(Observer
* observer
) {
112 observer_list_
.AddObserver(observer
);
115 void SigninManagerFactory::RemoveObserver(Observer
* observer
) {
116 observer_list_
.RemoveObserver(observer
);
119 void SigninManagerFactory::NotifyObserversOfSigninManagerCreationForTesting(
120 SigninManagerBase
* manager
) {
121 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerCreated(manager
));
124 KeyedService
* SigninManagerFactory::BuildServiceInstanceFor(
125 content::BrowserContext
* context
) const {
126 SigninManagerBase
* service
= NULL
;
127 Profile
* profile
= static_cast<Profile
*>(context
);
128 SigninClient
* client
=
129 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile
);
130 #if defined(OS_CHROMEOS)
131 service
= new SigninManagerBase(client
);
133 service
= new SigninManager(
134 client
, ProfileOAuth2TokenServiceFactory::GetForProfile(profile
));
136 service
->Initialize(g_browser_process
->local_state());
137 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerCreated(service
));
141 void SigninManagerFactory::BrowserContextShutdown(
142 content::BrowserContext
* context
) {
143 SigninManagerBase
* manager
= static_cast<SigninManagerBase
*>(
144 GetServiceForBrowserContext(context
, false));
146 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerShutdown(manager
));
147 BrowserContextKeyedServiceFactory::BrowserContextShutdown(context
);