| 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" | 8 #include "chrome/browser/ui/browser_finder.h" |
| 9 #include "chrome/browser/ui/browser_window.h" | 9 #include "chrome/browser/ui/browser_window.h" |
| 10 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" | 10 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" |
| 11 #include "components/constrained_window/constrained_window_views.h" | 11 #include "components/constrained_window/constrained_window_views.h" |
| 12 #include "components/web_modal/popup_manager.h" |
| 13 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 12 #include "content/public/browser/native_web_keyboard_event.h" | 14 #include "content/public/browser/native_web_keyboard_event.h" |
| 15 #include "content/public/browser/render_view_host.h" |
| 13 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 14 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" | 17 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" |
| 15 #include "ui/views/controls/webview/webview.h" | 18 #include "ui/views/controls/webview/webview.h" |
| 19 #include "ui/views/view.h" |
| 16 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 17 #include "ui/views/window/dialog_delegate.h" | 21 #include "ui/views/window/dialog_delegate.h" |
| 18 #include "ui/web_dialogs/web_dialog_delegate.h" | 22 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 19 #include "ui/web_dialogs/web_dialog_ui.h" | 23 #include "ui/web_dialogs/web_dialog_ui.h" |
| 20 | 24 |
| 21 namespace { | 25 namespace { |
| 22 | 26 |
| 27 class InitiatorWebContentsObserver |
| 28 : public content::WebContentsObserver { |
| 29 public: |
| 30 explicit InitiatorWebContentsObserver(content::WebContents* web_contents) |
| 31 : content::WebContentsObserver(web_contents) { |
| 32 } |
| 33 |
| 34 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(InitiatorWebContentsObserver); |
| 36 }; |
| 37 |
| 23 class WebDialogWebContentsDelegateViews | 38 class WebDialogWebContentsDelegateViews |
| 24 : public ui::WebDialogWebContentsDelegate { | 39 : public ui::WebDialogWebContentsDelegate { |
| 25 public: | 40 public: |
| 26 WebDialogWebContentsDelegateViews(content::BrowserContext* browser_context, | 41 WebDialogWebContentsDelegateViews(content::BrowserContext* browser_context, |
| 27 content::WebContents* initiator, | 42 InitiatorWebContentsObserver* observer, |
| 28 views::WebView* web_view) | 43 views::WebView* web_view) |
| 29 : ui::WebDialogWebContentsDelegate(browser_context, | 44 : ui::WebDialogWebContentsDelegate(browser_context, |
| 30 new ChromeWebContentsHandler()), | 45 new ChromeWebContentsHandler()), |
| 31 initiator_(initiator), | 46 initiator_observer_(observer), |
| 32 web_view_(web_view) { | 47 web_view_(web_view) { |
| 33 } | 48 } |
| 34 ~WebDialogWebContentsDelegateViews() override {} | 49 ~WebDialogWebContentsDelegateViews() override {} |
| 35 | 50 |
| 36 // ui::WebDialogWebContentsDelegate: | 51 // ui::WebDialogWebContentsDelegate: |
| 37 void WebContentsFocused(content::WebContents* contents) override { | 52 void WebContentsFocused(content::WebContents* contents) override { |
| 38 // Ensure the WebView is focused when its WebContents is focused. | 53 // Ensure the WebView is focused when its WebContents is focused. |
| 39 web_view_->RequestFocus(); | 54 web_view_->RequestFocus(); |
| 40 } | 55 } |
| 41 void HandleKeyboardEvent( | 56 void HandleKeyboardEvent( |
| 42 content::WebContents* source, | 57 content::WebContents* source, |
| 43 const content::NativeWebKeyboardEvent& event) override { | 58 const content::NativeWebKeyboardEvent& event) override { |
| 44 // Forward shortcut keys in dialog to our initiator's delegate. | 59 // Forward shortcut keys in dialog to our initiator's delegate. |
| 45 // http://crbug.com/104586 | 60 // http://crbug.com/104586 |
| 46 // Disabled on Mac due to http://crbug.com/112173 | 61 // Disabled on Mac due to http://crbug.com/112173 |
| 47 #if !defined(OS_MACOSX) | 62 #if !defined(OS_MACOSX) |
| 48 auto delegate = initiator_->GetDelegate(); | 63 if (!initiator_observer_->web_contents()) |
| 64 return; |
| 65 |
| 66 auto delegate = initiator_observer_->web_contents()->GetDelegate(); |
| 49 if (!delegate) | 67 if (!delegate) |
| 50 return; | 68 return; |
| 51 delegate->HandleKeyboardEvent(initiator_, event); | 69 delegate->HandleKeyboardEvent(initiator_observer_->web_contents(), event); |
| 52 #endif | 70 #endif |
| 53 } | 71 } |
| 54 | 72 |
| 73 void ResizeDueToAutoResize(content::WebContents* source, |
| 74 const gfx::Size& preferred_size) override { |
| 75 if (!initiator_observer_->web_contents()) |
| 76 return; |
| 77 |
| 78 // Sets WebView's preferred size based on auto-resized contents. |
| 79 web_view_->SetPreferredSize(preferred_size); |
| 80 |
| 81 constrained_window::UpdateWebContentsModalDialogPosition( |
| 82 web_view_->GetWidget(), |
| 83 web_modal::WebContentsModalDialogManager::FromWebContents( |
| 84 initiator_observer_->web_contents())->delegate()-> |
| 85 GetWebContentsModalDialogHost()); |
| 86 } |
| 87 |
| 55 private: | 88 private: |
| 56 content::WebContents* initiator_; | 89 InitiatorWebContentsObserver* const initiator_observer_; |
| 57 views::WebView* web_view_; | 90 views::WebView* web_view_; |
| 58 | 91 |
| 59 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateViews); | 92 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateViews); |
| 60 }; | 93 }; |
| 61 | 94 |
| 62 class ConstrainedWebDialogDelegateViews | 95 class ConstrainedWebDialogDelegateViews |
| 63 : public ConstrainedWebDialogDelegateBase { | 96 : public ConstrainedWebDialogDelegateBase { |
| 64 public: | 97 public: |
| 65 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, | 98 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, |
| 66 ui::WebDialogDelegate* delegate, | 99 ui::WebDialogDelegate* delegate, |
| 67 content::WebContents* web_contents, | 100 InitiatorWebContentsObserver* observer, |
| 68 views::WebView* view) | 101 views::WebView* view) |
| 69 : ConstrainedWebDialogDelegateBase(context, delegate, | 102 : ConstrainedWebDialogDelegateBase(context, delegate, |
| 70 new WebDialogWebContentsDelegateViews(context, web_contents, view)), | 103 new WebDialogWebContentsDelegateViews(context, observer, view)), |
| 71 view_(view) {} | 104 view_(view) {} |
| 72 | 105 |
| 73 ~ConstrainedWebDialogDelegateViews() override {} | 106 ~ConstrainedWebDialogDelegateViews() override {} |
| 74 | 107 |
| 75 // ui::WebDialogWebContentsDelegate: | 108 // ui::WebDialogWebContentsDelegate: |
| 76 void CloseContents(content::WebContents* source) override { | 109 void CloseContents(content::WebContents* source) override { |
| 77 view_->GetWidget()->Close(); | 110 view_->GetWidget()->Close(); |
| 78 } | 111 } |
| 79 | 112 |
| 80 // contents::WebContentsDelegate: | 113 // contents::WebContentsDelegate: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 100 }; | 133 }; |
| 101 | 134 |
| 102 class ConstrainedWebDialogDelegateViewViews | 135 class ConstrainedWebDialogDelegateViewViews |
| 103 : public views::WebView, | 136 : public views::WebView, |
| 104 public ConstrainedWebDialogDelegate, | 137 public ConstrainedWebDialogDelegate, |
| 105 public views::WidgetDelegate { | 138 public views::WidgetDelegate { |
| 106 public: | 139 public: |
| 107 ConstrainedWebDialogDelegateViewViews( | 140 ConstrainedWebDialogDelegateViewViews( |
| 108 content::BrowserContext* browser_context, | 141 content::BrowserContext* browser_context, |
| 109 ui::WebDialogDelegate* delegate, | 142 ui::WebDialogDelegate* delegate, |
| 110 content::WebContents* web_contents) | 143 content::WebContents* web_contents, |
| 144 const gfx::Size& min_size, |
| 145 const gfx::Size& max_size) |
| 111 : views::WebView(browser_context), | 146 : views::WebView(browser_context), |
| 147 initiator_observer_(web_contents), |
| 112 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, | 148 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, |
| 113 web_contents, this)) { | 149 &initiator_observer_, |
| 150 this)), |
| 151 min_size_(min_size), |
| 152 max_size_(max_size) { |
| 114 SetWebContents(GetWebContents()); | 153 SetWebContents(GetWebContents()); |
| 115 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 154 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 116 } | 155 } |
| 117 ~ConstrainedWebDialogDelegateViewViews() override {} | 156 ~ConstrainedWebDialogDelegateViewViews() override {} |
| 118 | 157 |
| 119 // ConstrainedWebDialogDelegate: | 158 // ConstrainedWebDialogDelegate: |
| 120 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { | 159 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { |
| 121 return impl_->GetWebDialogDelegate(); | 160 return impl_->GetWebDialogDelegate(); |
| 122 } | 161 } |
| 123 ui::WebDialogDelegate* GetWebDialogDelegate() override { | 162 ui::WebDialogDelegate* GetWebDialogDelegate() override { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 200 |
| 162 // views::WebView: | 201 // views::WebView: |
| 163 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { | 202 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
| 164 // Pressing ESC closes the dialog. | 203 // Pressing ESC closes the dialog. |
| 165 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); | 204 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); |
| 166 GetWidget()->Close(); | 205 GetWidget()->Close(); |
| 167 return true; | 206 return true; |
| 168 } | 207 } |
| 169 gfx::Size GetPreferredSize() const override { | 208 gfx::Size GetPreferredSize() const override { |
| 170 gfx::Size size; | 209 gfx::Size size; |
| 171 if (!impl_->closed_via_webui()) | 210 if (!impl_->closed_via_webui()) { |
| 172 GetWebDialogDelegate()->GetDialogSize(&size); | 211 // Size is set here if the dialog has been auto-resized in |
| 212 // WebDialogWebContentsDelegateViews's ResizeDueToAutoResize. |
| 213 size = WebView::GetPreferredSize(); |
| 214 if (size.IsEmpty()) { |
| 215 // Size set here if dialog has not been auto-resized or |
| 216 // auto-resizable is not enabled. |
| 217 GetWebDialogDelegate()->GetDialogSize(&size); |
| 218 } |
| 219 } |
| 173 return size; | 220 return size; |
| 174 } | 221 } |
| 175 gfx::Size GetMinimumSize() const override { | 222 gfx::Size GetMinimumSize() const override { |
| 176 // Return an empty size so that we can be made smaller. | 223 return min_size_; |
| 177 return gfx::Size(); | 224 } |
| 225 gfx::Size GetMaximumSize() const override { |
| 226 return !max_size_.IsEmpty() ? max_size_ : WebView::GetMaximumSize(); |
| 227 } |
| 228 void RenderViewCreated(content::RenderViewHost* render_view_host) override { |
| 229 if (!max_size_.IsEmpty()) |
| 230 EnableAutoResize(); |
| 231 } |
| 232 void RenderViewHostChanged(content::RenderViewHost* old_host, |
| 233 content::RenderViewHost* new_host) override { |
| 234 if (!max_size_.IsEmpty()) |
| 235 EnableAutoResize(); |
| 236 } |
| 237 void DocumentOnLoadCompletedInMainFrame() override { |
| 238 if (!max_size_.IsEmpty()) { |
| 239 EnableAutoResize(); |
| 240 if (initiator_observer_.web_contents()) { |
| 241 web_modal::PopupManager* popup_manager = |
| 242 web_modal::PopupManager::FromWebContents( |
| 243 initiator_observer_.web_contents()); |
| 244 popup_manager->ShowModalDialog(GetWidget()->GetNativeView(), |
| 245 initiator_observer_.web_contents()); |
| 246 } |
| 247 } |
| 178 } | 248 } |
| 179 | 249 |
| 180 private: | 250 private: |
| 251 void EnableAutoResize() { |
| 252 content::RenderViewHost* render_view_host = |
| 253 GetWebContents()->GetRenderViewHost(); |
| 254 render_view_host->EnableAutoResize(min_size_, max_size_); |
| 255 } |
| 256 |
| 257 InitiatorWebContentsObserver initiator_observer_; |
| 258 |
| 181 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; | 259 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; |
| 182 | 260 |
| 261 // Minimum and maximum sizes to determine dialog bounds if |
| 262 // auto-resizing is enabled. |
| 263 const gfx::Size min_size_; |
| 264 const gfx::Size max_size_; |
| 265 |
| 183 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); | 266 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); |
| 184 }; | 267 }; |
| 185 | 268 |
| 186 } // namespace | 269 } // namespace |
| 187 | 270 |
| 188 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( | 271 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( |
| 189 content::BrowserContext* browser_context, | 272 content::BrowserContext* browser_context, |
| 190 ui::WebDialogDelegate* delegate, | 273 ui::WebDialogDelegate* delegate, |
| 191 content::WebContents* web_contents) { | 274 content::WebContents* web_contents) { |
| 192 ConstrainedWebDialogDelegateViewViews* dialog = | 275 ConstrainedWebDialogDelegateViewViews* dialog = |
| 193 new ConstrainedWebDialogDelegateViewViews( | 276 new ConstrainedWebDialogDelegateViewViews( |
| 194 browser_context, delegate, web_contents); | 277 browser_context, delegate, web_contents, |
| 278 gfx::Size(), gfx::Size()); |
| 195 constrained_window::ShowWebModalDialogViews(dialog, web_contents); | 279 constrained_window::ShowWebModalDialogViews(dialog, web_contents); |
| 196 return dialog; | 280 return dialog; |
| 197 } | 281 } |
| 282 |
| 283 ConstrainedWebDialogDelegate* CreateConstrainedWebDialogWithAutoResize( |
| 284 content::BrowserContext* browser_context, |
| 285 ui::WebDialogDelegate* delegate, |
| 286 content::WebContents* web_contents, |
| 287 const gfx::Size& min_size, |
| 288 const gfx::Size& max_size) { |
| 289 DCHECK(!min_size.IsEmpty()); |
| 290 DCHECK(!max_size.IsEmpty()); |
| 291 ConstrainedWebDialogDelegateViewViews* dialog = |
| 292 new ConstrainedWebDialogDelegateViewViews( |
| 293 browser_context, delegate, web_contents, |
| 294 min_size, max_size); |
| 295 constrained_window::CreateWebModalDialogViews(dialog, web_contents); |
| 296 return dialog; |
| 297 } |
| OLD | NEW |