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.
5 #include "chrome/browser/chromeos/login/webui_login_display.h"
8 #include "ash/wm/user_activity_detector.h"
9 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
10 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
11 #include "chrome/browser/chromeos/login/screen_locker.h"
12 #include "chrome/browser/chromeos/login/user_adding_screen.h"
13 #include "chrome/browser/chromeos/login/wallpaper_manager.h"
14 #include "chrome/browser/chromeos/login/webui_login_view.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/browser_window.h"
17 #include "chromeos/ime/input_method_manager.h"
18 #include "chromeos/ime/xkeyboard.h"
19 #include "grit/chromium_strings.h"
20 #include "grit/generated_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/views/widget/widget.h"
28 const int kPasswordClearTimeoutSec = 60;
32 // WebUILoginDisplay, public: --------------------------------------------------
34 WebUILoginDisplay::~WebUILoginDisplay() {
36 webui_handler_->ResetSigninScreenHandlerDelegate();
37 ash::UserActivityDetector* activity_detector = ash::Shell::GetInstance()->
38 user_activity_detector();
39 if (activity_detector->HasObserver(this))
40 activity_detector->RemoveObserver(this);
43 // LoginDisplay implementation: ------------------------------------------------
45 WebUILoginDisplay::WebUILoginDisplay(LoginDisplay::Delegate* delegate)
46 : LoginDisplay(delegate, gfx::Rect()),
48 show_new_user_(false),
49 webui_handler_(NULL) {
52 void WebUILoginDisplay::ClearAndEnablePassword() {
54 webui_handler_->ClearAndEnablePassword();
57 void WebUILoginDisplay::Init(const UserList& users,
61 // Testing that the delegate has been set.
65 show_guest_ = show_guest;
66 show_users_ = show_users;
67 show_new_user_ = show_new_user;
69 ash::UserActivityDetector* activity_detector = ash::Shell::GetInstance()->
70 user_activity_detector();
71 if (!activity_detector->HasObserver(this))
72 activity_detector->AddObserver(this);
75 void WebUILoginDisplay::OnPreferencesChanged() {
77 webui_handler_->OnPreferencesChanged();
80 void WebUILoginDisplay::OnBeforeUserRemoved(const std::string& username) {
81 for (UserList::iterator it = users_.begin(); it != users_.end(); ++it) {
82 if ((*it)->email() == username) {
89 void WebUILoginDisplay::OnUserImageChanged(const User& user) {
91 webui_handler_->OnUserImageChanged(user);
94 void WebUILoginDisplay::OnUserRemoved(const std::string& username) {
96 webui_handler_->OnUserRemoved(username);
99 void WebUILoginDisplay::OnFadeOut() {
102 void WebUILoginDisplay::OnLoginSuccess(const std::string& username) {
104 webui_handler_->OnLoginSuccess(username);
107 void WebUILoginDisplay::SetUIEnabled(bool is_enabled) {
108 // TODO(nkostylev): Cleanup this condition,
109 // see http://crbug.com/157885 and http://crbug.com/158255.
110 // Allow this call only before user sign in or at lock screen.
111 // If this call is made after new user signs in but login screen is still
112 // around that would trigger a sign in extension refresh.
114 (!UserManager::Get()->IsUserLoggedIn() ||
115 ScreenLocker::default_screen_locker())) {
116 ClearAndEnablePassword();
119 if (chromeos::LoginDisplayHost* host =
120 chromeos::LoginDisplayHostImpl::default_host()) {
121 if (chromeos::WebUILoginView* login_view = host->GetWebUILoginView())
122 login_view->SetUIEnabled(is_enabled);
126 void WebUILoginDisplay::SelectPod(int index) {
129 void WebUILoginDisplay::ShowBannerMessage(const std::string& message) {
132 webui_handler_->ShowBannerMessage(message);
135 void WebUILoginDisplay::ShowUserPodButton(
136 const std::string& username,
137 const std::string& iconURL,
138 const base::Closure& click_callback) {
141 webui_handler_->ShowUserPodButton(username, iconURL, click_callback);
144 void WebUILoginDisplay::ShowError(int error_msg_id,
146 HelpAppLauncher::HelpTopic help_topic_id) {
147 VLOG(1) << "Show error, error_id: " << error_msg_id
148 << ", attempts:" << login_attempts
149 << ", help_topic_id: " << help_topic_id;
153 std::string error_text;
154 switch (error_msg_id) {
155 case IDS_LOGIN_ERROR_AUTHENTICATING_HOSTED:
156 error_text = l10n_util::GetStringFUTF8(
157 error_msg_id, l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_OS_NAME));
159 case IDS_LOGIN_ERROR_CAPTIVE_PORTAL:
160 error_text = l10n_util::GetStringFUTF8(
161 error_msg_id, delegate()->GetConnectedNetworkName());
164 error_text = l10n_util::GetStringUTF8(error_msg_id);
168 // Only display hints about keyboard layout if the error is authentication-
170 if (error_msg_id != IDS_LOGIN_ERROR_WHITELIST &&
171 error_msg_id != IDS_LOGIN_ERROR_OWNER_KEY_LOST &&
172 error_msg_id != IDS_LOGIN_ERROR_OWNER_REQUIRED) {
173 // Display a warning if Caps Lock is on.
174 input_method::InputMethodManager* ime_manager =
175 input_method::InputMethodManager::Get();
176 if (ime_manager->GetXKeyboard()->CapsLockIsEnabled()) {
177 // TODO(ivankr): use a format string instead of concatenation.
179 l10n_util::GetStringUTF8(IDS_LOGIN_ERROR_CAPS_LOCK_HINT);
182 // Display a hint to switch keyboards if there are other active input
184 if (ime_manager->GetNumActiveInputMethods() > 1) {
186 l10n_util::GetStringUTF8(IDS_LOGIN_ERROR_KEYBOARD_SWITCH_HINT);
190 std::string help_link;
191 switch (error_msg_id) {
192 case IDS_LOGIN_ERROR_AUTHENTICATING_HOSTED:
193 help_link = l10n_util::GetStringUTF8(IDS_LEARN_MORE);
196 if (login_attempts > 1)
197 help_link = l10n_util::GetStringUTF8(IDS_LEARN_MORE);
201 webui_handler_->ShowError(login_attempts, error_text, help_link,
205 void WebUILoginDisplay::ShowErrorScreen(LoginDisplay::SigninError error_id) {
206 VLOG(1) << "Show error screen, error_id: " << error_id;
209 webui_handler_->ShowErrorScreen(error_id);
212 void WebUILoginDisplay::ShowGaiaPasswordChanged(const std::string& username) {
214 webui_handler_->ShowGaiaPasswordChanged(username);
217 void WebUILoginDisplay::ShowPasswordChangedDialog(bool show_password_error) {
219 webui_handler_->ShowPasswordChangedDialog(show_password_error);
222 void WebUILoginDisplay::ShowSigninUI(const std::string& email) {
224 webui_handler_->ShowSigninUI(email);
227 // WebUILoginDisplay, NativeWindowDelegate implementation: ---------------------
228 gfx::NativeWindow WebUILoginDisplay::GetNativeWindow() const {
229 return parent_window();
232 // WebUILoginDisplay, SigninScreenHandlerDelegate implementation: --------------
233 void WebUILoginDisplay::CancelPasswordChangedFlow() {
236 delegate_->CancelPasswordChangedFlow();
239 void WebUILoginDisplay::CancelUserAdding() {
240 if (!UserAddingScreen::Get()->IsRunning()) {
241 LOG(ERROR) << "User adding screen not running.";
244 UserAddingScreen::Get()->Cancel();
247 void WebUILoginDisplay::CreateAccount() {
250 delegate_->CreateAccount();
253 void WebUILoginDisplay::CompleteLogin(const UserContext& user_context) {
256 delegate_->CompleteLogin(user_context);
259 void WebUILoginDisplay::Login(const UserContext& user_context) {
262 delegate_->Login(user_context);
265 void WebUILoginDisplay::LoginAsRetailModeUser() {
268 delegate_->LoginAsRetailModeUser();
271 void WebUILoginDisplay::LoginAsGuest() {
274 delegate_->LoginAsGuest();
277 void WebUILoginDisplay::LoginAsPublicAccount(const std::string& username) {
280 delegate_->LoginAsPublicAccount(username);
283 void WebUILoginDisplay::MigrateUserData(const std::string& old_password) {
286 delegate_->MigrateUserData(old_password);
289 void WebUILoginDisplay::LoadWallpaper(const std::string& username) {
290 WallpaperManager::Get()->SetUserWallpaperDelayed(username);
293 void WebUILoginDisplay::LoadSigninWallpaper() {
294 WallpaperManager::Get()->SetDefaultWallpaperDelayed(UserManager::kSignInUser);
297 void WebUILoginDisplay::OnSigninScreenReady() {
299 delegate_->OnSigninScreenReady();
302 void WebUILoginDisplay::RemoveUser(const std::string& username) {
303 UserManager::Get()->RemoveUser(username, this);
306 void WebUILoginDisplay::ResyncUserData() {
309 delegate_->ResyncUserData();
312 void WebUILoginDisplay::ShowEnterpriseEnrollmentScreen() {
314 delegate_->OnStartEnterpriseEnrollment();
317 void WebUILoginDisplay::ShowKioskEnableScreen() {
319 delegate_->OnStartKioskEnableScreen();
322 void WebUILoginDisplay::ShowResetScreen() {
324 delegate_->OnStartDeviceReset();
327 void WebUILoginDisplay::ShowKioskAutolaunchScreen() {
329 delegate_->OnStartKioskAutolaunchScreen();
332 void WebUILoginDisplay::ShowWrongHWIDScreen() {
334 delegate_->ShowWrongHWIDScreen();
337 void WebUILoginDisplay::SetWebUIHandler(
338 LoginDisplayWebUIHandler* webui_handler) {
339 webui_handler_ = webui_handler;
342 void WebUILoginDisplay::ShowSigninScreenForCreds(
343 const std::string& username,
344 const std::string& password) {
346 webui_handler_->ShowSigninScreenForCreds(username, password);
349 const UserList& WebUILoginDisplay::GetUsers() const {
353 bool WebUILoginDisplay::IsShowGuest() const {
357 bool WebUILoginDisplay::IsShowUsers() const {
361 bool WebUILoginDisplay::IsShowNewUser() const {
362 return show_new_user_;
365 bool WebUILoginDisplay::IsSigninInProgress() const {
366 return delegate_->IsSigninInProgress();
369 bool WebUILoginDisplay::IsUserSigninCompleted() const {
370 return is_signin_completed();
373 void WebUILoginDisplay::SetDisplayEmail(const std::string& email) {
375 delegate_->SetDisplayEmail(email);
378 void WebUILoginDisplay::Signout() {
379 delegate_->Signout();
382 void WebUILoginDisplay::LoginAsKioskApp(const std::string& app_id,
383 bool diagnostic_mode) {
384 delegate_->LoginAsKioskApp(app_id, diagnostic_mode);
387 void WebUILoginDisplay::OnUserActivity(const ui::Event* event) {
388 if (!password_clear_timer_.IsRunning())
389 StartPasswordClearTimer();
390 password_clear_timer_.Reset();
392 delegate_->ResetPublicSessionAutoLoginTimer();
395 void WebUILoginDisplay::StartPasswordClearTimer() {
396 DCHECK(!password_clear_timer_.IsRunning());
397 password_clear_timer_.Start(FROM_HERE,
398 base::TimeDelta::FromSeconds(kPasswordClearTimeoutSec), this,
399 &WebUILoginDisplay::OnPasswordClearTimerExpired);
402 void WebUILoginDisplay::OnPasswordClearTimerExpired() {
404 webui_handler_->ClearUserPodPassword();
407 } // namespace chromeos