| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/constrained_web_dialog_delegate_base.h" | 5 #include "chrome/browser/ui/webui/constrained_web_dialog_delegate_base.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/ui/browser_finder.h" |
| 9 #include "chrome/browser/ui/browser_window.h" |
| 8 #include "chrome/browser/ui/views/constrained_window_views.h" | 10 #include "chrome/browser/ui/views/constrained_window_views.h" |
| 11 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" |
| 9 #include "content/public/browser/native_web_keyboard_event.h" | 12 #include "content/public/browser/native_web_keyboard_event.h" |
| 10 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 11 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" | 14 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" |
| 12 #include "ui/views/controls/webview/webview.h" | 15 #include "ui/views/controls/webview/webview.h" |
| 13 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
| 14 #include "ui/views/window/dialog_delegate.h" | 17 #include "ui/views/window/dialog_delegate.h" |
| 15 #include "ui/web_dialogs/web_dialog_delegate.h" | 18 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 16 #include "ui/web_dialogs/web_dialog_ui.h" | 19 #include "ui/web_dialogs/web_dialog_ui.h" |
| 17 | 20 |
| 18 #if defined(USE_AURA) | 21 namespace { |
| 19 #include "content/public/browser/render_widget_host_view.h" | 22 |
| 20 #include "ui/aura/client/focus_change_observer.h" | 23 class WebDialogWebContentsDelegateViews |
| 21 #include "ui/aura/client/focus_client.h" | 24 : public ui::WebDialogWebContentsDelegate { |
| 25 public: |
| 26 WebDialogWebContentsDelegateViews(content::BrowserContext* browser_context, |
| 27 content::WebContents* initiator, |
| 28 views::WebView* web_view) |
| 29 : ui::WebDialogWebContentsDelegate(browser_context, |
| 30 new ChromeWebContentsHandler()), |
| 31 initiator_(initiator), |
| 32 web_view_(web_view) { |
| 33 } |
| 34 virtual ~WebDialogWebContentsDelegateViews() {} |
| 35 |
| 36 // ui::WebDialogWebContentsDelegate: |
| 37 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE { |
| 38 // Ensure the WebView is focused when its WebContents is focused. |
| 39 web_view_->RequestFocus(); |
| 40 } |
| 41 virtual void HandleKeyboardEvent( |
| 42 content::WebContents* source, |
| 43 const content::NativeWebKeyboardEvent& event) OVERRIDE { |
| 44 // Forward shortcut keys in dialog to the browser. http://crbug.com/104586 |
| 45 // Disabled on Mac due to http://crbug.com/112173 |
| 46 #if !defined(OS_MACOSX) |
| 47 Browser* current_browser = chrome::FindBrowserWithWebContents(initiator_); |
| 48 if (!current_browser) |
| 49 return; |
| 50 current_browser->window()->HandleKeyboardEvent(event); |
| 22 #endif | 51 #endif |
| 52 } |
| 23 | 53 |
| 24 using ui::WebDialogDelegate; | 54 private: |
| 25 using ui::WebDialogWebContentsDelegate; | 55 content::WebContents* initiator_; |
| 56 views::WebView* web_view_; |
| 26 | 57 |
| 27 namespace { | 58 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateViews); |
| 59 }; |
| 28 | 60 |
| 29 class ConstrainedWebDialogDelegateViews | 61 class ConstrainedWebDialogDelegateViews |
| 30 : public ConstrainedWebDialogDelegateBase { | 62 : public ConstrainedWebDialogDelegateBase { |
| 31 public: | 63 public: |
| 32 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, | 64 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, |
| 33 WebDialogDelegate* delegate, | 65 ui::WebDialogDelegate* delegate, |
| 34 WebDialogWebContentsDelegate* tab_delegate, | 66 content::WebContents* web_contents, |
| 35 views::WebView* view) | 67 views::WebView* view) |
| 36 : ConstrainedWebDialogDelegateBase(context, delegate, tab_delegate), | 68 : ConstrainedWebDialogDelegateBase(context, delegate, |
| 69 new WebDialogWebContentsDelegateViews(context, web_contents, view)), |
| 37 view_(view) {} | 70 view_(view) {} |
| 38 | 71 |
| 39 virtual ~ConstrainedWebDialogDelegateViews() {} | 72 virtual ~ConstrainedWebDialogDelegateViews() {} |
| 40 | 73 |
| 41 // WebDialogWebContentsDelegate: | 74 // ui::WebDialogWebContentsDelegate: |
| 42 virtual void CloseContents(content::WebContents* source) OVERRIDE { | 75 virtual void CloseContents(content::WebContents* source) OVERRIDE { |
| 43 view_->GetWidget()->Close(); | 76 view_->GetWidget()->Close(); |
| 44 } | 77 } |
| 45 | 78 |
| 46 // contents::WebContentsDelegate: | 79 // contents::WebContentsDelegate: |
| 47 virtual void HandleKeyboardEvent( | 80 virtual void HandleKeyboardEvent( |
| 48 content::WebContents* source, | 81 content::WebContents* source, |
| 49 const content::NativeWebKeyboardEvent& event) OVERRIDE { | 82 const content::NativeWebKeyboardEvent& event) OVERRIDE { |
| 50 unhandled_keyboard_event_handler_.HandleKeyboardEvent( | 83 unhandled_keyboard_event_handler_.HandleKeyboardEvent( |
| 51 event, view_->GetFocusManager()); | 84 event, view_->GetFocusManager()); |
| 52 } | 85 } |
| 53 | 86 |
| 54 // ConstrainedWebDialogDelegate: | 87 // ConstrainedWebDialogDelegate: |
| 55 virtual web_modal::NativeWebContentsModalDialog GetNativeDialog() OVERRIDE { | 88 virtual web_modal::NativeWebContentsModalDialog GetNativeDialog() OVERRIDE { |
| 56 return view_->GetWidget()->GetNativeView(); | 89 return view_->GetWidget()->GetNativeView(); |
| 57 } | 90 } |
| 58 | 91 |
| 59 private: | 92 private: |
| 60 // Converts keyboard events on the WebContents to accelerators. | 93 // Converts keyboard events on the WebContents to accelerators. |
| 61 views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_; | 94 views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_; |
| 62 | 95 |
| 63 views::WebView* view_; | 96 views::WebView* view_; |
| 64 | 97 |
| 65 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViews); | 98 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViews); |
| 66 }; | 99 }; |
| 67 | 100 |
| 68 #if defined(USE_AURA) | |
| 69 // TODO(msw): Make this part of WebView? Modify various WebContentsDelegates? | |
| 70 class WebViewFocusHelper : public aura::client::FocusChangeObserver { | |
| 71 public: | |
| 72 explicit WebViewFocusHelper(views::WebView* web_view) | |
| 73 : web_view_(web_view), | |
| 74 window_(NULL), | |
| 75 focus_client_(NULL) { | |
| 76 if (web_view_ && web_view_->web_contents()) { | |
| 77 content::RenderWidgetHostView* host_view = | |
| 78 web_view_->web_contents()->GetRenderWidgetHostView(); | |
| 79 window_ = host_view ? host_view->GetNativeView() : NULL; | |
| 80 } | |
| 81 focus_client_ = window_ ? aura::client::GetFocusClient(window_) : NULL; | |
| 82 if (focus_client_) | |
| 83 focus_client_->AddObserver(this); | |
| 84 } | |
| 85 | |
| 86 virtual ~WebViewFocusHelper() { | |
| 87 if (focus_client_) | |
| 88 focus_client_->RemoveObserver(this); | |
| 89 } | |
| 90 | |
| 91 virtual void OnWindowFocused(aura::Window* gained_focus, | |
| 92 aura::Window* lost_focus) OVERRIDE { | |
| 93 if (gained_focus == window_ && !web_view_->HasFocus()) | |
| 94 web_view_->RequestFocus(); | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 views::WebView* web_view_; | |
| 99 aura::Window* window_; | |
| 100 aura::client::FocusClient* focus_client_; | |
| 101 | |
| 102 DISALLOW_COPY_AND_ASSIGN(WebViewFocusHelper); | |
| 103 }; | |
| 104 #endif | |
| 105 | |
| 106 class ConstrainedWebDialogDelegateViewViews | 101 class ConstrainedWebDialogDelegateViewViews |
| 107 : public views::WebView, | 102 : public views::WebView, |
| 108 public ConstrainedWebDialogDelegate, | 103 public ConstrainedWebDialogDelegate, |
| 109 public views::WidgetDelegate { | 104 public views::WidgetDelegate { |
| 110 public: | 105 public: |
| 111 ConstrainedWebDialogDelegateViewViews( | 106 ConstrainedWebDialogDelegateViewViews( |
| 112 content::BrowserContext* browser_context, | 107 content::BrowserContext* browser_context, |
| 113 WebDialogDelegate* delegate, | 108 ui::WebDialogDelegate* delegate, |
| 114 WebDialogWebContentsDelegate* tab_delegate) | 109 content::WebContents* web_contents) |
| 115 : views::WebView(browser_context), | 110 : views::WebView(browser_context), |
| 116 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, | 111 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, |
| 117 tab_delegate, this)) { | 112 web_contents, this)) { |
| 118 SetWebContents(GetWebContents()); | 113 SetWebContents(GetWebContents()); |
| 119 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 114 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 120 } | 115 } |
| 121 virtual ~ConstrainedWebDialogDelegateViewViews() {} | 116 virtual ~ConstrainedWebDialogDelegateViewViews() {} |
| 122 | 117 |
| 123 // ConstrainedWebDialogDelegate: | 118 // ConstrainedWebDialogDelegate: |
| 124 virtual const WebDialogDelegate* GetWebDialogDelegate() const OVERRIDE { | 119 virtual const ui::WebDialogDelegate* GetWebDialogDelegate() const OVERRIDE { |
| 125 return impl_->GetWebDialogDelegate(); | 120 return impl_->GetWebDialogDelegate(); |
| 126 } | 121 } |
| 127 virtual WebDialogDelegate* GetWebDialogDelegate() OVERRIDE { | 122 virtual ui::WebDialogDelegate* GetWebDialogDelegate() OVERRIDE { |
| 128 return impl_->GetWebDialogDelegate(); | 123 return impl_->GetWebDialogDelegate(); |
| 129 } | 124 } |
| 130 virtual void OnDialogCloseFromWebUI() OVERRIDE { | 125 virtual void OnDialogCloseFromWebUI() OVERRIDE { |
| 131 return impl_->OnDialogCloseFromWebUI(); | 126 return impl_->OnDialogCloseFromWebUI(); |
| 132 } | 127 } |
| 133 virtual void ReleaseWebContentsOnDialogClose() OVERRIDE { | 128 virtual void ReleaseWebContentsOnDialogClose() OVERRIDE { |
| 134 return impl_->ReleaseWebContentsOnDialogClose(); | 129 return impl_->ReleaseWebContentsOnDialogClose(); |
| 135 } | 130 } |
| 136 virtual web_modal::NativeWebContentsModalDialog GetNativeDialog() OVERRIDE { | 131 virtual web_modal::NativeWebContentsModalDialog GetNativeDialog() OVERRIDE { |
| 137 return impl_->GetNativeDialog(); | 132 return impl_->GetNativeDialog(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 gfx::Size size; | 179 gfx::Size size; |
| 185 if (!impl_->closed_via_webui()) | 180 if (!impl_->closed_via_webui()) |
| 186 GetWebDialogDelegate()->GetDialogSize(&size); | 181 GetWebDialogDelegate()->GetDialogSize(&size); |
| 187 return size; | 182 return size; |
| 188 } | 183 } |
| 189 virtual gfx::Size GetMinimumSize() const OVERRIDE { | 184 virtual gfx::Size GetMinimumSize() const OVERRIDE { |
| 190 // Return an empty size so that we can be made smaller. | 185 // Return an empty size so that we can be made smaller. |
| 191 return gfx::Size(); | 186 return gfx::Size(); |
| 192 } | 187 } |
| 193 | 188 |
| 194 void OnShow() { | |
| 195 #if defined(USE_AURA) | |
| 196 web_view_focus_helper_.reset(new WebViewFocusHelper(this)); | |
| 197 #endif | |
| 198 } | |
| 199 | |
| 200 private: | 189 private: |
| 201 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; | 190 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; |
| 202 #if defined(USE_AURA) | |
| 203 scoped_ptr<WebViewFocusHelper> web_view_focus_helper_; | |
| 204 #endif | |
| 205 | 191 |
| 206 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); | 192 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); |
| 207 }; | 193 }; |
| 208 | 194 |
| 209 } // namespace | 195 } // namespace |
| 210 | 196 |
| 211 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( | 197 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( |
| 212 content::BrowserContext* browser_context, | 198 content::BrowserContext* browser_context, |
| 213 WebDialogDelegate* delegate, | 199 ui::WebDialogDelegate* delegate, |
| 214 WebDialogWebContentsDelegate* tab_delegate, | |
| 215 content::WebContents* web_contents) { | 200 content::WebContents* web_contents) { |
| 216 ConstrainedWebDialogDelegateViewViews* dialog = | 201 ConstrainedWebDialogDelegateViewViews* dialog = |
| 217 new ConstrainedWebDialogDelegateViewViews( | 202 new ConstrainedWebDialogDelegateViewViews( |
| 218 browser_context, delegate, tab_delegate); | 203 browser_context, delegate, web_contents); |
| 219 ShowWebModalDialogViews(dialog, web_contents); | 204 ShowWebModalDialogViews(dialog, web_contents); |
| 220 dialog->OnShow(); | |
| 221 return dialog; | 205 return dialog; |
| 222 } | 206 } |
| OLD | NEW |