Home | History | Annotate | Download | only in login
      1 // Copyright 2013 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/gaia_screen_handler.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/values.h"
      9 #include "chrome/browser/browser_process.h"
     10 #include "chrome/browser/chromeos/login/user_manager.h"
     11 #include "chrome/browser/chromeos/settings/cros_settings.h"
     12 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
     13 #include "chromeos/settings/cros_settings_names.h"
     14 #include "google_apis/gaia/gaia_switches.h"
     15 #include "google_apis/gaia/gaia_urls.h"
     16 #include "grit/chromium_strings.h"
     17 #include "grit/generated_resources.h"
     18 #include "ui/base/l10n/l10n_util.h"
     19 
     20 namespace chromeos {
     21 
     22 namespace {
     23 
     24 const char kJsScreenPath[] = "login.GaiaSigninScreen";
     25 
     26 // Updates params dictionary passed to the auth extension with related
     27 // preferences from CrosSettings.
     28 void UpdateAuthParamsFromSettings(DictionaryValue* params,
     29                                   const CrosSettings* cros_settings) {
     30   bool allow_new_user = true;
     31   cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user);
     32   bool allow_guest = true;
     33   cros_settings->GetBoolean(kAccountsPrefAllowGuest, &allow_guest);
     34   // Account creation depends on Guest sign-in (http://crosbug.com/24570).
     35   params->SetBoolean("createAccount", allow_new_user && allow_guest);
     36   params->SetBoolean("guestSignin", allow_guest);
     37 }
     38 
     39 void UpdateAuthParams(DictionaryValue* params, bool has_users) {
     40   UpdateAuthParamsFromSettings(params, CrosSettings::Get());
     41 
     42   // Allow locally managed user creation only if:
     43   // 1. Enterprise managed device > is allowed by policy.
     44   // 2. Consumer device > owner exists.
     45   // 3. New users are allowed by owner.
     46 
     47   CrosSettings* cros_settings = CrosSettings::Get();
     48   bool allow_new_user = false;
     49   cros_settings->GetBoolean(kAccountsPrefAllowNewUser, &allow_new_user);
     50 
     51   bool managed_users_allowed =
     52       UserManager::Get()->AreLocallyManagedUsersAllowed();
     53   bool managed_users_can_create = true;
     54   int message_id = -1;
     55   if (!has_users) {
     56     managed_users_can_create = false;
     57     message_id = IDS_CREATE_LOCALLY_MANAGED_USER_NO_MANAGER_TEXT;
     58   }
     59   if (!allow_new_user) {
     60     managed_users_can_create = false;
     61     message_id = IDS_CREATE_LOCALLY_MANAGED_USER_CREATION_RESTRICTED_TEXT;
     62   }
     63 
     64   params->SetBoolean("managedUsersEnabled", managed_users_allowed);
     65   params->SetBoolean("managedUsersCanCreate", managed_users_can_create);
     66   if (!managed_users_can_create) {
     67     params->SetString("managedUsersRestrictionReason",
     68                       l10n_util::GetStringUTF16(message_id));
     69   }
     70 }
     71 
     72 }  // namespace
     73 
     74 GaiaContext::GaiaContext()
     75     : force_reload(false),
     76       is_local(false),
     77       password_changed(false),
     78       show_users(false),
     79       use_offline(false),
     80       has_users(false) {}
     81 
     82 GaiaScreenHandler::GaiaScreenHandler(
     83     const scoped_refptr<NetworkStateInformer>& network_state_informer)
     84     : BaseScreenHandler(kJsScreenPath),
     85       frame_state_(FRAME_STATE_UNKNOWN),
     86       frame_error_(net::OK),
     87       network_state_informer_(network_state_informer),
     88       signin_screen_handler_(NULL) {
     89   DCHECK(network_state_informer_.get());
     90 }
     91 
     92 GaiaScreenHandler::~GaiaScreenHandler() {}
     93 
     94 void GaiaScreenHandler::LoadGaia(const GaiaContext& context) {
     95   LOG(WARNING) << "LoadGaia() call.";
     96 
     97   DictionaryValue params;
     98 
     99   params.SetBoolean("forceReload", context.force_reload);
    100   params.SetBoolean("isLocal", context.is_local);
    101   params.SetBoolean("passwordChanged", context.password_changed);
    102   params.SetBoolean("isShowUsers", context.show_users);
    103   params.SetBoolean("useOffline", context.use_offline);
    104   params.SetString("email", context.email);
    105 
    106   UpdateAuthParams(&params, context.has_users);
    107 
    108   if (!context.use_offline) {
    109     const std::string app_locale = g_browser_process->GetApplicationLocale();
    110     if (!app_locale.empty())
    111       params.SetString("hl", app_locale);
    112   } else {
    113     base::DictionaryValue* localized_strings = new base::DictionaryValue();
    114     localized_strings->SetString(
    115         "stringEmail", l10n_util::GetStringUTF16(IDS_LOGIN_OFFLINE_EMAIL));
    116     localized_strings->SetString(
    117         "stringPassword",
    118         l10n_util::GetStringUTF16(IDS_LOGIN_OFFLINE_PASSWORD));
    119     localized_strings->SetString(
    120         "stringSignIn", l10n_util::GetStringUTF16(IDS_LOGIN_OFFLINE_SIGNIN));
    121     localized_strings->SetString(
    122         "stringEmptyEmail",
    123         l10n_util::GetStringUTF16(IDS_LOGIN_OFFLINE_EMPTY_EMAIL));
    124     localized_strings->SetString(
    125         "stringEmptyPassword",
    126         l10n_util::GetStringUTF16(IDS_LOGIN_OFFLINE_EMPTY_PASSWORD));
    127     localized_strings->SetString(
    128         "stringError", l10n_util::GetStringUTF16(IDS_LOGIN_OFFLINE_ERROR));
    129     params.Set("localizedStrings", localized_strings);
    130   }
    131 
    132   const GURL gaia_url =
    133       CommandLine::ForCurrentProcess()->HasSwitch(::switches::kGaiaUrl)
    134           ? GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
    135                 ::switches::kGaiaUrl))
    136           : GaiaUrls::GetInstance()->gaia_url();
    137   params.SetString("gaiaUrl", gaia_url.spec());
    138 
    139   frame_state_ = FRAME_STATE_LOADING;
    140   CallJS("loadAuthExtension", params);
    141 }
    142 
    143 void GaiaScreenHandler::UpdateGaia(const GaiaContext& context) {
    144   DictionaryValue params;
    145   UpdateAuthParams(&params, context.has_users);
    146   CallJS("updateAuthExtension", params);
    147 }
    148 
    149 void GaiaScreenHandler::ReloadGaia() {
    150   if (frame_state_ == FRAME_STATE_LOADING)
    151     return;
    152   NetworkStateInformer::State state = network_state_informer_->state();
    153   if (state != NetworkStateInformer::ONLINE) {
    154     LOG(WARNING) << "Skipping reloading of Gaia since "
    155                  << "network state="
    156                  << NetworkStateInformer::StatusString(state);
    157     return;
    158   }
    159   LOG(WARNING) << "Reloading Gaia.";
    160   frame_state_ = FRAME_STATE_LOADING;
    161   CallJS("doReload");
    162 }
    163 
    164 void GaiaScreenHandler::DeclareLocalizedValues(
    165     LocalizedValuesBuilder* builder) {
    166   builder->Add("signinScreenTitle", IDS_SIGNIN_SCREEN_TITLE);
    167   builder->Add("signinScreenPasswordChanged",
    168                IDS_SIGNIN_SCREEN_PASSWORD_CHANGED);
    169   builder->Add("createAccount", IDS_CREATE_ACCOUNT_HTML);
    170   builder->Add("guestSignin", IDS_BROWSE_WITHOUT_SIGNING_IN_HTML);
    171   builder->Add("createLocallyManagedUser",
    172                IDS_CREATE_LOCALLY_MANAGED_USER_HTML);
    173   builder->Add("createManagedUserFeatureName",
    174                IDS_CREATE_LOCALLY_MANAGED_USER_FEATURE_NAME);
    175 
    176   // Strings used by no password warning dialog.
    177   builder->Add("noPasswordWarningTitle", IDS_LOGIN_NO_PASSWORD_WARNING_TITLE);
    178   builder->Add("noPasswordWarningBody", IDS_LOGIN_NO_PASSWORD_WARNING);
    179   builder->Add("noPasswordWarningOkButton",
    180                IDS_LOGIN_NO_PASSWORD_WARNING_DISMISS_BUTTON);
    181 }
    182 
    183 void GaiaScreenHandler::Initialize() {}
    184 
    185 void GaiaScreenHandler::RegisterMessages() {
    186   AddCallback("frameLoadingCompleted",
    187               &GaiaScreenHandler::HandleFrameLoadingCompleted);
    188 }
    189 
    190 void GaiaScreenHandler::HandleFrameLoadingCompleted(int status) {
    191   const net::Error frame_error = static_cast<net::Error>(-status);
    192   if (frame_error == net::ERR_ABORTED) {
    193     LOG(WARNING) << "Ignoring Gaia frame error: " << frame_error;
    194     return;
    195   }
    196   frame_error_ = frame_error;
    197   if (frame_error == net::OK) {
    198     LOG(INFO) << "Gaia is loaded";
    199     frame_state_ = FRAME_STATE_LOADED;
    200   } else {
    201     LOG(WARNING) << "Gaia frame error: " << frame_error_;
    202     frame_state_ = FRAME_STATE_ERROR;
    203   }
    204 
    205   if (network_state_informer_->state() != NetworkStateInformer::ONLINE)
    206     return;
    207   if (frame_state_ == FRAME_STATE_LOADED)
    208     UpdateState(ErrorScreenActor::ERROR_REASON_UPDATE);
    209   else if (frame_state_ == FRAME_STATE_ERROR)
    210     UpdateState(ErrorScreenActor::ERROR_REASON_FRAME_ERROR);
    211 }
    212 
    213 void GaiaScreenHandler::UpdateState(ErrorScreenActor::ErrorReason reason) {
    214   if (signin_screen_handler_)
    215     signin_screen_handler_->UpdateState(reason);
    216 }
    217 
    218 void GaiaScreenHandler::SetSigninScreenHandler(SigninScreenHandler* handler) {
    219   signin_screen_handler_ = handler;
    220 }
    221 
    222 }  // namespace chromeos
    223