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
3// found in the LICENSE file.
4
5#include "chrome/browser/chromeos/login/session/chrome_session_manager.h"
6
7#include "base/command_line.h"
8#include "base/logging.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/sys_info.h"
11#include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
12#include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
13#include "chrome/browser/chromeos/login/session/kiosk_auto_launcher_session_manager_delegate.h"
14#include "chrome/browser/chromeos/login/session/login_oobe_session_manager_delegate.h"
15#include "chrome/browser/chromeos/login/session/restore_after_crash_session_manager_delegate.h"
16#include "chrome/browser/chromeos/login/session/stub_login_session_manager_delegate.h"
17#include "chrome/browser/profiles/profile.h"
18#include "chromeos/chromeos_switches.h"
19#include "chromeos/login/user_names.h"
20
21namespace chromeos {
22
23namespace {
24
25bool ShouldAutoLaunchKioskApp(const base::CommandLine& command_line) {
26  KioskAppManager* app_manager = KioskAppManager::Get();
27  return command_line.HasSwitch(switches::kLoginManager) &&
28         !command_line.HasSwitch(switches::kForceLoginManagerInTests) &&
29         app_manager->IsAutoLaunchEnabled() &&
30         KioskAppLaunchError::Get() == KioskAppLaunchError::NONE;
31}
32
33}  // namespace
34
35// static
36scoped_ptr<session_manager::SessionManager>
37ChromeSessionManager::CreateSessionManager(
38    const base::CommandLine& parsed_command_line,
39    Profile* profile,
40    bool is_running_test) {
41  // Tests should be able to tune login manager before showing it. Thus only
42  // show login UI (login and out-of-box) in normal (non-testing) mode with
43  // --login-manager switch and if test passed --force-login-manager-in-tests.
44  bool force_login_screen_in_test =
45      parsed_command_line.HasSwitch(switches::kForceLoginManagerInTests);
46
47  std::string login_user_id =
48      parsed_command_line.GetSwitchValueASCII(switches::kLoginUser);
49
50  if (ShouldAutoLaunchKioskApp(parsed_command_line)) {
51    VLOG(1) << "Starting Chrome with KioskAutoLauncherSessionManagerDelegate";
52    return scoped_ptr<session_manager::SessionManager>(new ChromeSessionManager(
53        new KioskAutoLauncherSessionManagerDelegate()));
54  } else if (parsed_command_line.HasSwitch(switches::kLoginManager) &&
55             (!is_running_test || force_login_screen_in_test)) {
56    VLOG(1) << "Starting Chrome with LoginOobeSessionManagerDelegate";
57    return scoped_ptr<session_manager::SessionManager>(
58        new ChromeSessionManager(new LoginOobeSessionManagerDelegate()));
59  } else if (!base::SysInfo::IsRunningOnChromeOS() &&
60             login_user_id == login::kStubUser) {
61    VLOG(1) << "Starting Chrome with StubLoginSessionManagerDelegate";
62    return scoped_ptr<session_manager::SessionManager>(new ChromeSessionManager(
63        new StubLoginSessionManagerDelegate(profile, login_user_id)));
64  } else {
65    VLOG(1) << "Starting Chrome with  RestoreAfterCrashSessionManagerDelegate";
66    // Restarting Chrome inside existing user session. Possible cases:
67    // 1. Chrome is restarted after crash.
68    // 2. Chrome is started in browser_tests skipping the login flow.
69    // 3. Chrome is started on dev machine i.e. not on Chrome OS device w/o
70    //    login flow. In that case --login-user=[chromeos::login::kStubUser] is
71    //    added. See PreEarlyInitialization().
72    return scoped_ptr<session_manager::SessionManager>(new ChromeSessionManager(
73        new RestoreAfterCrashSessionManagerDelegate(profile, login_user_id)));
74  }
75}
76
77ChromeSessionManager::ChromeSessionManager(
78    session_manager::SessionManagerDelegate* delegate) {
79  Initialize(delegate);
80}
81
82ChromeSessionManager::~ChromeSessionManager() {
83}
84
85}  // namespace chromeos
86