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 |
| 26 class InitiatorWebContentsObserver | |
|
msw
2014/12/04 19:27:19
Why do you need to subclass content::WebContentsOb
apacible
2014/12/09 17:07:58
The WebContentsObserver constructor is protected.
| |
| 27 : public content::WebContentsObserver { | |
| 28 public: | |
| 29 explicit InitiatorWebContentsObserver(content::WebContents* web_contents) | |
| 30 : content::WebContentsObserver(web_contents) { | |
| 31 } | |
| 32 }; | |
|
msw
2014/12/04 19:27:19
nit: DISALLOW_COPY_AND_ASSIGN
apacible
2014/12/09 17:07:58
Done.
| |
| 33 | |
| 23 class WebDialogWebContentsDelegateViews | 34 class WebDialogWebContentsDelegateViews |
| 24 : public ui::WebDialogWebContentsDelegate { | 35 : public ui::WebDialogWebContentsDelegate { |
| 25 public: | 36 public: |
| 26 WebDialogWebContentsDelegateViews(content::BrowserContext* browser_context, | 37 WebDialogWebContentsDelegateViews(content::BrowserContext* browser_context, |
| 27 content::WebContents* initiator, | 38 InitiatorWebContentsObserver* observer, |
| 28 views::WebView* web_view) | 39 views::WebView* web_view) |
| 29 : ui::WebDialogWebContentsDelegate(browser_context, | 40 : ui::WebDialogWebContentsDelegate(browser_context, |
| 30 new ChromeWebContentsHandler()), | 41 new ChromeWebContentsHandler()), |
| 31 initiator_(initiator), | 42 observer_(observer), |
| 32 web_view_(web_view) { | 43 web_view_(web_view) { |
| 33 } | 44 } |
| 34 ~WebDialogWebContentsDelegateViews() override {} | 45 ~WebDialogWebContentsDelegateViews() override {} |
| 35 | 46 |
| 36 // ui::WebDialogWebContentsDelegate: | 47 // ui::WebDialogWebContentsDelegate: |
| 37 void WebContentsFocused(content::WebContents* contents) override { | 48 void WebContentsFocused(content::WebContents* contents) override { |
| 38 // Ensure the WebView is focused when its WebContents is focused. | 49 // Ensure the WebView is focused when its WebContents is focused. |
| 39 web_view_->RequestFocus(); | 50 web_view_->RequestFocus(); |
| 40 } | 51 } |
| 41 void HandleKeyboardEvent( | 52 void HandleKeyboardEvent( |
| 42 content::WebContents* source, | 53 content::WebContents* source, |
| 43 const content::NativeWebKeyboardEvent& event) override { | 54 const content::NativeWebKeyboardEvent& event) override { |
| 44 // Forward shortcut keys in dialog to our initiator's delegate. | 55 // Forward shortcut keys in dialog to our initiator's delegate. |
| 45 // http://crbug.com/104586 | 56 // http://crbug.com/104586 |
| 46 // Disabled on Mac due to http://crbug.com/112173 | 57 // Disabled on Mac due to http://crbug.com/112173 |
| 47 #if !defined(OS_MACOSX) | 58 #if !defined(OS_MACOSX) |
| 48 auto delegate = initiator_->GetDelegate(); | 59 if (!observer_->web_contents()) |
| 60 return; | |
| 61 | |
| 62 auto delegate = observer_->web_contents()->GetDelegate(); | |
| 49 if (!delegate) | 63 if (!delegate) |
| 50 return; | 64 return; |
| 51 delegate->HandleKeyboardEvent(initiator_, event); | 65 delegate->HandleKeyboardEvent(observer_->web_contents(), event); |
| 52 #endif | 66 #endif |
| 53 } | 67 } |
| 54 | 68 |
| 69 void ResizeDueToAutoResize(content::WebContents* source, | |
| 70 const gfx::Size& preferred_size) override { | |
| 71 if (!observer_->web_contents()) | |
| 72 return; | |
| 73 | |
| 74 web_view_->SetPreferredSize(preferred_size); | |
| 75 | |
| 76 constrained_window::UpdateWebContentsModalDialogPosition( | |
| 77 web_view_->GetWidget(), | |
| 78 web_modal::WebContentsModalDialogManager::FromWebContents( | |
| 79 observer_->web_contents())->delegate()-> | |
| 80 GetWebContentsModalDialogHost()); | |
| 81 } | |
| 82 | |
| 55 private: | 83 private: |
| 56 content::WebContents* initiator_; | 84 InitiatorWebContentsObserver* const observer_; |
|
msw
2014/12/04 19:27:19
nit: Use a more descriptive name, like |initiator_
apacible
2014/12/09 17:07:58
Done.
| |
| 57 views::WebView* web_view_; | 85 views::WebView* web_view_; |
| 58 | 86 |
| 59 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateViews); | 87 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateViews); |
| 60 }; | 88 }; |
| 61 | 89 |
| 62 class ConstrainedWebDialogDelegateViews | 90 class ConstrainedWebDialogDelegateViews |
| 63 : public ConstrainedWebDialogDelegateBase { | 91 : public ConstrainedWebDialogDelegateBase { |
| 64 public: | 92 public: |
| 65 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, | 93 ConstrainedWebDialogDelegateViews(content::BrowserContext* context, |
| 66 ui::WebDialogDelegate* delegate, | 94 ui::WebDialogDelegate* delegate, |
| 67 content::WebContents* web_contents, | 95 InitiatorWebContentsObserver* observer, |
| 68 views::WebView* view) | 96 views::WebView* view) |
| 69 : ConstrainedWebDialogDelegateBase(context, delegate, | 97 : ConstrainedWebDialogDelegateBase(context, delegate, |
| 70 new WebDialogWebContentsDelegateViews(context, web_contents, view)), | 98 new WebDialogWebContentsDelegateViews(context, observer, view)), |
| 71 view_(view) {} | 99 view_(view) {} |
| 72 | 100 |
| 73 ~ConstrainedWebDialogDelegateViews() override {} | 101 ~ConstrainedWebDialogDelegateViews() override {} |
| 74 | 102 |
| 75 // ui::WebDialogWebContentsDelegate: | 103 // ui::WebDialogWebContentsDelegate: |
| 76 void CloseContents(content::WebContents* source) override { | 104 void CloseContents(content::WebContents* source) override { |
| 77 view_->GetWidget()->Close(); | 105 view_->GetWidget()->Close(); |
| 78 } | 106 } |
| 79 | 107 |
| 80 // contents::WebContentsDelegate: | 108 // contents::WebContentsDelegate: |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 100 }; | 128 }; |
| 101 | 129 |
| 102 class ConstrainedWebDialogDelegateViewViews | 130 class ConstrainedWebDialogDelegateViewViews |
| 103 : public views::WebView, | 131 : public views::WebView, |
| 104 public ConstrainedWebDialogDelegate, | 132 public ConstrainedWebDialogDelegate, |
| 105 public views::WidgetDelegate { | 133 public views::WidgetDelegate { |
| 106 public: | 134 public: |
| 107 ConstrainedWebDialogDelegateViewViews( | 135 ConstrainedWebDialogDelegateViewViews( |
| 108 content::BrowserContext* browser_context, | 136 content::BrowserContext* browser_context, |
| 109 ui::WebDialogDelegate* delegate, | 137 ui::WebDialogDelegate* delegate, |
| 110 content::WebContents* web_contents) | 138 content::WebContents* web_contents, |
| 139 const gfx::Size& min_size, | |
| 140 const gfx::Size& max_size) | |
| 111 : views::WebView(browser_context), | 141 : views::WebView(browser_context), |
| 142 observer_(web_contents), | |
| 112 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, | 143 impl_(new ConstrainedWebDialogDelegateViews(browser_context, delegate, |
| 113 web_contents, this)) { | 144 &observer_, this)), |
| 145 min_size_(min_size), | |
| 146 max_size_(max_size) { | |
| 114 SetWebContents(GetWebContents()); | 147 SetWebContents(GetWebContents()); |
| 115 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | 148 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 116 } | 149 } |
|
msw
2014/12/04 19:27:19
Remove this incorrect indentation change.
apacible
2014/12/09 17:07:58
Done.
| |
| 117 ~ConstrainedWebDialogDelegateViewViews() override {} | 150 ~ConstrainedWebDialogDelegateViewViews() override {} |
| 118 | 151 |
| 119 // ConstrainedWebDialogDelegate: | 152 // ConstrainedWebDialogDelegate: |
| 120 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { | 153 const ui::WebDialogDelegate* GetWebDialogDelegate() const override { |
| 121 return impl_->GetWebDialogDelegate(); | 154 return impl_->GetWebDialogDelegate(); |
| 122 } | 155 } |
| 123 ui::WebDialogDelegate* GetWebDialogDelegate() override { | 156 ui::WebDialogDelegate* GetWebDialogDelegate() override { |
| 124 return impl_->GetWebDialogDelegate(); | 157 return impl_->GetWebDialogDelegate(); |
| 125 } | 158 } |
| 126 void OnDialogCloseFromWebUI() override { | 159 void OnDialogCloseFromWebUI() override { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 | 194 |
| 162 // views::WebView: | 195 // views::WebView: |
| 163 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { | 196 bool AcceleratorPressed(const ui::Accelerator& accelerator) override { |
| 164 // Pressing ESC closes the dialog. | 197 // Pressing ESC closes the dialog. |
| 165 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); | 198 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); |
| 166 GetWidget()->Close(); | 199 GetWidget()->Close(); |
| 167 return true; | 200 return true; |
| 168 } | 201 } |
| 169 gfx::Size GetPreferredSize() const override { | 202 gfx::Size GetPreferredSize() const override { |
| 170 gfx::Size size; | 203 gfx::Size size; |
| 171 if (!impl_->closed_via_webui()) | 204 if (!impl_->closed_via_webui()) { |
| 172 GetWebDialogDelegate()->GetDialogSize(&size); | 205 size = WebView::GetPreferredSize(); |
|
msw
2014/12/04 19:27:19
I'm worried that this might be conflating the size
apacible
2014/12/09 17:07:58
Done.
| |
| 206 if (size.IsEmpty()) | |
| 207 GetWebDialogDelegate()->GetDialogSize(&size); | |
| 208 } | |
| 173 return size; | 209 return size; |
| 174 } | 210 } |
| 175 gfx::Size GetMinimumSize() const override { | 211 gfx::Size GetMinimumSize() const override { |
| 176 // Return an empty size so that we can be made smaller. | 212 return min_size_; |
| 177 return gfx::Size(); | 213 } |
| 214 gfx::Size GetMaximumSize() const override { | |
| 215 return !max_size_.IsEmpty() ? max_size_ : WebView::GetMaximumSize(); | |
| 216 } | |
| 217 void RenderViewCreated(content::RenderViewHost* render_view_host) override { | |
| 218 if (!max_size_.IsEmpty()) | |
| 219 EnableAutoResize(); | |
| 220 } | |
| 221 void RenderViewHostChanged(content::RenderViewHost* old_host, | |
| 222 content::RenderViewHost* new_host) override { | |
| 223 if (!max_size_.IsEmpty()) | |
| 224 EnableAutoResize(); | |
| 225 } | |
| 226 void DocumentOnLoadCompletedInMainFrame() override { | |
| 227 if (!max_size_.IsEmpty()) { | |
| 228 EnableAutoResize(); | |
| 229 if (observer_.web_contents()) | |
| 230 constrained_window::ShowWebModalDialogViews(this, | |
| 231 observer_.web_contents()); | |
| 232 } | |
| 178 } | 233 } |
| 179 | 234 |
| 180 private: | 235 private: |
| 236 void EnableAutoResize() { | |
| 237 content::RenderViewHost* render_view_host = | |
| 238 GetWebContents()->GetRenderViewHost(); | |
| 239 render_view_host->EnableAutoResize(min_size_, max_size_); | |
| 240 } | |
| 241 | |
| 242 InitiatorWebContentsObserver observer_; | |
|
msw
2014/12/04 19:27:19
ditto nit: Use a more descriptive name, like |init
apacible
2014/12/09 17:07:58
Done.
| |
| 243 | |
| 181 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; | 244 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; |
| 182 | 245 |
| 246 const gfx::Size min_size_; | |
| 247 const gfx::Size max_size_; | |
| 248 | |
| 183 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); | 249 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); |
| 184 }; | 250 }; |
| 185 | 251 |
| 186 } // namespace | 252 } // namespace |
| 187 | 253 |
| 188 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( | 254 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( |
| 189 content::BrowserContext* browser_context, | 255 content::BrowserContext* browser_context, |
| 190 ui::WebDialogDelegate* delegate, | 256 ui::WebDialogDelegate* delegate, |
| 191 content::WebContents* web_contents) { | 257 content::WebContents* web_contents) { |
| 192 ConstrainedWebDialogDelegateViewViews* dialog = | 258 ConstrainedWebDialogDelegateViewViews* dialog = |
| 193 new ConstrainedWebDialogDelegateViewViews( | 259 new ConstrainedWebDialogDelegateViewViews( |
| 194 browser_context, delegate, web_contents); | 260 browser_context, delegate, web_contents, |
| 261 gfx::Size(), gfx::Size()); | |
| 195 constrained_window::ShowWebModalDialogViews(dialog, web_contents); | 262 constrained_window::ShowWebModalDialogViews(dialog, web_contents); |
| 196 return dialog; | 263 return dialog; |
| 197 } | 264 } |
| 265 | |
| 266 ConstrainedWebDialogDelegate* CreateConstrainedWebDialogWithAutoResize( | |
| 267 content::BrowserContext* browser_context, | |
| 268 ui::WebDialogDelegate* delegate, | |
| 269 content::WebContents* web_contents, | |
| 270 const gfx::Size& min_size, | |
| 271 const gfx::Size& max_size) { | |
| 272 DCHECK(!min_size.IsEmpty()); | |
| 273 DCHECK(!max_size.IsEmpty()); | |
| 274 ConstrainedWebDialogDelegateViewViews* dialog = | |
| 275 new ConstrainedWebDialogDelegateViewViews( | |
| 276 browser_context, delegate, web_contents, | |
| 277 min_size, max_size); | |
| 278 constrained_window::CreateWebModalDialogViews(dialog, web_contents); | |
| 279 return dialog; | |
| 280 } | |
| OLD | NEW |