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

Side by Side Diff: chrome/browser/chromeos/login/session/chrome_session_manager.cc

Issue 2444383008: session_manager: Create ChromeSessionManager early (Closed)
Patch Set: for #4 Created 4 years, 1 month 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/login/session/chrome_session_manager.h" 5 #include "chrome/browser/chromeos/login/session/chrome_session_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/sys_info.h" 11 #include "base/sys_info.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/browser_process_platform_part_chromeos.h"
12 #include "chrome/browser/chrome_notification_types.h" 14 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h" 15 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
14 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" 16 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
15 #include "chrome/browser/chromeos/login/session/kiosk_auto_launcher_session_mana ger_delegate.h" 17 #include "chrome/browser/chromeos/arc/arc_auth_service.h"
16 #include "chrome/browser/chromeos/login/session/login_oobe_session_manager_deleg ate.h" 18 #include "chrome/browser/chromeos/login/login_wizard.h"
17 #include "chrome/browser/chromeos/login/session/restore_after_crash_session_mana ger_delegate.h" 19 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
18 #include "chrome/browser/chromeos/login/session/stub_login_session_manager_deleg ate.h" 20 #include "chrome/browser/chromeos/login/wizard_controller.h"
21 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
19 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/signin/signin_manager_factory.h"
24 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h"
27 #include "chromeos/audio/cras_audio_handler.h"
20 #include "chromeos/chromeos_switches.h" 28 #include "chromeos/chromeos_switches.h"
21 #include "chromeos/cryptohome/cryptohome_parameters.h" 29 #include "chromeos/cryptohome/cryptohome_parameters.h"
30 #include "chromeos/dbus/dbus_thread_manager.h"
31 #include "chromeos/dbus/session_manager_client.h"
32 #include "components/arc/arc_bridge_service.h"
33 #include "components/arc/arc_service_manager.h"
34 #include "components/prefs/pref_service.h"
22 #include "components/signin/core/account_id/account_id.h" 35 #include "components/signin/core/account_id/account_id.h"
36 #include "components/signin/core/browser/signin_manager.h"
23 #include "components/user_manager/user_manager.h" 37 #include "components/user_manager/user_manager.h"
24 #include "components/user_manager/user_names.h" 38 #include "components/user_manager/user_names.h"
25 #include "content/public/browser/notification_service.h" 39 #include "content/public/browser/notification_service.h"
40 #include "content/public/common/content_switches.h"
26 41
27 namespace chromeos { 42 namespace chromeos {
28 43
29 namespace { 44 namespace {
30 45
46 // Whether kiosk auto launch should be started.
31 bool ShouldAutoLaunchKioskApp(const base::CommandLine& command_line) { 47 bool ShouldAutoLaunchKioskApp(const base::CommandLine& command_line) {
32 KioskAppManager* app_manager = KioskAppManager::Get(); 48 KioskAppManager* app_manager = KioskAppManager::Get();
33 return command_line.HasSwitch(switches::kLoginManager) && 49 return command_line.HasSwitch(switches::kLoginManager) &&
34 !command_line.HasSwitch(switches::kForceLoginManagerInTests) && 50 !command_line.HasSwitch(switches::kForceLoginManagerInTests) &&
35 app_manager->IsAutoLaunchEnabled() && 51 app_manager->IsAutoLaunchEnabled() &&
36 KioskAppLaunchError::Get() == KioskAppLaunchError::NONE; 52 KioskAppLaunchError::Get() == KioskAppLaunchError::NONE;
37 } 53 }
38 54
55 // Starts kiosk app auto launch and shows the splash screen.
56 void StartKioskSession() {
57 // Kiosk app launcher starts with login state.
58 session_manager::SessionManager::Get()->SetSessionState(
59 session_manager::SessionState::LOGIN_PRIMARY);
60
61 ShowLoginWizard(chromeos::WizardController::kAppLaunchSplashScreenName);
62
63 // Login screen is skipped but 'login-prompt-visible' signal is still needed.
64 VLOG(1) << "Kiosk app auto launch >> login-prompt-visible";
65 DBusThreadManager::Get()->GetSessionManagerClient()->EmitLoginPromptVisible();
66 }
67
68 // Starts the login/oobe screen.
69 void StartLoginOobeSession() {
70 // State will be defined once out-of-box/login branching is complete.
71 ShowLoginWizard(std::string());
72
73 // Reset reboot after update flag when login screen is shown.
74 policy::BrowserPolicyConnectorChromeOS* connector =
75 g_browser_process->platform_part()->browser_policy_connector_chromeos();
76 if (!connector->IsEnterpriseManaged()) {
77 PrefService* local_state = g_browser_process->local_state();
78 local_state->ClearPref(prefs::kRebootAfterUpdate);
79 }
80 }
81
82 // Restores user sessions for a crash-and-restarted chrome.
83 void StartRestoreAfterCrashSession(Profile* user_profile,
84 const std::string& login_user_id) {
85 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
86
87 session_manager::SessionManager::Get()->SetSessionState(
88 session_manager::SessionState::ACTIVE);
89
90 // TODO(xiyuan): Identify tests that do not set this kLoginUser flag but
91 // still rely on "stub user" session. Keeping existing behavior to avoid
92 // breaking tests.
93 if (command_line->HasSwitch(chromeos::switches::kLoginUser)) {
94 // This is done in SessionManager::OnProfileCreated during normal login.
95 UserSessionManager* user_session_mgr = UserSessionManager::GetInstance();
96 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
97 const user_manager::User* user = user_manager->GetActiveUser();
98 if (!user) {
99 // This is possible if crash occured after profile removal
100 // (see crbug.com/178290 for some more info).
101 LOG(ERROR) << "Could not get active user after crash.";
102 return;
103 }
104 user_session_mgr->InitRlz(user_profile);
105 user_session_mgr->InitializeCerts(user_profile);
106 user_session_mgr->InitializeCRLSetFetcher(user);
107 user_session_mgr->InitializeCertificateTransparencyComponents(user);
108
109 if (arc::ArcBridgeService::GetEnabled(
110 base::CommandLine::ForCurrentProcess())) {
111 DCHECK(arc::ArcServiceManager::Get());
112 std::unique_ptr<BooleanPrefMember> arc_enabled_pref =
113 base::MakeUnique<BooleanPrefMember>();
114 arc_enabled_pref->Init(prefs::kArcEnabled, user_profile->GetPrefs());
115 arc::ArcServiceManager::Get()->OnPrimaryUserProfilePrepared(
116 multi_user_util::GetAccountIdFromProfile(user_profile),
117 std::move(arc_enabled_pref));
118 DCHECK(arc::ArcAuthService::Get());
119 arc::ArcAuthService::Get()->OnPrimaryUserProfilePrepared(user_profile);
120 }
121
122 // Send the PROFILE_PREPARED notification and call SessionStarted()
123 // so that the Launcher and other Profile dependent classes are created.
124 content::NotificationService::current()->Notify(
125 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
126 content::NotificationService::AllSources(),
127 content::Details<Profile>(user_profile));
128
129 // This call will set session state to SESSION_STATE_ACTIVE (same one).
130 session_manager::SessionManager::Get()->SessionStarted();
131
132 // Now is the good time to retrieve other logged in users for this session.
133 // First user has been already marked as logged in and active in
134 // PreProfileInit(). Restore sessions for other users in the background.
135 user_session_mgr->RestoreActiveSessions();
136 }
137
138 bool is_running_test = command_line->HasSwitch(::switches::kTestName) ||
139 command_line->HasSwitch(::switches::kTestType);
140
141 if (!is_running_test) {
142 // Enable CrasAudioHandler logging when chrome restarts after crashing.
143 if (chromeos::CrasAudioHandler::IsInitialized())
144 chromeos::CrasAudioHandler::Get()->LogErrors();
145
146 // We did not log in (we crashed or are debugging), so we need to
147 // restore Sync.
148 UserSessionManager::GetInstance()->RestoreAuthenticationSession(
149 user_profile);
150 }
151 }
152
153 // Starts a user session with stub user.
154 void StartStubLoginSession(Profile* user_profile,
155 const std::string& login_user_id) {
156 session_manager::SessionManager::Get()->SetSessionState(
157 session_manager::SessionState::ACTIVE);
158
159 // For dev machines and stub user emulate as if sync has been initialized.
160 SigninManagerFactory::GetForProfile(user_profile)
161 ->SetAuthenticatedAccountInfo(login_user_id, login_user_id);
162 StartRestoreAfterCrashSession(user_profile, login_user_id);
163 }
164
39 } // namespace 165 } // namespace
40 166
41 // static 167 ChromeSessionManager::ChromeSessionManager() {}
42 std::unique_ptr<session_manager::SessionManager> 168 ChromeSessionManager::~ChromeSessionManager() {}
43 ChromeSessionManager::CreateSessionManager( 169
170 void ChromeSessionManager::Initialize(
44 const base::CommandLine& parsed_command_line, 171 const base::CommandLine& parsed_command_line,
45 Profile* profile, 172 Profile* profile,
46 bool is_running_test) { 173 bool is_running_test) {
47 // Tests should be able to tune login manager before showing it. Thus only 174 // Tests should be able to tune login manager before showing it. Thus only
48 // show login UI (login and out-of-box) in normal (non-testing) mode with 175 // show login UI (login and out-of-box) in normal (non-testing) mode with
49 // --login-manager switch and if test passed --force-login-manager-in-tests. 176 // --login-manager switch and if test passed --force-login-manager-in-tests.
50 bool force_login_screen_in_test = 177 bool force_login_screen_in_test =
51 parsed_command_line.HasSwitch(switches::kForceLoginManagerInTests); 178 parsed_command_line.HasSwitch(switches::kForceLoginManagerInTests);
52 179
53 const std::string cryptohome_id = 180 const std::string cryptohome_id =
54 parsed_command_line.GetSwitchValueASCII(switches::kLoginUser); 181 parsed_command_line.GetSwitchValueASCII(switches::kLoginUser);
55 const AccountId login_account_id( 182 const AccountId login_account_id(
56 cryptohome::Identification::FromString(cryptohome_id).GetAccountId()); 183 cryptohome::Identification::FromString(cryptohome_id).GetAccountId());
57 184
58 KioskAppManager::RemoveObsoleteCryptohomes(); 185 KioskAppManager::RemoveObsoleteCryptohomes();
59 186
60 if (ShouldAutoLaunchKioskApp(parsed_command_line)) { 187 if (ShouldAutoLaunchKioskApp(parsed_command_line)) {
61 VLOG(1) << "Starting Chrome with KioskAutoLauncherSessionManagerDelegate"; 188 VLOG(1) << "Starting Chrome with kiosk auto launch.";
62 return std::unique_ptr<session_manager::SessionManager>( 189 StartKioskSession();
63 new ChromeSessionManager( 190 return;
64 new KioskAutoLauncherSessionManagerDelegate()));
65 } else if (parsed_command_line.HasSwitch(switches::kLoginManager) &&
66 (!is_running_test || force_login_screen_in_test)) {
67 VLOG(1) << "Starting Chrome with LoginOobeSessionManagerDelegate";
68 return std::unique_ptr<session_manager::SessionManager>(
69 new ChromeSessionManager(new LoginOobeSessionManagerDelegate()));
70 } else if (!base::SysInfo::IsRunningOnChromeOS() &&
71 login_account_id == user_manager::StubAccountId()) {
72 VLOG(1) << "Starting Chrome with StubLoginSessionManagerDelegate";
73 return std::unique_ptr<session_manager::SessionManager>(
74 new ChromeSessionManager(new StubLoginSessionManagerDelegate(
75 profile, login_account_id.GetUserEmail())));
76 } else {
77 VLOG(1) << "Starting Chrome with RestoreAfterCrashSessionManagerDelegate";
78 // Restarting Chrome inside existing user session. Possible cases:
79 // 1. Chrome is restarted after crash.
80 // 2. Chrome is restarted for Guest session.
81 // 3. Chrome is started in browser_tests skipping the login flow.
82 // 4. Chrome is started on dev machine i.e. not on Chrome OS device w/o
83 // login flow. In that case
84 // --login-user=[user_manager::kStubUser] is
85 // added. See PreEarlyInitialization().
86 return std::unique_ptr<session_manager::SessionManager>(
87 new ChromeSessionManager(new RestoreAfterCrashSessionManagerDelegate(
88 profile, login_account_id.GetUserEmail())));
89 } 191 }
90 }
91 192
92 ChromeSessionManager::ChromeSessionManager( 193 if (parsed_command_line.HasSwitch(switches::kLoginManager) &&
93 session_manager::SessionManagerDelegate* delegate) { 194 (!is_running_test || force_login_screen_in_test)) {
94 Initialize(delegate); 195 VLOG(1) << "Starting Chrome with login/oobe screen.";
95 } 196 StartLoginOobeSession();
197 return;
198 }
96 199
97 ChromeSessionManager::~ChromeSessionManager() { 200 if (!base::SysInfo::IsRunningOnChromeOS() &&
201 login_account_id == user_manager::StubAccountId()) {
202 VLOG(1) << "Starting Chrome with stub login.";
203 StartStubLoginSession(profile, login_account_id.GetUserEmail());
204 return;
205 }
206
207 VLOG(1) << "Starting Chrome with restart after crash session.";
208 // Restarting Chrome inside existing user session. Possible cases:
209 // 1. Chrome is restarted after crash.
210 // 2. Chrome is restarted for Guest session.
211 // 3. Chrome is started in browser_tests skipping the login flow.
212 // 4. Chrome is started on dev machine i.e. not on Chrome OS device w/o
213 // login flow. In that case --login-user=[user_manager::kStubUser] is
214 // added. See PreEarlyInitialization().
215 StartRestoreAfterCrashSession(profile, login_account_id.GetUserEmail());
98 } 216 }
99 217
100 void ChromeSessionManager::SessionStarted() { 218 void ChromeSessionManager::SessionStarted() {
101 session_manager::SessionManager::SessionStarted(); 219 session_manager::SessionManager::SessionStarted();
102 SetSessionState(session_manager::SessionState::ACTIVE); 220 SetSessionState(session_manager::SessionState::ACTIVE);
103 221
104 // Notifies UserManager so that it can update login state. 222 // Notifies UserManager so that it can update login state.
105 user_manager::UserManager* user_manager = user_manager::UserManager::Get(); 223 user_manager::UserManager* user_manager = user_manager::UserManager::Get();
106 if (user_manager) 224 if (user_manager)
107 user_manager->OnSessionStarted(); 225 user_manager->OnSessionStarted();
108 226
109 content::NotificationService::current()->Notify( 227 content::NotificationService::current()->Notify(
110 chrome::NOTIFICATION_SESSION_STARTED, 228 chrome::NOTIFICATION_SESSION_STARTED,
111 content::Source<session_manager::SessionManager>(this), 229 content::Source<session_manager::SessionManager>(this),
112 content::Details<const user_manager::User>( 230 content::Details<const user_manager::User>(
113 user_manager->GetActiveUser())); 231 user_manager->GetActiveUser()));
114 } 232 }
115 233
116 } // namespace chromeos 234 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698