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.
5 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
8 #include "ash/system/tray/system_tray.h"
10 #include "base/callback.h"
11 #include "base/debug/trace_event.h"
12 #include "base/i18n/rtl.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/chromeos/accessibility/accessibility_util.h"
17 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
18 #include "chrome/browser/chromeos/login/ui/proxy_settings_dialog.h"
19 #include "chrome/browser/chromeos/login/ui/webui_login_display.h"
20 #include "chrome/browser/chromeos/profiles/profile_helper.h"
21 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
22 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
23 #include "chrome/browser/media/media_stream_infobar_delegate.h"
24 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
25 #include "chrome/browser/renderer_preferences_util.h"
26 #include "chrome/browser/sessions/session_tab_helper.h"
27 #include "chrome/browser/ui/autofill/chrome_autofill_client.h"
28 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
29 #include "chrome/browser/ui/zoom/zoom_controller.h"
30 #include "chrome/common/render_messages.h"
31 #include "chromeos/dbus/dbus_thread_manager.h"
32 #include "chromeos/dbus/session_manager_client.h"
33 #include "chromeos/network/network_state.h"
34 #include "chromeos/network/network_state_handler.h"
35 #include "components/password_manager/core/browser/password_manager.h"
36 #include "components/web_modal/web_contents_modal_dialog_manager.h"
37 #include "content/public/browser/notification_service.h"
38 #include "content/public/browser/render_frame_host.h"
39 #include "content/public/browser/render_view_host.h"
40 #include "content/public/browser/render_widget_host_view.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/browser/web_ui.h"
43 #include "content/public/common/renderer_preferences.h"
44 #include "third_party/WebKit/public/web/WebInputEvent.h"
45 #include "ui/gfx/rect.h"
46 #include "ui/gfx/size.h"
47 #include "ui/views/controls/webview/webview.h"
48 #include "ui/views/widget/widget.h"
50 using content::NativeWebKeyboardEvent
;
51 using content::RenderViewHost
;
52 using content::WebContents
;
53 using web_modal::WebContentsModalDialogManager
;
57 // These strings must be kept in sync with handleAccelerator()
58 // in display_manager.js.
59 const char kAccelNameCancel
[] = "cancel";
60 const char kAccelNameEnrollment
[] = "enrollment";
61 const char kAccelNameKioskEnable
[] = "kiosk_enable";
62 const char kAccelNameVersion
[] = "version";
63 const char kAccelNameReset
[] = "reset";
64 const char kAccelFocusPrev
[] = "focus_prev";
65 const char kAccelFocusNext
[] = "focus_next";
66 const char kAccelNameDeviceRequisition
[] = "device_requisition";
67 const char kAccelNameDeviceRequisitionRemora
[] = "device_requisition_remora";
68 const char kAccelNameDeviceRequisitionShark
[] = "device_requisition_shark";
69 const char kAccelNameAppLaunchBailout
[] = "app_launch_bailout";
70 const char kAccelNameAppLaunchNetworkConfig
[] = "app_launch_network_config";
71 const char kAccelNameEmbeddedSignin
[] = "embedded_signin";
73 // A class to change arrow key traversal behavior when it's alive.
74 class ScopedArrowKeyTraversal
{
76 explicit ScopedArrowKeyTraversal(bool new_arrow_key_tranversal_enabled
)
77 : previous_arrow_key_traversal_enabled_(
78 views::FocusManager::arrow_key_traversal_enabled()) {
79 views::FocusManager::set_arrow_key_traversal_enabled(
80 new_arrow_key_tranversal_enabled
);
82 ~ScopedArrowKeyTraversal() {
83 views::FocusManager::set_arrow_key_traversal_enabled(
84 previous_arrow_key_traversal_enabled_
);
88 const bool previous_arrow_key_traversal_enabled_
;
89 DISALLOW_COPY_AND_ASSIGN(ScopedArrowKeyTraversal
);
97 const char WebUILoginView::kViewClassName
[] =
98 "browser/chromeos/login/WebUILoginView";
100 // WebUILoginView public: ------------------------------------------------------
102 WebUILoginView::WebUILoginView()
103 : webui_login_(NULL
),
105 webui_visible_(false),
106 should_emit_login_prompt_visible_(true),
107 forward_keyboard_event_(true) {
109 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
110 content::NotificationService::AllSources());
112 chrome::NOTIFICATION_LOGIN_NETWORK_ERROR_SHOWN
,
113 content::NotificationService::AllSources());
115 accel_map_
[ui::Accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
)] =
117 accel_map_
[ui::Accelerator(ui::VKEY_E
,
118 ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
)] =
119 kAccelNameEnrollment
;
120 accel_map_
[ui::Accelerator(
121 ui::VKEY_G
, ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
| ui::EF_SHIFT_DOWN
)] =
122 kAccelNameEmbeddedSignin
;
123 accel_map_
[ui::Accelerator(ui::VKEY_K
,
124 ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
)] =
125 kAccelNameKioskEnable
;
126 accel_map_
[ui::Accelerator(ui::VKEY_V
, ui::EF_ALT_DOWN
)] =
128 accel_map_
[ui::Accelerator(ui::VKEY_R
,
129 ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
| ui::EF_SHIFT_DOWN
)] =
132 accel_map_
[ui::Accelerator(ui::VKEY_LEFT
, ui::EF_NONE
)] =
134 accel_map_
[ui::Accelerator(ui::VKEY_RIGHT
, ui::EF_NONE
)] =
137 // Use KEY_RELEASED because Gaia consumes KEY_PRESSED for up/down key.
138 ui::Accelerator
key_up(ui::VKEY_UP
, ui::EF_NONE
);
139 key_up
.set_type(ui::ET_KEY_RELEASED
);
140 ui::Accelerator
key_down(ui::VKEY_DOWN
, ui::EF_NONE
);
141 key_down
.set_type(ui::ET_KEY_RELEASED
);
142 accel_map_
[key_up
] = kAccelFocusPrev
;
143 accel_map_
[key_down
] = kAccelFocusNext
;
145 accel_map_
[ui::Accelerator(
146 ui::VKEY_D
, ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
| ui::EF_SHIFT_DOWN
)] =
147 kAccelNameDeviceRequisition
;
149 ui::Accelerator(ui::VKEY_H
, ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
)] =
150 kAccelNameDeviceRequisitionRemora
;
152 ui::Accelerator(ui::VKEY_H
,
153 ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
| ui::EF_SHIFT_DOWN
)] =
154 kAccelNameDeviceRequisitionShark
;
156 accel_map_
[ui::Accelerator(ui::VKEY_S
,
157 ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
)] =
158 kAccelNameAppLaunchBailout
;
160 accel_map_
[ui::Accelerator(ui::VKEY_N
,
161 ui::EF_CONTROL_DOWN
| ui::EF_ALT_DOWN
)] =
162 kAccelNameAppLaunchNetworkConfig
;
164 for (AccelMap::iterator
i(accel_map_
.begin()); i
!= accel_map_
.end(); ++i
)
165 AddAccelerator(i
->first
);
168 WebUILoginView::~WebUILoginView() {
169 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver
,
173 #if !defined(USE_ATHENA)
174 if (ash::Shell::GetInstance()->HasPrimaryStatusArea()) {
175 ash::Shell::GetInstance()->GetPrimarySystemTray()->
176 SetNextFocusableView(NULL
);
181 void WebUILoginView::Init() {
182 Profile
* signin_profile
= ProfileHelper::GetSigninProfile();
183 webui_login_
= new views::WebView(signin_profile
);
184 webui_login_
->set_allow_accelerators(true);
185 AddChildView(webui_login_
);
187 WebContents
* web_contents
= webui_login_
->GetWebContents();
189 // Ensure that the login UI has a tab ID, which will allow the GAIA auth
190 // extension's background script to tell it apart from a captive portal window
191 // that may be opened on top of this UI.
192 SessionTabHelper::CreateForWebContents(web_contents
);
194 // Create the password manager that is needed for the proxy.
195 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
197 autofill::ChromeAutofillClient::FromWebContents(web_contents
));
199 // LoginHandlerViews uses a constrained window for the password manager view.
200 WebContentsModalDialogManager::CreateForWebContents(web_contents
);
201 WebContentsModalDialogManager::FromWebContents(web_contents
)->
203 if (!popup_manager_
.get())
204 popup_manager_
.reset(new web_modal::PopupManager(this));
205 popup_manager_
->RegisterWith(web_contents
);
207 web_contents
->SetDelegate(this);
208 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
210 WebContentsObserver::Observe(web_contents
);
211 content::RendererPreferences
* prefs
= web_contents
->GetMutableRendererPrefs();
212 renderer_preferences_util::UpdateFromSystemSettings(
213 prefs
, signin_profile
, web_contents
);
216 const char* WebUILoginView::GetClassName() const {
217 return kViewClassName
;
220 void WebUILoginView::RequestFocus() {
221 webui_login_
->RequestFocus();
224 web_modal::WebContentsModalDialogHost
*
225 WebUILoginView::GetWebContentsModalDialogHost() {
229 gfx::NativeView
WebUILoginView::GetHostView() const {
230 return GetWidget()->GetNativeView();
233 gfx::Point
WebUILoginView::GetDialogPosition(const gfx::Size
& size
) {
234 // Center the widget.
235 gfx::Size widget_size
= GetWidget()->GetWindowBoundsInScreen().size();
236 return gfx::Point(widget_size
.width() / 2 - size
.width() / 2,
237 widget_size
.height() / 2 - size
.height() / 2);
240 gfx::Size
WebUILoginView::GetMaximumDialogSize() {
241 return GetWidget()->GetWindowBoundsInScreen().size();
244 void WebUILoginView::AddObserver(
245 web_modal::ModalDialogHostObserver
* observer
) {
246 if (observer
&& !observer_list_
.HasObserver(observer
))
247 observer_list_
.AddObserver(observer
);
250 void WebUILoginView::RemoveObserver(
251 web_modal::ModalDialogHostObserver
* observer
) {
252 observer_list_
.RemoveObserver(observer
);
255 bool WebUILoginView::AcceleratorPressed(
256 const ui::Accelerator
& accelerator
) {
257 AccelMap::const_iterator entry
= accel_map_
.find(accelerator
);
258 if (entry
== accel_map_
.end())
264 content::WebUI
* web_ui
= GetWebUI();
266 base::StringValue
accel_name(entry
->second
);
267 web_ui
->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
274 gfx::NativeWindow
WebUILoginView::GetNativeWindow() const {
275 return GetWidget()->GetNativeWindow();
278 void WebUILoginView::LoadURL(const GURL
& url
) {
279 webui_login_
->LoadInitialURL(url
);
280 webui_login_
->RequestFocus();
282 // TODO(nkostylev): Use WebContentsObserver::RenderViewCreated to track
283 // when RenderView is created.
284 GetWebContents()->GetRenderViewHost()->GetView()->SetBackgroundColor(
285 SK_ColorTRANSPARENT
);
288 content::WebUI
* WebUILoginView::GetWebUI() {
289 return webui_login_
->web_contents()->GetWebUI();
292 content::WebContents
* WebUILoginView::GetWebContents() {
293 return webui_login_
->web_contents();
296 void WebUILoginView::OpenProxySettings() {
297 const NetworkState
* network
=
298 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
300 LOG(ERROR
) << "No default network found!";
303 ProxySettingsDialog
* dialog
=
304 new ProxySettingsDialog(ProfileHelper::GetSigninProfile(),
305 *network
, NULL
, GetNativeWindow());
309 void WebUILoginView::OnPostponedShow() {
310 set_is_hidden(false);
311 OnLoginPromptVisible();
314 void WebUILoginView::SetStatusAreaVisible(bool visible
) {
315 #if !defined(USE_ATHENA)
316 if (ash::Shell::GetInstance()->HasPrimaryStatusArea()) {
317 ash::SystemTray
* tray
= ash::Shell::GetInstance()->GetPrimarySystemTray();
319 // Tray may have been initialized being hidden.
320 tray
->SetVisible(visible
);
321 tray
->GetWidget()->Show();
323 tray
->GetWidget()->Hide();
329 void WebUILoginView::SetUIEnabled(bool enabled
) {
330 forward_keyboard_event_
= enabled
;
331 #if !defined(USE_ATHENA)
332 ash::Shell::GetInstance()->GetPrimarySystemTray()->SetEnabled(enabled
);
336 void WebUILoginView::AddFrameObserver(FrameObserver
* frame_observer
) {
337 DCHECK(frame_observer
);
338 DCHECK(!frame_observer_list_
.HasObserver(frame_observer
));
339 frame_observer_list_
.AddObserver(frame_observer
);
342 void WebUILoginView::RemoveFrameObserver(FrameObserver
* frame_observer
) {
343 DCHECK(frame_observer
);
344 DCHECK(frame_observer_list_
.HasObserver(frame_observer
));
345 frame_observer_list_
.RemoveObserver(frame_observer
);
348 // WebUILoginView protected: ---------------------------------------------------
350 void WebUILoginView::Layout() {
351 DCHECK(webui_login_
);
352 webui_login_
->SetBoundsRect(bounds());
354 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver
,
356 OnPositionRequiresUpdate());
359 void WebUILoginView::OnLocaleChanged() {
362 void WebUILoginView::ChildPreferredSizeChanged(View
* child
) {
367 void WebUILoginView::AboutToRequestFocusFromTabTraversal(bool reverse
) {
368 // Return the focus to the web contents.
369 webui_login_
->web_contents()->FocusThroughTabTraversal(reverse
);
370 GetWidget()->Activate();
371 webui_login_
->web_contents()->Focus();
374 void WebUILoginView::Observe(int type
,
375 const content::NotificationSource
& source
,
376 const content::NotificationDetails
& details
) {
378 case chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
:
379 case chrome::NOTIFICATION_LOGIN_NETWORK_ERROR_SHOWN
: {
380 OnLoginPromptVisible();
381 registrar_
.RemoveAll();
385 NOTREACHED() << "Unexpected notification " << type
;
389 // WebUILoginView private: -----------------------------------------------------
391 bool WebUILoginView::HandleContextMenu(
392 const content::ContextMenuParams
& params
) {
393 // Do not show the context menu.
401 void WebUILoginView::HandleKeyboardEvent(content::WebContents
* source
,
402 const NativeWebKeyboardEvent
& event
) {
403 if (forward_keyboard_event_
) {
404 // Disable arrow key traversal because arrow keys are handled via
405 // accelerator when this view has focus.
406 ScopedArrowKeyTraversal
arrow_key_traversal(false);
408 unhandled_keyboard_event_handler_
.HandleKeyboardEvent(event
,
412 // Make sure error bubble is cleared on keyboard event. This is needed
413 // when the focus is inside an iframe. Only clear on KeyDown to prevent hiding
414 // an immediate authentication error (See crbug.com/103643).
415 if (event
.type
== blink::WebInputEvent::KeyDown
) {
416 content::WebUI
* web_ui
= GetWebUI();
418 web_ui
->CallJavascriptFunction("cr.ui.Oobe.clearErrors");
422 bool WebUILoginView::IsPopupOrPanel(const WebContents
* source
) const {
426 bool WebUILoginView::TakeFocus(content::WebContents
* source
, bool reverse
) {
427 // In case of blocked UI (ex.: sign in is in progress)
428 // we should not process focus change events.
429 if (!forward_keyboard_event_
)
432 #if !defined(USE_ATHENA)
433 ash::SystemTray
* tray
= ash::Shell::GetInstance()->GetPrimarySystemTray();
434 if (tray
&& tray
->GetWidget()->IsVisible()) {
435 tray
->SetNextFocusableView(this);
436 ash::Shell::GetInstance()->RotateFocus(reverse
? ash::Shell::BACKWARD
:
437 ash::Shell::FORWARD
);
444 void WebUILoginView::RequestMediaAccessPermission(
445 WebContents
* web_contents
,
446 const content::MediaStreamRequest
& request
,
447 const content::MediaResponseCallback
& callback
) {
448 if (MediaStreamInfoBarDelegate::Create(web_contents
, request
, callback
))
449 NOTREACHED() << "Media stream not allowed for WebUI";
452 bool WebUILoginView::CheckMediaAccessPermission(
453 content::WebContents
* web_contents
,
454 const GURL
& security_origin
,
455 content::MediaStreamType type
) {
456 return MediaCaptureDevicesDispatcher::GetInstance()
457 ->CheckMediaAccessPermission(web_contents
, security_origin
, type
);
460 bool WebUILoginView::PreHandleGestureEvent(
461 content::WebContents
* source
,
462 const blink::WebGestureEvent
& event
) {
463 // Disable pinch zooming.
464 return event
.type
== blink::WebGestureEvent::GesturePinchBegin
||
465 event
.type
== blink::WebGestureEvent::GesturePinchUpdate
||
466 event
.type
== blink::WebGestureEvent::GesturePinchEnd
;
469 void WebUILoginView::DidFailProvisionalLoad(
470 content::RenderFrameHost
* render_frame_host
,
471 const GURL
& validated_url
,
473 const base::string16
& error_description
) {
474 FOR_EACH_OBSERVER(FrameObserver
,
475 frame_observer_list_
,
476 OnFrameError(render_frame_host
->GetFrameName()));
477 if (render_frame_host
->GetFrameName() != "gaia-frame")
480 GetWebUI()->CallJavascriptFunction("login.GaiaSigninScreen.onFrameError",
481 base::FundamentalValue(-error_code
),
482 base::StringValue(validated_url
.spec()));
485 void WebUILoginView::OnLoginPromptVisible() {
486 // If we're hidden than will generate this signal once we're shown.
487 if (is_hidden_
|| webui_visible_
) {
488 VLOG(1) << "Login WebUI >> not emitting signal, hidden: " << is_hidden_
;
491 TRACE_EVENT0("chromeos", "WebUILoginView::OnLoginPromoptVisible");
492 if (should_emit_login_prompt_visible_
) {
493 VLOG(1) << "Login WebUI >> login-prompt-visible";
494 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
495 EmitLoginPromptVisible();
498 webui_visible_
= true;
501 void WebUILoginView::ReturnFocus(bool reverse
) {
502 // Return the focus to the web contents.
503 webui_login_
->web_contents()->FocusThroughTabTraversal(reverse
);
504 GetWidget()->Activate();
507 } // namespace chromeos