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/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "chrome/browser/ui/browser_finder.h" | 10 #include "chrome/browser/ui/browser_finder.h" |
| 11 #include "chrome/browser/ui/browser_window.h" | 11 #include "chrome/browser/ui/browser_window.h" |
| 12 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" | 12 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h" |
| 13 #include "components/constrained_window/constrained_window_views.h" | 13 #include "components/constrained_window/constrained_window_views.h" |
| 14 #include "components/web_modal/web_contents_modal_dialog_manager.h" | 14 #include "components/web_modal/web_contents_modal_dialog_manager.h" |
| 15 #include "content/public/browser/native_web_keyboard_event.h" | 15 #include "content/public/browser/native_web_keyboard_event.h" |
| 16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" | 18 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h" |
| 19 #include "ui/views/controls/webview/webview.h" | 19 #include "ui/views/controls/webview/webview.h" |
| 20 #include "ui/views/view.h" | 20 #include "ui/views/view.h" |
| 21 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
| 22 #include "ui/views/window/dialog_delegate.h" | 22 #include "ui/views/window/dialog_delegate.h" |
| 23 #include "ui/web_dialogs/web_dialog_delegate.h" | 23 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 24 #include "ui/web_dialogs/web_dialog_ui.h" | 24 #include "ui/web_dialogs/web_dialog_ui.h" |
| 25 | 25 |
| 26 #if defined(OS_WIN) | |
| 27 #include "ui/base/win/shell.h" | |
| 28 #endif | |
| 29 | |
| 26 namespace { | 30 namespace { |
| 27 | 31 |
| 28 // WebContentsObserver that tracks the lifetime of the WebContents to avoid | 32 // WebContentsObserver that tracks the lifetime of the WebContents to avoid |
| 29 // potential use after destruction. | 33 // potential use after destruction. |
| 30 class InitiatorWebContentsObserver | 34 class InitiatorWebContentsObserver |
| 31 : public content::WebContentsObserver { | 35 : public content::WebContentsObserver { |
| 32 public: | 36 public: |
| 33 explicit InitiatorWebContentsObserver(content::WebContents* web_contents) | 37 explicit InitiatorWebContentsObserver(content::WebContents* web_contents) |
| 34 : content::WebContentsObserver(web_contents) { | 38 : content::WebContentsObserver(web_contents) {} |
| 35 } | |
| 36 | 39 |
| 37 private: | 40 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(InitiatorWebContentsObserver); | 41 DISALLOW_COPY_AND_ASSIGN(InitiatorWebContentsObserver); |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 class WebDialogWebContentsDelegateViews | 44 class WebDialogWebContentsDelegateViews |
| 42 : public ui::WebDialogWebContentsDelegate { | 45 : public ui::WebDialogWebContentsDelegate { |
| 43 public: | 46 public: |
| 44 WebDialogWebContentsDelegateViews(content::BrowserContext* browser_context, | 47 WebDialogWebContentsDelegateViews(content::BrowserContext* browser_context, |
| 45 InitiatorWebContentsObserver* observer, | 48 InitiatorWebContentsObserver* observer, |
| 46 views::WebView* web_view) | 49 views::WebView* web_view) |
| 47 : ui::WebDialogWebContentsDelegate(browser_context, | 50 : ui::WebDialogWebContentsDelegate(browser_context, |
| 48 new ChromeWebContentsHandler()), | 51 new ChromeWebContentsHandler()), |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 class ConstrainedWebDialogDelegateViewViews | 140 class ConstrainedWebDialogDelegateViewViews |
| 138 : public views::WebView, | 141 : public views::WebView, |
| 139 public ConstrainedWebDialogDelegate, | 142 public ConstrainedWebDialogDelegate, |
| 140 public views::WidgetDelegate { | 143 public views::WidgetDelegate { |
| 141 public: | 144 public: |
| 142 ConstrainedWebDialogDelegateViewViews( | 145 ConstrainedWebDialogDelegateViewViews( |
| 143 content::BrowserContext* browser_context, | 146 content::BrowserContext* browser_context, |
| 144 ui::WebDialogDelegate* delegate, | 147 ui::WebDialogDelegate* delegate, |
| 145 content::WebContents* web_contents, | 148 content::WebContents* web_contents, |
| 146 const gfx::Size& min_size, | 149 const gfx::Size& min_size, |
| 147 const gfx::Size& max_size) | 150 const gfx::Size& max_size, |
| 151 bool is_nonclipped) | |
| 148 : views::WebView(browser_context), | 152 : views::WebView(browser_context), |
| 149 initiator_observer_(web_contents), | 153 initiator_observer_(web_contents), |
| 150 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, | 154 impl_(new ConstrainedWebDialogDelegateViews(browser_context, |
| 155 delegate, | |
| 151 &initiator_observer_, | 156 &initiator_observer_, |
| 152 this)), | 157 this)), |
| 153 min_size_(min_size), | 158 min_size_(min_size), |
| 154 max_size_(max_size) { | 159 max_size_(max_size), |
| 160 is_nonclipped_(is_nonclipped) { | |
| 155 SetWebContents(GetWebContents()); | 161 SetWebContents(GetWebContents()); |
| 156 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 162 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 157 if (!max_size_.IsEmpty()) | 163 if (!max_size_.IsEmpty()) |
| 158 EnableAutoResize(); | 164 EnableAutoResize(); |
| 159 } | 165 } |
| 160 ~ConstrainedWebDialogDelegateViewViews() override {} | 166 ~ConstrainedWebDialogDelegateViewViews() override {} |
| 161 | 167 |
| 162 // ConstrainedWebDialogDelegate: | 168 // ConstrainedWebDialogDelegate: |
| 163 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { | 169 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { |
| 164 return impl_->GetWebDialogDelegate(); | 170 return impl_->GetWebDialogDelegate(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 if (!max_size_.IsEmpty()) | 237 if (!max_size_.IsEmpty()) |
| 232 EnableAutoResize(); | 238 EnableAutoResize(); |
| 233 } | 239 } |
| 234 void RenderViewHostChanged(content::RenderViewHost* old_host, | 240 void RenderViewHostChanged(content::RenderViewHost* old_host, |
| 235 content::RenderViewHost* new_host) override { | 241 content::RenderViewHost* new_host) override { |
| 236 if (!max_size_.IsEmpty()) | 242 if (!max_size_.IsEmpty()) |
| 237 EnableAutoResize(); | 243 EnableAutoResize(); |
| 238 } | 244 } |
| 239 void DocumentOnLoadCompletedInMainFrame() override { | 245 void DocumentOnLoadCompletedInMainFrame() override { |
| 240 if (!max_size_.IsEmpty() && initiator_observer_.web_contents()) { | 246 if (!max_size_.IsEmpty() && initiator_observer_.web_contents()) { |
| 241 web_modal::WebContentsModalDialogManager::FromWebContents( | 247 if (is_nonclipped_) { |
| 242 initiator_observer_.web_contents()) | 248 web_modal::WebContentsModalDialogManager::FromWebContents( |
|
Lei Zhang
2016/08/18 20:30:57
Combine the same code in the if and else branch fo
rbpotter
2016/08/19 01:59:09
Non applicable at this point, this code was refact
| |
| 243 ->ShowModalDialog(GetWidget()->GetNativeWindow()); | 249 initiator_observer_.web_contents()) |
| 250 ->ShowNonClippedModalDialog(GetWidget()->GetNativeWindow()); | |
| 251 } else { | |
| 252 web_modal::WebContentsModalDialogManager::FromWebContents( | |
| 253 initiator_observer_.web_contents()) | |
| 254 ->ShowModalDialog(GetWidget()->GetNativeWindow()); | |
| 255 } | |
| 244 } | 256 } |
| 245 } | 257 } |
| 246 | 258 |
| 247 private: | 259 private: |
| 248 void EnableAutoResize() { | 260 void EnableAutoResize() { |
| 249 content::RenderViewHost* render_view_host = | 261 content::RenderViewHost* render_view_host = |
| 250 GetWebContents()->GetRenderViewHost(); | 262 GetWebContents()->GetRenderViewHost(); |
| 251 render_view_host->EnableAutoResize(min_size_, max_size_); | 263 render_view_host->EnableAutoResize(min_size_, max_size_); |
| 252 } | 264 } |
| 253 | 265 |
| 254 InitiatorWebContentsObserver initiator_observer_; | 266 InitiatorWebContentsObserver initiator_observer_; |
| 255 | 267 |
| 256 std::unique_ptr<ConstrainedWebDialogDelegateViews> impl_; | 268 std::unique_ptr<ConstrainedWebDialogDelegateViews> impl_; |
| 257 | 269 |
| 258 // Minimum and maximum sizes to determine dialog bounds for auto-resizing. | 270 // Minimum and maximum sizes to determine dialog bounds for auto-resizing. |
| 259 const gfx::Size min_size_; | 271 const gfx::Size min_size_; |
| 260 const gfx::Size max_size_; | 272 const gfx::Size max_size_; |
| 261 | 273 |
| 274 // If true, dialog has bounds that are not constrained by the parent window's | |
| 275 // bounds, so can extend outside of the parent window when resized | |
|
Lei Zhang
2016/08/18 20:30:57
s/so/so it/
rbpotter
2016/08/19 01:59:09
Done.
| |
| 276 const bool is_nonclipped_; | |
| 277 | |
| 262 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); | 278 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); |
| 263 }; | 279 }; |
| 264 | 280 |
| 265 } // namespace | 281 } // namespace |
| 266 | 282 |
| 267 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( | 283 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( |
| 268 content::BrowserContext* browser_context, | 284 content::BrowserContext* browser_context, |
| 269 ui::WebDialogDelegate* delegate, | 285 ui::WebDialogDelegate* delegate, |
| 270 content::WebContents* web_contents) { | 286 content::WebContents* web_contents) { |
| 271 ConstrainedWebDialogDelegateViewViews* dialog = | 287 ConstrainedWebDialogDelegateViewViews* dialog = |
| 272 new ConstrainedWebDialogDelegateViewViews( | 288 new ConstrainedWebDialogDelegateViewViews(browser_context, delegate, |
| 273 browser_context, delegate, web_contents, | 289 web_contents, gfx::Size(), |
| 274 gfx::Size(), gfx::Size()); | 290 gfx::Size(), false); |
| 275 constrained_window::ShowWebModalDialogViews(dialog, web_contents); | 291 constrained_window::ShowWebModalDialogViews(dialog, web_contents); |
| 276 return dialog; | 292 return dialog; |
| 277 } | 293 } |
| 278 | 294 |
| 295 ConstrainedWebDialogDelegate* ShowNonClippedConstrainedWebDialog( | |
| 296 content::BrowserContext* browser_context, | |
| 297 ui::WebDialogDelegate* delegate, | |
| 298 content::WebContents* web_contents) { | |
| 299 ConstrainedWebDialogDelegateViewViews* dialog = | |
| 300 new ConstrainedWebDialogDelegateViewViews(browser_context, delegate, | |
| 301 web_contents, gfx::Size(), | |
| 302 gfx::Size(), true); | |
| 303 constrained_window::ShowNonClippedWebModalDialogViews(dialog, web_contents); | |
| 304 return dialog; | |
| 305 } | |
| 306 | |
| 279 ConstrainedWebDialogDelegate* ShowConstrainedWebDialogWithAutoResize( | 307 ConstrainedWebDialogDelegate* ShowConstrainedWebDialogWithAutoResize( |
| 280 content::BrowserContext* browser_context, | 308 content::BrowserContext* browser_context, |
| 281 ui::WebDialogDelegate* delegate, | 309 ui::WebDialogDelegate* delegate, |
| 282 content::WebContents* web_contents, | 310 content::WebContents* web_contents, |
| 283 const gfx::Size& min_size, | 311 const gfx::Size& min_size, |
| 284 const gfx::Size& max_size) { | 312 const gfx::Size& max_size) { |
| 285 DCHECK(!min_size.IsEmpty()); | 313 DCHECK(!min_size.IsEmpty()); |
| 286 DCHECK(!max_size.IsEmpty()); | 314 DCHECK(!max_size.IsEmpty()); |
| 287 ConstrainedWebDialogDelegateViewViews* dialog = | 315 ConstrainedWebDialogDelegateViewViews* dialog = |
| 288 new ConstrainedWebDialogDelegateViewViews( | 316 new ConstrainedWebDialogDelegateViewViews( |
| 289 browser_context, delegate, web_contents, | 317 browser_context, delegate, web_contents, min_size, max_size, false); |
| 290 min_size, max_size); | |
| 291 constrained_window::CreateWebModalDialogViews(dialog, web_contents); | 318 constrained_window::CreateWebModalDialogViews(dialog, web_contents); |
| 292 return dialog; | 319 return dialog; |
| 293 } | 320 } |
| OLD | NEW |