Home | History | Annotate | Download | only in signin
      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.
      4 
      5 #include "chrome/browser/signin/signin_manager_factory.h"
      6 
      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/pref_registry/pref_registry_syncable.h"
     16 #include "components/signin/core/browser/signin_manager.h"
     17 
     18 SigninManagerFactory::SigninManagerFactory()
     19     : BrowserContextKeyedServiceFactory(
     20         "SigninManager",
     21         BrowserContextDependencyManager::GetInstance()) {
     22   DependsOn(ChromeSigninClientFactory::GetInstance());
     23   DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
     24 }
     25 
     26 SigninManagerFactory::~SigninManagerFactory() {
     27 }
     28 
     29 #if defined(OS_CHROMEOS)
     30 // static
     31 SigninManagerBase* SigninManagerFactory::GetForProfileIfExists(
     32     Profile* profile) {
     33   return static_cast<SigninManagerBase*>(
     34       GetInstance()->GetServiceForBrowserContext(profile, false));
     35 }
     36 
     37 // static
     38 SigninManagerBase* SigninManagerFactory::GetForProfile(
     39     Profile* profile) {
     40   return static_cast<SigninManagerBase*>(
     41       GetInstance()->GetServiceForBrowserContext(profile, true));
     42 }
     43 
     44 #else
     45 // static
     46 SigninManager* SigninManagerFactory::GetForProfile(Profile* profile) {
     47   return static_cast<SigninManager*>(
     48       GetInstance()->GetServiceForBrowserContext(profile, true));
     49 }
     50 
     51 // static
     52 SigninManager* SigninManagerFactory::GetForProfileIfExists(Profile* profile) {
     53   return static_cast<SigninManager*>(
     54       GetInstance()->GetServiceForBrowserContext(profile, false));
     55 }
     56 #endif
     57 
     58 // static
     59 SigninManagerFactory* SigninManagerFactory::GetInstance() {
     60   return Singleton<SigninManagerFactory>::get();
     61 }
     62 
     63 void SigninManagerFactory::RegisterProfilePrefs(
     64     user_prefs::PrefRegistrySyncable* registry) {
     65   registry->RegisterStringPref(
     66       prefs::kGoogleServicesLastUsername,
     67       std::string(),
     68       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     69   registry->RegisterStringPref(
     70       prefs::kGoogleServicesUserAccountId,
     71       std::string(),
     72       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     73   registry->RegisterStringPref(
     74       prefs::kGoogleServicesUsername,
     75       std::string(),
     76       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     77   registry->RegisterStringPref(
     78       prefs::kGoogleServicesSigninScopedDeviceId,
     79       std::string(),
     80       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     81   registry->RegisterBooleanPref(
     82       prefs::kAutologinEnabled,
     83       true,
     84       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     85   registry->RegisterBooleanPref(
     86       prefs::kReverseAutologinEnabled,
     87       true,
     88       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     89   registry->RegisterListPref(prefs::kReverseAutologinRejectedEmailList,
     90                              new base::ListValue,
     91                              user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     92   registry->RegisterInt64Pref(
     93       prefs::kSignedInTime,
     94       base::Time().ToInternalValue(),
     95       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     96   chrome::RegisterLocalAuthPrefs(registry);
     97 }
     98 
     99 // static
    100 void SigninManagerFactory::RegisterPrefs(PrefRegistrySimple* registry) {
    101   registry->RegisterStringPref(prefs::kGoogleServicesUsernamePattern,
    102                                std::string());
    103 }
    104 
    105 void SigninManagerFactory::AddObserver(Observer* observer) {
    106   observer_list_.AddObserver(observer);
    107 }
    108 
    109 void SigninManagerFactory::RemoveObserver(Observer* observer) {
    110   observer_list_.RemoveObserver(observer);
    111 }
    112 
    113 void SigninManagerFactory::NotifyObserversOfSigninManagerCreationForTesting(
    114     SigninManagerBase* manager) {
    115   FOR_EACH_OBSERVER(Observer, observer_list_, SigninManagerCreated(manager));
    116 }
    117 
    118 KeyedService* SigninManagerFactory::BuildServiceInstanceFor(
    119     content::BrowserContext* context) const {
    120   SigninManagerBase* service = NULL;
    121   Profile* profile = static_cast<Profile*>(context);
    122   SigninClient* client =
    123       ChromeSigninClientFactory::GetInstance()->GetForProfile(profile);
    124 #if defined(OS_CHROMEOS)
    125   service = new SigninManagerBase(client);
    126 #else
    127   service = new SigninManager(
    128       client, ProfileOAuth2TokenServiceFactory::GetForProfile(profile));
    129 #endif
    130   service->Initialize(g_browser_process->local_state());
    131   FOR_EACH_OBSERVER(Observer, observer_list_, SigninManagerCreated(service));
    132   return service;
    133 }
    134 
    135 void SigninManagerFactory::BrowserContextShutdown(
    136     content::BrowserContext* context) {
    137   SigninManagerBase* manager = static_cast<SigninManagerBase*>(
    138       GetServiceForBrowserContext(context, false));
    139   if (manager)
    140     FOR_EACH_OBSERVER(Observer, observer_list_, SigninManagerShutdown(manager));
    141   BrowserContextKeyedServiceFactory::BrowserContextShutdown(context);
    142 }
    143