15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_registry_simple.h"
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/browser_process.h"
9effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/profiles/profile.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/signin/chrome_signin_client_factory.h"
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/signin/local_auth.h"
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/content/browser_context_dependency_manager.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h"
16e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SigninManagerFactory::SigninManagerFactory()
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    : BrowserContextKeyedServiceFactory(
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        "SigninManager",
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)        BrowserContextDependencyManager::GetInstance()) {
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DependsOn(ChromeSigninClientFactory::GetInstance());
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)SigninManagerFactory::~SigninManagerFactory() {
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_CHROMEOS)
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)SigninManagerBase* SigninManagerFactory::GetForProfileIfExists(
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    Profile* profile) {
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return static_cast<SigninManagerBase*>(
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetInstance()->GetServiceForBrowserContext(profile, false));
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// static
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)SigninManagerBase* SigninManagerFactory::GetForProfile(
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    Profile* profile) {
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return static_cast<SigninManagerBase*>(
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetInstance()->GetServiceForBrowserContext(profile, true));
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#else
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SigninManager* SigninManagerFactory::GetForProfile(Profile* profile) {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return static_cast<SigninManager*>(
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetInstance()->GetServiceForBrowserContext(profile, true));
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SigninManager* SigninManagerFactory::GetForProfileIfExists(Profile* profile) {
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return static_cast<SigninManager*>(
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      GetInstance()->GetServiceForBrowserContext(profile, false));
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SigninManagerFactory* SigninManagerFactory::GetInstance() {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return Singleton<SigninManagerFactory>::get();
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid SigninManagerFactory::RegisterProfilePrefs(
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterStringPref(
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      prefs::kGoogleServicesLastUsername,
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::string(),
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterStringPref(
70a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      prefs::kGoogleServicesUserAccountId,
71a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      std::string(),
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
73a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  registry->RegisterStringPref(
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      prefs::kGoogleServicesUsername,
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      std::string(),
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  registry->RegisterStringPref(
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      prefs::kGoogleServicesSigninScopedDeviceId,
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      std::string(),
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterBooleanPref(
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      prefs::kAutologinEnabled,
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      true,
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterBooleanPref(
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      prefs::kReverseAutologinEnabled,
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      true,
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  registry->RegisterListPref(prefs::kReverseAutologinRejectedEmailList,
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                             new base::ListValue,
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  registry->RegisterInt64Pref(
936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      prefs::kSignedInTime,
946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      base::Time().ToInternalValue(),
956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  chrome::RegisterLocalAuthPrefs(registry);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void SigninManagerFactory::RegisterPrefs(PrefRegistrySimple* registry) {
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterStringPref(prefs::kGoogleServicesUsernamePattern,
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                               std::string());
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SigninManagerFactory::AddObserver(Observer* observer) {
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  observer_list_.AddObserver(observer);
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SigninManagerFactory::RemoveObserver(Observer* observer) {
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  observer_list_.RemoveObserver(observer);
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SigninManagerFactory::NotifyObserversOfSigninManagerCreationForTesting(
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SigninManagerBase* manager) {
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FOR_EACH_OBSERVER(Observer, observer_list_, SigninManagerCreated(manager));
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)KeyedService* SigninManagerFactory::BuildServiceInstanceFor(
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    content::BrowserContext* context) const {
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SigninManagerBase* service = NULL;
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  Profile* profile = static_cast<Profile*>(context);
122effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SigninClient* client =
123effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      ChromeSigninClientFactory::GetInstance()->GetForProfile(profile);
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#if defined(OS_CHROMEOS)
125effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  service = new SigninManagerBase(client);
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#else
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  service = new SigninManager(
128effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      client, ProfileOAuth2TokenServiceFactory::GetForProfile(profile));
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif
130e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  service->Initialize(g_browser_process->local_state());
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FOR_EACH_OBSERVER(Observer, observer_list_, SigninManagerCreated(service));
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return service;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void SigninManagerFactory::BrowserContextShutdown(
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    content::BrowserContext* context) {
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SigninManagerBase* manager = static_cast<SigninManagerBase*>(
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GetServiceForBrowserContext(context, false));
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (manager)
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    FOR_EACH_OBSERVER(Observer, observer_list_, SigninManagerShutdown(manager));
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  BrowserContextKeyedServiceFactory::BrowserContextShutdown(context);
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
143