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/views/constrained_window_views.h" | 8 #include "chrome/browser/ui/views/constrained_window_views.h" |
9 #include "content/public/browser/native_web_keyboard_event.h" | 9 #include "content/public/browser/native_web_keyboard_event.h" |
10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
11 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" | 11 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" |
12 #include "ui/views/controls/webview/webview.h" | 12 #include "ui/views/controls/webview/webview.h" |
13 #include "ui/views/widget/widget.h" | 13 #include "ui/views/widget/widget.h" |
14 #include "ui/views/window/dialog_delegate.h" | 14 #include "ui/views/window/dialog_delegate.h" |
15 #include "ui/web_dialogs/web_dialog_delegate.h" | 15 #include "ui/web_dialogs/web_dialog_delegate.h" |
16 #include "ui/web_dialogs/web_dialog_ui.h" | 16 #include "ui/web_dialogs/web_dialog_ui.h" |
17 | 17 |
| 18 #if defined(USE_AURA) |
| 19 #include "content/public/browser/render_widget_host_view.h" |
| 20 #include "ui/aura/client/focus_change_observer.h" |
| 21 #include "ui/aura/client/focus_client.h" |
| 22 #endif |
| 23 |
18 using ui::WebDialogDelegate; | 24 using ui::WebDialogDelegate; |
19 using ui::WebDialogWebContentsDelegate; | 25 using ui::WebDialogWebContentsDelegate; |
20 | 26 |
21 namespace { | 27 namespace { |
22 | 28 |
23 class ConstrainedWebDialogDelegateViews | 29 class ConstrainedWebDialogDelegateViews |
24 : public ConstrainedWebDialogDelegateBase { | 30 : public ConstrainedWebDialogDelegateBase { |
25 public: | 31 public: |
26 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, | 32 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, |
27 WebDialogDelegate* delegate, | 33 WebDialogDelegate* delegate, |
(...skipping 24 matching lines...) Expand all Loading... |
52 | 58 |
53 private: | 59 private: |
54 // Converts keyboard events on the WebContents to accelerators. | 60 // Converts keyboard events on the WebContents to accelerators. |
55 views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_; | 61 views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_; |
56 | 62 |
57 views::WebView* view_; | 63 views::WebView* view_; |
58 | 64 |
59 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViews); | 65 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViews); |
60 }; | 66 }; |
61 | 67 |
| 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 |
62 class ConstrainedWebDialogDelegateViewViews | 106 class ConstrainedWebDialogDelegateViewViews |
63 : public views::WebView, | 107 : public views::WebView, |
64 public ConstrainedWebDialogDelegate, | 108 public ConstrainedWebDialogDelegate, |
65 public views::WidgetDelegate { | 109 public views::WidgetDelegate { |
66 public: | 110 public: |
67 ConstrainedWebDialogDelegateViewViews( | 111 ConstrainedWebDialogDelegateViewViews( |
68 content::BrowserContext* browser_context, | 112 content::BrowserContext* browser_context, |
69 WebDialogDelegate* delegate, | 113 WebDialogDelegate* delegate, |
70 WebDialogWebContentsDelegate* tab_delegate) | 114 WebDialogWebContentsDelegate* tab_delegate) |
71 : views::WebView(browser_context), | 115 : views::WebView(browser_context), |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 gfx::Size size; | 184 gfx::Size size; |
141 if (!impl_->closed_via_webui()) | 185 if (!impl_->closed_via_webui()) |
142 GetWebDialogDelegate()->GetDialogSize(&size); | 186 GetWebDialogDelegate()->GetDialogSize(&size); |
143 return size; | 187 return size; |
144 } | 188 } |
145 virtual gfx::Size GetMinimumSize() const OVERRIDE { | 189 virtual gfx::Size GetMinimumSize() const OVERRIDE { |
146 // Return an empty size so that we can be made smaller. | 190 // Return an empty size so that we can be made smaller. |
147 return gfx::Size(); | 191 return gfx::Size(); |
148 } | 192 } |
149 | 193 |
| 194 void OnShow() { |
| 195 #if defined(USE_AURA) |
| 196 web_view_focus_helper_.reset(new WebViewFocusHelper(this)); |
| 197 #endif |
| 198 } |
| 199 |
150 private: | 200 private: |
151 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; | 201 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; |
| 202 #if defined(USE_AURA) |
| 203 scoped_ptr<WebViewFocusHelper> web_view_focus_helper_; |
| 204 #endif |
152 | 205 |
153 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); | 206 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); |
154 }; | 207 }; |
155 | 208 |
156 } // namespace | 209 } // namespace |
157 | 210 |
158 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( | 211 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( |
159 content::BrowserContext* browser_context, | 212 content::BrowserContext* browser_context, |
160 WebDialogDelegate* delegate, | 213 WebDialogDelegate* delegate, |
161 WebDialogWebContentsDelegate* tab_delegate, | 214 WebDialogWebContentsDelegate* tab_delegate, |
162 content::WebContents* web_contents) { | 215 content::WebContents* web_contents) { |
163 ConstrainedWebDialogDelegateViewViews* dialog = | 216 ConstrainedWebDialogDelegateViewViews* dialog = |
164 new ConstrainedWebDialogDelegateViewViews( | 217 new ConstrainedWebDialogDelegateViewViews( |
165 browser_context, delegate, tab_delegate); | 218 browser_context, delegate, tab_delegate); |
166 ShowWebModalDialogViews(dialog, web_contents); | 219 ShowWebModalDialogViews(dialog, web_contents); |
| 220 dialog->OnShow(); |
167 return dialog; | 221 return dialog; |
168 } | 222 } |
OLD | NEW |