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