| 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/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 9 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 10 #include "chrome/browser/ui/views/constrained_window_views.h" | 10 #include "chrome/browser/ui/views/constrained_window_views.h" |
| 11 #include "chrome/browser/ui/views/unhandled_keyboard_event_handler.h" |
| 12 #include "content/public/browser/native_web_keyboard_event.h" |
| 11 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 12 #include "ui/gfx/size.h" | 14 #include "ui/gfx/size.h" |
| 13 #include "ui/views/controls/webview/webview.h" | 15 #include "ui/views/controls/webview/webview.h" |
| 14 #include "ui/views/view.h" | 16 #include "ui/views/view.h" |
| 15 #include "ui/views/widget/widget_delegate.h" | 17 #include "ui/views/widget/widget_delegate.h" |
| 16 #include "ui/web_dialogs/web_dialog_delegate.h" | 18 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 17 #include "ui/web_dialogs/web_dialog_ui.h" | 19 #include "ui/web_dialogs/web_dialog_ui.h" |
| 18 | 20 |
| 19 using content::WebContents; | 21 using content::WebContents; |
| 20 using ui::ConstrainedWebDialogDelegate; | 22 using ui::ConstrainedWebDialogDelegate; |
| 21 using ui::ConstrainedWebDialogUI; | 23 using ui::ConstrainedWebDialogUI; |
| 22 using ui::WebDialogDelegate; | 24 using ui::WebDialogDelegate; |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 class ConstrainedWebDialogDelegateViews | 28 class ConstrainedWebDialogDelegateViews |
| 27 : public ConstrainedWebDialogDelegateBase { | 29 : public ConstrainedWebDialogDelegateBase { |
| 28 public: | 30 public: |
| 29 ConstrainedWebDialogDelegateViews( | 31 ConstrainedWebDialogDelegateViews( |
| 30 Profile* profile, | 32 Profile* profile, |
| 31 WebDialogDelegate* delegate, | 33 WebDialogDelegate* delegate, |
| 32 WebDialogWebContentsDelegate* tab_delegate) | 34 WebDialogWebContentsDelegate* tab_delegate, |
| 33 : ConstrainedWebDialogDelegateBase(profile, delegate, tab_delegate) { | 35 views::WebView* view) |
| 36 : ConstrainedWebDialogDelegateBase(profile, delegate, tab_delegate), |
| 37 view_(view) { |
| 34 WebContents* web_contents = tab()->web_contents(); | 38 WebContents* web_contents = tab()->web_contents(); |
| 35 if (tab_delegate) { | 39 if (tab_delegate) { |
| 36 set_override_tab_delegate(tab_delegate); | 40 set_override_tab_delegate(tab_delegate); |
| 37 web_contents->SetDelegate(tab_delegate); | 41 web_contents->SetDelegate(tab_delegate); |
| 38 } else { | 42 } else { |
| 39 web_contents->SetDelegate(this); | 43 web_contents->SetDelegate(this); |
| 40 } | 44 } |
| 41 } | 45 } |
| 42 | 46 |
| 43 virtual ~ConstrainedWebDialogDelegateViews() {} | 47 virtual ~ConstrainedWebDialogDelegateViews() {} |
| 44 | 48 |
| 45 // WebDialogWebContentsDelegate interface. | 49 // WebDialogWebContentsDelegate interface. |
| 46 virtual void CloseContents(WebContents* source) OVERRIDE { | 50 virtual void CloseContents(WebContents* source) OVERRIDE { |
| 47 window()->CloseConstrainedWindow(); | 51 window()->CloseConstrainedWindow(); |
| 48 } | 52 } |
| 49 | 53 |
| 54 // contents::WebContentsDelegate |
| 55 virtual void HandleKeyboardEvent( |
| 56 const content::NativeWebKeyboardEvent& event) OVERRIDE { |
| 57 unhandled_keyboard_event_handler_.HandleKeyboardEvent( |
| 58 event, view_->GetFocusManager()); |
| 59 } |
| 60 |
| 50 private: | 61 private: |
| 62 // Converts keyboard events on the WebContents to accelerators. |
| 63 UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_; |
| 64 |
| 65 views::WebView* view_; |
| 66 |
| 51 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViews); | 67 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViews); |
| 52 }; | 68 }; |
| 53 | 69 |
| 54 } // namespace | 70 } // namespace |
| 55 | 71 |
| 56 class ConstrainedWebDialogDelegateViewViews | 72 class ConstrainedWebDialogDelegateViewViews |
| 57 : public views::WebView, | 73 : public views::WebView, |
| 58 public ConstrainedWebDialogDelegate, | 74 public ConstrainedWebDialogDelegate, |
| 59 public views::WidgetDelegate { | 75 public views::WidgetDelegate { |
| 60 public: | 76 public: |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 122 } |
| 107 virtual string16 GetWindowTitle() const OVERRIDE { | 123 virtual string16 GetWindowTitle() const OVERRIDE { |
| 108 return impl_->closed_via_webui() ? string16() : | 124 return impl_->closed_via_webui() ? string16() : |
| 109 GetWebDialogDelegate()->GetDialogTitle(); | 125 GetWebDialogDelegate()->GetDialogTitle(); |
| 110 } | 126 } |
| 111 virtual views::View* GetContentsView() OVERRIDE { | 127 virtual views::View* GetContentsView() OVERRIDE { |
| 112 return this; | 128 return this; |
| 113 } | 129 } |
| 114 | 130 |
| 115 // views::WebView overrides. | 131 // views::WebView overrides. |
| 132 virtual bool AcceleratorPressed( |
| 133 const ui::Accelerator& accelerator) OVERRIDE { |
| 134 // Pressing ESC closes the dialog. |
| 135 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); |
| 136 window()->CloseConstrainedWindow(); |
| 137 return true; |
| 138 } |
| 116 virtual gfx::Size GetPreferredSize() OVERRIDE { | 139 virtual gfx::Size GetPreferredSize() OVERRIDE { |
| 117 gfx::Size size; | 140 gfx::Size size; |
| 118 if (!impl_->closed_via_webui()) | 141 if (!impl_->closed_via_webui()) |
| 119 GetWebDialogDelegate()->GetDialogSize(&size); | 142 GetWebDialogDelegate()->GetDialogSize(&size); |
| 120 return size; | 143 return size; |
| 121 } | 144 } |
| 122 | 145 |
| 123 private: | 146 private: |
| 124 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; | 147 scoped_ptr<ConstrainedWebDialogDelegateViews> impl_; |
| 125 | 148 |
| 126 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); | 149 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewViews); |
| 127 }; | 150 }; |
| 128 | 151 |
| 129 ConstrainedWebDialogDelegateViewViews::ConstrainedWebDialogDelegateViewViews( | 152 ConstrainedWebDialogDelegateViewViews::ConstrainedWebDialogDelegateViewViews( |
| 130 Profile* profile, | 153 Profile* profile, |
| 131 WebDialogDelegate* delegate, | 154 WebDialogDelegate* delegate, |
| 132 WebDialogWebContentsDelegate* tab_delegate) | 155 WebDialogWebContentsDelegate* tab_delegate) |
| 133 : views::WebView(profile), | 156 : views::WebView(profile), |
| 134 impl_(new ConstrainedWebDialogDelegateViews(profile, | 157 impl_(new ConstrainedWebDialogDelegateViews(profile, |
| 135 delegate, | 158 delegate, |
| 136 tab_delegate)) { | 159 tab_delegate, |
| 160 this)) { |
| 137 SetWebContents(tab()->web_contents()); | 161 SetWebContents(tab()->web_contents()); |
| 162 |
| 163 // Pressing ESC closes the dialog. |
| 164 set_allow_accelerators(true); |
| 165 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 138 } | 166 } |
| 139 | 167 |
| 140 ConstrainedWebDialogDelegateViewViews::~ConstrainedWebDialogDelegateViewViews()
{ | 168 ConstrainedWebDialogDelegateViewViews::~ConstrainedWebDialogDelegateViewViews()
{ |
| 141 } | 169 } |
| 142 | 170 |
| 143 ConstrainedWebDialogDelegate* ui::CreateConstrainedWebDialog( | 171 ConstrainedWebDialogDelegate* ui::CreateConstrainedWebDialog( |
| 144 Profile* profile, | 172 Profile* profile, |
| 145 WebDialogDelegate* delegate, | 173 WebDialogDelegate* delegate, |
| 146 WebDialogWebContentsDelegate* tab_delegate, | 174 WebDialogWebContentsDelegate* tab_delegate, |
| 147 TabContents* container) { | 175 TabContents* container) { |
| 148 ConstrainedWebDialogDelegateViewViews* constrained_delegate = | 176 ConstrainedWebDialogDelegateViewViews* constrained_delegate = |
| 149 new ConstrainedWebDialogDelegateViewViews(profile, delegate, tab_delegate)
; | 177 new ConstrainedWebDialogDelegateViewViews(profile, delegate, tab_delegate)
; |
| 150 ConstrainedWindow* constrained_window = | 178 ConstrainedWindow* constrained_window = |
| 151 new ConstrainedWindowViews(container, constrained_delegate); | 179 new ConstrainedWindowViews(container, constrained_delegate); |
| 152 constrained_delegate->set_window(constrained_window); | 180 constrained_delegate->set_window(constrained_window); |
| 153 return constrained_delegate; | 181 return constrained_delegate; |
| 154 } | 182 } |
| OLD | NEW |