| 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/html_dialog_ui.h" | 5 #include "chrome/browser/ui/webui/html_dialog_ui.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/property_bag.h" | 10 #include "base/property_bag.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.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" | |
| 15 #include "content/public/browser/notification_service.h" | 14 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/common/bindings_policy.h" | 16 #include "content/public/common/bindings_policy.h" |
| 17 | 17 |
| 18 using content::WebContents; |
| 19 |
| 18 static base::LazyInstance<base::PropertyAccessor<HtmlDialogUIDelegate*> > | 20 static base::LazyInstance<base::PropertyAccessor<HtmlDialogUIDelegate*> > |
| 19 g_html_dialog_ui_property_accessor = LAZY_INSTANCE_INITIALIZER; | 21 g_html_dialog_ui_property_accessor = LAZY_INSTANCE_INITIALIZER; |
| 20 | 22 |
| 21 HtmlDialogUI::HtmlDialogUI(TabContents* tab_contents) | 23 HtmlDialogUI::HtmlDialogUI(WebContents* web_contents) |
| 22 : ChromeWebUI(tab_contents) { | 24 : ChromeWebUI(web_contents) { |
| 23 } | 25 } |
| 24 | 26 |
| 25 HtmlDialogUI::~HtmlDialogUI() { | 27 HtmlDialogUI::~HtmlDialogUI() { |
| 26 // Don't unregister our property. During the teardown of the TabContents, | 28 // Don't unregister our property. During the teardown of the WebContents, |
| 27 // this will be deleted, but the TabContents will already be destroyed. | 29 // this will be deleted, but the WebContents will already be destroyed. |
| 28 // | 30 // |
| 29 // This object is owned indirectly by the TabContents. WebUIs can change, so | 31 // This object is owned indirectly by the WebContents. WebUIs can change, so |
| 30 // it's scary if this WebUI is changed out and replaced with something else, | 32 // it's scary if this WebUI is changed out and replaced with something else, |
| 31 // since the property will still point to the old delegate. But the delegate | 33 // since the property will still point to the old delegate. But the delegate |
| 32 // is itself the owner of the TabContents for a dialog so will be in scope, | 34 // is itself the owner of the WebContents for a dialog so will be in scope, |
| 33 // and the HTML dialogs won't swap WebUIs anyway since they don't navigate. | 35 // and the HTML dialogs won't swap WebUIs anyway since they don't navigate. |
| 34 } | 36 } |
| 35 | 37 |
| 36 void HtmlDialogUI::CloseDialog(const base::ListValue* args) { | 38 void HtmlDialogUI::CloseDialog(const base::ListValue* args) { |
| 37 OnDialogClosed(args); | 39 OnDialogClosed(args); |
| 38 } | 40 } |
| 39 | 41 |
| 40 // static | 42 // static |
| 41 base::PropertyAccessor<HtmlDialogUIDelegate*>& | 43 base::PropertyAccessor<HtmlDialogUIDelegate*>& |
| 42 HtmlDialogUI::GetPropertyAccessor() { | 44 HtmlDialogUI::GetPropertyAccessor() { |
| 43 return g_html_dialog_ui_property_accessor.Get(); | 45 return g_html_dialog_ui_property_accessor.Get(); |
| 44 } | 46 } |
| 45 | 47 |
| 46 //////////////////////////////////////////////////////////////////////////////// | 48 //////////////////////////////////////////////////////////////////////////////// |
| 47 // Private: | 49 // Private: |
| 48 | 50 |
| 49 void HtmlDialogUI::RenderViewCreated(RenderViewHost* render_view_host) { | 51 void HtmlDialogUI::RenderViewCreated(RenderViewHost* render_view_host) { |
| 50 // Hook up the javascript function calls, also known as chrome.send("foo") | 52 // Hook up the javascript function calls, also known as chrome.send("foo") |
| 51 // calls in the HTML, to the actual C++ functions. | 53 // calls in the HTML, to the actual C++ functions. |
| 52 RegisterMessageCallback("DialogClose", | 54 RegisterMessageCallback("DialogClose", |
| 53 base::Bind(&HtmlDialogUI::OnDialogClosed, base::Unretained(this))); | 55 base::Bind(&HtmlDialogUI::OnDialogClosed, base::Unretained(this))); |
| 54 | 56 |
| 55 // Pass the arguments to the renderer supplied by the delegate. | 57 // Pass the arguments to the renderer supplied by the delegate. |
| 56 std::string dialog_args; | 58 std::string dialog_args; |
| 57 std::vector<WebUIMessageHandler*> handlers; | 59 std::vector<WebUIMessageHandler*> handlers; |
| 58 HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty( | 60 HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty( |
| 59 tab_contents()->GetPropertyBag()); | 61 web_contents()->GetPropertyBag()); |
| 60 if (delegate) { | 62 if (delegate) { |
| 61 dialog_args = (*delegate)->GetDialogArgs(); | 63 dialog_args = (*delegate)->GetDialogArgs(); |
| 62 (*delegate)->GetWebUIMessageHandlers(&handlers); | 64 (*delegate)->GetWebUIMessageHandlers(&handlers); |
| 63 } | 65 } |
| 64 | 66 |
| 65 if (0 != (bindings_ & content::BINDINGS_POLICY_WEB_UI)) | 67 if (0 != (bindings_ & content::BINDINGS_POLICY_WEB_UI)) |
| 66 render_view_host->SetWebUIProperty("dialogArguments", dialog_args); | 68 render_view_host->SetWebUIProperty("dialogArguments", dialog_args); |
| 67 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin(); | 69 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin(); |
| 68 it != handlers.end(); ++it) { | 70 it != handlers.end(); ++it) { |
| 69 AddMessageHandler(*it); | 71 AddMessageHandler(*it); |
| 70 } | 72 } |
| 71 | 73 |
| 72 content::NotificationService::current()->Notify( | 74 content::NotificationService::current()->Notify( |
| 73 chrome::NOTIFICATION_HTML_DIALOG_SHOWN, | 75 chrome::NOTIFICATION_HTML_DIALOG_SHOWN, |
| 74 content::Source<WebUI>(this), | 76 content::Source<WebUI>(this), |
| 75 content::Details<RenderViewHost>(render_view_host)); | 77 content::Details<RenderViewHost>(render_view_host)); |
| 76 | 78 |
| 77 ChromeWebUI::RenderViewCreated(render_view_host); | 79 ChromeWebUI::RenderViewCreated(render_view_host); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void HtmlDialogUI::OnDialogClosed(const ListValue* args) { | 82 void HtmlDialogUI::OnDialogClosed(const ListValue* args) { |
| 81 HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty( | 83 HtmlDialogUIDelegate** delegate = GetPropertyAccessor().GetProperty( |
| 82 tab_contents()->GetPropertyBag()); | 84 web_contents()->GetPropertyBag()); |
| 83 if (delegate) { | 85 if (delegate) { |
| 84 std::string json_retval; | 86 std::string json_retval; |
| 85 if (args && !args->empty() && !args->GetString(0, &json_retval)) | 87 if (args && !args->empty() && !args->GetString(0, &json_retval)) |
| 86 NOTREACHED() << "Could not read JSON argument"; | 88 NOTREACHED() << "Could not read JSON argument"; |
| 87 | 89 |
| 88 (*delegate)->OnDialogClosed(json_retval); | 90 (*delegate)->OnDialogClosed(json_retval); |
| 89 } | 91 } |
| 90 } | 92 } |
| 91 | 93 |
| 92 ExternalHtmlDialogUI::ExternalHtmlDialogUI(TabContents* tab_contents) | 94 ExternalHtmlDialogUI::ExternalHtmlDialogUI(WebContents* web_contents) |
| 93 : HtmlDialogUI(tab_contents) { | 95 : HtmlDialogUI(web_contents) { |
| 94 // Non-file based UI needs to not have access to the Web UI bindings | 96 // Non-file based UI needs to not have access to the Web UI bindings |
| 95 // for security reasons. The code hosting the dialog should provide | 97 // for security reasons. The code hosting the dialog should provide |
| 96 // dialog specific functionality through other bindings and methods | 98 // dialog specific functionality through other bindings and methods |
| 97 // that are scoped in duration to the dialogs existence. | 99 // that are scoped in duration to the dialogs existence. |
| 98 bindings_ &= ~content::BINDINGS_POLICY_WEB_UI; | 100 bindings_ &= ~content::BINDINGS_POLICY_WEB_UI; |
| 99 } | 101 } |
| 100 | 102 |
| 101 ExternalHtmlDialogUI::~ExternalHtmlDialogUI() { | 103 ExternalHtmlDialogUI::~ExternalHtmlDialogUI() { |
| 102 } | 104 } |
| 103 | 105 |
| 104 bool HtmlDialogUIDelegate::HandleContextMenu(const ContextMenuParams& params) { | 106 bool HtmlDialogUIDelegate::HandleContextMenu(const ContextMenuParams& params) { |
| 105 return false; | 107 return false; |
| 106 } | 108 } |
| OLD | NEW |