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.
5 #include "chrome/browser/chromeos/login/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/login_display_host_impl.h"
18 #include "chrome/browser/chromeos/login/proxy_settings_dialog.h"
19 #include "chrome/browser/chromeos/login/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_stream_infobar_delegate.h"
23 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
24 #include "chrome/browser/renderer_preferences_util.h"
25 #include "chrome/browser/sessions/session_tab_helper.h"
26 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
27 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
28 #include "chrome/common/render_messages.h"
29 #include "chromeos/dbus/dbus_thread_manager.h"
30 #include "chromeos/dbus/session_manager_client.h"
31 #include "chromeos/network/network_state.h"
32 #include "chromeos/network/network_state_handler.h"
33 #include "components/password_manager/core/browser/password_manager.h"
34 #include "components/web_modal/web_contents_modal_dialog_manager.h"
35 #include "content/public/browser/notification_service.h"
36 #include "content/public/browser/render_view_host.h"
37 #include "content/public/browser/render_widget_host_view.h"
38 #include "content/public/browser/web_contents.h"
39 #include "content/public/browser/web_ui.h"
40 #include "third_party/WebKit/public/web/WebInputEvent.h"
41 #include "ui/gfx/rect.h"
42 #include "ui/gfx/size.h"
43 #include "ui/views/controls/webview/webview.h"
44 #include "ui/views/widget/widget.h"
46 using content::NativeWebKeyboardEvent;
47 using content::RenderViewHost;
48 using content::WebContents;
49 using web_modal::WebContentsModalDialogManager;
53 // These strings must be kept in sync with handleAccelerator()
54 // in display_manager.js.
55 const char kAccelNameCancel[] = "cancel";
56 const char kAccelNameEnrollment[] = "enrollment";
57 const char kAccelNameKioskEnable[] = "kiosk_enable";
58 const char kAccelNameVersion[] = "version";
59 const char kAccelNameReset[] = "reset";
60 const char kAccelFocusPrev[] = "focus_prev";
61 const char kAccelFocusNext[] = "focus_next";
62 const char kAccelNameDeviceRequisition[] = "device_requisition";
63 const char kAccelNameDeviceRequisitionRemora[] = "device_requisition_remora";
64 const char kAccelNameAppLaunchBailout[] = "app_launch_bailout";
65 const char kAccelNameAppLaunchNetworkConfig[] = "app_launch_network_config";
67 // A class to change arrow key traversal behavior when it's alive.
68 class ScopedArrowKeyTraversal {
70 explicit ScopedArrowKeyTraversal(bool new_arrow_key_tranversal_enabled)
71 : previous_arrow_key_traversal_enabled_(
72 views::FocusManager::arrow_key_traversal_enabled()) {
73 views::FocusManager::set_arrow_key_traversal_enabled(
74 new_arrow_key_tranversal_enabled);
76 ~ScopedArrowKeyTraversal() {
77 views::FocusManager::set_arrow_key_traversal_enabled(
78 previous_arrow_key_traversal_enabled_);
82 const bool previous_arrow_key_traversal_enabled_;
83 DISALLOW_COPY_AND_ASSIGN(ScopedArrowKeyTraversal);
91 const char WebUILoginView::kViewClassName[] =
92 "browser/chromeos/login/WebUILoginView";
94 // WebUILoginView public: ------------------------------------------------------
96 WebUILoginView::WebUILoginView()
99 webui_visible_(false),
100 should_emit_login_prompt_visible_(true),
101 forward_keyboard_event_(true) {
103 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
104 content::NotificationService::AllSources());
106 chrome::NOTIFICATION_LOGIN_NETWORK_ERROR_SHOWN,
107 content::NotificationService::AllSources());
109 accel_map_[ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)] =
111 accel_map_[ui::Accelerator(ui::VKEY_E,
112 ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN)] =
113 kAccelNameEnrollment;
114 accel_map_[ui::Accelerator(ui::VKEY_K,
115 ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN)] =
116 kAccelNameKioskEnable;
117 accel_map_[ui::Accelerator(ui::VKEY_V, ui::EF_ALT_DOWN)] =
119 accel_map_[ui::Accelerator(ui::VKEY_R,
120 ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN)] =
123 accel_map_[ui::Accelerator(ui::VKEY_LEFT, ui::EF_NONE)] =
125 accel_map_[ui::Accelerator(ui::VKEY_RIGHT, ui::EF_NONE)] =
128 // Use KEY_RELEASED because Gaia consumes KEY_PRESSED for up/down key.
129 ui::Accelerator key_up(ui::VKEY_UP, ui::EF_NONE);
130 key_up.set_type(ui::ET_KEY_RELEASED);
131 ui::Accelerator key_down(ui::VKEY_DOWN, ui::EF_NONE);
132 key_down.set_type(ui::ET_KEY_RELEASED);
133 accel_map_[key_up] = kAccelFocusPrev;
134 accel_map_[key_down] = kAccelFocusNext;
136 accel_map_[ui::Accelerator(
137 ui::VKEY_D, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN | ui::EF_SHIFT_DOWN)] =
138 kAccelNameDeviceRequisition;
140 ui::Accelerator(ui::VKEY_H, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN)] =
141 kAccelNameDeviceRequisitionRemora;
143 accel_map_[ui::Accelerator(ui::VKEY_S,
144 ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN)] =
145 kAccelNameAppLaunchBailout;
147 accel_map_[ui::Accelerator(ui::VKEY_N,
148 ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN)] =
149 kAccelNameAppLaunchNetworkConfig;
151 for (AccelMap::iterator i(accel_map_.begin()); i != accel_map_.end(); ++i)
152 AddAccelerator(i->first);
155 WebUILoginView::~WebUILoginView() {
156 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver,
160 if (ash::Shell::GetInstance()->HasPrimaryStatusArea()) {
161 ash::Shell::GetInstance()->GetPrimarySystemTray()->
162 SetNextFocusableView(NULL);
166 void WebUILoginView::Init() {
167 Profile* signin_profile = ProfileHelper::GetSigninProfile();
168 auth_extension_.reset(new ScopedGaiaAuthExtension(signin_profile));
169 webui_login_ = new views::WebView(signin_profile);
170 webui_login_->set_allow_accelerators(true);
171 AddChildView(webui_login_);
173 WebContents* web_contents = webui_login_->GetWebContents();
175 // Ensure that the login UI has a tab ID, which will allow the GAIA auth
176 // extension's background script to tell it apart from a captive portal window
177 // that may be opened on top of this UI.
178 SessionTabHelper::CreateForWebContents(web_contents);
180 // Create the password manager that is needed for the proxy.
181 ChromePasswordManagerClient::CreateForWebContentsWithAutofillManagerDelegate(
183 autofill::TabAutofillManagerDelegate::FromWebContents(web_contents));
185 // LoginHandlerViews uses a constrained window for the password manager view.
186 WebContentsModalDialogManager::CreateForWebContents(web_contents);
187 WebContentsModalDialogManager::FromWebContents(web_contents)->
190 web_contents->SetDelegate(this);
191 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
193 WebContentsObserver::Observe(web_contents);
194 renderer_preferences_util::UpdateFromSystemSettings(
195 web_contents->GetMutableRendererPrefs(),
199 const char* WebUILoginView::GetClassName() const {
200 return kViewClassName;
203 web_modal::WebContentsModalDialogHost*
204 WebUILoginView::GetWebContentsModalDialogHost() {
208 gfx::NativeView WebUILoginView::GetHostView() const {
209 return GetWidget()->GetNativeView();
212 gfx::Point WebUILoginView::GetDialogPosition(const gfx::Size& size) {
213 // Center the widget.
214 gfx::Size widget_size = GetWidget()->GetWindowBoundsInScreen().size();
215 return gfx::Point(widget_size.width() / 2 - size.width() / 2,
216 widget_size.height() / 2 - size.height() / 2);
219 gfx::Size WebUILoginView::GetMaximumDialogSize() {
220 return GetWidget()->GetWindowBoundsInScreen().size();
223 void WebUILoginView::AddObserver(
224 web_modal::ModalDialogHostObserver* observer) {
225 if (observer && !observer_list_.HasObserver(observer))
226 observer_list_.AddObserver(observer);
229 void WebUILoginView::RemoveObserver(
230 web_modal::ModalDialogHostObserver* observer) {
231 observer_list_.RemoveObserver(observer);
234 bool WebUILoginView::AcceleratorPressed(
235 const ui::Accelerator& accelerator) {
236 AccelMap::const_iterator entry = accel_map_.find(accelerator);
237 if (entry == accel_map_.end())
243 content::WebUI* web_ui = GetWebUI();
245 base::StringValue accel_name(entry->second);
246 web_ui->CallJavascriptFunction("cr.ui.Oobe.handleAccelerator",
253 gfx::NativeWindow WebUILoginView::GetNativeWindow() const {
254 return GetWidget()->GetNativeWindow();
257 void WebUILoginView::LoadURL(const GURL & url) {
258 webui_login_->LoadInitialURL(url);
259 webui_login_->RequestFocus();
261 // TODO(nkostylev): Use WebContentsObserver::RenderViewCreated to track
262 // when RenderView is created.
263 // Use a background with transparency to trigger transparency in Webkit.
265 background.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
266 background.allocPixels();
267 background.eraseARGB(0x00, 0x00, 0x00, 0x00);
268 content::RenderViewHost* host = GetWebContents()->GetRenderViewHost();
269 host->GetView()->SetBackground(background);
272 content::WebUI* WebUILoginView::GetWebUI() {
273 return webui_login_->web_contents()->GetWebUI();
276 content::WebContents* WebUILoginView::GetWebContents() {
277 return webui_login_->web_contents();
280 void WebUILoginView::OpenProxySettings() {
281 const NetworkState* network =
282 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
284 LOG(ERROR) << "No default network found!";
287 ProxySettingsDialog* dialog =
288 new ProxySettingsDialog(ProfileHelper::GetSigninProfile(),
289 *network, NULL, GetNativeWindow());
293 void WebUILoginView::OnPostponedShow() {
294 set_is_hidden(false);
295 OnLoginPromptVisible();
298 void WebUILoginView::SetStatusAreaVisible(bool visible) {
299 if (ash::Shell::GetInstance()->HasPrimaryStatusArea()) {
300 ash::SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray();
302 // Tray may have been initialized being hidden.
303 tray->SetVisible(visible);
304 tray->GetWidget()->Show();
306 tray->GetWidget()->Hide();
311 void WebUILoginView::SetUIEnabled(bool enabled) {
312 forward_keyboard_event_ = enabled;
313 ash::Shell::GetInstance()->GetPrimarySystemTray()->SetEnabled(enabled);
316 // WebUILoginView protected: ---------------------------------------------------
318 void WebUILoginView::Layout() {
319 DCHECK(webui_login_);
320 webui_login_->SetBoundsRect(bounds());
322 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver,
324 OnPositionRequiresUpdate());
327 void WebUILoginView::OnLocaleChanged() {
330 void WebUILoginView::ChildPreferredSizeChanged(View* child) {
335 void WebUILoginView::AboutToRequestFocusFromTabTraversal(bool reverse) {
336 // Return the focus to the web contents.
337 webui_login_->web_contents()->FocusThroughTabTraversal(reverse);
338 GetWidget()->Activate();
339 webui_login_->web_contents()->Focus();
342 void WebUILoginView::Observe(int type,
343 const content::NotificationSource& source,
344 const content::NotificationDetails& details) {
346 case chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE:
347 case chrome::NOTIFICATION_LOGIN_NETWORK_ERROR_SHOWN: {
348 OnLoginPromptVisible();
349 registrar_.RemoveAll();
353 NOTREACHED() << "Unexpected notification " << type;
357 // WebUILoginView private: -----------------------------------------------------
359 bool WebUILoginView::HandleContextMenu(
360 const content::ContextMenuParams& params) {
361 // Do not show the context menu.
369 void WebUILoginView::HandleKeyboardEvent(content::WebContents* source,
370 const NativeWebKeyboardEvent& event) {
371 if (forward_keyboard_event_) {
372 // Disable arrow key traversal because arrow keys are handled via
373 // accelerator when this view has focus.
374 ScopedArrowKeyTraversal arrow_key_traversal(false);
376 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event,
380 // Make sure error bubble is cleared on keyboard event. This is needed
381 // when the focus is inside an iframe. Only clear on KeyDown to prevent hiding
382 // an immediate authentication error (See crbug.com/103643).
383 if (event.type == blink::WebInputEvent::KeyDown) {
384 content::WebUI* web_ui = GetWebUI();
386 web_ui->CallJavascriptFunction("cr.ui.Oobe.clearErrors");
390 bool WebUILoginView::IsPopupOrPanel(const WebContents* source) const {
394 bool WebUILoginView::TakeFocus(content::WebContents* source, bool reverse) {
395 // In case of blocked UI (ex.: sign in is in progress)
396 // we should not process focus change events.
397 if (!forward_keyboard_event_)
400 ash::SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray();
401 if (tray && tray->GetWidget()->IsVisible()) {
402 tray->SetNextFocusableView(this);
403 ash::Shell::GetInstance()->RotateFocus(reverse ? ash::Shell::BACKWARD :
404 ash::Shell::FORWARD);
410 void WebUILoginView::RequestMediaAccessPermission(
411 WebContents* web_contents,
412 const content::MediaStreamRequest& request,
413 const content::MediaResponseCallback& callback) {
414 if (MediaStreamInfoBarDelegate::Create(web_contents, request, callback))
415 NOTREACHED() << "Media stream not allowed for WebUI";
418 bool WebUILoginView::PreHandleGestureEvent(
419 content::WebContents* source,
420 const blink::WebGestureEvent& event) {
421 // Disable pinch zooming.
422 return event.type == blink::WebGestureEvent::GesturePinchBegin ||
423 event.type == blink::WebGestureEvent::GesturePinchUpdate ||
424 event.type == blink::WebGestureEvent::GesturePinchEnd;
427 void WebUILoginView::DidFailProvisionalLoad(
429 const base::string16& frame_unique_name,
431 const GURL& validated_url,
433 const base::string16& error_description,
434 content::RenderViewHost* render_view_host) {
435 if (frame_unique_name != base::UTF8ToUTF16("gaia-frame"))
438 GetWebUI()->CallJavascriptFunction("login.GaiaSigninScreen.onFrameError",
439 base::FundamentalValue(-error_code),
440 base::StringValue(validated_url.spec()));
443 void WebUILoginView::OnLoginPromptVisible() {
444 // If we're hidden than will generate this signal once we're shown.
445 if (is_hidden_ || webui_visible_) {
446 LOG(WARNING) << "Login WebUI >> not emitting signal, hidden: "
450 TRACE_EVENT0("chromeos", "WebUILoginView::OnLoginPromoptVisible");
451 if (should_emit_login_prompt_visible_) {
452 LOG(WARNING) << "Login WebUI >> login-prompt-visible";
453 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
454 EmitLoginPromptVisible();
457 webui_visible_ = true;
460 void WebUILoginView::ReturnFocus(bool reverse) {
461 // Return the focus to the web contents.
462 webui_login_->web_contents()->FocusThroughTabTraversal(reverse);
463 GetWidget()->Activate();
466 } // namespace chromeos