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