Chromium Code Reviews| 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/web_contents_modal_dialog_manager.h" | |
| 12 #include "content/public/browser/native_web_keyboard_event.h" | 13 #include "content/public/browser/native_web_keyboard_event.h" |
| 14 #include "content/public/browser/render_view_host.h" | |
| 13 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 14 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" | 16 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" |
| 15 #include "ui/views/controls/webview/webview.h" | 17 #include "ui/views/controls/webview/webview.h" |
| 18 #include "ui/views/view.h" | |
| 16 #include "ui/views/widget/widget.h" | 19 #include "ui/views/widget/widget.h" |
| 17 #include "ui/views/window/dialog_delegate.h" | 20 #include "ui/views/window/dialog_delegate.h" |
| 18 #include "ui/web_dialogs/web_dialog_delegate.h" | 21 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 19 #include "ui/web_dialogs/web_dialog_ui.h" | 22 #include "ui/web_dialogs/web_dialog_ui.h" |
| 20 | 23 |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 23 class WebDialogWebContentsDelegateViews | 26 class WebDialogWebContentsDelegateViews |
| 24 : public ui::WebDialogWebContentsDelegate { | 27 : public ui::WebDialogWebContentsDelegate { |
| 25 public: | 28 public: |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 45 // http://crbug.com/104586 | 48 // http://crbug.com/104586 |
| 46 // Disabled on Mac due to http://crbug.com/112173 | 49 // Disabled on Mac due to http://crbug.com/112173 |
| 47 #if !defined(OS_MACOSX) | 50 #if !defined(OS_MACOSX) |
| 48 auto delegate = initiator_->GetDelegate(); | 51 auto delegate = initiator_->GetDelegate(); |
| 49 if (!delegate) | 52 if (!delegate) |
| 50 return; | 53 return; |
| 51 delegate->HandleKeyboardEvent(initiator_, event); | 54 delegate->HandleKeyboardEvent(initiator_, event); |
| 52 #endif | 55 #endif |
| 53 } | 56 } |
| 54 | 57 |
| 58 void ResizeDueToAutoResize(content::WebContents* source, | |
| 59 const gfx::Size& preferred_size) override { | |
| 60 web_view_->SetPreferredSize(preferred_size); | |
| 61 | |
| 62 constrained_window::UpdateWebContentsModalDialogPosition( | |
| 63 web_view_->GetWidget(), | |
| 64 web_modal::WebContentsModalDialogManager::FromWebContents( | |
| 65 initiator_)->delegate()->GetWebContentsModalDialogHost()); | |
| 66 } | |
| 67 | |
| 55 private: | 68 private: |
| 56 content::WebContents* initiator_; | 69 content::WebContents* initiator_; |
| 57 views::WebView* web_view_; | 70 views::WebView* web_view_; |
| 58 | 71 |
| 59 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateViews); | 72 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateViews); |
| 60 }; | 73 }; |
| 61 | 74 |
| 62 class ConstrainedWebDialogDelegateViews | 75 class ConstrainedWebDialogDelegateViews |
| 63 : public ConstrainedWebDialogDelegateBase { | 76 : public ConstrainedWebDialogDelegateBase { |
| 64 public: | 77 public: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 }; | 113 }; |
| 101 | 114 |
| 102 class ConstrainedWebDialogDelegateViewViews | 115 class ConstrainedWebDialogDelegateViewViews |
| 103 : public views::WebView, | 116 : public views::WebView, |
| 104 public ConstrainedWebDialogDelegate, | 117 public ConstrainedWebDialogDelegate, |
| 105 public views::WidgetDelegate { | 118 public views::WidgetDelegate { |
| 106 public: | 119 public: |
| 107 ConstrainedWebDialogDelegateViewViews( | 120 ConstrainedWebDialogDelegateViewViews( |
| 108 content::BrowserContext* browser_context, | 121 content::BrowserContext* browser_context, |
| 109 ui::WebDialogDelegate* delegate, | 122 ui::WebDialogDelegate* delegate, |
| 110 content::WebContents* web_contents) | 123 content::WebContents* web_contents, |
| 124 gfx::Size min_size, | |
|
Bernhard Bauer
2014/11/27 12:41:34
Pass by const reference?
apacible
2014/12/01 17:35:57
Done.
Bernhard Bauer
2014/12/01 17:46:04
Const *reference* please.
apacible
2014/12/01 18:00:56
Oops, fixed.
| |
| 125 gfx::Size max_size) | |
| 111 : views::WebView(browser_context), | 126 : views::WebView(browser_context), |
| 112 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, | 127 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, |
| 113 web_contents, this)) { | 128 web_contents, this)), |
| 129 initiator_web_contents_(web_contents), | |
| 130 min_size_(min_size), | |
| 131 max_size_(max_size) { | |
| 114 SetWebContents(GetWebContents()); | 132 SetWebContents(GetWebContents()); |
| 115 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 133 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 116 } | 134 } |
| 117 ~ConstrainedWebDialogDelegateViewViews() override {} | 135 ~ConstrainedWebDialogDelegateViewViews() override {} |
| 118 | 136 |
| 119 // ConstrainedWebDialogDelegate: | 137 // ConstrainedWebDialogDelegate: |
| 120 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { | 138 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { |
| 121 return impl_->GetWebDialogDelegate(); | 139 return impl_->GetWebDialogDelegate(); |
| 122 } | 140 } |
| 123 ui::WebDialogDelegate* GetWebDialogDelegate() override { | 141 ui::WebDialogDelegate* GetWebDialogDelegate() override { |
| 124 return impl_->GetWebDialogDelegate(); | 142 return impl_->GetWebDialogDelegate(); |
| 125 } | 143 } |
| 126 void OnDialogCloseFromWebUI() override { | 144 void OnDialogCloseFromWebUI() override { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 | 179 |
| 162 // views::WebView: | 180 // views::WebView: |
| 163 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { | 181 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
| 164 // Pressing ESC closes the dialog. | 182 // Pressing ESC closes the dialog. |
| 165 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); | 183 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); |
| 166 GetWidget()->Close(); | 184 GetWidget()->Close(); |
| 167 return true; | 185 return true; |
| 168 } | 186 } |
| 169 gfx::Size GetPreferredSize() const override { | 187 gfx::Size GetPreferredSize() const override { |
| 170 gfx::Size size; | 188 gfx::Size size; |
| 171 if (!impl_->closed_via_webui()) | 189 if (!impl_->closed_via_webui()) { |
| 172 GetWebDialogDelegate()->GetDialogSize(&size); | 190 size = WebView::GetPreferredSize(); |
| 191 if (size.IsEmpty()) | |
| 192 GetWebDialogDelegate()->GetDialogSize(&size); | |
| 193 } | |
| 173 return size; | 194 return size; |
| 174 } | 195 } |
| 175 gfx::Size GetMinimumSize() const override { | 196 gfx::Size GetMinimumSize() const override { |
| 176 // Return an empty size so that we can be made smaller. | 197 return min_size_; |
| 177 return gfx::Size(); | 198 } |
| 199 gfx::Size GetMaximumSize() const override { | |
| 200 return !max_size_.IsEmpty() ? max_size_ : WebView::GetMaximumSize(); | |
| 201 } | |
| 202 void RenderViewCreated(content::RenderViewHost* render_view_host) override { | |
| 203 if (!max_size_.IsEmpty()) | |
| 204 EnableAutoResize(); | |
| 205 } | |
| 206 void RenderViewHostChanged(content::RenderViewHost* old_host, | |
| 207 content::RenderViewHost* new_host) override { | |
| 208 if (!max_size_.IsEmpty()) | |
| 209 EnableAutoResize(); | |
| 210 } | |
| 211 void DocumentOnLoadCompletedInMainFrame() override { | |
| 212 if (!max_size_.IsEmpty()) { | |
| 213 EnableAutoResize(); | |
| 214 constrained_window::ShowWebModalDialogViews(this, | |
| 215 initiator_web_contents_); | |
| 216 } | |
| 178 } | 217 } |
| 179 | 218 |
| 180 private: | 219 private: |
| 181 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; | 220 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; |
| 182 | 221 |
| 222 content::WebContents* const initiator_web_contents_; | |
|
Bernhard Bauer
2014/11/27 12:41:34
You want the pointer value to be const? Usually we
miu
2014/11/27 19:53:54
I asked for this change on a prior round of review
| |
| 223 | |
| 224 const gfx::Size min_size_; | |
| 225 const gfx::Size max_size_; | |
| 226 | |
| 227 void EnableAutoResize() { | |
| 228 content::RenderViewHost* render_view_host = | |
| 229 GetWebContents()->GetRenderViewHost(); | |
| 230 DCHECK(render_view_host); | |
|
Bernhard Bauer
2014/11/27 12:41:33
This is somewhat unnecessary, as you will crash on
apacible
2014/12/01 17:35:57
Done.
| |
| 231 render_view_host->EnableAutoResize(min_size_, max_size_); | |
| 232 } | |
| 233 | |
| 183 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); | 234 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); |
| 184 }; | 235 }; |
| 185 | 236 |
| 186 } // namespace | 237 } // namespace |
| 187 | 238 |
| 188 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( | 239 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( |
| 189 content::BrowserContext* browser_context, | 240 content::BrowserContext* browser_context, |
| 190 ui::WebDialogDelegate* delegate, | 241 ui::WebDialogDelegate* delegate, |
| 191 content::WebContents* web_contents) { | 242 content::WebContents* web_contents) { |
| 192 ConstrainedWebDialogDelegateViewViews* dialog = | 243 ConstrainedWebDialogDelegateViewViews* dialog = |
| 193 new ConstrainedWebDialogDelegateViewViews( | 244 new ConstrainedWebDialogDelegateViewViews( |
| 194 browser_context, delegate, web_contents); | 245 browser_context, delegate, web_contents, |
| 246 gfx::Size(), gfx::Size()); | |
| 195 constrained_window::ShowWebModalDialogViews(dialog, web_contents); | 247 constrained_window::ShowWebModalDialogViews(dialog, web_contents); |
| 196 return dialog; | 248 return dialog; |
| 197 } | 249 } |
| 250 | |
| 251 ConstrainedWebDialogDelegate* CreateConstrainedWebDialogWithAutoResize( | |
| 252 content::BrowserContext* browser_context, | |
| 253 ui::WebDialogDelegate* delegate, | |
| 254 content::WebContents* web_contents, | |
| 255 gfx::Size min_size, | |
| 256 gfx::Size max_size) { | |
| 257 DCHECK(!min_size.IsEmpty()); | |
| 258 DCHECK(!max_size.IsEmpty()); | |
| 259 ConstrainedWebDialogDelegateViewViews* dialog = | |
| 260 new ConstrainedWebDialogDelegateViewViews( | |
| 261 browser_context, delegate, web_contents, | |
| 262 min_size, max_size); | |
| 263 constrained_window::CreateWebModalDialogViews(dialog, web_contents); | |
| 264 return dialog; | |
| 265 } | |
| OLD | NEW |