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/signin/chrome_signin_manager_delegate.h"
     10 #include "chrome/browser/signin/signin_manager.h"
     11 #include "chrome/browser/signin/token_service_factory.h"
     12 #include "chrome/common/pref_names.h"
     13 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
     14 #include "components/user_prefs/pref_registry_syncable.h"
     15 
     16 SigninManagerFactory::SigninManagerFactory()
     17     : BrowserContextKeyedServiceFactory(
     18         "SigninManager",
     19         BrowserContextDependencyManager::GetInstance()) {
     20   DependsOn(TokenServiceFactory::GetInstance());
     21 }
     22 
     23 SigninManagerFactory::~SigninManagerFactory() {}
     24 
     25 #if defined(OS_CHROMEOS)
     26 // static
     27 SigninManagerBase* SigninManagerFactory::GetForProfileIfExists(
     28     Profile* profile) {
     29   return static_cast<SigninManagerBase*>(
     30       GetInstance()->GetServiceForBrowserContext(profile, false));
     31 }
     32 
     33 // static
     34 SigninManagerBase* SigninManagerFactory::GetForProfile(
     35     Profile* profile) {
     36   return static_cast<SigninManagerBase*>(
     37       GetInstance()->GetServiceForBrowserContext(profile, true));
     38 }
     39 
     40 #else
     41 // static
     42 SigninManager* SigninManagerFactory::GetForProfile(Profile* profile) {
     43   return static_cast<SigninManager*>(
     44       GetInstance()->GetServiceForBrowserContext(profile, true));
     45 }
     46 
     47 // static
     48 SigninManager* SigninManagerFactory::GetForProfileIfExists(Profile* profile) {
     49   return static_cast<SigninManager*>(
     50       GetInstance()->GetServiceForBrowserContext(profile, false));
     51 }
     52 #endif
     53 
     54 // static
     55 SigninManagerFactory* SigninManagerFactory::GetInstance() {
     56   return Singleton<SigninManagerFactory>::get();
     57 }
     58 
     59 void SigninManagerFactory::RegisterProfilePrefs(
     60     user_prefs::PrefRegistrySyncable* registry) {
     61   registry->RegisterStringPref(
     62       prefs::kGoogleServicesLastUsername,
     63       std::string(),
     64       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     65   registry->RegisterStringPref(
     66       prefs::kGoogleServicesUsername,
     67       std::string(),
     68       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     69   registry->RegisterBooleanPref(
     70       prefs::kAutologinEnabled,
     71       true,
     72       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     73   registry->RegisterBooleanPref(
     74       prefs::kReverseAutologinEnabled,
     75       true,
     76       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     77   registry->RegisterListPref(prefs::kReverseAutologinRejectedEmailList,
     78                              new ListValue,
     79                              user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     80 }
     81 
     82 // static
     83 void SigninManagerFactory::RegisterPrefs(PrefRegistrySimple* registry) {
     84   registry->RegisterStringPref(prefs::kGoogleServicesUsernamePattern,
     85                                std::string());
     86 }
     87 
     88 BrowserContextKeyedService* SigninManagerFactory::BuildServiceInstanceFor(
     89     content::BrowserContext* context) const {
     90   SigninManagerBase* service = NULL;
     91   Profile* profile = static_cast<Profile*>(context);
     92 #if defined(OS_CHROMEOS)
     93   service = new SigninManagerBase();
     94 #else
     95   service = new SigninManager(
     96       scoped_ptr<SigninManagerDelegate>(
     97           new ChromeSigninManagerDelegate(profile)));
     98 #endif
     99   service->Initialize(profile, g_browser_process->local_state());
    100   return service;
    101 }
    102