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