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/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 AddChildView(web_view_); | |
| 67 SetLayoutManager(new views::FillLayout); | |
| 68 // Pressing the ESC key will close the dialog. | |
| 69 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, false, false, false)); | |
| 63 } | 70 } |
| 64 | 71 |
| 65 HtmlDialogView::~HtmlDialogView() { | 72 HtmlDialogView::~HtmlDialogView() { |
| 66 } | 73 } |
| 67 | 74 |
| 68 //////////////////////////////////////////////////////////////////////////////// | 75 //////////////////////////////////////////////////////////////////////////////// |
| 69 // HtmlDialogView, views::View implementation: | 76 // HtmlDialogView, views::View implementation: |
| 70 | 77 |
| 71 gfx::Size HtmlDialogView::GetPreferredSize() { | 78 gfx::Size HtmlDialogView::GetPreferredSize() { |
| 72 gfx::Size out; | 79 gfx::Size out; |
| 73 if (delegate_) | 80 if (delegate_) |
| 74 delegate_->GetMinimumDialogSize(&out); | 81 delegate_->GetMinimumDialogSize(&out); |
| 75 return out; | 82 return out; |
| 76 } | 83 } |
| 77 | 84 |
| 78 bool HtmlDialogView::AcceleratorPressed(const ui::Accelerator& accelerator) { | 85 bool HtmlDialogView::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 79 // Pressing ESC closes the dialog. | 86 // Pressing ESC closes the dialog. |
| 80 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); | 87 DCHECK_EQ(ui::VKEY_ESCAPE, accelerator.key_code()); |
| 81 OnDialogClosed(std::string()); | 88 OnDialogClosed(std::string()); |
| 82 return true; | 89 return true; |
| 83 } | 90 } |
| 84 | 91 |
| 85 void HtmlDialogView::ViewHierarchyChanged( | 92 void HtmlDialogView::ViewHierarchyChanged(bool is_add, |
| 86 bool is_add, View* parent, View* child) { | 93 views::View* parent, |
| 87 DOMView::ViewHierarchyChanged(is_add, parent, child); | 94 views::View* child) { |
| 88 if (is_add && GetWidget() && !initialized_) { | 95 if (is_add && GetWidget()) |
| 89 initialized_ = true; | 96 InitDialog(); |
| 90 RegisterDialogAccelerators(); | |
| 91 } | |
| 92 } | 97 } |
| 93 | 98 |
| 94 //////////////////////////////////////////////////////////////////////////////// | 99 //////////////////////////////////////////////////////////////////////////////// |
| 95 // HtmlDialogView, views::WidgetDelegate implementation: | 100 // HtmlDialogView, views::WidgetDelegate implementation: |
| 96 | 101 |
| 97 bool HtmlDialogView::CanResize() const { | 102 bool HtmlDialogView::CanResize() const { |
| 98 return true; | 103 return true; |
| 99 } | 104 } |
| 100 | 105 |
| 101 ui::ModalType HtmlDialogView::GetModalType() const { | 106 ui::ModalType HtmlDialogView::GetModalType() const { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 120 // dialog. | 125 // dialog. |
| 121 if (delegate_) | 126 if (delegate_) |
| 122 OnDialogClosed(""); | 127 OnDialogClosed(""); |
| 123 } | 128 } |
| 124 | 129 |
| 125 views::View* HtmlDialogView::GetContentsView() { | 130 views::View* HtmlDialogView::GetContentsView() { |
| 126 return this; | 131 return this; |
| 127 } | 132 } |
| 128 | 133 |
| 129 views::View* HtmlDialogView::GetInitiallyFocusedView() { | 134 views::View* HtmlDialogView::GetInitiallyFocusedView() { |
| 130 return this; | 135 return web_view_; |
| 131 } | 136 } |
| 132 | 137 |
| 133 bool HtmlDialogView::ShouldShowWindowTitle() const { | 138 bool HtmlDialogView::ShouldShowWindowTitle() const { |
| 134 return ShouldShowDialogTitle(); | 139 return ShouldShowDialogTitle(); |
| 135 } | 140 } |
| 136 | 141 |
| 137 views::Widget* HtmlDialogView::GetWidget() { | 142 views::Widget* HtmlDialogView::GetWidget() { |
| 138 return View::GetWidget(); | 143 return View::GetWidget(); |
| 139 } | 144 } |
| 140 | 145 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 HtmlDialogTabContentsDelegate::AddNewContents( | 279 HtmlDialogTabContentsDelegate::AddNewContents( |
| 275 source, new_contents, disposition, initial_pos, user_gesture); | 280 source, new_contents, disposition, initial_pos, user_gesture); |
| 276 } | 281 } |
| 277 | 282 |
| 278 void HtmlDialogView::LoadingStateChanged(content::WebContents* source) { | 283 void HtmlDialogView::LoadingStateChanged(content::WebContents* source) { |
| 279 if (delegate_) | 284 if (delegate_) |
| 280 delegate_->OnLoadingStateChanged(source); | 285 delegate_->OnLoadingStateChanged(source); |
| 281 } | 286 } |
| 282 | 287 |
| 283 //////////////////////////////////////////////////////////////////////////////// | 288 //////////////////////////////////////////////////////////////////////////////// |
| 284 // HtmlDialogView: | 289 // HtmlDialogView, protected: |
| 290 | |
| 291 | |
| 292 void HtmlDialogView::OnRenderHostCreated(content::RenderViewHost* host) { | |
| 293 } | |
| 294 | |
| 295 void HtmlDialogView::OnTabMainFrameLoaded() { | |
| 296 } | |
| 297 | |
| 298 void HtmlDialogView::OnTabMainFrameRender() { | |
| 299 tab_watcher_.reset(); | |
| 300 } | |
| 301 | |
| 302 //////////////////////////////////////////////////////////////////////////////// | |
| 303 // HtmlDialogView, private: | |
| 285 | 304 |
| 286 void HtmlDialogView::InitDialog() { | 305 void HtmlDialogView::InitDialog() { |
| 287 // Now Init the DOMView. This view runs in its own process to render the html. | 306 static bool initialized = false; |
|
sky
2012/04/20 22:59:37
How come initialized is static?
Ben Goodger (Google)
2012/04/21 04:49:28
so I can only init once.
sky
2012/04/21 16:13:31
Doesn't this need to set up a bunch of state that
| |
| 288 DOMView::Init(profile(), NULL); | 307 if (initialized) |
| 308 return; | |
| 309 initialized = true; | |
| 289 | 310 |
| 290 WebContents* web_contents = dom_contents_->web_contents(); | 311 content::WebContents* web_contents = web_view_->GetWebContents(); |
| 291 web_contents->SetDelegate(this); | 312 web_contents->SetDelegate(this); |
| 292 | 313 |
| 293 // Set the delegate. This must be done before loading the page. See | 314 // Set the delegate. This must be done before loading the page. See |
| 294 // the comment above HtmlDialogUI in its header file for why. | 315 // the comment above HtmlDialogUI in its header file for why. |
| 295 HtmlDialogUI::GetPropertyAccessor().SetProperty( | 316 HtmlDialogUI::GetPropertyAccessor().SetProperty( |
| 296 web_contents->GetPropertyBag(), this); | 317 web_contents->GetPropertyBag(), this); |
| 297 tab_watcher_.reset(new TabRenderWatcher(web_contents, this)); | 318 tab_watcher_.reset(new TabRenderWatcher(web_contents, this)); |
| 298 | 319 |
| 299 if (delegate_) { | 320 if (delegate_) { |
| 300 gfx::Size out; | 321 gfx::Size out; |
| 301 delegate_->GetDialogSize(&out); | 322 delegate_->GetDialogSize(&out); |
| 302 if (!out.IsEmpty() && GetWidget()) | 323 if (!out.IsEmpty() && GetWidget()) |
| 303 GetWidget()->CenterWindow(out); | 324 GetWidget()->CenterWindow(out); |
| 304 } | 325 } |
| 305 | 326 |
| 306 DOMView::LoadURL(GetDialogContentURL()); | 327 web_view_->LoadInitialURL(GetDialogContentURL()); |
| 307 } | 328 } |
| 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 |