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