Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chromeos / login / login_state.cc
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.
4
5 #include "chromeos/login/login_state.h"
6
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/sys_info.h"
10 #include "chromeos/chromeos_switches.h"
11
12 namespace chromeos {
13
14 namespace {
15
16 // When running a Chrome OS build outside of a device (i.e. on a developer's
17 // workstation) and not running as login-manager, pretend like we're always
18 // logged in.
19 bool AlwaysLoggedInByDefault() {
20   return !base::SysInfo::IsRunningOnChromeOS() &&
21       !CommandLine::ForCurrentProcess()->HasSwitch(switches::kLoginManager);
22 }
23
24 }  // namespace
25
26 static LoginState* g_login_state = NULL;
27
28 // static
29 void LoginState::Initialize() {
30   CHECK(!g_login_state);
31   g_login_state = new LoginState();
32 }
33
34 // static
35 void LoginState::Shutdown() {
36   CHECK(g_login_state);
37   delete g_login_state;
38   g_login_state = NULL;
39 }
40
41 // static
42 LoginState* LoginState::Get() {
43   CHECK(g_login_state) << "LoginState::Get() called before Initialize()";
44   return g_login_state;
45 }
46
47 // static
48 bool LoginState::IsInitialized() {
49   return g_login_state;
50 }
51
52 void LoginState::AddObserver(Observer* observer) {
53   observer_list_.AddObserver(observer);
54 }
55
56 void LoginState::RemoveObserver(Observer* observer) {
57   observer_list_.RemoveObserver(observer);
58 }
59
60 void LoginState::SetLoggedInStateAndPrimaryUser(
61     LoggedInState state,
62     LoggedInUserType type,
63     const std::string& primary_user_hash) {
64   DCHECK(type != LOGGED_IN_USER_NONE);
65   primary_user_hash_ = primary_user_hash;
66   VLOG(1) << "LoggedInStateUser: " << primary_user_hash;
67   SetLoggedInState(state, type);
68 }
69
70 void LoginState::SetLoggedInState(LoggedInState state, LoggedInUserType type) {
71   CHECK_NE(LOGGED_IN_USER_RETAIL_MODE, type);
72   if (state == logged_in_state_ && type == logged_in_user_type_)
73     return;
74   VLOG(1) << "LoggedInState: " << state << " UserType: " << type;
75   logged_in_state_ = state;
76   logged_in_user_type_ = type;
77   NotifyObservers();
78 }
79
80 LoginState::LoggedInUserType LoginState::GetLoggedInUserType() const {
81   return logged_in_user_type_;
82 }
83
84 bool LoginState::IsUserLoggedIn() const {
85   if (always_logged_in_)
86     return true;
87   return logged_in_state_ == LOGGED_IN_ACTIVE;
88 }
89
90 bool LoginState::IsInSafeMode() const {
91   DCHECK(!always_logged_in_ || logged_in_state_ != LOGGED_IN_SAFE_MODE);
92   return logged_in_state_ == LOGGED_IN_SAFE_MODE;
93 }
94
95 bool LoginState::IsGuestSessionUser() const {
96   return logged_in_user_type_ == LOGGED_IN_USER_GUEST;
97 }
98
99 bool LoginState::IsPublicSessionUser() const {
100   return logged_in_user_type_ == LOGGED_IN_USER_PUBLIC_ACCOUNT;
101 }
102
103 bool LoginState::IsKioskApp() const {
104   return logged_in_user_type_ == LOGGED_IN_USER_KIOSK_APP;
105 }
106
107 bool LoginState::UserHasNetworkProfile() const {
108   if (!IsUserLoggedIn())
109     return false;
110   return logged_in_user_type_ != LOGGED_IN_USER_PUBLIC_ACCOUNT;
111 }
112
113 bool LoginState::IsUserAuthenticated() const {
114   return logged_in_user_type_ == LOGGED_IN_USER_REGULAR ||
115          logged_in_user_type_ == LOGGED_IN_USER_OWNER ||
116          logged_in_user_type_ == LOGGED_IN_USER_SUPERVISED;
117 }
118
119 bool LoginState::IsUserGaiaAuthenticated() const {
120   return logged_in_user_type_ == LOGGED_IN_USER_REGULAR ||
121          logged_in_user_type_ == LOGGED_IN_USER_OWNER;
122 }
123
124 // Private methods
125
126 LoginState::LoginState() : logged_in_state_(LOGGED_IN_NONE),
127                            logged_in_user_type_(LOGGED_IN_USER_NONE),
128                            always_logged_in_(AlwaysLoggedInByDefault()) {
129 }
130
131 LoginState::~LoginState() {
132 }
133
134 void LoginState::NotifyObservers() {
135   FOR_EACH_OBSERVER(LoginState::Observer, observer_list_,
136                     LoggedInStateChanged());
137 }
138
139 }  // namespace chromeos