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/views/html_dialog_view.h" | 5 #include "chrome/browser/ui/views/html_dialog_view.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
| 9 #include "base/property_bag.h" |
9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser_dialogs.h" | 12 #include "chrome/browser/ui/browser_dialogs.h" |
12 #include "chrome/browser/ui/webui/html_dialog_controller.h" | 13 #include "chrome/browser/ui/webui/html_dialog_controller.h" |
13 #include "content/public/browser/native_web_keyboard_event.h" | 14 #include "content/public/browser/native_web_keyboard_event.h" |
14 #include "content/public/browser/notification_details.h" | 15 #include "content/public/browser/notification_details.h" |
15 #include "content/public/browser/notification_source.h" | 16 #include "content/public/browser/notification_source.h" |
16 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "ui/base/keycodes/keyboard_codes.h" | 19 #include "ui/base/keycodes/keyboard_codes.h" |
| 20 #include "ui/views/controls/webview/webview.h" |
19 #include "ui/views/events/event.h" | 21 #include "ui/views/events/event.h" |
| 22 #include "ui/views/layout/fill_layout.h" |
20 #include "ui/views/widget/root_view.h" | 23 #include "ui/views/widget/root_view.h" |
21 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
22 | 25 |
23 #if defined(USE_AURA) | 26 #if defined(USE_AURA) |
24 #include "ui/aura/event.h" | 27 #include "ui/aura/event.h" |
25 #include "ui/views/widget/native_widget_aura.h" | 28 #include "ui/views/widget/native_widget_aura.h" |
26 #endif | 29 #endif |
27 | 30 |
28 using content::WebContents; | 31 using content::WebContents; |
29 using content::WebUIMessageHandler; | 32 using content::WebUIMessageHandler; |
30 | 33 |
31 namespace browser { | 34 namespace browser { |
32 | 35 |
33 // Declared in browser_dialogs.h so that others don't need to depend on our .h. | 36 // Declared in browser_dialogs.h so that others don't need to depend on our .h. |
34 gfx::NativeWindow ShowHtmlDialog(gfx::NativeWindow parent, | 37 gfx::NativeWindow ShowHtmlDialog(gfx::NativeWindow parent, |
35 Profile* profile, | 38 Profile* profile, |
36 Browser* browser, | 39 Browser* browser, |
37 HtmlDialogUIDelegate* delegate, | 40 HtmlDialogUIDelegate* delegate, |
38 DialogStyle style) { | 41 DialogStyle style) { |
39 HtmlDialogView* html_view = new HtmlDialogView(profile, browser, delegate); | 42 views::Widget* widget = views::Widget::CreateWindowWithParent( |
40 views::Widget::CreateWindowWithParent(html_view, parent); | 43 new HtmlDialogView(profile, browser, delegate), |
41 html_view->InitDialog(); | 44 parent); |
42 html_view->GetWidget()->Show(); | 45 widget->Show(); |
43 return html_view->GetWidget()->GetNativeWindow(); | 46 return widget->GetNativeWindow(); |
44 } | 47 } |
45 | 48 |
46 void CloseHtmlDialog(gfx::NativeWindow window) { | 49 void CloseHtmlDialog(gfx::NativeWindow window) { |
47 views::Widget::GetWidgetForNativeWindow(window)->Close(); | 50 views::Widget::GetWidgetForNativeWindow(window)->Close(); |
48 } | 51 } |
49 | 52 |
50 } // namespace browser | 53 } // namespace browser |
51 | 54 |
52 //////////////////////////////////////////////////////////////////////////////// | 55 //////////////////////////////////////////////////////////////////////////////// |
53 // HtmlDialogView, public: | 56 // HtmlDialogView, public: |
54 | 57 |
55 HtmlDialogView::HtmlDialogView(Profile* profile, | 58 HtmlDialogView::HtmlDialogView(Profile* profile, |
56 Browser* browser, | 59 Browser* browser, |
57 HtmlDialogUIDelegate* delegate) | 60 HtmlDialogUIDelegate* delegate) |
58 : DOMView(), | 61 : HtmlDialogTabContentsDelegate(profile), |
59 HtmlDialogTabContentsDelegate(profile), | |
60 initialized_(false), | 62 initialized_(false), |
61 delegate_(delegate), | 63 delegate_(delegate), |
62 dialog_controller_(new HtmlDialogController(this, profile, browser)) { | 64 dialog_controller_(new HtmlDialogController(this, profile, browser)), |
| 65 web_view_(new views::WebView(profile)) { |
| 66 web_view_->set_allow_accelerators(true); |
| 67 AddChildView(web_view_); |
| 68 SetLayoutManager(new views::FillLayout); |
| 69 // Pressing the ESC key will close the dialog. |
| 70 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, false, false, false)); |
63 } | 71 } |
64 | 72 |
65 HtmlDialogView::~HtmlDialogView() { | 73 HtmlDialogView::~HtmlDialogView() { |
66 } | 74 } |
67 | 75 |
| 76 content::WebContents* HtmlDialogView::web_contents() { |
| 77 return web_view_->web_contents(); |
| 78 } |
| 79 |
68 //////////////////////////////////////////////////////////////////////////////// | 80 //////////////////////////////////////////////////////////////////////////////// |
69 // HtmlDialogView, views::View implementation: | 81 // HtmlDialogView, views::View implementation: |
70 | 82 |
71 gfx::Size HtmlDialogView::GetPreferredSize() { | 83 gfx::Size HtmlDialogView::GetPreferredSize() { |
72 gfx::Size out; | 84 gfx::Size out; |
73 if (delegate_) | 85 if (delegate_) |
74 delegate_->GetMinimumDialogSize(&out); | 86 delegate_->GetMinimumDialogSize(&out); |
75 return out; | 87 return out; |
76 } | 88 } |
77 | 89 |
78 bool HtmlDialogView::AcceleratorPressed(const ui::Accelerator& accelerator) { | 90 bool HtmlDialogView::AcceleratorPressed(const ui::Accelerator& accelerator) { |
79 // Pressing ESC closes the dialog. | 91 // Pressing ESC closes the dialog. |
80 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); | 92 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); |
81 OnDialogClosed(std::string()); | 93 OnDialogClosed(std::string()); |
82 return true; | 94 return true; |
83 } | 95 } |
84 | 96 |
85 void HtmlDialogView::ViewHierarchyChanged( | 97 void HtmlDialogView::ViewHierarchyChanged(bool is_add, |
86 bool is_add, View* parent, View* child) { | 98 views::View* parent, |
87 DOMView::ViewHierarchyChanged(is_add, parent, child); | 99 views::View* child) { |
88 if (is_add && GetWidget() && !initialized_) { | 100 if (is_add && GetWidget()) |
89 initialized_ = true; | 101 InitDialog(); |
90 RegisterDialogAccelerators(); | |
91 } | |
92 } | 102 } |
93 | 103 |
94 //////////////////////////////////////////////////////////////////////////////// | 104 //////////////////////////////////////////////////////////////////////////////// |
95 // HtmlDialogView, views::WidgetDelegate implementation: | 105 // HtmlDialogView, views::WidgetDelegate implementation: |
96 | 106 |
97 bool HtmlDialogView::CanResize() const { | 107 bool HtmlDialogView::CanResize() const { |
98 return true; | 108 return true; |
99 } | 109 } |
100 | 110 |
101 ui::ModalType HtmlDialogView::GetModalType() const { | 111 ui::ModalType HtmlDialogView::GetModalType() const { |
(...skipping 18 matching lines...) Expand all Loading... |
120 // dialog. | 130 // dialog. |
121 if (delegate_) | 131 if (delegate_) |
122 OnDialogClosed(""); | 132 OnDialogClosed(""); |
123 } | 133 } |
124 | 134 |
125 views::View* HtmlDialogView::GetContentsView() { | 135 views::View* HtmlDialogView::GetContentsView() { |
126 return this; | 136 return this; |
127 } | 137 } |
128 | 138 |
129 views::View* HtmlDialogView::GetInitiallyFocusedView() { | 139 views::View* HtmlDialogView::GetInitiallyFocusedView() { |
130 return this; | 140 return web_view_; |
131 } | 141 } |
132 | 142 |
133 bool HtmlDialogView::ShouldShowWindowTitle() const { | 143 bool HtmlDialogView::ShouldShowWindowTitle() const { |
134 return ShouldShowDialogTitle(); | 144 return ShouldShowDialogTitle(); |
135 } | 145 } |
136 | 146 |
137 views::Widget* HtmlDialogView::GetWidget() { | 147 views::Widget* HtmlDialogView::GetWidget() { |
138 return View::GetWidget(); | 148 return View::GetWidget(); |
139 } | 149 } |
140 | 150 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 HtmlDialogTabContentsDelegate::AddNewContents( | 284 HtmlDialogTabContentsDelegate::AddNewContents( |
275 source, new_contents, disposition, initial_pos, user_gesture); | 285 source, new_contents, disposition, initial_pos, user_gesture); |
276 } | 286 } |
277 | 287 |
278 void HtmlDialogView::LoadingStateChanged(content::WebContents* source) { | 288 void HtmlDialogView::LoadingStateChanged(content::WebContents* source) { |
279 if (delegate_) | 289 if (delegate_) |
280 delegate_->OnLoadingStateChanged(source); | 290 delegate_->OnLoadingStateChanged(source); |
281 } | 291 } |
282 | 292 |
283 //////////////////////////////////////////////////////////////////////////////// | 293 //////////////////////////////////////////////////////////////////////////////// |
284 // HtmlDialogView: | 294 // HtmlDialogView, TabRenderWatcher::Delegate implementation: |
| 295 |
| 296 void HtmlDialogView::OnRenderHostCreated(content::RenderViewHost* host) { |
| 297 } |
| 298 |
| 299 void HtmlDialogView::OnTabMainFrameLoaded() { |
| 300 } |
| 301 |
| 302 void HtmlDialogView::OnTabMainFrameRender() { |
| 303 tab_watcher_.reset(); |
| 304 } |
| 305 |
| 306 //////////////////////////////////////////////////////////////////////////////// |
| 307 // HtmlDialogView, private: |
285 | 308 |
286 void HtmlDialogView::InitDialog() { | 309 void HtmlDialogView::InitDialog() { |
287 // Now Init the DOMView. This view runs in its own process to render the html. | 310 content::WebContents* web_contents = web_view_->GetWebContents(); |
288 DOMView::Init(profile(), NULL); | 311 if (web_contents->GetDelegate() == this) |
| 312 return; |
289 | 313 |
290 WebContents* web_contents = dom_contents_->web_contents(); | |
291 web_contents->SetDelegate(this); | 314 web_contents->SetDelegate(this); |
292 | 315 |
293 // Set the delegate. This must be done before loading the page. See | 316 // Set the delegate. This must be done before loading the page. See |
294 // the comment above HtmlDialogUI in its header file for why. | 317 // the comment above HtmlDialogUI in its header file for why. |
295 HtmlDialogUI::GetPropertyAccessor().SetProperty( | 318 HtmlDialogUI::GetPropertyAccessor().SetProperty( |
296 web_contents->GetPropertyBag(), this); | 319 web_contents->GetPropertyBag(), this); |
297 tab_watcher_.reset(new TabRenderWatcher(web_contents, this)); | 320 tab_watcher_.reset(new TabRenderWatcher(web_contents, this)); |
298 | 321 |
299 if (delegate_) { | 322 if (delegate_) { |
300 gfx::Size out; | 323 gfx::Size out; |
301 delegate_->GetDialogSize(&out); | 324 delegate_->GetDialogSize(&out); |
302 if (!out.IsEmpty() && GetWidget()) | 325 if (!out.IsEmpty() && GetWidget()) |
303 GetWidget()->CenterWindow(out); | 326 GetWidget()->CenterWindow(out); |
304 } | 327 } |
305 | 328 |
306 DOMView::LoadURL(GetDialogContentURL()); | 329 web_view_->LoadInitialURL(GetDialogContentURL()); |
307 } | 330 } |
308 | |
309 void HtmlDialogView::RegisterDialogAccelerators() { | |
310 // Pressing the ESC key will close the dialog. | |
311 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, false, false, false)); | |
312 } | |
313 | |
314 void HtmlDialogView::OnRenderHostCreated(content::RenderViewHost* host) { | |
315 } | |
316 | |
317 void HtmlDialogView::OnTabMainFrameLoaded() { | |
318 } | |
319 | |
320 void HtmlDialogView::OnTabMainFrameRender() { | |
321 tab_watcher_.reset(); | |
322 } | |
OLD | NEW |