- add sources.
[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/command_line.h"
8 #include "base/path_service.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/chromeos/login/login_display_host.h"
13 #include "chrome/browser/chromeos/login/mock_authenticator.h"
14 #include "chrome/browser/chromeos/login/supervised_user_manager.h"
15 #include "chrome/browser/first_run/first_run.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/ui/startup/startup_browser_creator.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/chrome_paths.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                                     const std::string& display_email,
59                                     bool has_cookies,
60                                     bool has_active_session,
61                                     LoginUtils::Delegate* delegate) {
62   UserManager::Get()->UserLoggedIn(
63       user_context.username, user_context.username_hash, false);
64   Profile* profile;
65   if (should_launch_browser_) {
66     profile = CreateProfile(user_context.username);
67   } else {
68     TestingProfile* testing_profile = new TestingProfile();
69     testing_profile->set_profile_name(user_context.username);
70
71     profile = testing_profile;
72     g_browser_process->profile_manager()->
73         RegisterTestingProfile(profile, false, false);
74   }
75
76   if (UserManager::Get()->IsLoggedInAsLocallyManagedUser()) {
77     User* active_user = UserManager::Get()->GetActiveUser();
78     std::string managed_user_sync_id =
79         UserManager::Get()->GetSupervisedUserManager()->
80             GetUserSyncId(active_user->email());
81     if (managed_user_sync_id.empty())
82       managed_user_sync_id = "DUMMY ID";
83     profile->GetPrefs()->SetString(prefs::kManagedUserId,
84                                    managed_user_sync_id);
85   }
86
87   content::NotificationService::current()->Notify(
88       chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
89       content::NotificationService::AllSources(),
90       content::Details<Profile>(profile));
91   if (delegate)
92     delegate->OnProfilePrepared(profile);
93 }
94
95 void FakeLoginUtils::DelegateDeleted(LoginUtils::Delegate* delegate) {
96   NOTREACHED() << "Method not implemented.";
97 }
98
99 void FakeLoginUtils::CompleteOffTheRecordLogin(const GURL& start_url) {
100   NOTREACHED() << "Method not implemented.";
101 }
102
103 void FakeLoginUtils::SetFirstLoginPrefs(PrefService* prefs) {
104   NOTREACHED() << "Method not implemented.";
105 }
106
107 scoped_refptr<Authenticator> FakeLoginUtils::CreateAuthenticator(
108     LoginStatusConsumer* consumer) {
109   authenticator_ =
110       new MockAuthenticator(consumer, expected_username_, expected_password_);
111   return authenticator_;
112 }
113
114 void FakeLoginUtils::RestoreAuthenticationSession(Profile* profile) {
115   NOTREACHED() << "Method not implemented.";
116 }
117
118 void FakeLoginUtils::InitRlzDelayed(Profile* user_profile) {
119   NOTREACHED() << "Method not implemented.";
120 }
121
122 Profile* FakeLoginUtils::CreateProfile(const std::string& username_hash) {
123   base::FilePath path;
124   PathService::Get(chrome::DIR_USER_DATA, &path);
125   path = path.AppendASCII(chrome::kProfileDirPrefix + username_hash);
126   Profile* profile = g_browser_process->profile_manager()->GetProfile(path);
127   return profile;
128 }
129
130 void FakeLoginUtils::SetExpectedCredentials(const std::string& username,
131                                             const std::string& password) {
132   expected_username_ = username;
133   expected_password_ = password;
134   if (authenticator_.get()) {
135     static_cast<MockAuthenticator*>(authenticator_.get())->
136         SetExpectedCredentials(username, password);
137   }
138 }
139
140 }  //  namespace chromeos