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/local_auth.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/keyed_service/content/browser_context_dependency_manager.h"
16 #include "components/pref_registry/pref_registry_syncable.h"
17 #include "components/signin/core/browser/signin_manager.h"
19 SigninManagerFactory::SigninManagerFactory()
20 : BrowserContextKeyedServiceFactory(
22 BrowserContextDependencyManager::GetInstance()) {
23 DependsOn(ChromeSigninClientFactory::GetInstance());
24 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
25 DependsOn(AccountTrackerServiceFactory::GetInstance());
28 SigninManagerFactory::~SigninManagerFactory() {
31 #if defined(OS_CHROMEOS)
33 SigninManagerBase
* SigninManagerFactory::GetForProfileIfExists(
35 return static_cast<SigninManagerBase
*>(
36 GetInstance()->GetServiceForBrowserContext(profile
, false));
39 const SigninManagerBase
* SigninManagerFactory::GetForProfileIfExists(
40 const Profile
* profile
) {
41 return static_cast<const SigninManagerBase
*>(
42 GetInstance()->GetServiceForBrowserContext(
43 const_cast<Profile
*>(profile
), false));
47 SigninManagerBase
* SigninManagerFactory::GetForProfile(
49 return static_cast<SigninManagerBase
*>(
50 GetInstance()->GetServiceForBrowserContext(profile
, true));
55 SigninManager
* SigninManagerFactory::GetForProfile(Profile
* profile
) {
56 return static_cast<SigninManager
*>(
57 GetInstance()->GetServiceForBrowserContext(profile
, true));
61 SigninManager
* SigninManagerFactory::GetForProfileIfExists(Profile
* profile
) {
62 return static_cast<SigninManager
*>(
63 GetInstance()->GetServiceForBrowserContext(profile
, false));
67 const SigninManager
* SigninManagerFactory::GetForProfileIfExists(
68 const Profile
* profile
) {
69 return static_cast<const SigninManager
*>(
70 GetInstance()->GetServiceForBrowserContext(
71 const_cast<Profile
*>(profile
), false));
76 SigninManagerFactory
* SigninManagerFactory::GetInstance() {
77 return Singleton
<SigninManagerFactory
>::get();
80 void SigninManagerFactory::RegisterProfilePrefs(
81 user_prefs::PrefRegistrySyncable
* registry
) {
82 registry
->RegisterStringPref(
83 prefs::kGoogleServicesHostedDomain
,
85 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
86 registry
->RegisterStringPref(
87 prefs::kGoogleServicesLastUsername
,
89 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
90 registry
->RegisterInt64Pref(
91 prefs::kGoogleServicesRefreshTokenAnnotateScheduledTime
,
92 base::Time().ToInternalValue(),
93 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
94 registry
->RegisterStringPref(
95 prefs::kGoogleServicesSigninScopedDeviceId
,
97 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
98 registry
->RegisterStringPref(
99 prefs::kGoogleServicesUserAccountId
,
101 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
102 registry
->RegisterStringPref(
103 prefs::kGoogleServicesUsername
,
105 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
106 registry
->RegisterBooleanPref(
107 prefs::kAutologinEnabled
,
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
110 registry
->RegisterBooleanPref(
111 prefs::kReverseAutologinEnabled
,
113 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
114 registry
->RegisterListPref(prefs::kReverseAutologinRejectedEmailList
,
116 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
117 registry
->RegisterInt64Pref(
118 prefs::kSignedInTime
,
119 base::Time().ToInternalValue(),
120 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
121 LocalAuth::RegisterLocalAuthPrefs(registry
);
125 void SigninManagerFactory::RegisterPrefs(PrefRegistrySimple
* registry
) {
126 registry
->RegisterStringPref(prefs::kGoogleServicesUsernamePattern
,
130 void SigninManagerFactory::AddObserver(Observer
* observer
) {
131 observer_list_
.AddObserver(observer
);
134 void SigninManagerFactory::RemoveObserver(Observer
* observer
) {
135 observer_list_
.RemoveObserver(observer
);
138 void SigninManagerFactory::NotifyObserversOfSigninManagerCreationForTesting(
139 SigninManagerBase
* manager
) {
140 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerCreated(manager
));
143 KeyedService
* SigninManagerFactory::BuildServiceInstanceFor(
144 content::BrowserContext
* context
) const {
145 SigninManagerBase
* service
= NULL
;
146 Profile
* profile
= static_cast<Profile
*>(context
);
147 SigninClient
* client
=
148 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile
);
149 #if defined(OS_CHROMEOS)
150 service
= new SigninManagerBase(client
);
152 service
= new SigninManager(
154 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
155 AccountTrackerServiceFactory::GetForProfile(profile
));
157 service
->Initialize(g_browser_process
->local_state());
158 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerCreated(service
));
162 void SigninManagerFactory::BrowserContextShutdown(
163 content::BrowserContext
* context
) {
164 SigninManagerBase
* manager
= static_cast<SigninManagerBase
*>(
165 GetServiceForBrowserContext(context
, false));
167 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerShutdown(manager
));
168 BrowserContextKeyedServiceFactory::BrowserContextShutdown(context
);