Home | History | Annotate | Download | only in login
      1 // Copyright 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 "chrome/browser/chromeos/login/fake_login_utils.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/prefs/pref_service.h"
      9 #include "chrome/browser/chrome_notification_types.h"
     10 #include "chrome/browser/chromeos/login/auth/mock_authenticator.h"
     11 #include "chrome/browser/chromeos/login/auth/user_context.h"
     12 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
     13 #include "chrome/browser/chromeos/login/user_flow.h"
     14 #include "chrome/browser/chromeos/login/users/supervised_user_manager.h"
     15 #include "chrome/browser/chromeos/login/users/user.h"
     16 #include "chrome/browser/chromeos/login/users/user_manager.h"
     17 #include "chrome/browser/first_run/first_run.h"
     18 #include "chrome/browser/profiles/profile.h"
     19 #include "chrome/browser/ui/startup/startup_browser_creator.h"
     20 #include "chrome/common/pref_names.h"
     21 #include "chrome/test/base/testing_profile.h"
     22 #include "content/public/browser/notification_service.h"
     23 #include "testing/gtest/include/gtest/gtest.h"
     24 
     25 namespace chromeos {
     26 
     27 FakeLoginUtils::FakeLoginUtils() : should_launch_browser_(false) {}
     28 
     29 FakeLoginUtils::~FakeLoginUtils() {}
     30 
     31 void FakeLoginUtils::DoBrowserLaunch(Profile* profile,
     32                                      LoginDisplayHost* login_host) {
     33 
     34   if (!UserManager::Get()->GetCurrentUserFlow()->ShouldLaunchBrowser()) {
     35       UserManager::Get()->GetCurrentUserFlow()->LaunchExtraSteps(profile);
     36       return;
     37   }
     38   login_host->BeforeSessionStart();
     39   if (should_launch_browser_) {
     40     StartupBrowserCreator browser_creator;
     41     chrome::startup::IsFirstRun first_run =
     42         first_run::IsChromeFirstRun() ? chrome::startup::IS_FIRST_RUN
     43                                       : chrome::startup::IS_NOT_FIRST_RUN;
     44     ASSERT_TRUE(
     45         browser_creator.LaunchBrowser(*CommandLine::ForCurrentProcess(),
     46                                       profile,
     47                                       base::FilePath(),
     48                                       chrome::startup::IS_PROCESS_STARTUP,
     49                                       first_run,
     50                                       NULL));
     51   }
     52   if (login_host)
     53     login_host->Finalize();
     54   UserManager::Get()->SessionStarted();
     55 }
     56 
     57 void FakeLoginUtils::PrepareProfile(const UserContext& user_context,
     58                                     bool has_cookies,
     59                                     bool has_active_session,
     60                                     LoginUtils::Delegate* delegate) {
     61   UserManager::Get()->UserLoggedIn(
     62       user_context.GetUserID(), user_context.GetUserIDHash(), false);
     63   User* user = UserManager::Get()->FindUserAndModify(user_context.GetUserID());
     64   DCHECK(user);
     65 
     66   // Make sure that we get the real Profile instead of the login Profile.
     67   user->set_profile_is_created();
     68   Profile* profile = UserManager::Get()->GetProfileByUser(user);
     69   profile->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
     70                                  user_context.GetUserID());
     71 
     72   if (UserManager::Get()->IsLoggedInAsLocallyManagedUser()) {
     73     User* active_user = UserManager::Get()->GetActiveUser();
     74     std::string supervised_user_sync_id =
     75         UserManager::Get()->GetSupervisedUserManager()->
     76             GetUserSyncId(active_user->email());
     77     if (supervised_user_sync_id.empty())
     78       supervised_user_sync_id = "DUMMY ID";
     79     profile->GetPrefs()->SetString(prefs::kSupervisedUserId,
     80                                    supervised_user_sync_id);
     81   }
     82 
     83   content::NotificationService::current()->Notify(
     84       chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
     85       content::NotificationService::AllSources(),
     86       content::Details<Profile>(profile));
     87   if (delegate)
     88     delegate->OnProfilePrepared(profile);
     89 }
     90 
     91 void FakeLoginUtils::DelegateDeleted(LoginUtils::Delegate* delegate) {
     92   NOTREACHED() << "Method not implemented.";
     93 }
     94 
     95 void FakeLoginUtils::CompleteOffTheRecordLogin(const GURL& start_url) {
     96   NOTREACHED() << "Method not implemented.";
     97 }
     98 
     99 scoped_refptr<Authenticator> FakeLoginUtils::CreateAuthenticator(
    100     LoginStatusConsumer* consumer) {
    101   authenticator_ = new MockAuthenticator(consumer, expected_user_context_);
    102   return authenticator_;
    103 }
    104 
    105 void FakeLoginUtils::SetExpectedCredentials(const UserContext& user_context) {
    106   expected_user_context_ = user_context;
    107   if (authenticator_) {
    108     static_cast<MockAuthenticator*>(authenticator_.get())->
    109         SetExpectedCredentials(user_context);
    110   }
    111 }
    112 
    113 }  //  namespace chromeos
    114