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