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/account_tracker_service_factory.h"
11 #include "chrome/browser/signin/chrome_signin_client_factory.h"
12 #include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
13 #include "chrome/browser/signin/local_auth.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/keyed_service/content/browser_context_dependency_manager.h"
17 #include "components/pref_registry/pref_registry_syncable.h"
18 #include "components/signin/core/browser/signin_manager.h"
20 SigninManagerFactory::SigninManagerFactory()
21 : BrowserContextKeyedServiceFactory(
23 BrowserContextDependencyManager::GetInstance()) {
24 DependsOn(ChromeSigninClientFactory::GetInstance());
25 DependsOn(GaiaCookieManagerServiceFactory::GetInstance());
26 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
27 DependsOn(AccountTrackerServiceFactory::GetInstance());
30 SigninManagerFactory::~SigninManagerFactory() {
33 #if defined(OS_CHROMEOS)
35 SigninManagerBase
* SigninManagerFactory::GetForProfileIfExists(
37 return static_cast<SigninManagerBase
*>(
38 GetInstance()->GetServiceForBrowserContext(profile
, false));
41 const SigninManagerBase
* SigninManagerFactory::GetForProfileIfExists(
42 const Profile
* profile
) {
43 return static_cast<const SigninManagerBase
*>(
44 GetInstance()->GetServiceForBrowserContext(
45 const_cast<Profile
*>(profile
), false));
49 SigninManagerBase
* SigninManagerFactory::GetForProfile(
51 return static_cast<SigninManagerBase
*>(
52 GetInstance()->GetServiceForBrowserContext(profile
, true));
57 SigninManager
* SigninManagerFactory::GetForProfile(Profile
* profile
) {
58 return static_cast<SigninManager
*>(
59 GetInstance()->GetServiceForBrowserContext(profile
, true));
63 SigninManager
* SigninManagerFactory::GetForProfileIfExists(Profile
* profile
) {
64 return static_cast<SigninManager
*>(
65 GetInstance()->GetServiceForBrowserContext(profile
, false));
69 const SigninManager
* SigninManagerFactory::GetForProfileIfExists(
70 const Profile
* profile
) {
71 return static_cast<const SigninManager
*>(
72 GetInstance()->GetServiceForBrowserContext(
73 const_cast<Profile
*>(profile
), false));
78 SigninManagerFactory
* SigninManagerFactory::GetInstance() {
79 return Singleton
<SigninManagerFactory
>::get();
82 void SigninManagerFactory::RegisterProfilePrefs(
83 user_prefs::PrefRegistrySyncable
* registry
) {
84 registry
->RegisterStringPref(
85 prefs::kGoogleServicesHostedDomain
,
87 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
88 registry
->RegisterStringPref(
89 prefs::kGoogleServicesLastUsername
,
91 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
92 registry
->RegisterInt64Pref(
93 prefs::kGoogleServicesRefreshTokenAnnotateScheduledTime
,
94 base::Time().ToInternalValue(),
95 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
96 registry
->RegisterStringPref(
97 prefs::kGoogleServicesSigninScopedDeviceId
,
99 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
100 registry
->RegisterStringPref(
101 prefs::kGoogleServicesUserAccountId
,
103 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
104 registry
->RegisterStringPref(
105 prefs::kGoogleServicesUsername
,
107 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
108 registry
->RegisterBooleanPref(
109 prefs::kAutologinEnabled
,
111 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
112 registry
->RegisterBooleanPref(
113 prefs::kReverseAutologinEnabled
,
115 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
116 registry
->RegisterListPref(prefs::kReverseAutologinRejectedEmailList
,
118 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
119 registry
->RegisterInt64Pref(
120 prefs::kSignedInTime
,
121 base::Time().ToInternalValue(),
122 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
123 LocalAuth::RegisterLocalAuthPrefs(registry
);
127 void SigninManagerFactory::RegisterPrefs(PrefRegistrySimple
* registry
) {
128 registry
->RegisterStringPref(prefs::kGoogleServicesUsernamePattern
,
132 void SigninManagerFactory::AddObserver(Observer
* observer
) {
133 observer_list_
.AddObserver(observer
);
136 void SigninManagerFactory::RemoveObserver(Observer
* observer
) {
137 observer_list_
.RemoveObserver(observer
);
140 void SigninManagerFactory::NotifyObserversOfSigninManagerCreationForTesting(
141 SigninManagerBase
* manager
) {
142 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerCreated(manager
));
145 KeyedService
* SigninManagerFactory::BuildServiceInstanceFor(
146 content::BrowserContext
* context
) const {
147 SigninManagerBase
* service
= NULL
;
148 Profile
* profile
= static_cast<Profile
*>(context
);
149 SigninClient
* client
=
150 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile
);
151 #if defined(OS_CHROMEOS)
152 service
= new SigninManagerBase(client
);
154 service
= new SigninManager(
156 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
157 AccountTrackerServiceFactory::GetForProfile(profile
),
158 GaiaCookieManagerServiceFactory::GetForProfile(profile
));
160 service
->Initialize(g_browser_process
->local_state());
161 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerCreated(service
));
165 void SigninManagerFactory::BrowserContextShutdown(
166 content::BrowserContext
* context
) {
167 SigninManagerBase
* manager
= static_cast<SigninManagerBase
*>(
168 GetServiceForBrowserContext(context
, false));
170 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerShutdown(manager
));
171 BrowserContextKeyedServiceFactory::BrowserContextShutdown(context
);