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