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, | |
| 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 min_size_(min_size), | |
| 130 max_size_(max_size) { | |
| 114 SetWebContents(GetWebContents()); | 131 SetWebContents(GetWebContents()); |
| 115 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 132 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 116 } | 133 |
| 134 initiator_web_contents_ = web_contents; | |
|
miu
2014/11/26 22:17:36
Please put this in the initializer list above.
apacible
2014/11/26 22:42:50
Done.
| |
| 135 } | |
| 117 ~ConstrainedWebDialogDelegateViewViews() override {} | 136 ~ConstrainedWebDialogDelegateViewViews() override {} |
| 118 | 137 |
| 119 // ConstrainedWebDialogDelegate: | 138 // ConstrainedWebDialogDelegate: |
| 120 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { | 139 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { |
| 121 return impl_->GetWebDialogDelegate(); | 140 return impl_->GetWebDialogDelegate(); |
| 122 } | 141 } |
| 123 ui::WebDialogDelegate* GetWebDialogDelegate() override { | 142 ui::WebDialogDelegate* GetWebDialogDelegate() override { |
| 124 return impl_->GetWebDialogDelegate(); | 143 return impl_->GetWebDialogDelegate(); |
| 125 } | 144 } |
| 126 void OnDialogCloseFromWebUI() override { | 145 void OnDialogCloseFromWebUI() override { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 | 180 |
| 162 // views::WebView: | 181 // views::WebView: |
| 163 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { | 182 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
| 164 // Pressing ESC closes the dialog. | 183 // Pressing ESC closes the dialog. |
| 165 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); | 184 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); |
| 166 GetWidget()->Close(); | 185 GetWidget()->Close(); |
| 167 return true; | 186 return true; |
| 168 } | 187 } |
| 169 gfx::Size GetPreferredSize() const override { | 188 gfx::Size GetPreferredSize() const override { |
| 170 gfx::Size size; | 189 gfx::Size size; |
| 171 if (!impl_->closed_via_webui()) | 190 if (!impl_->closed_via_webui()) { |
| 172 GetWebDialogDelegate()->GetDialogSize(&size); | 191 size = WebView::GetPreferredSize(); |
| 192 if (size.IsEmpty()) | |
| 193 GetWebDialogDelegate()->GetDialogSize(&size); | |
| 194 } | |
| 173 return size; | 195 return size; |
| 174 } | 196 } |
| 175 gfx::Size GetMinimumSize() const override { | 197 gfx::Size GetMinimumSize() const override { |
| 176 // Return an empty size so that we can be made smaller. | 198 return min_size_; |
| 177 return gfx::Size(); | 199 } |
| 200 gfx::Size GetMaximumSize() const override { | |
| 201 return !max_size_.IsEmpty() ? max_size_ : WebView::GetMaximumSize(); | |
| 202 } | |
| 203 void RenderViewCreated(content::RenderViewHost* render_view_host) override { | |
| 204 if (!max_size_.IsEmpty()) | |
| 205 EnableAutoResize(); | |
| 206 } | |
| 207 void RenderViewHostChanged(content::RenderViewHost* old_host, | |
| 208 content::RenderViewHost* new_host) override { | |
| 209 if (!max_size_.IsEmpty()) | |
| 210 EnableAutoResize(); | |
| 211 } | |
| 212 void DocumentOnLoadCompletedInMainFrame() override { | |
| 213 if (!max_size_.IsEmpty()) { | |
| 214 EnableAutoResize(); | |
| 215 constrained_window::ShowWebModalDialogViews(this, | |
| 216 initiator_web_contents_); | |
| 217 } | |
| 178 } | 218 } |
| 179 | 219 |
| 180 private: | 220 private: |
| 181 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; | 221 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; |
| 182 | 222 |
| 223 content::WebContents* initiator_web_contents_; | |
|
miu
2014/11/26 22:17:36
Should be:
content::WebContents* const initiato
apacible
2014/11/26 22:42:50
Done.
| |
| 224 | |
| 225 const gfx::Size min_size_; | |
| 226 const gfx::Size max_size_; | |
| 227 | |
| 228 void EnableAutoResize() { | |
| 229 content::RenderViewHost* render_view_host = | |
| 230 GetWebContents()->GetRenderViewHost(); | |
| 231 DCHECK(render_view_host); | |
| 232 render_view_host->EnableAutoResize(min_size_, max_size_); | |
| 233 } | |
| 234 | |
| 183 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); | 235 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); |
| 184 }; | 236 }; |
| 185 | 237 |
| 186 } // namespace | 238 } // namespace |
| 187 | 239 |
| 188 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( | 240 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( |
| 189 content::BrowserContext* browser_context, | 241 content::BrowserContext* browser_context, |
| 190 ui::WebDialogDelegate* delegate, | 242 ui::WebDialogDelegate* delegate, |
| 191 content::WebContents* web_contents) { | 243 content::WebContents* web_contents) { |
| 192 ConstrainedWebDialogDelegateViewViews* dialog = | 244 ConstrainedWebDialogDelegateViewViews* dialog = |
| 193 new ConstrainedWebDialogDelegateViewViews( | 245 new ConstrainedWebDialogDelegateViewViews( |
| 194 browser_context, delegate, web_contents); | 246 browser_context, delegate, web_contents, |
| 247 gfx::Size(), gfx::Size()); | |
| 195 constrained_window::ShowWebModalDialogViews(dialog, web_contents); | 248 constrained_window::ShowWebModalDialogViews(dialog, web_contents); |
| 196 return dialog; | 249 return dialog; |
| 197 } | 250 } |
| 251 | |
| 252 ConstrainedWebDialogDelegate* CreateConstrainedWebDialogWithAutoResize( | |
| 253 content::BrowserContext* browser_context, | |
| 254 ui::WebDialogDelegate* delegate, | |
| 255 content::WebContents* web_contents, | |
| 256 gfx::Size min_size, | |
| 257 gfx::Size max_size) { | |
| 258 DCHECK(!min_size.IsEmpty()); | |
| 259 DCHECK(!max_size.IsEmpty()); | |
| 260 ConstrainedWebDialogDelegateViewViews* dialog = | |
| 261 new ConstrainedWebDialogDelegateViewViews( | |
| 262 browser_context, delegate, web_contents, | |
| 263 min_size, max_size); | |
| 264 constrained_window::CreateWebModalDialogViews(dialog, web_contents); | |
| 265 return dialog; | |
| 266 } | |
| OLD | NEW |