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(prefs::kGoogleServicesHostedDomain
,
86 registry
->RegisterStringPref(prefs::kGoogleServicesLastUsername
,
88 registry
->RegisterInt64Pref(
89 prefs::kGoogleServicesRefreshTokenAnnotateScheduledTime
,
90 base::Time().ToInternalValue());
91 registry
->RegisterStringPref(prefs::kGoogleServicesSigninScopedDeviceId
,
93 registry
->RegisterStringPref(prefs::kGoogleServicesAccountId
, std::string());
94 registry
->RegisterStringPref(prefs::kGoogleServicesUserAccountId
,
96 registry
->RegisterBooleanPref(prefs::kAutologinEnabled
, true);
97 registry
->RegisterBooleanPref(prefs::kReverseAutologinEnabled
, true);
98 registry
->RegisterListPref(prefs::kReverseAutologinRejectedEmailList
,
100 registry
->RegisterInt64Pref(prefs::kSignedInTime
,
101 base::Time().ToInternalValue());
103 LocalAuth::RegisterLocalAuthPrefs(registry
);
105 // Deprecated prefs: will be removed in a future release.
106 registry
->RegisterStringPref(prefs::kGoogleServicesUsername
, std::string());
110 void SigninManagerFactory::RegisterPrefs(PrefRegistrySimple
* registry
) {
111 registry
->RegisterStringPref(prefs::kGoogleServicesUsernamePattern
,
115 void SigninManagerFactory::AddObserver(Observer
* observer
) {
116 observer_list_
.AddObserver(observer
);
119 void SigninManagerFactory::RemoveObserver(Observer
* observer
) {
120 observer_list_
.RemoveObserver(observer
);
123 void SigninManagerFactory::NotifyObserversOfSigninManagerCreationForTesting(
124 SigninManagerBase
* manager
) {
125 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerCreated(manager
));
128 KeyedService
* SigninManagerFactory::BuildServiceInstanceFor(
129 content::BrowserContext
* context
) const {
130 SigninManagerBase
* service
= NULL
;
131 Profile
* profile
= static_cast<Profile
*>(context
);
132 SigninClient
* client
=
133 ChromeSigninClientFactory::GetInstance()->GetForProfile(profile
);
134 #if defined(OS_CHROMEOS)
135 service
= new SigninManagerBase(
137 AccountTrackerServiceFactory::GetForProfile(profile
));
139 service
= new SigninManager(
141 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
142 AccountTrackerServiceFactory::GetForProfile(profile
),
143 GaiaCookieManagerServiceFactory::GetForProfile(profile
));
145 service
->Initialize(g_browser_process
->local_state());
146 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerCreated(service
));
150 void SigninManagerFactory::BrowserContextShutdown(
151 content::BrowserContext
* context
) {
152 SigninManagerBase
* manager
= static_cast<SigninManagerBase
*>(
153 GetServiceForBrowserContext(context
, false));
155 FOR_EACH_OBSERVER(Observer
, observer_list_
, SigninManagerShutdown(manager
));
156 BrowserContextKeyedServiceFactory::BrowserContextShutdown(context
);