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