diff options
author | stevenjb@chromium.org <stevenjb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-18 19:41:11 +0000 |
---|---|---|
committer | stevenjb@chromium.org <stevenjb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-18 19:41:11 +0000 |
commit | c57397ab5e0deb5145d3dae05f6554bf783710a7 (patch) | |
tree | 35dd178bcac0eb457404157d1e406674a3f67bfa /chromeos/login | |
parent | 48f72eff4997e153cee5972d9cc6a4a73dfbedf4 (diff) | |
download | chromium_src-c57397ab5e0deb5145d3dae05f6554bf783710a7.zip chromium_src-c57397ab5e0deb5145d3dae05f6554bf783710a7.tar.gz chromium_src-c57397ab5e0deb5145d3dae05f6554bf783710a7.tar.bz2 |
Add LoginState class to src/chromeos/login (redux)
This is the same as https://chromiumcodereview.appspot.com/13495003
rebased.
Hopefully the unit tests are more robust now and will not be
broken by the added tests changing test order this time.
BUG=226495
TBR=xiyuan@chromium.org,nkostylev@chromium.org,bartfab@chromium.org,sky@chromium.org
Review URL: https://codereview.chromium.org/14269004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@194986 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos/login')
-rw-r--r-- | chromeos/login/login_state.cc | 105 | ||||
-rw-r--r-- | chromeos/login/login_state.h | 81 | ||||
-rw-r--r-- | chromeos/login/login_state_unittest.cc | 76 |
3 files changed, 262 insertions, 0 deletions
diff --git a/chromeos/login/login_state.cc b/chromeos/login/login_state.cc new file mode 100644 index 0000000..75fed89 --- /dev/null +++ b/chromeos/login/login_state.cc @@ -0,0 +1,105 @@ +// 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" + +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::SetLoggedInState(LoggedInState state, + LoggedInUserType type) { + if (state == logged_in_state_) + return; + VLOG(1) << "LoggedInState: " << state << " UserType: " << type; + logged_in_state_ = state; + logged_in_user_type_ = type; + NotifyObservers(); +} + +LoginState::LoggedInState LoginState::GetLoggedInState() const { + if (AlwaysLoggedIn()) + return LOGGED_IN_ACTIVE; + return logged_in_state_; +} + +LoginState::LoggedInUserType LoginState::GetLoggedInUserType() const { + return logged_in_user_type_; +} + +bool LoginState::IsUserLoggedIn() const { + return GetLoggedInState() == LOGGED_IN_ACTIVE; +} + +bool LoginState::IsUserAuthenticated() const { + LoggedInUserType type = logged_in_user_type_; + return type == chromeos::LoginState::LOGGED_IN_USER_REGULAR || + type == chromeos::LoginState::LOGGED_IN_USER_OWNER || + type == chromeos::LoginState::LOGGED_IN_USER_LOCALLY_MANAGED; +} + +// Private methods + +LoginState::LoginState() : logged_in_state_(LOGGED_IN_OOBE), + logged_in_user_type_(LOGGED_IN_USER_NONE) { +} + +LoginState::~LoginState() { +} + +void LoginState::NotifyObservers() { + FOR_EACH_OBSERVER(LoginState::Observer, observer_list_, + LoggedInStateChanged(GetLoggedInState())); +} + +} // namespace chromeos diff --git a/chromeos/login/login_state.h b/chromeos/login/login_state.h new file mode 100644 index 0000000..1d05788 --- /dev/null +++ b/chromeos/login/login_state.h @@ -0,0 +1,81 @@ +// 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. + +#ifndef CHROMEOS_LOGIN_LOGIN_STATE_H_ +#define CHROMEOS_LOGIN_LOGIN_STATE_H_ + +#include "base/basictypes.h" +#include "base/observer_list.h" +#include "chromeos/chromeos_export.h" + +namespace chromeos { + +// Tracks the login state of chrome, accessible to Ash and other chromeos code. +class CHROMEOS_EXPORT LoginState { + public: + enum LoggedInState { + LOGGED_IN_OOBE, // Out of box experience not completed + LOGGED_IN_NONE, // Not logged in + LOGGED_IN_ACTIVE // A user has logged in + }; + + enum LoggedInUserType { + LOGGED_IN_USER_NONE, // User is not logged in + LOGGED_IN_USER_REGULAR, // A regular user is logged in + LOGGED_IN_USER_OWNER, // The owner of the device is logged in + LOGGED_IN_USER_GUEST, // A guest is logged in (i.e. incognito) + LOGGED_IN_USER_RETAIL_MODE, // Is in retail mode + LOGGED_IN_USER_PUBLIC_ACCOUNT, // A public account is logged in + LOGGED_IN_USER_LOCALLY_MANAGED, // A locally managed user is logged in + LOGGED_IN_USER_KIOSK_APP // Is in kiosk app mode + }; + + class Observer { + public: + // Called when the login state changes. + virtual void LoggedInStateChanged(LoggedInState state) = 0; + + protected: + virtual ~Observer() {} + }; + + // Manage singleton instance. + static void Initialize(); + static void Shutdown(); + static LoginState* Get(); + static bool IsInitialized(); + + // Add/remove observers. + void AddObserver(Observer* observer); + void RemoveObserver(Observer* observer); + + // Set the logged in state and user type. + void SetLoggedInState(LoggedInState state, LoggedInUserType type); + + // Get the logged in state / user type. + LoggedInState GetLoggedInState() const; + LoggedInUserType GetLoggedInUserType() const; + + // Returns true if |logged_in_state_| is active. + bool IsUserLoggedIn() const; + + // Returns true if the user is an authenticated user (i.e. non public account) + bool IsUserAuthenticated() const; + + private: + LoginState(); + virtual ~LoginState(); + + void NotifyObservers(); + + LoggedInState logged_in_state_; + LoggedInUserType logged_in_user_type_; + ObserverList<Observer> observer_list_; + + DISALLOW_COPY_AND_ASSIGN(LoginState); +}; + +} // namespace chromeos + +#endif // CHROMEOS_LOGIN_LOGIN_STATE_H_ diff --git a/chromeos/login/login_state_unittest.cc b/chromeos/login/login_state_unittest.cc new file mode 100644 index 0000000..3522e49 --- /dev/null +++ b/chromeos/login/login_state_unittest.cc @@ -0,0 +1,76 @@ +// Copyright (c) 2012 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/command_line.h" +#include "base/compiler_specific.h" +#include "base/message_loop.h" +#include "chromeos/chromeos_switches.h" +#include "chromeos/dbus/dbus_thread_manager.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace chromeos { + +class LoginStateTest : public testing::Test, + public LoginState::Observer { + public: + LoginStateTest() + : logged_in_state_(LoginState::LOGGED_IN_OOBE), + logged_in_user_type_(LoginState::LOGGED_IN_USER_NONE) { + } + virtual ~LoginStateTest() { + } + + // testing::Test + virtual void SetUp() OVERRIDE { + CommandLine::ForCurrentProcess()->AppendSwitch(switches::kLoginManager); + // Initialize DBusThreadManager with a stub implementation. + DBusThreadManager::InitializeWithStub(); + LoginState::Initialize(); + LoginState::Get()->AddObserver(this); + } + + virtual void TearDown() OVERRIDE { + LoginState::Get()->RemoveObserver(this); + LoginState::Shutdown(); + DBusThreadManager::Shutdown(); + } + + // LoginState::Observer + virtual void LoggedInStateChanged(LoginState::LoggedInState state) OVERRIDE { + logged_in_state_ = state; + logged_in_user_type_ = LoginState::Get()->GetLoggedInUserType(); + } + + protected: + MessageLoopForUI message_loop_; + LoginState::LoggedInState logged_in_state_; + LoginState::LoggedInUserType logged_in_user_type_; + + private: + DISALLOW_COPY_AND_ASSIGN(LoginStateTest); +}; + +TEST_F(LoginStateTest, TestLoginState) { + EXPECT_EQ(LoginState::LOGGED_IN_OOBE, logged_in_state_); + EXPECT_EQ(LoginState::LOGGED_IN_OOBE, LoginState::Get()->GetLoggedInState()); + EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, logged_in_user_type_); + EXPECT_EQ(LoginState::LOGGED_IN_USER_NONE, + LoginState::Get()->GetLoggedInUserType()); + // Setting login state to ACTIVE. + LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE, + LoginState::LOGGED_IN_USER_REGULAR); + EXPECT_EQ(LoginState::LOGGED_IN_ACTIVE, + LoginState::Get()->GetLoggedInState()); + EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR, + LoginState::Get()->GetLoggedInUserType()); + EXPECT_TRUE(LoginState::Get()->IsUserLoggedIn()); + // Run the message loop, observer should update members. + message_loop_.RunUntilIdle(); + EXPECT_EQ(LoginState::LOGGED_IN_ACTIVE, logged_in_state_); + EXPECT_EQ(LoginState::LOGGED_IN_USER_REGULAR, logged_in_user_type_); +} + +} // namespace |