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