Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromeos / login / login_state.cc
blob07a7f6b2c2c87c6ac2fc16639043cb8d1553bd50
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"
13 namespace chromeos {
15 namespace {
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
19 // logged in.
20 bool AlwaysLoggedInByDefault() {
21 return !base::SysInfo::IsRunningOnChromeOS() &&
22 !base::CommandLine::ForCurrentProcess()->HasSwitch(
23 switches::kLoginManager);
26 } // namespace
28 static LoginState* g_login_state = NULL;
30 // static
31 void LoginState::Initialize() {
32 CHECK(!g_login_state);
33 g_login_state = new LoginState();
36 // static
37 void LoginState::Shutdown() {
38 CHECK(g_login_state);
39 delete g_login_state;
40 g_login_state = NULL;
43 // static
44 LoginState* LoginState::Get() {
45 CHECK(g_login_state) << "LoginState::Get() called before Initialize()";
46 return g_login_state;
49 // static
50 bool LoginState::IsInitialized() {
51 return g_login_state;
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(
63 LoggedInState state,
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_)
74 return;
75 LOGIN_LOG(EVENT) << "LoggedInState: " << state << " UserType: " << type;
76 logged_in_state_ = state;
77 logged_in_user_type_ = type;
78 NotifyObservers();
81 LoginState::LoggedInUserType LoginState::GetLoggedInUserType() const {
82 return logged_in_user_type_;
85 bool LoginState::IsUserLoggedIn() const {
86 if (always_logged_in_)
87 return true;
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())
110 return false;
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;
125 // Private methods
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