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/ui/webui/chromeos/login/error_screen_handler.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "base/time/time.h"
     10 #include "chrome/browser/chrome_notification_types.h"
     11 #include "chrome/browser/chromeos/login/captive_portal_window_proxy.h"
     12 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
     13 #include "chrome/browser/chromeos/login/webui_login_view.h"
     14 #include "chrome/browser/chromeos/net/network_portal_detector.h"
     15 #include "chrome/browser/ui/webui/chromeos/login/native_window_delegate.h"
     16 #include "chrome/browser/ui/webui/chromeos/login/network_state_informer.h"
     17 #include "chromeos/dbus/dbus_thread_manager.h"
     18 #include "chromeos/dbus/power_manager_client.h"
     19 #include "chromeos/dbus/session_manager_client.h"
     20 #include "grit/chromium_strings.h"
     21 #include "grit/generated_resources.h"
     22 
     23 namespace {
     24 
     25 const char kJsScreenPath[] = "login.ErrorMessageScreen";
     26 
     27 }  // namespace
     28 
     29 namespace chromeos {
     30 
     31 ErrorScreenHandler::ErrorScreenHandler(
     32     const scoped_refptr<NetworkStateInformer>& network_state_informer)
     33     : BaseScreenHandler(kJsScreenPath),
     34       network_state_informer_(network_state_informer),
     35       show_on_init_(false) {
     36   DCHECK(network_state_informer_.get());
     37 }
     38 
     39 ErrorScreenHandler::~ErrorScreenHandler() {
     40 }
     41 
     42 void ErrorScreenHandler::Show(OobeDisplay::Screen parent_screen,
     43                               base::DictionaryValue* params) {
     44   if (!page_is_ready()) {
     45     show_on_init_ = true;
     46     return;
     47   }
     48   parent_screen_ = parent_screen;
     49   ShowScreen(OobeUI::kScreenErrorMessage, params);
     50   NetworkErrorShown();
     51   NetworkPortalDetector::Get()->EnableLazyDetection();
     52   LOG(WARNING) << "Offline message is displayed";
     53 }
     54 
     55 void ErrorScreenHandler::Hide() {
     56   if (parent_screen_ == OobeUI::SCREEN_UNKNOWN)
     57     return;
     58   std::string screen_name;
     59   if (GetScreenName(parent_screen_, &screen_name))
     60     ShowScreen(screen_name.c_str(), NULL);
     61   NetworkPortalDetector::Get()->DisableLazyDetection();
     62   LOG(WARNING) << "Offline message is hidden";
     63 }
     64 
     65 void ErrorScreenHandler::FixCaptivePortal() {
     66   if (!captive_portal_window_proxy_.get()) {
     67     content::WebContents* web_contents =
     68         LoginDisplayHostImpl::default_host()->GetWebUILoginView()->
     69             GetWebContents();
     70     captive_portal_window_proxy_.reset(
     71         new CaptivePortalWindowProxy(network_state_informer_.get(),
     72                                      web_contents));
     73   }
     74   captive_portal_window_proxy_->ShowIfRedirected();
     75 }
     76 
     77 void ErrorScreenHandler::ShowCaptivePortal() {
     78   // This call is an explicit user action
     79   // i.e. clicking on link so force dialog show.
     80   FixCaptivePortal();
     81   captive_portal_window_proxy_->Show();
     82 }
     83 
     84 void ErrorScreenHandler::HideCaptivePortal() {
     85   if (captive_portal_window_proxy_.get())
     86     captive_portal_window_proxy_->Close();
     87 }
     88 
     89 void ErrorScreenHandler::SetUIState(ErrorScreen::UIState ui_state) {
     90   ui_state_ = ui_state;
     91   if (page_is_ready())
     92     CallJS("setUIState", static_cast<int>(ui_state_));
     93 }
     94 
     95 void ErrorScreenHandler::SetErrorState(ErrorScreen::ErrorState error_state,
     96                                        const std::string& network) {
     97   error_state_ = error_state;
     98   network_ = network;
     99   if (page_is_ready())
    100     CallJS("setErrorState", static_cast<int>(error_state_), network);
    101 }
    102 
    103 void ErrorScreenHandler::AllowGuestSignin(bool allowed) {
    104   guest_signin_allowed_ = allowed;
    105   if (page_is_ready())
    106     CallJS("allowGuestSignin", allowed);
    107 }
    108 
    109 void ErrorScreenHandler::AllowOfflineLogin(bool allowed) {
    110   offline_login_allowed_ = allowed;
    111   if (page_is_ready())
    112     CallJS("allowOfflineLogin", allowed);
    113 }
    114 
    115 void ErrorScreenHandler::NetworkErrorShown() {
    116   content::NotificationService::current()->Notify(
    117       chrome::NOTIFICATION_LOGIN_NETWORK_ERROR_SHOWN,
    118       content::NotificationService::AllSources(),
    119       content::NotificationService::NoDetails());
    120 }
    121 
    122 bool ErrorScreenHandler::GetScreenName(OobeUI::Screen screen,
    123                                        std::string* name) const {
    124   OobeUI* oobe_ui = static_cast<OobeUI*>(web_ui()->GetController());
    125   if (!oobe_ui)
    126     return false;
    127   *name = oobe_ui->GetScreenName(screen);
    128   return true;
    129 }
    130 
    131 void ErrorScreenHandler::HandleShowCaptivePortal() {
    132   ShowCaptivePortal();
    133 }
    134 
    135 void ErrorScreenHandler::HandleHideCaptivePortal() {
    136   HideCaptivePortal();
    137 }
    138 
    139 void ErrorScreenHandler::HandleLocalStateErrorPowerwashButtonClicked() {
    140   chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
    141       StartDeviceWipe();
    142 }
    143 
    144 void ErrorScreenHandler::HandleRebootButtonClicked() {
    145   chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RequestRestart();
    146 }
    147 
    148 void ErrorScreenHandler::RegisterMessages() {
    149   AddCallback("showCaptivePortal",
    150               &ErrorScreenHandler::HandleShowCaptivePortal);
    151   AddCallback("hideCaptivePortal",
    152               &ErrorScreenHandler::HandleHideCaptivePortal);
    153   AddCallback("localStateErrorPowerwashButtonClicked",
    154               &ErrorScreenHandler::HandleLocalStateErrorPowerwashButtonClicked);
    155   AddCallback("rebootButtonClicked",
    156               &ErrorScreenHandler::HandleRebootButtonClicked);
    157 }
    158 
    159 void ErrorScreenHandler::DeclareLocalizedValues(
    160     LocalizedValuesBuilder* builder) {
    161   builder->Add("loginErrorTitle", IDS_LOGIN_ERROR_TITLE);
    162   builder->Add("signinOfflineMessageBody", IDS_LOGIN_OFFLINE_MESSAGE);
    163   builder->Add("kioskOfflineMessageBody", IDS_KIOSK_OFFLINE_MESSAGE);
    164   builder->Add("captivePortalTitle", IDS_LOGIN_MAYBE_CAPTIVE_PORTAL_TITLE);
    165   builder->Add("captivePortalMessage", IDS_LOGIN_MAYBE_CAPTIVE_PORTAL);
    166   builder->Add("captivePortalProxyMessage",
    167                IDS_LOGIN_MAYBE_CAPTIVE_PORTAL_PROXY);
    168   builder->Add("captivePortalNetworkSelect",
    169                IDS_LOGIN_MAYBE_CAPTIVE_PORTAL_NETWORK_SELECT);
    170   builder->Add("signinProxyMessageText", IDS_LOGIN_PROXY_ERROR_MESSAGE);
    171   builder->Add("updateOfflineMessageBody", IDS_UPDATE_OFFLINE_MESSAGE);
    172   builder->Add("updateProxyMessageText", IDS_UPDATE_PROXY_ERROR_MESSAGE);
    173   builder->AddF("localStateErrorText0", IDS_LOCAL_STATE_ERROR_TEXT_0,
    174                 IDS_SHORT_PRODUCT_NAME);
    175   builder->Add("localStateErrorText1", IDS_LOCAL_STATE_ERROR_TEXT_1);
    176   builder->Add("localStateErrorPowerwashButton",
    177                IDS_LOCAL_STATE_ERROR_POWERWASH_BUTTON);
    178   builder->Add("rebootButton", IDS_RELAUNCH_BUTTON);
    179 }
    180 
    181 void ErrorScreenHandler::Initialize() {
    182   if (!page_is_ready())
    183     return;
    184   if (show_on_init_) {
    185     base::DictionaryValue params;
    186     params.SetInteger("uiState", static_cast<int>(ui_state_));
    187     params.SetInteger("errorState", static_cast<int>(error_state_));
    188     params.SetString("network", network_);
    189     params.SetBoolean("guestSigninAllowed", guest_signin_allowed_);
    190     params.SetBoolean("offlineLoginAllowed", offline_login_allowed_);
    191     Show(parent_screen_, &params);
    192     show_on_init_ = false;
    193   }
    194 }
    195 
    196 }  // namespace chromeos
    197