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/constrained_html_ui.h" 6 7 #include "base/lazy_instance.h" 8 #include "base/values.h" 9 #include "chrome/browser/ui/webui/html_dialog_ui.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<ConstrainedHtmlUIDelegate*> > 15 g_constrained_html_ui_property_accessor(base::LINKER_INITIALIZED); 16 17 ConstrainedHtmlUI::ConstrainedHtmlUI(TabContents* contents) 18 : WebUI(contents) { 19 } 20 21 ConstrainedHtmlUI::~ConstrainedHtmlUI() { 22 } 23 24 void ConstrainedHtmlUI::RenderViewCreated( 25 RenderViewHost* render_view_host) { 26 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); 27 if (!delegate) 28 return; 29 30 HtmlDialogUIDelegate* dialog_delegate = delegate->GetHtmlDialogUIDelegate(); 31 std::vector<WebUIMessageHandler*> handlers; 32 dialog_delegate->GetWebUIMessageHandlers(&handlers); 33 render_view_host->SetWebUIProperty("dialogArguments", 34 dialog_delegate->GetDialogArgs()); 35 for (std::vector<WebUIMessageHandler*>::iterator it = handlers.begin(); 36 it != handlers.end(); ++it) { 37 (*it)->Attach(this); 38 AddMessageHandler(*it); 39 } 40 41 // Add a "DialogClose" callback which matches HTMLDialogUI behavior. 42 RegisterMessageCallback("DialogClose", 43 NewCallback(this, &ConstrainedHtmlUI::OnDialogClose)); 44 } 45 46 void ConstrainedHtmlUI::OnDialogClose(const ListValue* args) { 47 ConstrainedHtmlUIDelegate* delegate = GetConstrainedDelegate(); 48 if (!delegate) 49 return; 50 51 std::string json_retval; 52 if (!args->GetString(0, &json_retval)) 53 NOTREACHED() << "Could not read JSON argument"; 54 delegate->GetHtmlDialogUIDelegate()->OnDialogClosed(json_retval); 55 delegate->OnDialogClose(); 56 } 57 58 ConstrainedHtmlUIDelegate* 59 ConstrainedHtmlUI::GetConstrainedDelegate() { 60 ConstrainedHtmlUIDelegate** property = 61 GetPropertyAccessor().GetProperty(tab_contents()->property_bag()); 62 return property ? *property : NULL; 63 } 64 65 // static 66 PropertyAccessor<ConstrainedHtmlUIDelegate*>& 67 ConstrainedHtmlUI::GetPropertyAccessor() { 68 return g_constrained_html_ui_property_accessor.Get(); 69 } 70