Home | History | Annotate | Download | only in login
      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/shell.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"
     23 
     24 namespace chromeos {
     25 
     26 namespace {
     27 
     28 const int kPasswordClearTimeoutSec = 60;
     29 
     30 }
     31 
     32 // WebUILoginDisplay, public: --------------------------------------------------
     33 
     34 WebUILoginDisplay::~WebUILoginDisplay() {
     35   if (webui_handler_)
     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);
     41 }
     42 
     43 // LoginDisplay implementation: ------------------------------------------------
     44 
     45 WebUILoginDisplay::WebUILoginDisplay(LoginDisplay::Delegate* delegate)
     46     : LoginDisplay(delegate, gfx::Rect()),
     47       show_guest_(false),
     48       show_new_user_(false),
     49       webui_handler_(NULL) {
     50 }
     51 
     52 void WebUILoginDisplay::ClearAndEnablePassword() {
     53   if (webui_handler_)
     54       webui_handler_->ClearAndEnablePassword();
     55 }
     56 
     57 void WebUILoginDisplay::Init(const UserList& users,
     58                              bool show_guest,
     59                              bool show_users,
     60                              bool show_new_user) {
     61   // Testing that the delegate has been set.
     62   DCHECK(delegate_);
     63 
     64   users_ = users;
     65   show_guest_ = show_guest;
     66   show_users_ = show_users;
     67   show_new_user_ = show_new_user;
     68 
     69   ash::UserActivityDetector* activity_detector = ash::Shell::GetInstance()->
     70       user_activity_detector();
     71   if (!activity_detector->HasObserver(this))
     72     activity_detector->AddObserver(this);
     73 }
     74 
     75 void WebUILoginDisplay::OnPreferencesChanged() {
     76   if (webui_handler_)
     77     webui_handler_->OnPreferencesChanged();
     78 }
     79 
     80 void WebUILoginDisplay::OnBeforeUserRemoved(const std::string& username) {
     81   for (UserList::iterator it = users_.begin(); it != users_.end(); ++it) {
     82     if ((*it)->email() == username) {
     83       users_.erase(it);
     84       break;
     85     }
     86   }
     87 }
     88 
     89 void WebUILoginDisplay::OnUserImageChanged(const User& user) {
     90   if (webui_handler_)
     91     webui_handler_->OnUserImageChanged(user);
     92 }
     93 
     94 void WebUILoginDisplay::OnUserRemoved(const std::string& username) {
     95   if (webui_handler_)
     96     webui_handler_->OnUserRemoved(username);
     97 }
     98 
     99 void WebUILoginDisplay::OnFadeOut() {
    100 }
    101 
    102 void WebUILoginDisplay::OnLoginSuccess(const std::string& username) {
    103   if (webui_handler_)
    104     webui_handler_->OnLoginSuccess(username);
    105 }
    106 
    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.
    113   if (is_enabled &&
    114       (!UserManager::Get()->IsUserLoggedIn() ||
    115        ScreenLocker::default_screen_locker())) {
    116     ClearAndEnablePassword();
    117   }
    118 
    119   if (chromeos::LoginDisplayHost* host =
    120           chromeos::LoginDisplayHostImpl::default_host()) {
    121     if (chromeos::WebUILoginView* login_view = host->GetWebUILoginView())
    122       login_view->SetUIEnabled(is_enabled);
    123   }
    124 }
    125 
    126 void WebUILoginDisplay::SelectPod(int index) {
    127 }
    128 
    129 void WebUILoginDisplay::ShowBannerMessage(const std::string& message) {
    130   if (!webui_handler_)
    131     return;
    132   webui_handler_->ShowBannerMessage(message);
    133 }
    134 
    135 void WebUILoginDisplay::ShowUserPodButton(
    136     const std::string& username,
    137     const std::string& iconURL,
    138     const base::Closure& click_callback) {
    139   if (!webui_handler_)
    140     return;
    141   webui_handler_->ShowUserPodButton(username, iconURL, click_callback);
    142 }
    143 
    144 void WebUILoginDisplay::ShowError(int error_msg_id,
    145                                   int login_attempts,
    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;
    150   if (!webui_handler_)
    151     return;
    152 
    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));
    158       break;
    159     case IDS_LOGIN_ERROR_CAPTIVE_PORTAL:
    160       error_text = l10n_util::GetStringFUTF8(
    161           error_msg_id, delegate()->GetConnectedNetworkName());
    162       break;
    163     default:
    164       error_text = l10n_util::GetStringUTF8(error_msg_id);
    165       break;
    166   }
    167 
    168   // Only display hints about keyboard layout if the error is authentication-
    169   // related.
    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.
    178       error_text += "\n" +
    179           l10n_util::GetStringUTF8(IDS_LOGIN_ERROR_CAPS_LOCK_HINT);
    180     }
    181 
    182     // Display a hint to switch keyboards if there are other active input
    183     // methods.
    184     if (ime_manager->GetNumActiveInputMethods() > 1) {
    185       error_text += "\n" +
    186           l10n_util::GetStringUTF8(IDS_LOGIN_ERROR_KEYBOARD_SWITCH_HINT);
    187     }
    188   }
    189 
    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);
    194       break;
    195     default:
    196       if (login_attempts > 1)
    197         help_link = l10n_util::GetStringUTF8(IDS_LEARN_MORE);
    198       break;
    199   }
    200 
    201   webui_handler_->ShowError(login_attempts, error_text, help_link,
    202                             help_topic_id);
    203 }
    204 
    205 void WebUILoginDisplay::ShowErrorScreen(LoginDisplay::SigninError error_id) {
    206   VLOG(1) << "Show error screen, error_id: " << error_id;
    207   if (!webui_handler_)
    208     return;
    209   webui_handler_->ShowErrorScreen(error_id);
    210 }
    211 
    212 void WebUILoginDisplay::ShowGaiaPasswordChanged(const std::string& username) {
    213   if (webui_handler_)
    214     webui_handler_->ShowGaiaPasswordChanged(username);
    215 }
    216 
    217 void WebUILoginDisplay::ShowPasswordChangedDialog(bool show_password_error) {
    218   if (webui_handler_)
    219     webui_handler_->ShowPasswordChangedDialog(show_password_error);
    220 }
    221 
    222 void WebUILoginDisplay::ShowSigninUI(const std::string& email) {
    223   if (webui_handler_)
    224     webui_handler_->ShowSigninUI(email);
    225 }
    226 
    227 // WebUILoginDisplay, NativeWindowDelegate implementation: ---------------------
    228 gfx::NativeWindow WebUILoginDisplay::GetNativeWindow() const {
    229   return parent_window();
    230 }
    231 
    232 // WebUILoginDisplay, SigninScreenHandlerDelegate implementation: --------------
    233 void WebUILoginDisplay::CancelPasswordChangedFlow() {
    234   DCHECK(delegate_);
    235   if (delegate_)
    236     delegate_->CancelPasswordChangedFlow();
    237 }
    238 
    239 void WebUILoginDisplay::CancelUserAdding() {
    240   if (!UserAddingScreen::Get()->IsRunning()) {
    241     LOG(ERROR) << "User adding screen not running.";
    242     return;
    243   }
    244   UserAddingScreen::Get()->Cancel();
    245 }
    246 
    247 void WebUILoginDisplay::CreateAccount() {
    248   DCHECK(delegate_);
    249   if (delegate_)
    250     delegate_->CreateAccount();
    251 }
    252 
    253 void WebUILoginDisplay::CompleteLogin(const UserContext& user_context) {
    254   DCHECK(delegate_);
    255   if (delegate_)
    256     delegate_->CompleteLogin(user_context);
    257 }
    258 
    259 void WebUILoginDisplay::Login(const UserContext& user_context) {
    260   DCHECK(delegate_);
    261   if (delegate_)
    262     delegate_->Login(user_context);
    263 }
    264 
    265 void WebUILoginDisplay::LoginAsRetailModeUser() {
    266   DCHECK(delegate_);
    267   if (delegate_)
    268     delegate_->LoginAsRetailModeUser();
    269 }
    270 
    271 void WebUILoginDisplay::LoginAsGuest() {
    272   DCHECK(delegate_);
    273   if (delegate_)
    274     delegate_->LoginAsGuest();
    275 }
    276 
    277 void WebUILoginDisplay::LoginAsPublicAccount(const std::string& username) {
    278   DCHECK(delegate_);
    279   if (delegate_)
    280     delegate_->LoginAsPublicAccount(username);
    281 }
    282 
    283 void WebUILoginDisplay::MigrateUserData(const std::string& old_password) {
    284   DCHECK(delegate_);
    285   if (delegate_)
    286     delegate_->MigrateUserData(old_password);
    287 }
    288 
    289 void WebUILoginDisplay::LoadWallpaper(const std::string& username) {
    290   WallpaperManager::Get()->SetUserWallpaper(username);
    291 }
    292 
    293 void WebUILoginDisplay::LoadSigninWallpaper() {
    294   WallpaperManager::Get()->SetDefaultWallpaper();
    295 }
    296 
    297 void WebUILoginDisplay::OnSigninScreenReady() {
    298   if (delegate_)
    299     delegate_->OnSigninScreenReady();
    300 }
    301 
    302 void WebUILoginDisplay::RemoveUser(const std::string& username) {
    303   UserManager::Get()->RemoveUser(username, this);
    304 }
    305 
    306 void WebUILoginDisplay::ResyncUserData() {
    307   DCHECK(delegate_);
    308   if (delegate_)
    309     delegate_->ResyncUserData();
    310 }
    311 
    312 void WebUILoginDisplay::ShowEnterpriseEnrollmentScreen() {
    313   if (delegate_)
    314     delegate_->OnStartEnterpriseEnrollment();
    315 }
    316 
    317 void WebUILoginDisplay::ShowKioskEnableScreen() {
    318   if (delegate_)
    319     delegate_->OnStartKioskEnableScreen();
    320 }
    321 
    322 void WebUILoginDisplay::ShowResetScreen() {
    323   if (delegate_)
    324     delegate_->OnStartDeviceReset();
    325 }
    326 
    327 void WebUILoginDisplay::ShowKioskAutolaunchScreen() {
    328   if (delegate_)
    329     delegate_->OnStartKioskAutolaunchScreen();
    330 }
    331 
    332 void WebUILoginDisplay::ShowWrongHWIDScreen() {
    333   if (delegate_)
    334     delegate_->ShowWrongHWIDScreen();
    335 }
    336 
    337 void WebUILoginDisplay::SetWebUIHandler(
    338     LoginDisplayWebUIHandler* webui_handler) {
    339   webui_handler_ = webui_handler;
    340 }
    341 
    342 void WebUILoginDisplay::ShowSigninScreenForCreds(
    343     const std::string& username,
    344     const std::string& password) {
    345   if (webui_handler_)
    346     webui_handler_->ShowSigninScreenForCreds(username, password);
    347 }
    348 
    349 const UserList& WebUILoginDisplay::GetUsers() const {
    350   return users_;
    351 }
    352 
    353 bool WebUILoginDisplay::IsShowGuest() const {
    354   return show_guest_;
    355 }
    356 
    357 bool WebUILoginDisplay::IsShowUsers() const {
    358   return show_users_;
    359 }
    360 
    361 bool WebUILoginDisplay::IsShowNewUser() const {
    362   return show_new_user_;
    363 }
    364 
    365 bool WebUILoginDisplay::IsSigninInProgress() const {
    366   return delegate_->IsSigninInProgress();
    367 }
    368 
    369 bool WebUILoginDisplay::IsUserSigninCompleted() const {
    370   return is_signin_completed();
    371 }
    372 
    373 void WebUILoginDisplay::SetDisplayEmail(const std::string& email) {
    374   if (delegate_)
    375     delegate_->SetDisplayEmail(email);
    376 }
    377 
    378 void WebUILoginDisplay::Signout() {
    379   delegate_->Signout();
    380 }
    381 
    382 void WebUILoginDisplay::LoginAsKioskApp(const std::string& app_id) {
    383   delegate_->LoginAsKioskApp(app_id);
    384 }
    385 
    386 void WebUILoginDisplay::OnUserActivity(const ui::Event* event) {
    387   if (!password_clear_timer_.IsRunning())
    388     StartPasswordClearTimer();
    389   password_clear_timer_.Reset();
    390   if (delegate_)
    391     delegate_->ResetPublicSessionAutoLoginTimer();
    392 }
    393 
    394 void WebUILoginDisplay::StartPasswordClearTimer() {
    395   DCHECK(!password_clear_timer_.IsRunning());
    396   password_clear_timer_.Start(FROM_HERE,
    397       base::TimeDelta::FromSeconds(kPasswordClearTimeoutSec), this,
    398       &WebUILoginDisplay::OnPasswordClearTimerExpired);
    399 }
    400 
    401 void WebUILoginDisplay::OnPasswordClearTimerExpired() {
    402   if (webui_handler_)
    403     webui_handler_->ClearUserPodPassword();
    404 }
    405 
    406 }  // namespace chromeos
    407