Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / webui_screen_locker.cc
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/chromeos/login/webui_screen_locker.h"
6
7 #include "ash/shell.h"
8 #include "ash/wm/lock_state_controller.h"
9 #include "ash/wm/lock_state_observer.h"
10 #include "base/command_line.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_shutdown.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/chromeos/accessibility/accessibility_util.h"
17 #include "chrome/browser/chromeos/login/helper.h"
18 #include "chrome/browser/chromeos/login/screen_locker.h"
19 #include "chrome/browser/chromeos/login/user_manager.h"
20 #include "chrome/browser/chromeos/login/webui_login_display.h"
21 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
22 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
23 #include "chrome/common/url_constants.h"
24 #include "chromeos/dbus/dbus_thread_manager.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/notification_types.h"
28 #include "content/public/browser/render_widget_host_view.h"
29 #include "content/public/browser/web_ui.h"
30 #include "ui/aura/client/capture_client.h"
31 #include "ui/aura/window_event_dispatcher.h"
32 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/base/x/x11_util.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/views/controls/webview/webview.h"
36
37 namespace {
38
39 // URL which corresponds to the login WebUI.
40 const char kLoginURL[] = "chrome://oobe/lock";
41
42 }  // namespace
43
44 namespace chromeos {
45
46 ////////////////////////////////////////////////////////////////////////////////
47 // WebUIScreenLocker implementation.
48
49 WebUIScreenLocker::WebUIScreenLocker(ScreenLocker* screen_locker)
50     : ScreenLockerDelegate(screen_locker),
51       lock_ready_(false),
52       webui_ready_(false),
53       network_state_helper_(new login::NetworkStateHelper),
54       weak_factory_(this) {
55   set_should_emit_login_prompt_visible(false);
56   ash::Shell::GetInstance()->lock_state_controller()->AddObserver(this);
57   DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
58 }
59
60 void WebUIScreenLocker::LockScreen() {
61   gfx::Rect bounds(ash::Shell::GetScreen()->GetPrimaryDisplay().bounds());
62
63   lock_time_ = base::TimeTicks::Now();
64   LockWindow* lock_window = LockWindow::Create();
65   lock_window->set_observer(this);
66   lock_window_ = lock_window->GetWidget();
67   lock_window_->AddObserver(this);
68   WebUILoginView::Init();
69   lock_window_->SetContentsView(this);
70   lock_window_->Show();
71   LoadURL(GURL(kLoginURL));
72   lock_window->Grab();
73
74   login_display_.reset(new WebUILoginDisplay(this));
75   login_display_->set_background_bounds(bounds);
76   login_display_->set_parent_window(GetNativeWindow());
77   login_display_->Init(screen_locker()->users(), false, true, false);
78
79   static_cast<OobeUI*>(GetWebUI()->GetController())->ShowSigninScreen(
80       LoginScreenContext(), login_display_.get(), login_display_.get());
81
82   registrar_.Add(this,
83                  chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED,
84                  content::NotificationService::AllSources());
85 }
86
87 void WebUIScreenLocker::ScreenLockReady() {
88   UMA_HISTOGRAM_TIMES("LockScreen.LockReady",
89                       base::TimeTicks::Now() - lock_time_);
90   ScreenLockerDelegate::ScreenLockReady();
91   SetInputEnabled(true);
92 }
93
94 void WebUIScreenLocker::OnAuthenticate() {
95 }
96
97 void WebUIScreenLocker::SetInputEnabled(bool enabled) {
98   login_display_->SetUIEnabled(enabled);
99 }
100
101 void WebUIScreenLocker::ShowBannerMessage(const std::string& message) {
102   if (!webui_ready_)
103     return;
104   login_display_->ShowBannerMessage(message);
105 }
106
107 void WebUIScreenLocker::ShowUserPodButton(
108     const std::string& username,
109     const std::string& iconURL,
110     const base::Closure& click_callback) {
111   if (!webui_ready_)
112     return;
113   login_display_->ShowUserPodButton(username, iconURL, click_callback);
114 }
115
116 void WebUIScreenLocker::HideUserPodButton(const std::string& username) {
117   if (!webui_ready_)
118     return;
119   login_display_->HideUserPodButton(username);
120 }
121
122 void WebUIScreenLocker::SetAuthType(const std::string& username,
123                                     LoginDisplay::AuthType auth_type,
124                                     const std::string& initial_value) {
125   if (!webui_ready_)
126     return;
127   login_display_->SetAuthType(username, auth_type, initial_value);
128 }
129
130 LoginDisplay::AuthType WebUIScreenLocker::GetAuthType(
131     const std::string& username) const {
132   // Return default auth type if login display is not ready.
133   if (!webui_ready_)
134     return LoginDisplay::OFFLINE_PASSWORD;
135   return login_display_->GetAuthType(username);
136 }
137
138 void WebUIScreenLocker::ShowErrorMessage(
139     int error_msg_id,
140     HelpAppLauncher::HelpTopic help_topic_id) {
141   login_display_->ShowError(error_msg_id,
142                   0 /* login_attempts */,
143                   help_topic_id);
144 }
145
146 void WebUIScreenLocker::AnimateAuthenticationSuccess() {
147   GetWebUI()->CallJavascriptFunction("cr.ui.Oobe.animateAuthenticationSuccess");
148 }
149
150 void WebUIScreenLocker::ClearErrors() {
151   GetWebUI()->CallJavascriptFunction("cr.ui.Oobe.clearErrors");
152 }
153
154 gfx::NativeWindow WebUIScreenLocker::GetNativeWindow() const {
155   return lock_window_->GetNativeWindow();
156 }
157
158 content::WebUI* WebUIScreenLocker::GetAssociatedWebUI() {
159   return GetWebUI();
160 }
161
162 void WebUIScreenLocker::FocusUserPod() {
163   if (!webui_ready_)
164     return;
165   webui_login_->RequestFocus();
166   GetWebUI()->CallJavascriptFunction("cr.ui.Oobe.forceLockedUserPodFocus");
167 }
168
169 WebUIScreenLocker::~WebUIScreenLocker() {
170   DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
171   ash::Shell::GetInstance()->
172       lock_state_controller()->RemoveObserver(this);
173   // In case of shutdown, lock_window_ may be deleted before WebUIScreenLocker.
174   if (lock_window_) {
175     lock_window_->RemoveObserver(this);
176     lock_window_->Close();
177   }
178   // If LockScreen() was called, we need to clear the signin screen handler
179   // delegate set in ShowSigninScreen so that it no longer points to us.
180   if (login_display_.get()) {
181     static_cast<OobeUI*>(GetWebUI()->GetController())->
182         ResetSigninScreenHandlerDelegate();
183   }
184 }
185
186 void WebUIScreenLocker::OnLockWebUIReady() {
187   VLOG(1) << "WebUI ready; lock window is "
188           << (lock_ready_ ? "too" : "not");
189   webui_ready_ = true;
190   if (lock_ready_)
191     ScreenLockReady();
192 }
193
194 void WebUIScreenLocker::OnLockBackgroundDisplayed() {
195   UMA_HISTOGRAM_TIMES("LockScreen.BackgroundReady",
196                       base::TimeTicks::Now() - lock_time_);
197 }
198
199 ////////////////////////////////////////////////////////////////////////////////
200 // WebUIScreenLocker, content::NotificationObserver implementation:
201
202 void WebUIScreenLocker::Observe(
203     int type,
204     const content::NotificationSource& source,
205     const content::NotificationDetails& details) {
206   switch (type) {
207     case chrome::NOTIFICATION_LOGIN_USER_IMAGE_CHANGED: {
208       const User& user = *content::Details<User>(details).ptr();
209       login_display_->OnUserImageChanged(user);
210       break;
211     }
212     default:
213       WebUILoginView::Observe(type, source, details);
214   }
215 }
216
217 ////////////////////////////////////////////////////////////////////////////////
218 // WebUIScreenLocker, LoginDisplay::Delegate implementation:
219
220 void WebUIScreenLocker::CancelPasswordChangedFlow()  {
221   NOTREACHED();
222 }
223
224 void WebUIScreenLocker::CreateAccount() {
225   NOTREACHED();
226 }
227
228 void WebUIScreenLocker::CompleteLogin(const UserContext& user_context) {
229   NOTREACHED();
230 }
231
232 base::string16 WebUIScreenLocker::GetConnectedNetworkName() {
233   return network_state_helper_->GetCurrentNetworkName();
234 }
235
236 bool WebUIScreenLocker::IsSigninInProgress() const {
237   // The way how screen locker is implemented right now there's no
238   // GAIA sign in in progress in any case.
239   return false;
240 }
241
242 void WebUIScreenLocker::Login(const UserContext& user_context) {
243   chromeos::ScreenLocker::default_screen_locker()->Authenticate(user_context);
244 }
245
246 void WebUIScreenLocker::LoginAsRetailModeUser() {
247   NOTREACHED();
248 }
249
250 void WebUIScreenLocker::LoginAsGuest() {
251   NOTREACHED();
252 }
253
254 void WebUIScreenLocker::MigrateUserData(const std::string& old_password) {
255   NOTREACHED();
256 }
257
258 void WebUIScreenLocker::LoginAsPublicAccount(const std::string& username) {
259   NOTREACHED();
260 }
261
262 void WebUIScreenLocker::OnSigninScreenReady() {
263 }
264
265 void WebUIScreenLocker::OnUserSelected(const std::string& username) {
266 }
267
268 void WebUIScreenLocker::OnStartEnterpriseEnrollment() {
269   NOTREACHED();
270 }
271
272 void WebUIScreenLocker::OnStartKioskEnableScreen() {
273   NOTREACHED();
274 }
275
276 void WebUIScreenLocker::OnStartKioskAutolaunchScreen() {
277   NOTREACHED();
278 }
279
280 void WebUIScreenLocker::ShowWrongHWIDScreen() {
281   NOTREACHED();
282 }
283
284 void WebUIScreenLocker::ResetPublicSessionAutoLoginTimer() {
285 }
286
287 void WebUIScreenLocker::ResyncUserData() {
288   NOTREACHED();
289 }
290
291 void WebUIScreenLocker::SetDisplayEmail(const std::string& email) {
292   NOTREACHED();
293 }
294
295 void WebUIScreenLocker::Signout() {
296   chromeos::ScreenLocker::default_screen_locker()->Signout();
297 }
298
299 void WebUIScreenLocker::LoginAsKioskApp(const std::string& app_id,
300                                         bool diagnostic_mode) {
301   NOTREACHED();
302 }
303
304 ////////////////////////////////////////////////////////////////////////////////
305 // LockWindow::Observer implementation:
306
307 void WebUIScreenLocker::OnLockWindowReady() {
308   VLOG(1) << "Lock window ready; WebUI is " << (webui_ready_ ? "too" : "not");
309   lock_ready_ = true;
310   if (webui_ready_)
311     ScreenLockReady();
312 }
313
314 ////////////////////////////////////////////////////////////////////////////////
315 // SessionLockStateObserver override.
316
317 void WebUIScreenLocker::OnLockStateEvent(
318     ash::LockStateObserver::EventType event) {
319   if (event == ash::LockStateObserver::EVENT_LOCK_ANIMATION_FINISHED) {
320     // Release capture if any.
321     aura::client::GetCaptureClient(GetNativeWindow()->GetRootWindow())->
322         SetCapture(NULL);
323     GetWebUI()->CallJavascriptFunction("cr.ui.Oobe.animateOnceFullyDisplayed");
324   }
325 }
326
327 ////////////////////////////////////////////////////////////////////////////////
328 // WidgetObserver override.
329
330 void WebUIScreenLocker::OnWidgetDestroying(views::Widget* widget) {
331   lock_window_->RemoveObserver(this);
332   lock_window_ = NULL;
333 }
334
335 ////////////////////////////////////////////////////////////////////////////////
336 // PowerManagerClient::Observer overrides.
337
338 void WebUIScreenLocker::LidEventReceived(bool open,
339                                          const base::TimeTicks& time) {
340   content::BrowserThread::PostTask(
341       content::BrowserThread::UI,
342       FROM_HERE,
343       base::Bind(&WebUIScreenLocker::FocusUserPod, weak_factory_.GetWeakPtr()));
344 }
345
346 void WebUIScreenLocker::SuspendDone(const base::TimeDelta& sleep_duration) {
347   content::BrowserThread::PostTask(
348       content::BrowserThread::UI,
349       FROM_HERE,
350       base::Bind(&WebUIScreenLocker::FocusUserPod, weak_factory_.GetWeakPtr()));
351 }
352
353 void WebUIScreenLocker::RenderProcessGone(base::TerminationStatus status) {
354   if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID &&
355       status != base::TERMINATION_STATUS_NORMAL_TERMINATION) {
356     LOG(ERROR) << "Renderer crash on lock screen";
357     Signout();
358   }
359 }
360
361 }  // namespace chromeos