Home | History | Annotate | Download | only in session
      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/restore_after_crash_session_manager_delegate.h"
      6 
      7 #include "base/command_line.h"
      8 #include "chrome/browser/chrome_notification_types.h"
      9 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/common/chrome_switches.h"
     12 #include "chromeos/audio/cras_audio_handler.h"
     13 #include "chromeos/chromeos_switches.h"
     14 #include "components/user_manager/user_manager.h"
     15 #include "content/public/browser/notification_service.h"
     16 
     17 namespace chromeos {
     18 
     19 RestoreAfterCrashSessionManagerDelegate::
     20     RestoreAfterCrashSessionManagerDelegate(Profile* profile,
     21                                             const std::string& login_user_id)
     22     : profile_(profile), login_user_id_(login_user_id) {
     23 }
     24 
     25 RestoreAfterCrashSessionManagerDelegate::
     26     ~RestoreAfterCrashSessionManagerDelegate() {
     27 }
     28 
     29 void RestoreAfterCrashSessionManagerDelegate::Start() {
     30   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
     31 
     32   session_manager_->SetSessionState(session_manager::SESSION_STATE_ACTIVE);
     33 
     34   // TODO(nkostylev): Identify tests that do not set this kLoginUser flag but
     35   // still rely on "stub user" session. Keeping existing behavior to avoid
     36   // breaking tests.
     37   if (command_line->HasSwitch(chromeos::switches::kLoginUser)) {
     38     // This is done in SessionManager::OnProfileCreated during normal login.
     39     UserSessionManager* user_session_mgr = UserSessionManager::GetInstance();
     40     user_session_mgr->InitRlz(profile());
     41     user_session_mgr->InitializeCerts(profile());
     42 
     43     // Send the PROFILE_PREPARED notification and call SessionStarted()
     44     // so that the Launcher and other Profile dependent classes are created.
     45     content::NotificationService::current()->Notify(
     46         chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
     47         content::NotificationService::AllSources(),
     48         content::Details<Profile>(profile()));
     49 
     50     // This call will set session state to SESSION_STATE_ACTIVE (same one).
     51     user_manager::UserManager::Get()->SessionStarted();
     52 
     53     // Now is the good time to retrieve other logged in users for this session.
     54     // First user has been already marked as logged in and active in
     55     // PreProfileInit(). Restore sessions for other users in the background.
     56     user_session_mgr->RestoreActiveSessions();
     57   }
     58 
     59   bool is_running_test = command_line->HasSwitch(::switches::kTestName) ||
     60                          command_line->HasSwitch(::switches::kTestType);
     61 
     62   if (!is_running_test) {
     63     // Enable CrasAudioHandler logging when chrome restarts after crashing.
     64     if (chromeos::CrasAudioHandler::IsInitialized())
     65       chromeos::CrasAudioHandler::Get()->LogErrors();
     66 
     67     // We did not log in (we crashed or are debugging), so we need to
     68     // restore Sync.
     69     UserSessionManager::GetInstance()->RestoreAuthenticationSession(profile());
     70   }
     71 }
     72 
     73 }  // namespace chromeos
     74