Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(781)

Side by Side Diff: chromeos/login/login_state.cc

Issue 13495003: Add LoginState class to src/chromeos/login (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Restore LOGGED_IN_KIOSK_APP Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/chromeos/chromeos_version.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "chromeos/chromeos_switches.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12
13 namespace chromeos {
14
15 namespace {
16
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 AlwaysLoggedIn() {
21 return !base::chromeos::IsRunningOnChromeOS() &&
22 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kLoginManager);
23 }
24
25 } // namespace
26
27 static LoginState* g_login_state = NULL;
28
29 // static
30 void LoginState::Initialize() {
31 CHECK(!g_login_state);
32 g_login_state = new LoginState();
33 }
34
35 // static
36 void LoginState::Shutdown() {
37 CHECK(g_login_state);
38 delete g_login_state;
39 g_login_state = NULL;
40 }
41
42 // static
43 LoginState* LoginState::Get() {
44 CHECK(g_login_state) << "LoginState::Get() called before Initialize()";
45 return g_login_state;
46 }
47
48 // static
49 bool LoginState::IsInitialized() {
50 return !!g_login_state;
51 }
52
53 void LoginState::AddObserver(Observer* observer) {
54 observer_list_.AddObserver(observer);
55 }
56
57 void LoginState::RemoveObserver(Observer* observer) {
58 observer_list_.RemoveObserver(observer);
59 }
60
61 void LoginState::SetLoginState(LoginState::State state) {
62 if (state == state_)
63 return;
64 VLOG(1) << "Login State: " << state;
65 state_ = state;
66 NotifyObservers();
67 }
68
69 LoginState::State LoginState::GetLoginState() const {
70 if (state_ == LOGGED_IN_OOBE || state_ == LOGGED_IN_NONE) {
71 if (AlwaysLoggedIn()) {
72 // Fake a login state when running in a dev environment.
73 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kGuestSession))
74 return LOGGED_IN_GUEST;
75 else
76 return LOGGED_IN_OWNER;
77 }
78 return state_;
79 }
80 // |state_| may get set before |session_started_|.
81 if (!session_started_)
82 return LOGGED_IN_NONE;
83 if (screen_locked_)
84 return LOGGED_IN_LOCKED;
85 return state_;
86 }
87
88 bool LoginState::IsUserLoggedIn() {
89 State login_state = GetLoginState();
90 if (login_state == LOGGED_IN_OOBE || login_state == LOGGED_IN_NONE)
91 return false;
92 return true;
93 }
94
95 void LoginState::SetSessionStarted(bool session_started) {
96 session_started_ = session_started;
97 NotifyObservers();
98 }
99
100 bool LoginState::IsSessionStarted() const {
101 return session_started_;
102 }
103
104 // Private methods
105
106 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
107 session_started_(false),
108 screen_locked_(false) {
109 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this);
110 }
111
112 LoginState::~LoginState() {
113 DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver(this);
114 }
115
116 void LoginState::LockScreen() {
117 screen_locked_ = true;
118 NotifyObservers();
119 }
120
121 void LoginState::UnlockScreen() {
122 screen_locked_ = false;
123 NotifyObservers();
124 }
125
126 void LoginState::NotifyObservers() {
127 FOR_EACH_OBSERVER(LoginState::Observer, observer_list_,
128 LoginStateChanged(GetLoginState()));
129 }
130
131 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698