| OLD | NEW |
| (Empty) |
| 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/chromeos/login/login_utils.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/singleton.h" | |
| 12 #include "chrome/browser/chromeos/login/auth/chrome_cryptohome_authenticator.h" | |
| 13 #include "chrome/browser/chromeos/login/lock/screen_locker.h" | |
| 14 #include "chrome/browser/chromeos/login/session/user_session_manager.h" | |
| 15 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h" | |
| 16 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
| 17 #include "chromeos/chromeos_switches.h" | |
| 18 #include "chromeos/settings/cros_settings_names.h" | |
| 19 | |
| 20 namespace chromeos { | |
| 21 | |
| 22 class LoginUtilsImpl : public LoginUtils, | |
| 23 public UserSessionManagerDelegate { | |
| 24 public: | |
| 25 LoginUtilsImpl() | |
| 26 : delegate_(NULL) { | |
| 27 } | |
| 28 | |
| 29 ~LoginUtilsImpl() override {} | |
| 30 | |
| 31 // LoginUtils implementation: | |
| 32 void DoBrowserLaunch(Profile* profile, LoginDisplayHost* login_host) override; | |
| 33 void PrepareProfile(const UserContext& user_context, | |
| 34 bool has_auth_cookies, | |
| 35 bool has_active_session, | |
| 36 LoginUtils::Delegate* delegate) override; | |
| 37 void DelegateDeleted(LoginUtils::Delegate* delegate) override; | |
| 38 scoped_refptr<Authenticator> CreateAuthenticator( | |
| 39 AuthStatusConsumer* consumer) override; | |
| 40 | |
| 41 // UserSessionManager::Delegate implementation: | |
| 42 void OnProfilePrepared(Profile* profile, bool browser_launched) override; | |
| 43 | |
| 44 private: | |
| 45 // Has to be scoped_refptr, see comment for CreateAuthenticator(...). | |
| 46 scoped_refptr<Authenticator> authenticator_; | |
| 47 | |
| 48 // Delegate to be fired when the profile will be prepared. | |
| 49 LoginUtils::Delegate* delegate_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(LoginUtilsImpl); | |
| 52 }; | |
| 53 | |
| 54 class LoginUtilsWrapper { | |
| 55 public: | |
| 56 static LoginUtilsWrapper* GetInstance() { | |
| 57 return Singleton<LoginUtilsWrapper>::get(); | |
| 58 } | |
| 59 | |
| 60 LoginUtils* get() { | |
| 61 base::AutoLock create(create_lock_); | |
| 62 if (!ptr_.get()) | |
| 63 reset(new LoginUtilsImpl); | |
| 64 return ptr_.get(); | |
| 65 } | |
| 66 | |
| 67 void reset(LoginUtils* ptr) { | |
| 68 ptr_.reset(ptr); | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 friend struct DefaultSingletonTraits<LoginUtilsWrapper>; | |
| 73 | |
| 74 LoginUtilsWrapper() {} | |
| 75 | |
| 76 base::Lock create_lock_; | |
| 77 scoped_ptr<LoginUtils> ptr_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(LoginUtilsWrapper); | |
| 80 }; | |
| 81 | |
| 82 void LoginUtilsImpl::DoBrowserLaunch(Profile* profile, | |
| 83 LoginDisplayHost* login_host) { | |
| 84 UserSessionManager::GetInstance()->DoBrowserLaunch(profile, login_host); | |
| 85 } | |
| 86 | |
| 87 void LoginUtilsImpl::PrepareProfile( | |
| 88 const UserContext& user_context, | |
| 89 bool has_auth_cookies, | |
| 90 bool has_active_session, | |
| 91 LoginUtils::Delegate* delegate) { | |
| 92 // TODO(nkostylev): We have to initialize LoginUtils delegate as long | |
| 93 // as it coexist with SessionManager. | |
| 94 delegate_ = delegate; | |
| 95 | |
| 96 UserSessionManager::StartSessionType start_session_type = | |
| 97 UserAddingScreen::Get()->IsRunning() ? | |
| 98 UserSessionManager::SECONDARY_USER_SESSION : | |
| 99 UserSessionManager::PRIMARY_USER_SESSION; | |
| 100 | |
| 101 // For the transition part LoginUtils will just delegate profile | |
| 102 // creation and initialization to SessionManager. Later LoginUtils will be | |
| 103 // removed and all LoginUtils clients will just work with SessionManager | |
| 104 // directly. | |
| 105 UserSessionManager::GetInstance()->StartSession(user_context, | |
| 106 start_session_type, | |
| 107 authenticator_, | |
| 108 has_auth_cookies, | |
| 109 has_active_session, | |
| 110 this); | |
| 111 } | |
| 112 | |
| 113 void LoginUtilsImpl::DelegateDeleted(LoginUtils::Delegate* delegate) { | |
| 114 if (delegate_ == delegate) | |
| 115 delegate_ = NULL; | |
| 116 } | |
| 117 | |
| 118 scoped_refptr<Authenticator> LoginUtilsImpl::CreateAuthenticator( | |
| 119 AuthStatusConsumer* consumer) { | |
| 120 // Screen locker needs new Authenticator instance each time. | |
| 121 if (ScreenLocker::default_screen_locker()) { | |
| 122 if (authenticator_.get()) | |
| 123 authenticator_->SetConsumer(NULL); | |
| 124 authenticator_ = NULL; | |
| 125 } | |
| 126 | |
| 127 if (authenticator_.get() == NULL) { | |
| 128 authenticator_ = new ChromeCryptohomeAuthenticator(consumer); | |
| 129 } else { | |
| 130 // TODO(nkostylev): Fix this hack by improving Authenticator dependencies. | |
| 131 authenticator_->SetConsumer(consumer); | |
| 132 } | |
| 133 return authenticator_; | |
| 134 } | |
| 135 | |
| 136 void LoginUtilsImpl::OnProfilePrepared(Profile* profile, | |
| 137 bool browser_launched) { | |
| 138 if (delegate_) | |
| 139 delegate_->OnProfilePrepared(profile, browser_launched); | |
| 140 } | |
| 141 | |
| 142 // static | |
| 143 LoginUtils* LoginUtils::Get() { | |
| 144 return LoginUtilsWrapper::GetInstance()->get(); | |
| 145 } | |
| 146 | |
| 147 // static | |
| 148 void LoginUtils::Set(LoginUtils* mock) { | |
| 149 LoginUtilsWrapper::GetInstance()->reset(mock); | |
| 150 } | |
| 151 | |
| 152 // static | |
| 153 bool LoginUtils::IsWhitelisted(const std::string& username, | |
| 154 bool* wildcard_match) { | |
| 155 // Skip whitelist check for tests. | |
| 156 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 157 chromeos::switches::kOobeSkipPostLogin)) { | |
| 158 return true; | |
| 159 } | |
| 160 | |
| 161 CrosSettings* cros_settings = CrosSettings::Get(); | |
| 162 bool allow_new_user = false; | |
| 163 cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user); | |
| 164 if (allow_new_user) | |
| 165 return true; | |
| 166 return cros_settings->FindEmailInList( | |
| 167 kAccountsPrefUsers, username, wildcard_match); | |
| 168 } | |
| 169 | |
| 170 } // namespace chromeos | |
| OLD | NEW |