1 // Copyright (c) 2013 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.
5 #include "chromeos/login/login_state.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/sys_info.h"
10 #include "chromeos/chromeos_switches.h"
11 #include "components/device_event_log/device_event_log.h"
17 // When running a Chrome OS build outside of a device (i.e. on a developer's
18 // workstation) and not running as login-manager, pretend like we're always
20 bool AlwaysLoggedInByDefault() {
21 return !base::SysInfo::IsRunningOnChromeOS() &&
22 !base::CommandLine::ForCurrentProcess()->HasSwitch(
23 switches::kLoginManager
);
28 static LoginState
* g_login_state
= NULL
;
31 void LoginState::Initialize() {
32 CHECK(!g_login_state
);
33 g_login_state
= new LoginState();
37 void LoginState::Shutdown() {
44 LoginState
* LoginState::Get() {
45 CHECK(g_login_state
) << "LoginState::Get() called before Initialize()";
50 bool LoginState::IsInitialized() {
54 void LoginState::AddObserver(Observer
* observer
) {
55 observer_list_
.AddObserver(observer
);
58 void LoginState::RemoveObserver(Observer
* observer
) {
59 observer_list_
.RemoveObserver(observer
);
62 void LoginState::SetLoggedInStateAndPrimaryUser(
64 LoggedInUserType type
,
65 const std::string
& primary_user_hash
) {
66 DCHECK(type
!= LOGGED_IN_USER_NONE
);
67 primary_user_hash_
= primary_user_hash
;
68 LOGIN_LOG(EVENT
) << "LoggedInStateUser: " << primary_user_hash
;
69 SetLoggedInState(state
, type
);
72 void LoginState::SetLoggedInState(LoggedInState state
, LoggedInUserType type
) {
73 if (state
== logged_in_state_
&& type
== logged_in_user_type_
)
75 LOGIN_LOG(EVENT
) << "LoggedInState: " << state
<< " UserType: " << type
;
76 logged_in_state_
= state
;
77 logged_in_user_type_
= type
;
81 LoginState::LoggedInUserType
LoginState::GetLoggedInUserType() const {
82 return logged_in_user_type_
;
85 bool LoginState::IsUserLoggedIn() const {
86 if (always_logged_in_
)
88 return logged_in_state_
== LOGGED_IN_ACTIVE
;
91 bool LoginState::IsInSafeMode() const {
92 DCHECK(!always_logged_in_
|| logged_in_state_
!= LOGGED_IN_SAFE_MODE
);
93 return logged_in_state_
== LOGGED_IN_SAFE_MODE
;
96 bool LoginState::IsGuestSessionUser() const {
97 return logged_in_user_type_
== LOGGED_IN_USER_GUEST
;
100 bool LoginState::IsPublicSessionUser() const {
101 return logged_in_user_type_
== LOGGED_IN_USER_PUBLIC_ACCOUNT
;
104 bool LoginState::IsKioskApp() const {
105 return logged_in_user_type_
== LOGGED_IN_USER_KIOSK_APP
;
108 bool LoginState::UserHasNetworkProfile() const {
109 if (!IsUserLoggedIn())
111 return logged_in_user_type_
!= LOGGED_IN_USER_PUBLIC_ACCOUNT
;
114 bool LoginState::IsUserAuthenticated() const {
115 return logged_in_user_type_
== LOGGED_IN_USER_REGULAR
||
116 logged_in_user_type_
== LOGGED_IN_USER_OWNER
||
117 logged_in_user_type_
== LOGGED_IN_USER_SUPERVISED
;
120 bool LoginState::IsUserGaiaAuthenticated() const {
121 return logged_in_user_type_
== LOGGED_IN_USER_REGULAR
||
122 logged_in_user_type_
== LOGGED_IN_USER_OWNER
;
127 LoginState::LoginState() : logged_in_state_(LOGGED_IN_NONE
),
128 logged_in_user_type_(LOGGED_IN_USER_NONE
),
129 always_logged_in_(AlwaysLoggedInByDefault()) {
132 LoginState::~LoginState() {
135 void LoginState::NotifyObservers() {
136 FOR_EACH_OBSERVER(LoginState::Observer
, observer_list_
,
137 LoggedInStateChanged());
140 } // namespace chromeos