| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_html_ui.h" | 5 #include "chrome/browser/ui/webui/constrained_html_ui.h" |
| 6 | 6 |
| 7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/ui/gtk/constrained_window_gtk.h" | 8 #include "chrome/browser/ui/gtk/constrained_window_gtk.h" |
| 9 #include "chrome/browser/ui/gtk/tab_contents_container_gtk.h" | 9 #include "chrome/browser/ui/gtk/tab_contents_container_gtk.h" |
| 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 11 #include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h" | 11 #include "chrome/browser/ui/webui/html_dialog_tab_contents_delegate.h" |
| 12 #include "chrome/browser/ui/webui/html_dialog_ui.h" | 12 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
| 13 #include "content/browser/renderer_host/render_view_host.h" | 13 #include "content/browser/renderer_host/render_view_host.h" |
| 14 #include "content/browser/tab_contents/tab_contents.h" | 14 #include "content/browser/tab_contents/tab_contents.h" |
| 15 #include "content/common/notification_source.h" | 15 #include "content/common/notification_source.h" |
| 16 #include "ui/base/gtk/gtk_hig_constants.h" | 16 #include "ui/base/gtk/gtk_hig_constants.h" |
| 17 #include "ui/gfx/rect.h" | 17 #include "ui/gfx/rect.h" |
| 18 | 18 |
| 19 class ConstrainedHtmlDelegateGtk : public ConstrainedWindowGtkDelegate, | 19 class ConstrainedHtmlDelegateGtk : public ConstrainedWindowGtkDelegate, |
| 20 public HtmlDialogTabContentsDelegate, | 20 public HtmlDialogTabContentsDelegate, |
| 21 public ConstrainedHtmlUIDelegate { | 21 public ConstrainedHtmlUIDelegate { |
| 22 public: | 22 public: |
| 23 ConstrainedHtmlDelegateGtk(Profile* profile, | 23 ConstrainedHtmlDelegateGtk(Profile* profile, |
| 24 HtmlDialogUIDelegate* delegate); | 24 HtmlDialogUIDelegate* delegate); |
| 25 | 25 |
| 26 virtual ~ConstrainedHtmlDelegateGtk(); | 26 virtual ~ConstrainedHtmlDelegateGtk() { |
| 27 if (release_tab_on_close_) |
| 28 ignore_result(tab_.release()); |
| 29 } |
| 27 | 30 |
| 28 // ConstrainedWindowGtkDelegate ---------------------------------------------- | 31 // ConstrainedWindowGtkDelegate ---------------------------------------------- |
| 29 virtual GtkWidget* GetWidgetRoot() OVERRIDE { | 32 virtual GtkWidget* GetWidgetRoot() OVERRIDE { |
| 30 return tab_contents_container_.widget(); | 33 return tab_contents_container_.widget(); |
| 31 } | 34 } |
| 32 virtual GtkWidget* GetFocusWidget() OVERRIDE { | 35 virtual GtkWidget* GetFocusWidget() OVERRIDE { |
| 33 return tab_.tab_contents()->GetContentNativeView(); | 36 return tab_->tab_contents()->GetContentNativeView(); |
| 34 } | 37 } |
| 35 virtual void DeleteDelegate() OVERRIDE { | 38 virtual void DeleteDelegate() OVERRIDE { |
| 36 if (!closed_via_webui_) | 39 if (!closed_via_webui_) |
| 37 html_delegate_->OnDialogClosed(""); | 40 html_delegate_->OnDialogClosed(""); |
| 38 delete this; | 41 delete this; |
| 39 } | 42 } |
| 40 | |
| 41 // ConstrainedHtmlDelegate --------------------------------------------- | |
| 42 virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() OVERRIDE; | |
| 43 virtual void OnDialogCloseFromWebUI() OVERRIDE; | |
| 44 virtual bool GetBackgroundColor(GdkColor* color) OVERRIDE { | 43 virtual bool GetBackgroundColor(GdkColor* color) OVERRIDE { |
| 45 *color = ui::kGdkWhite; | 44 *color = ui::kGdkWhite; |
| 46 return true; | 45 return true; |
| 47 } | 46 } |
| 48 | 47 |
| 48 // ConstrainedHtmlUIDelegate ------------------------------------------------- |
| 49 virtual HtmlDialogUIDelegate* GetHtmlDialogUIDelegate() OVERRIDE { |
| 50 return html_delegate_; |
| 51 } |
| 52 virtual void OnDialogCloseFromWebUI() OVERRIDE { |
| 53 closed_via_webui_ = true; |
| 54 window_->CloseConstrainedWindow(); |
| 55 } |
| 56 virtual void ReleaseTabContentsOnDialogClose() OVERRIDE { |
| 57 release_tab_on_close_ = true; |
| 58 } |
| 59 virtual ConstrainedWindow* window() OVERRIDE { |
| 60 return window_; |
| 61 } |
| 62 virtual TabContentsWrapper* tab() OVERRIDE { |
| 63 return tab_.get(); |
| 64 } |
| 65 |
| 49 // HtmlDialogTabContentsDelegate --------------------------------------------- | 66 // HtmlDialogTabContentsDelegate --------------------------------------------- |
| 50 virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) | 67 virtual void HandleKeyboardEvent( |
| 51 OVERRIDE {} | 68 const NativeWebKeyboardEvent& event) OVERRIDE { |
| 69 } |
| 52 | 70 |
| 53 void set_window(ConstrainedWindow* window) { | 71 void set_window(ConstrainedWindow* window) { |
| 54 window_ = window; | 72 window_ = window; |
| 55 } | 73 } |
| 56 | 74 |
| 57 private: | 75 private: |
| 58 TabContentsWrapper tab_; | 76 scoped_ptr<TabContentsWrapper> tab_; |
| 59 TabContentsContainerGtk tab_contents_container_; | 77 TabContentsContainerGtk tab_contents_container_; |
| 60 HtmlDialogUIDelegate* html_delegate_; | 78 HtmlDialogUIDelegate* html_delegate_; |
| 61 | 79 |
| 62 // The constrained window that owns |this|. It's saved here because it needs | 80 // The constrained window that owns |this|. It's saved here because it needs |
| 63 // to be closed in response to the WebUI OnDialogClose callback. | 81 // to be closed in response to the WebUI OnDialogClose callback. |
| 64 ConstrainedWindow* window_; | 82 ConstrainedWindow* window_; |
| 65 | 83 |
| 66 // Was the dialog closed from WebUI (in which case |html_delegate_|'s | 84 // Was the dialog closed from WebUI (in which case |html_delegate_|'s |
| 67 // OnDialogClosed() method has already been called)? | 85 // OnDialogClosed() method has already been called)? |
| 68 bool closed_via_webui_; | 86 bool closed_via_webui_; |
| 87 |
| 88 // If true, release |tab_| on close instead of destroying it. |
| 89 bool release_tab_on_close_; |
| 69 }; | 90 }; |
| 70 | 91 |
| 71 ConstrainedHtmlDelegateGtk::ConstrainedHtmlDelegateGtk( | 92 ConstrainedHtmlDelegateGtk::ConstrainedHtmlDelegateGtk( |
| 72 Profile* profile, | 93 Profile* profile, |
| 73 HtmlDialogUIDelegate* delegate) | 94 HtmlDialogUIDelegate* delegate) |
| 74 : HtmlDialogTabContentsDelegate(profile), | 95 : HtmlDialogTabContentsDelegate(profile), |
| 75 tab_(new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL, NULL)), | |
| 76 tab_contents_container_(NULL), | 96 tab_contents_container_(NULL), |
| 77 html_delegate_(delegate), | 97 html_delegate_(delegate), |
| 78 window_(NULL), | 98 window_(NULL), |
| 79 closed_via_webui_(false) { | 99 closed_via_webui_(false), |
| 80 tab_.tab_contents()->set_delegate(this); | 100 release_tab_on_close_(false) { |
| 101 TabContents* tab_contents = |
| 102 new TabContents(profile, NULL, MSG_ROUTING_NONE, NULL, NULL); |
| 103 tab_.reset(new TabContentsWrapper(tab_contents)); |
| 104 tab_contents->set_delegate(this); |
| 81 | 105 |
| 82 // Set |this| as a property on the tab contents so that the ConstrainedHtmlUI | 106 // Set |this| as a property on the tab contents so that the ConstrainedHtmlUI |
| 83 // can get a reference to |this|. | 107 // can get a reference to |this|. |
| 84 ConstrainedHtmlUI::GetPropertyAccessor().SetProperty( | 108 ConstrainedHtmlUI::GetPropertyAccessor().SetProperty( |
| 85 tab_.tab_contents()->property_bag(), this); | 109 tab_contents->property_bag(), this); |
| 86 | 110 |
| 87 tab_.tab_contents()->controller().LoadURL( | 111 tab_contents->controller().LoadURL(delegate->GetDialogContentURL(), GURL(), |
| 88 delegate->GetDialogContentURL(), GURL(), | 112 content::PAGE_TRANSITION_START_PAGE, |
| 89 content::PAGE_TRANSITION_START_PAGE, std::string()); | 113 std::string()); |
| 90 tab_contents_container_.SetTab(&tab_); | 114 tab_contents_container_.SetTab(tab_.get()); |
| 91 | 115 |
| 92 gfx::Size dialog_size; | 116 gfx::Size dialog_size; |
| 93 delegate->GetDialogSize(&dialog_size); | 117 delegate->GetDialogSize(&dialog_size); |
| 94 gtk_widget_set_size_request(GTK_WIDGET(tab_contents_container_.widget()), | 118 gtk_widget_set_size_request(GTK_WIDGET(tab_contents_container_.widget()), |
| 95 dialog_size.width(), | 119 dialog_size.width(), |
| 96 dialog_size.height()); | 120 dialog_size.height()); |
| 97 | 121 |
| 98 gtk_widget_show_all(GetWidgetRoot()); | 122 gtk_widget_show_all(GetWidgetRoot()); |
| 99 } | 123 } |
| 100 | 124 |
| 101 ConstrainedHtmlDelegateGtk::~ConstrainedHtmlDelegateGtk() { | |
| 102 } | |
| 103 | |
| 104 HtmlDialogUIDelegate* | |
| 105 ConstrainedHtmlDelegateGtk::GetHtmlDialogUIDelegate() { | |
| 106 return html_delegate_; | |
| 107 } | |
| 108 | |
| 109 void ConstrainedHtmlDelegateGtk::OnDialogCloseFromWebUI() { | |
| 110 closed_via_webui_ = true; | |
| 111 window_->CloseConstrainedWindow(); | |
| 112 } | |
| 113 | |
| 114 // static | 125 // static |
| 115 ConstrainedWindow* ConstrainedHtmlUI::CreateConstrainedHtmlDialog( | 126 ConstrainedHtmlUIDelegate* ConstrainedHtmlUI::CreateConstrainedHtmlDialog( |
| 116 Profile* profile, | 127 Profile* profile, |
| 117 HtmlDialogUIDelegate* delegate, | 128 HtmlDialogUIDelegate* delegate, |
| 118 TabContentsWrapper* overshadowed) { | 129 TabContentsWrapper* overshadowed) { |
| 119 ConstrainedHtmlDelegateGtk* constrained_delegate = | 130 ConstrainedHtmlDelegateGtk* constrained_delegate = |
| 120 new ConstrainedHtmlDelegateGtk(profile, delegate); | 131 new ConstrainedHtmlDelegateGtk(profile, delegate); |
| 121 ConstrainedWindow* constrained_window = | 132 ConstrainedWindow* constrained_window = |
| 122 new ConstrainedWindowGtk(overshadowed, constrained_delegate); | 133 new ConstrainedWindowGtk(overshadowed, constrained_delegate); |
| 123 constrained_delegate->set_window(constrained_window); | 134 constrained_delegate->set_window(constrained_window); |
| 124 return constrained_window; | 135 return constrained_delegate; |
| 125 } | 136 } |
| OLD | NEW |