Chromium Code Reviews| Index: chromeos/login/login_state.cc |
| diff --git a/chromeos/login/login_state.cc b/chromeos/login/login_state.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1be6df07ca1ead50a8abf1265dcf17a89f63fe88 |
| --- /dev/null |
| +++ b/chromeos/login/login_state.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chromeos/login/login_state.h" |
| + |
| +#include "base/chromeos/chromeos_version.h" |
| +#include "base/command_line.h" |
| +#include "base/logging.h" |
| +#include "chromeos/chromeos_switches.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +// When running a Chrome OS build outside of a device (i.e. on a developer's |
| +// workstation) and not running as login-manager, pretend like we're always |
| +// logged in. |
| +bool AlwaysLoggedIn() { |
| + return !base::chromeos::IsRunningOnChromeOS() && |
| + !CommandLine::ForCurrentProcess()->HasSwitch(switches::kLoginManager); |
| +} |
| + |
| +} // namespace |
| + |
| +static LoginState* g_login_state = NULL; |
| + |
| +// static |
| +void LoginState::Initialize() { |
| + CHECK(!g_login_state); |
| + g_login_state = new LoginState(); |
| +} |
| + |
| +// static |
| +void LoginState::Shutdown() { |
| + CHECK(g_login_state); |
| + delete g_login_state; |
| + g_login_state = NULL; |
| +} |
| + |
| +// static |
| +LoginState* LoginState::Get() { |
| + CHECK(g_login_state) << "LoginState::Get() called before Initialize()"; |
| + return g_login_state; |
| +} |
| + |
| +// static |
| +bool LoginState::IsInitialized() { |
| + return !!g_login_state; |
|
bartfab (slow)
2013/04/05 13:16:33
Nit: The !! for converting to a bool are useful in
stevenjb
2013/04/05 18:17:15
We have this a lot in our code; I forget where I r
|
| +} |
| + |
| +void LoginState::AddObserver(Observer* observer) { |
| + observer_list_.AddObserver(observer); |
| +} |
| + |
| +void LoginState::RemoveObserver(Observer* observer) { |
| + observer_list_.RemoveObserver(observer); |
| +} |
| + |
| +void LoginState::SetLoginState(LoginState::State state) { |
| + if (state == state_) |
| + return; |
| + VLOG(1) << "Login State: " << state; |
| + state_ = state; |
| + NotifyObservers(); |
| +} |
| + |
| +LoginState::State LoginState::GetLoginState() const { |
| + if (state_ == LOGGED_IN_OOBE || state_ == LOGGED_IN_NONE) { |
| + if (AlwaysLoggedIn()) { |
|
Nikita (slow)
2013/04/05 04:08:53
niut: I don't think that you specifically need thi
stevenjb
2013/04/05 18:17:15
I don't want to change this logic with this CL. I
|
| + // Fake a login state when running in a dev environment. |
| + if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kGuestSession)) |
| + return LOGGED_IN_GUEST; |
| + else |
| + return LOGGED_IN_OWNER; |
| + } |
| + return state_; |
| + } |
| + // |state_| may get set before |session_started_|. |
|
bartfab (slow)
2013/04/05 13:16:33
|state_| actually always gets set before |session_
stevenjb
2013/04/05 18:17:15
I didn't want to make any assumptions or change th
bartfab (slow)
2013/04/08 15:18:36
I agree that changing existing functionality is al
|
| + if (!session_started_) |
| + return LOGGED_IN_NONE; |
| + if (screen_locked_) |
| + return LOGGED_IN_LOCKED; |
| + return state_; |
| +} |
| + |
| +bool LoginState::IsUserLoggedIn() { |
| + State login_state = GetLoginState(); |
| + if (login_state == LOGGED_IN_OOBE || login_state == LOGGED_IN_NONE) |
|
bartfab (slow)
2013/04/05 13:16:33
Another case where a consumer has to know about th
|
| + return false; |
| + return true; |
| +} |
| + |
| +void LoginState::SetSessionStarted(bool session_started) { |
| + session_started_ = session_started; |
| + NotifyObservers(); |
| +} |
| + |
| +bool LoginState::IsSessionStarted() const { |
| + return session_started_; |
| +} |
| + |
| +// Private methods |
| + |
| +LoginState::LoginState() : state_(LOGGED_IN_OOBE), |
| + session_started_(false), |
| + screen_locked_(false) { |
| + DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this); |
| +} |
| + |
| +LoginState::~LoginState() { |
| + DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver(this); |
| +} |
| + |
| +void LoginState::LockScreen() { |
| + screen_locked_ = true; |
| + NotifyObservers(); |
| +} |
| + |
| +void LoginState::UnlockScreen() { |
| + screen_locked_ = false; |
| + NotifyObservers(); |
| +} |
| + |
| +void LoginState::NotifyObservers() { |
| + FOR_EACH_OBSERVER(LoginState::Observer, observer_list_, |
| + LoginStateChanged(GetLoginState())); |
| +} |
| + |
| +} // namespace chromeos |