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