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/app_launch_signin_screen.h"
6
7#include "base/values.h"
8#include "chrome/browser/chromeos/login/help_app_launcher.h"
9#include "chrome/browser/chromeos/login/login_utils.h"
10#include "chrome/browser/chromeos/login/screens/user_selection_screen.h"
11#include "chrome/browser/signin/screenlock_bridge.h"
12#include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
13#include "content/public/browser/browser_thread.h"
14#include "content/public/browser/web_ui.h"
15#include "grit/generated_resources.h"
16#include "ui/base/l10n/l10n_util.h"
17
18namespace chromeos {
19
20UserManager* AppLaunchSigninScreen::test_user_manager_ = NULL;
21
22AppLaunchSigninScreen::AppLaunchSigninScreen(
23    OobeUI* oobe_ui, Delegate* delegate)
24    : oobe_ui_(oobe_ui),
25      delegate_(delegate),
26      webui_handler_(NULL) {
27}
28
29AppLaunchSigninScreen::~AppLaunchSigninScreen() {
30  oobe_ui_->ResetSigninScreenHandlerDelegate();
31}
32
33void AppLaunchSigninScreen::Show() {
34  InitOwnerUserList();
35  oobe_ui_->web_ui()->CallJavascriptFunction(
36      "login.AccountPickerScreen.setShouldShowApps",
37      base::FundamentalValue(false));
38  oobe_ui_->ShowSigninScreen(LoginScreenContext(), this, NULL);
39}
40
41void AppLaunchSigninScreen::InitOwnerUserList() {
42  UserManager* user_manager = GetUserManager();
43  const std::string& owner_email = user_manager->GetOwnerEmail();
44  const UserList& all_users = user_manager->GetUsers();
45
46  owner_user_list_.clear();
47  for (UserList::const_iterator it = all_users.begin();
48       it != all_users.end();
49       ++it) {
50    User* user = *it;
51    if (user->email() == owner_email) {
52      owner_user_list_.push_back(user);
53      break;
54    }
55  }
56}
57
58// static
59void AppLaunchSigninScreen::SetUserManagerForTesting(
60    UserManager* user_manager) {
61  test_user_manager_ = user_manager;
62}
63
64UserManager* AppLaunchSigninScreen::GetUserManager() {
65  return test_user_manager_ ? test_user_manager_ : UserManager::Get();
66}
67
68void AppLaunchSigninScreen::CancelPasswordChangedFlow() {
69  NOTREACHED();
70}
71
72void AppLaunchSigninScreen::CancelUserAdding() {
73  NOTREACHED();
74}
75
76void AppLaunchSigninScreen::CreateAccount() {
77  NOTREACHED();
78}
79
80void AppLaunchSigninScreen::CompleteLogin(const UserContext& user_context) {
81  NOTREACHED();
82}
83
84void AppLaunchSigninScreen::Login(const UserContext& user_context) {
85  // Note: LoginUtils::CreateAuthenticator doesn't necessarily create
86  // a new Authenticator object, and could reuse an existing one.
87  authenticator_ = LoginUtils::Get()->CreateAuthenticator(this);
88  content::BrowserThread::PostTask(
89      content::BrowserThread::UI, FROM_HERE,
90      base::Bind(&Authenticator::AuthenticateToUnlock,
91                 authenticator_.get(),
92                 user_context));
93}
94
95void AppLaunchSigninScreen::LoginAsRetailModeUser() {
96  NOTREACHED();
97}
98
99void AppLaunchSigninScreen::LoginAsGuest() {
100  NOTREACHED();
101}
102
103void AppLaunchSigninScreen::MigrateUserData(const std::string& old_password) {
104  NOTREACHED();
105}
106
107void AppLaunchSigninScreen::LoginAsPublicAccount(const std::string& username) {
108  NOTREACHED();
109}
110
111void AppLaunchSigninScreen::LoadWallpaper(const std::string& username) {
112}
113
114void AppLaunchSigninScreen::LoadSigninWallpaper() {
115}
116
117void AppLaunchSigninScreen::OnSigninScreenReady() {
118}
119
120void AppLaunchSigninScreen::RemoveUser(const std::string& username) {
121  NOTREACHED();
122}
123
124void AppLaunchSigninScreen::ResyncUserData() {
125  NOTREACHED();
126}
127
128void AppLaunchSigninScreen::ShowEnterpriseEnrollmentScreen() {
129  NOTREACHED();
130}
131
132void AppLaunchSigninScreen::ShowKioskEnableScreen() {
133  NOTREACHED();
134}
135
136void AppLaunchSigninScreen::ShowKioskAutolaunchScreen() {
137  NOTREACHED();
138}
139
140void AppLaunchSigninScreen::ShowWrongHWIDScreen() {
141  NOTREACHED();
142}
143
144void AppLaunchSigninScreen::SetWebUIHandler(
145    LoginDisplayWebUIHandler* webui_handler) {
146  webui_handler_ = webui_handler;
147}
148
149void AppLaunchSigninScreen::ShowSigninScreenForCreds(
150    const std::string& username,
151    const std::string& password) {
152  NOTREACHED();
153}
154
155const UserList& AppLaunchSigninScreen::GetUsers() const {
156  if (test_user_manager_) {
157    return test_user_manager_->GetUsers();
158  }
159  return owner_user_list_;
160}
161
162bool AppLaunchSigninScreen::IsShowGuest() const {
163  return false;
164}
165
166bool AppLaunchSigninScreen::IsShowUsers() const {
167  return true;
168}
169
170bool AppLaunchSigninScreen::IsSigninInProgress() const {
171  // Return true to suppress network processing in the signin screen.
172  return true;
173}
174
175bool AppLaunchSigninScreen::IsUserSigninCompleted() const {
176  return false;
177}
178
179void AppLaunchSigninScreen::SetDisplayEmail(const std::string& email) {
180  return;
181}
182
183void AppLaunchSigninScreen::Signout() {
184  NOTREACHED();
185}
186
187void AppLaunchSigninScreen::LoginAsKioskApp(const std::string& app_id,
188                                            bool diagnostic_mode) {
189  NOTREACHED();
190}
191
192void AppLaunchSigninScreen::OnLoginFailure(const LoginFailure& error) {
193  LOG(ERROR) << "Unlock failure: " << error.reason();
194  webui_handler_->ClearAndEnablePassword();
195  webui_handler_->ShowError(
196     0,
197     l10n_util::GetStringUTF8(IDS_LOGIN_ERROR_AUTHENTICATING_KIOSK),
198     std::string(),
199     HelpAppLauncher::HELP_CANT_ACCESS_ACCOUNT_OFFLINE);
200}
201
202void AppLaunchSigninScreen::OnLoginSuccess(const UserContext& user_context) {
203  delegate_->OnOwnerSigninSuccess();
204}
205
206void AppLaunchSigninScreen::HandleGetUsers() {
207  base::ListValue users_list;
208  const UserList& users = GetUsers();
209
210  for (UserList::const_iterator it = users.begin(); it != users.end(); ++it) {
211    ScreenlockBridge::LockHandler::AuthType initial_auth_type =
212        UserSelectionScreen::ShouldForceOnlineSignIn(*it)
213            ? ScreenlockBridge::LockHandler::ONLINE_SIGN_IN
214            : ScreenlockBridge::LockHandler::OFFLINE_PASSWORD;
215    base::DictionaryValue* user_dict = new base::DictionaryValue();
216    UserSelectionScreen::FillUserDictionary(
217        *it, true, false, initial_auth_type, user_dict);
218    users_list.Append(user_dict);
219  }
220
221  webui_handler_->LoadUsers(users_list, false, false);
222}
223
224void AppLaunchSigninScreen::SetAuthType(
225    const std::string& username,
226    ScreenlockBridge::LockHandler::AuthType auth_type) {
227  return;
228}
229
230ScreenlockBridge::LockHandler::AuthType AppLaunchSigninScreen::GetAuthType(
231    const std::string& username) const {
232  return ScreenlockBridge::LockHandler::OFFLINE_PASSWORD;
233}
234
235}  // namespace chromeos
236