webui_login_display.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
1// Copyright 2014 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/ui/webui_login_display.h"
6
7#include "ash/shell.h"
8#include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
9#include "chrome/browser/chromeos/login/lock/screen_locker.h"
10#include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
11#include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
12#include "chrome/browser/chromeos/login/ui/webui_login_view.h"
13#include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
14#include "chrome/browser/profiles/profile_manager.h"
15#include "chrome/browser/ui/browser_window.h"
16#include "chromeos/ime/ime_keyboard.h"
17#include "chromeos/ime/input_method_manager.h"
18#include "grit/chromium_strings.h"
19#include "grit/generated_resources.h"
20#include "ui/base/l10n/l10n_util.h"
21#include "ui/views/widget/widget.h"
22#include "ui/wm/core/user_activity_detector.h"
23
24namespace chromeos {
25
26// WebUILoginDisplay, public: --------------------------------------------------
27
28WebUILoginDisplay::~WebUILoginDisplay() {
29  if (webui_handler_)
30    webui_handler_->ResetSigninScreenHandlerDelegate();
31  wm::UserActivityDetector* activity_detector = ash::Shell::GetInstance()->
32      user_activity_detector();
33  if (activity_detector->HasObserver(this))
34    activity_detector->RemoveObserver(this);
35}
36
37// LoginDisplay implementation: ------------------------------------------------
38
39WebUILoginDisplay::WebUILoginDisplay(LoginDisplay::Delegate* delegate)
40    : LoginDisplay(delegate, gfx::Rect()),
41      show_guest_(false),
42      show_new_user_(false),
43      webui_handler_(NULL),
44      gaia_screen_(new GaiaScreen()),
45      user_selection_screen_(new UserSelectionScreen()) {
46}
47
48void WebUILoginDisplay::ClearAndEnablePassword() {
49  if (webui_handler_)
50      webui_handler_->ClearAndEnablePassword();
51}
52
53void WebUILoginDisplay::Init(const UserList& users,
54                             bool show_guest,
55                             bool show_users,
56                             bool show_new_user) {
57  // Testing that the delegate has been set.
58  DCHECK(delegate_);
59
60  user_selection_screen_->Init(users, show_guest);
61  show_guest_ = show_guest;
62  show_users_ = show_users;
63  show_new_user_ = show_new_user;
64
65  wm::UserActivityDetector* activity_detector = ash::Shell::GetInstance()->
66      user_activity_detector();
67  if (!activity_detector->HasObserver(this))
68    activity_detector->AddObserver(this);
69}
70
71// ---- Common methods
72
73// ---- User selection screen methods
74
75void WebUILoginDisplay::OnBeforeUserRemoved(const std::string& username) {
76  user_selection_screen_->OnBeforeUserRemoved(username);
77}
78
79void WebUILoginDisplay::OnUserRemoved(const std::string& username) {
80  user_selection_screen_->OnUserRemoved(username);
81}
82
83void WebUILoginDisplay::OnUserImageChanged(const User& user) {
84  user_selection_screen_->OnUserImageChanged(user);
85}
86
87void WebUILoginDisplay::HandleGetUsers() {
88  user_selection_screen_->HandleGetUsers();
89}
90
91const UserList& WebUILoginDisplay::GetUsers() const {
92  return user_selection_screen_->GetUsers();
93}
94
95// User selection screen, screen lock API
96
97void WebUILoginDisplay::SetAuthType(
98    const std::string& username,
99    ScreenlockBridge::LockHandler::AuthType auth_type) {
100  user_selection_screen_->SetAuthType(username, auth_type);
101}
102
103ScreenlockBridge::LockHandler::AuthType WebUILoginDisplay::GetAuthType(
104    const std::string& username) const {
105  return user_selection_screen_->GetAuthType(username);
106}
107
108// ---- Gaia screen methods
109
110// ---- Not yet classified methods
111
112void WebUILoginDisplay::OnPreferencesChanged() {
113  if (webui_handler_)
114    webui_handler_->OnPreferencesChanged();
115}
116
117void WebUILoginDisplay::SetUIEnabled(bool is_enabled) {
118  // TODO(nkostylev): Cleanup this condition,
119  // see http://crbug.com/157885 and http://crbug.com/158255.
120  // Allow this call only before user sign in or at lock screen.
121  // If this call is made after new user signs in but login screen is still
122  // around that would trigger a sign in extension refresh.
123  if (is_enabled && (!UserManager::Get()->IsUserLoggedIn() ||
124                     ScreenLocker::default_screen_locker())) {
125    ClearAndEnablePassword();
126  }
127
128  if (chromeos::LoginDisplayHost* host =
129          chromeos::LoginDisplayHostImpl::default_host()) {
130    if (chromeos::WebUILoginView* login_view = host->GetWebUILoginView())
131      login_view->SetUIEnabled(is_enabled);
132  }
133}
134
135void WebUILoginDisplay::ShowError(int error_msg_id,
136                                  int login_attempts,
137                                  HelpAppLauncher::HelpTopic help_topic_id) {
138  VLOG(1) << "Show error, error_id: " << error_msg_id
139          << ", attempts:" << login_attempts
140          <<  ", help_topic_id: " << help_topic_id;
141  if (!webui_handler_)
142    return;
143
144  std::string error_text;
145  switch (error_msg_id) {
146    case IDS_LOGIN_ERROR_AUTHENTICATING_HOSTED:
147      error_text = l10n_util::GetStringFUTF8(
148          error_msg_id, l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME));
149      break;
150    case IDS_LOGIN_ERROR_CAPTIVE_PORTAL:
151      error_text = l10n_util::GetStringFUTF8(
152          error_msg_id, delegate()->GetConnectedNetworkName());
153      break;
154    default:
155      error_text = l10n_util::GetStringUTF8(error_msg_id);
156      break;
157  }
158
159  // Only display hints about keyboard layout if the error is authentication-
160  // related.
161  if (error_msg_id != IDS_LOGIN_ERROR_WHITELIST &&
162      error_msg_id != IDS_LOGIN_ERROR_OWNER_KEY_LOST &&
163      error_msg_id != IDS_LOGIN_ERROR_OWNER_REQUIRED) {
164    // Display a warning if Caps Lock is on.
165    input_method::InputMethodManager* ime_manager =
166        input_method::InputMethodManager::Get();
167    if (ime_manager->GetImeKeyboard()->CapsLockIsEnabled()) {
168      // TODO(ivankr): use a format string instead of concatenation.
169      error_text += "\n" +
170          l10n_util::GetStringUTF8(IDS_LOGIN_ERROR_CAPS_LOCK_HINT);
171    }
172
173    // Display a hint to switch keyboards if there are other active input
174    // methods.
175    if (ime_manager->GetNumActiveInputMethods() > 1) {
176      error_text += "\n" +
177          l10n_util::GetStringUTF8(IDS_LOGIN_ERROR_KEYBOARD_SWITCH_HINT);
178    }
179  }
180
181  std::string help_link;
182  switch (error_msg_id) {
183    case IDS_LOGIN_ERROR_AUTHENTICATING_HOSTED:
184      help_link = l10n_util::GetStringUTF8(IDS_LEARN_MORE);
185      break;
186    default:
187      if (login_attempts > 1)
188        help_link = l10n_util::GetStringUTF8(IDS_LEARN_MORE);
189      break;
190  }
191
192  webui_handler_->ShowError(login_attempts, error_text, help_link,
193                            help_topic_id);
194}
195
196void WebUILoginDisplay::ShowErrorScreen(LoginDisplay::SigninError error_id) {
197  VLOG(1) << "Show error screen, error_id: " << error_id;
198  if (!webui_handler_)
199    return;
200  webui_handler_->ShowErrorScreen(error_id);
201}
202
203void WebUILoginDisplay::ShowGaiaPasswordChanged(const std::string& username) {
204  if (webui_handler_)
205    webui_handler_->ShowGaiaPasswordChanged(username);
206}
207
208void WebUILoginDisplay::ShowPasswordChangedDialog(bool show_password_error) {
209  if (webui_handler_)
210    webui_handler_->ShowPasswordChangedDialog(show_password_error);
211}
212
213void WebUILoginDisplay::ShowSigninUI(const std::string& email) {
214  if (webui_handler_)
215    webui_handler_->ShowSigninUI(email);
216}
217
218// WebUILoginDisplay, NativeWindowDelegate implementation: ---------------------
219gfx::NativeWindow WebUILoginDisplay::GetNativeWindow() const {
220  return parent_window();
221}
222
223// WebUILoginDisplay, SigninScreenHandlerDelegate implementation: --------------
224void WebUILoginDisplay::CancelPasswordChangedFlow() {
225  DCHECK(delegate_);
226  if (delegate_)
227    delegate_->CancelPasswordChangedFlow();
228}
229
230void WebUILoginDisplay::CancelUserAdding() {
231  if (!UserAddingScreen::Get()->IsRunning()) {
232    LOG(ERROR) << "User adding screen not running.";
233    return;
234  }
235  UserAddingScreen::Get()->Cancel();
236}
237
238void WebUILoginDisplay::CreateAccount() {
239  DCHECK(delegate_);
240  if (delegate_)
241    delegate_->CreateAccount();
242}
243
244void WebUILoginDisplay::CompleteLogin(const UserContext& user_context) {
245  DCHECK(delegate_);
246  if (delegate_)
247    delegate_->CompleteLogin(user_context);
248}
249
250void WebUILoginDisplay::Login(const UserContext& user_context) {
251  DCHECK(delegate_);
252  if (delegate_)
253    delegate_->Login(user_context);
254}
255
256void WebUILoginDisplay::LoginAsRetailModeUser() {
257  DCHECK(delegate_);
258  if (delegate_)
259    delegate_->LoginAsRetailModeUser();
260}
261
262void WebUILoginDisplay::LoginAsGuest() {
263  DCHECK(delegate_);
264  if (delegate_)
265    delegate_->LoginAsGuest();
266}
267
268void WebUILoginDisplay::LoginAsPublicAccount(const std::string& username) {
269  DCHECK(delegate_);
270  if (delegate_)
271    delegate_->LoginAsPublicAccount(username);
272}
273
274void WebUILoginDisplay::MigrateUserData(const std::string& old_password) {
275  DCHECK(delegate_);
276  if (delegate_)
277    delegate_->MigrateUserData(old_password);
278}
279
280void WebUILoginDisplay::LoadWallpaper(const std::string& username) {
281  WallpaperManager::Get()->SetUserWallpaperDelayed(username);
282}
283
284void WebUILoginDisplay::LoadSigninWallpaper() {
285  WallpaperManager::Get()->SetDefaultWallpaperDelayed(UserManager::kSignInUser);
286}
287
288void WebUILoginDisplay::OnSigninScreenReady() {
289  if (delegate_)
290    delegate_->OnSigninScreenReady();
291}
292
293void WebUILoginDisplay::RemoveUser(const std::string& username) {
294  UserManager::Get()->RemoveUser(username, this);
295}
296
297void WebUILoginDisplay::ResyncUserData() {
298  DCHECK(delegate_);
299  if (delegate_)
300    delegate_->ResyncUserData();
301}
302
303void WebUILoginDisplay::ShowEnterpriseEnrollmentScreen() {
304  if (delegate_)
305    delegate_->OnStartEnterpriseEnrollment();
306}
307
308void WebUILoginDisplay::ShowKioskEnableScreen() {
309  if (delegate_)
310    delegate_->OnStartKioskEnableScreen();
311}
312
313void WebUILoginDisplay::ShowKioskAutolaunchScreen() {
314  if (delegate_)
315    delegate_->OnStartKioskAutolaunchScreen();
316}
317
318void WebUILoginDisplay::ShowWrongHWIDScreen() {
319  if (delegate_)
320    delegate_->ShowWrongHWIDScreen();
321}
322
323void WebUILoginDisplay::SetWebUIHandler(
324    LoginDisplayWebUIHandler* webui_handler) {
325  webui_handler_ = webui_handler;
326  gaia_screen_->SetHandler(webui_handler_);
327  user_selection_screen_->SetHandler(webui_handler_);
328}
329
330void WebUILoginDisplay::ShowSigninScreenForCreds(
331    const std::string& username,
332    const std::string& password) {
333  if (webui_handler_)
334    webui_handler_->ShowSigninScreenForCreds(username, password);
335}
336
337bool WebUILoginDisplay::IsShowGuest() const {
338  return show_guest_;
339}
340
341bool WebUILoginDisplay::IsShowUsers() const {
342  return show_users_;
343}
344
345bool WebUILoginDisplay::IsSigninInProgress() const {
346  return delegate_->IsSigninInProgress();
347}
348
349bool WebUILoginDisplay::IsUserSigninCompleted() const {
350  return is_signin_completed();
351}
352
353void WebUILoginDisplay::SetDisplayEmail(const std::string& email) {
354  if (delegate_)
355    delegate_->SetDisplayEmail(email);
356}
357
358void WebUILoginDisplay::Signout() {
359  delegate_->Signout();
360}
361
362void WebUILoginDisplay::LoginAsKioskApp(const std::string& app_id,
363                                        bool diagnostic_mode) {
364  delegate_->LoginAsKioskApp(app_id, diagnostic_mode);
365}
366
367void WebUILoginDisplay::OnUserActivity(const ui::Event* event) {
368  if (delegate_)
369    delegate_->ResetPublicSessionAutoLoginTimer();
370}
371
372
373}  // namespace chromeos
374