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; |
+} |
+ |
+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()) { |
+ // 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_|. |
+ 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) |
+ 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), |
xiyuan
2013/04/04 21:24:54
nit: should it be initialized to LOGGED_IN_UNKNOWN
stevenjb
2013/04/04 21:35:47
I got rid of LOGGED_IN_UNKNOWN, it doesn't really
|
+ 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 |