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