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/popup_manager.h" | |
|
miu
2014/11/26 00:47:43
Is this used anywhere?
apacible
2014/11/26 17:35:14
Nope, fixed.
| |
| 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 |
| 23 class WebDialogWebContentsDelegateViews | 27 class WebDialogWebContentsDelegateViews |
| 24 : public ui::WebDialogWebContentsDelegate { | 28 : public ui::WebDialogWebContentsDelegate { |
| 25 public: | 29 public: |
| 26 WebDialogWebContentsDelegateViews(content::BrowserContext* browser_context, | 30 WebDialogWebContentsDelegateViews(content::BrowserContext* browser_context, |
| 27 content::WebContents* initiator, | 31 content::WebContents* initiator, |
| 28 views::WebView* web_view) | 32 views::WebView* web_view) |
| 29 : ui::WebDialogWebContentsDelegate(browser_context, | 33 : ui::WebDialogWebContentsDelegate(browser_context, |
| 30 new ChromeWebContentsHandler()), | 34 new ChromeWebContentsHandler()), |
| 31 initiator_(initiator), | 35 initiator_(initiator), |
| 32 web_view_(web_view) { | 36 web_view_(web_view) { |
| 37 preferred_size_ = gfx::Size(); | |
| 33 } | 38 } |
| 34 ~WebDialogWebContentsDelegateViews() override {} | 39 ~WebDialogWebContentsDelegateViews() override {} |
| 35 | 40 |
| 36 // ui::WebDialogWebContentsDelegate: | 41 // ui::WebDialogWebContentsDelegate: |
| 37 void WebContentsFocused(content::WebContents* contents) override { | 42 void WebContentsFocused(content::WebContents* contents) override { |
| 38 // Ensure the WebView is focused when its WebContents is focused. | 43 // Ensure the WebView is focused when its WebContents is focused. |
| 39 web_view_->RequestFocus(); | 44 web_view_->RequestFocus(); |
| 40 } | 45 } |
| 41 void HandleKeyboardEvent( | 46 void HandleKeyboardEvent( |
| 42 content::WebContents* source, | 47 content::WebContents* source, |
| 43 const content::NativeWebKeyboardEvent& event) override { | 48 const content::NativeWebKeyboardEvent& event) override { |
| 44 // Forward shortcut keys in dialog to our initiator's delegate. | 49 // Forward shortcut keys in dialog to our initiator's delegate. |
| 45 // http://crbug.com/104586 | 50 // http://crbug.com/104586 |
| 46 // Disabled on Mac due to http://crbug.com/112173 | 51 // Disabled on Mac due to http://crbug.com/112173 |
| 47 #if !defined(OS_MACOSX) | 52 #if !defined(OS_MACOSX) |
| 48 auto delegate = initiator_->GetDelegate(); | 53 auto delegate = initiator_->GetDelegate(); |
| 49 if (!delegate) | 54 if (!delegate) |
| 50 return; | 55 return; |
| 51 delegate->HandleKeyboardEvent(initiator_, event); | 56 delegate->HandleKeyboardEvent(initiator_, event); |
| 52 #endif | 57 #endif |
| 53 } | 58 } |
| 54 | 59 |
| 60 void ResizeDueToAutoResize(content::WebContents* source, | |
| 61 const gfx::Size& preferred_size) override { | |
| 62 web_view_->SetPreferredSize(preferred_size); | |
| 63 preferred_size_ = preferred_size; | |
| 64 | |
| 65 constrained_window::UpdateWebContentsModalDialogPosition( | |
| 66 web_view_->GetWidget(), | |
| 67 web_modal::WebContentsModalDialogManager::FromWebContents( | |
| 68 initiator_)->delegate()->GetWebContentsModalDialogHost()); | |
| 69 web_view_->SetBoundsRect(web_view_->bounds()); | |
|
miu
2014/11/26 00:47:43
Is this call to SetBoundsRect() necessary? Seems
apacible
2014/11/26 17:35:13
No, and fixed.
| |
| 70 } | |
| 71 | |
| 72 gfx::Size GetPreferredSize() const { | |
|
miu
2014/11/26 00:47:43
I don't think this method is needed. See comments
apacible
2014/11/26 17:35:13
Done.
| |
| 73 return preferred_size_; | |
| 74 } | |
| 75 | |
| 55 private: | 76 private: |
| 56 content::WebContents* initiator_; | 77 content::WebContents* initiator_; |
| 57 views::WebView* web_view_; | 78 views::WebView* web_view_; |
| 79 gfx::Size preferred_size_; | |
|
miu
2014/11/26 00:47:43
Don't need this if you don't need GetPreferredSize
apacible
2014/11/26 17:35:13
Done.
| |
| 58 | 80 |
| 59 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateViews); | 81 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateViews); |
| 60 }; | 82 }; |
| 61 | 83 |
| 62 class ConstrainedWebDialogDelegateViews | 84 class ConstrainedWebDialogDelegateViews |
| 63 : public ConstrainedWebDialogDelegateBase { | 85 : public ConstrainedWebDialogDelegateBase { |
| 64 public: | 86 public: |
| 65 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, | 87 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, |
| 66 ui::WebDialogDelegate* delegate, | 88 ui::WebDialogDelegate* delegate, |
| 67 content::WebContents* web_contents, | 89 content::WebContents* web_contents, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 public ConstrainedWebDialogDelegate, | 126 public ConstrainedWebDialogDelegate, |
| 105 public views::WidgetDelegate { | 127 public views::WidgetDelegate { |
| 106 public: | 128 public: |
| 107 ConstrainedWebDialogDelegateViewViews( | 129 ConstrainedWebDialogDelegateViewViews( |
| 108 content::BrowserContext* browser_context, | 130 content::BrowserContext* browser_context, |
| 109 ui::WebDialogDelegate* delegate, | 131 ui::WebDialogDelegate* delegate, |
| 110 content::WebContents* web_contents) | 132 content::WebContents* web_contents) |
| 111 : views::WebView(browser_context), | 133 : views::WebView(browser_context), |
| 112 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, | 134 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, |
| 113 web_contents, this)) { | 135 web_contents, this)) { |
| 136 delegate_ = static_cast<WebDialogWebContentsDelegateViews*>( | |
| 137 GetWebContents()->GetDelegate()); | |
| 138 DCHECK(delegate_); | |
| 114 SetWebContents(GetWebContents()); | 139 SetWebContents(GetWebContents()); |
| 115 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 140 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 141 | |
| 142 web_contents_ = web_contents; | |
| 143 min_size_ = gfx::Size(); | |
|
miu
2014/11/26 00:47:43
In C++, both |min_size_| and |max_size_| are autom
apacible
2014/11/26 17:35:14
Done.
| |
| 144 max_size_ = gfx::Size(); | |
| 145 auto_resize_enabled_ = false; | |
| 116 } | 146 } |
| 147 ConstrainedWebDialogDelegateViewViews( | |
|
miu
2014/11/26 00:47:43
nit: Add newline above this line.
apacible
2014/11/26 17:35:14
Removed one constructor.
| |
| 148 content::BrowserContext* browser_context, | |
| 149 ui::WebDialogDelegate* delegate, | |
| 150 content::WebContents* web_contents, | |
| 151 gfx::Size min_size, | |
| 152 gfx::Size max_size) | |
| 153 : views::WebView(browser_context), | |
|
miu
2014/11/26 00:47:43
Chromium now allows C++11 delegated constructors,
apacible
2014/11/26 17:35:14
Combined both constructors into one.
| |
| 154 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, | |
| 155 web_contents, this)) { | |
| 156 delegate_ = static_cast<WebDialogWebContentsDelegateViews*>( | |
| 157 GetWebContents()->GetDelegate()); | |
| 158 DCHECK(delegate_); | |
| 159 SetWebContents(GetWebContents()); | |
| 160 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | |
| 161 | |
| 162 DCHECK(!min_size.IsEmpty()); | |
| 163 DCHECK(!max_size.IsEmpty()); | |
| 164 SetBoundsForAutoResize(min_size, max_size); | |
| 165 | |
| 166 web_contents_ = web_contents; | |
| 167 } | |
| 117 ~ConstrainedWebDialogDelegateViewViews() override {} | 168 ~ConstrainedWebDialogDelegateViewViews() override {} |
| 118 | 169 |
| 119 // ConstrainedWebDialogDelegate: | 170 // ConstrainedWebDialogDelegate: |
| 120 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { | 171 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { |
| 121 return impl_->GetWebDialogDelegate(); | 172 return impl_->GetWebDialogDelegate(); |
| 122 } | 173 } |
| 123 ui::WebDialogDelegate* GetWebDialogDelegate() override { | 174 ui::WebDialogDelegate* GetWebDialogDelegate() override { |
| 124 return impl_->GetWebDialogDelegate(); | 175 return impl_->GetWebDialogDelegate(); |
| 125 } | 176 } |
| 126 void OnDialogCloseFromWebUI() override { | 177 void OnDialogCloseFromWebUI() override { |
| 127 return impl_->OnDialogCloseFromWebUI(); | 178 return impl_->OnDialogCloseFromWebUI(); |
| 128 } | 179 } |
| 129 void ReleaseWebContentsOnDialogClose() override { | 180 void ReleaseWebContentsOnDialogClose() override { |
| 130 return impl_->ReleaseWebContentsOnDialogClose(); | 181 return impl_->ReleaseWebContentsOnDialogClose(); |
| 131 } | 182 } |
| 132 web_modal::NativeWebContentsModalDialog GetNativeDialog() override { | 183 web_modal::NativeWebContentsModalDialog GetNativeDialog() override { |
| 133 return impl_->GetNativeDialog(); | 184 return impl_->GetNativeDialog(); |
| 134 } | 185 } |
| 135 content::WebContents* GetWebContents() override { | 186 content::WebContents* GetWebContents() override { |
| 136 return impl_->GetWebContents(); | 187 return impl_->GetWebContents(); |
| 137 } | 188 } |
| 189 void EnableAutoResize(const gfx::Size& min_size, const gfx::Size& max_size) | |
|
miu
2014/11/26 00:47:43
This should be a private method. Also, you don't
apacible
2014/11/26 17:35:14
Done.
| |
| 190 override { | |
| 191 content::RenderViewHost* render_view_host = | |
| 192 GetWebContents()->GetRenderViewHost(); | |
| 193 DCHECK(render_view_host); | |
| 194 render_view_host->EnableAutoResize(min_size, max_size); | |
| 195 } | |
| 138 | 196 |
| 139 // views::WidgetDelegate: | 197 // views::WidgetDelegate: |
| 140 views::View* GetInitiallyFocusedView() override { return this; } | 198 views::View* GetInitiallyFocusedView() override { return this; } |
| 141 void WindowClosing() override { | 199 void WindowClosing() override { |
| 142 if (!impl_->closed_via_webui()) | 200 if (!impl_->closed_via_webui()) |
| 143 GetWebDialogDelegate()->OnDialogClosed(std::string()); | 201 GetWebDialogDelegate()->OnDialogClosed(std::string()); |
| 144 } | 202 } |
| 145 views::Widget* GetWidget() override { return View::GetWidget(); } | 203 views::Widget* GetWidget() override { return View::GetWidget(); } |
| 146 const views::Widget* GetWidget() const override { return View::GetWidget(); } | 204 const views::Widget* GetWidget() const override { return View::GetWidget(); } |
| 147 base::string16 GetWindowTitle() const override { | 205 base::string16 GetWindowTitle() const override { |
| 148 return impl_->closed_via_webui() ? base::string16() : | 206 return impl_->closed_via_webui() ? base::string16() : |
| 207 | |
|
miu
2014/11/26 00:47:43
nit: Remove this newline.
apacible
2014/11/26 17:35:14
Done.
| |
| 149 GetWebDialogDelegate()->GetDialogTitle(); | 208 GetWebDialogDelegate()->GetDialogTitle(); |
| 150 } | 209 } |
| 151 views::View* GetContentsView() override { return this; } | 210 views::View* GetContentsView() override { return this; } |
| 152 views::NonClientFrameView* CreateNonClientFrameView( | 211 views::NonClientFrameView* CreateNonClientFrameView( |
| 153 views::Widget* widget) override { | 212 views::Widget* widget) override { |
| 154 return views::DialogDelegate::CreateDialogFrameView(widget); | 213 return views::DialogDelegate::CreateDialogFrameView(widget); |
| 155 } | 214 } |
| 156 bool ShouldShowCloseButton() const override { | 215 bool ShouldShowCloseButton() const override { |
| 157 // No close button if the dialog doesn't want a title bar. | 216 // No close button if the dialog doesn't want a title bar. |
| 158 return impl_->GetWebDialogDelegate()->ShouldShowDialogTitle(); | 217 return impl_->GetWebDialogDelegate()->ShouldShowDialogTitle(); |
| 159 } | 218 } |
| 160 ui::ModalType GetModalType() const override { return ui::MODAL_TYPE_CHILD; } | 219 ui::ModalType GetModalType() const override { return ui::MODAL_TYPE_CHILD; } |
| 161 | 220 |
| 162 // views::WebView: | 221 // views::WebView: |
| 163 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { | 222 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
| 164 // Pressing ESC closes the dialog. | 223 // Pressing ESC closes the dialog. |
| 165 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); | 224 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); |
| 166 GetWidget()->Close(); | 225 GetWidget()->Close(); |
| 167 return true; | 226 return true; |
| 168 } | 227 } |
| 169 gfx::Size GetPreferredSize() const override { | 228 gfx::Size GetPreferredSize() const override { |
| 170 gfx::Size size; | 229 gfx::Size size; |
| 171 if (!impl_->closed_via_webui()) | 230 if (!impl_->closed_via_webui()) { |
| 172 GetWebDialogDelegate()->GetDialogSize(&size); | 231 size = delegate_->GetPreferredSize(); |
|
miu
2014/11/26 00:47:43
If you unwind the code, this looks identical to:
apacible
2014/11/26 17:35:14
Done.
| |
| 232 if (size.IsEmpty()) | |
| 233 GetWebDialogDelegate()->GetDialogSize(&size); | |
| 234 } | |
| 173 return size; | 235 return size; |
| 174 } | 236 } |
| 175 gfx::Size GetMinimumSize() const override { | 237 gfx::Size GetMinimumSize() const override { |
| 176 // Return an empty size so that we can be made smaller. | 238 // Return an empty size so that we can be made smaller. |
| 177 return gfx::Size(); | 239 return gfx::Size(); |
|
miu
2014/11/26 00:47:43
Should this method return min_size_ instead?
Also
apacible
2014/11/26 17:35:14
Done.
Talked offline about functionality of GetMa
| |
| 178 } | 240 } |
| 241 void RenderViewCreated(content::RenderViewHost* render_view_host) override { | |
| 242 if (auto_resize_enabled_) | |
| 243 EnableAutoResize(min_size_, max_size_); | |
| 244 } | |
| 245 void RenderViewHostChanged(content::RenderViewHost* old_host, | |
| 246 content::RenderViewHost* new_host) override { | |
| 247 if (auto_resize_enabled_) | |
| 248 EnableAutoResize(min_size_, max_size_); | |
| 249 } | |
| 250 void DocumentOnLoadCompletedInMainFrame() override { | |
| 251 if (auto_resize_enabled_) { | |
| 252 EnableAutoResize(min_size_, max_size_); | |
| 253 constrained_window::ShowWebModalDialogViews(this, web_contents_); | |
| 254 } | |
| 255 } | |
| 179 | 256 |
| 180 private: | 257 private: |
| 181 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; | 258 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; |
| 182 | 259 |
| 260 content::WebContents* web_contents_; | |
|
miu
2014/11/26 00:47:43
Don't need this. The code can always call the web
apacible
2014/11/26 17:35:13
Talked offline -- keeping web_contents_ but renami
| |
| 261 | |
| 262 gfx::Size min_size_; | |
|
miu
2014/11/26 00:47:43
Use 'const gfx::Size', since these are not modifie
apacible
2014/11/26 17:35:14
Done.
| |
| 263 gfx::Size max_size_; | |
| 264 | |
| 265 bool auto_resize_enabled_; | |
|
miu
2014/11/26 00:47:44
Don't need this extra bool member. The code could
apacible
2014/11/26 17:35:14
Done.
| |
| 266 | |
| 267 WebDialogWebContentsDelegateViews* delegate_; | |
|
miu
2014/11/26 00:47:43
Don't need this one, either. ;-)
apacible
2014/11/26 17:35:15
Done.
| |
| 268 | |
| 269 void SetBoundsForAutoResize(const gfx::Size& min_size, | |
|
miu
2014/11/26 00:47:43
Only the ctor calls this method. Assuming you agr
apacible
2014/11/26 17:35:14
Done.
| |
| 270 const gfx::Size& max_size) { | |
| 271 DCHECK(!min_size.IsEmpty()); | |
| 272 DCHECK(!max_size.IsEmpty()); | |
| 273 min_size_ = min_size; | |
| 274 max_size_ = max_size; | |
| 275 auto_resize_enabled_ = true; | |
| 276 } | |
| 277 | |
| 183 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); | 278 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); |
| 184 }; | 279 }; |
| 185 | 280 |
| 186 } // namespace | 281 } // namespace |
| 187 | 282 |
| 188 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( | 283 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( |
| 189 content::BrowserContext* browser_context, | 284 content::BrowserContext* browser_context, |
| 190 ui::WebDialogDelegate* delegate, | 285 ui::WebDialogDelegate* delegate, |
| 191 content::WebContents* web_contents) { | 286 content::WebContents* web_contents) { |
| 192 ConstrainedWebDialogDelegateViewViews* dialog = | 287 ConstrainedWebDialogDelegateViewViews* dialog = |
| 193 new ConstrainedWebDialogDelegateViewViews( | 288 new ConstrainedWebDialogDelegateViewViews( |
| 194 browser_context, delegate, web_contents); | 289 browser_context, delegate, web_contents); |
| 195 constrained_window::ShowWebModalDialogViews(dialog, web_contents); | 290 constrained_window::ShowWebModalDialogViews(dialog, web_contents); |
| 196 return dialog; | 291 return dialog; |
| 197 } | 292 } |
| 293 | |
| 294 ConstrainedWebDialogDelegate* CreateConstrainedWebDialogWithAutoResize( | |
| 295 content::BrowserContext* browser_context, | |
| 296 ui::WebDialogDelegate* delegate, | |
| 297 content::WebContents* web_contents, | |
| 298 gfx::Size min_size, | |
| 299 gfx::Size max_size) { | |
| 300 ConstrainedWebDialogDelegateViewViews* dialog = | |
| 301 new ConstrainedWebDialogDelegateViewViews( | |
| 302 browser_context, delegate, web_contents, | |
| 303 min_size, max_size); | |
| 304 constrained_window::CreateWebModalDialogViews(dialog, web_contents); | |
|
miu
2014/11/26 00:47:43
Did you mean to call ShowWebModalDialogViews() ins
apacible
2014/11/26 17:35:13
The showing happens later. I will rename the forme
| |
| 305 return dialog; | |
| 306 } | |
| OLD | NEW |