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 "content/browser/webui/web_ui.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/json/json_writer.h" | |
9 #include "base/stl_util.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "base/values.h" | |
12 #include "content/browser/child_process_security_policy.h" | |
13 #include "content/browser/renderer_host/render_process_host_impl.h" | |
14 #include "content/browser/renderer_host/render_view_host.h" | |
15 #include "content/browser/tab_contents/tab_contents.h" | |
16 #include "content/browser/webui/generic_handler.h" | |
17 #include "content/common/view_messages.h" | |
18 #include "content/public/browser/web_contents_view.h" | |
19 #include "content/public/browser/web_ui_controller.h" | |
20 #include "content/public/common/bindings_policy.h" | |
21 #include "content/public/common/content_switches.h" | |
22 | |
23 using content::WebContents; | |
24 using content::WebUIController; | |
25 using content::WebUIMessageHandler; | |
26 | |
27 namespace content { | |
28 | |
29 const WebUI::TypeID WebUI::kNoWebUI = NULL; | |
30 | |
31 // static | |
32 string16 WebUI::GetJavascriptCall( | |
33 const std::string& function_name, | |
34 const std::vector<const Value*>& arg_list) { | |
35 string16 parameters; | |
36 std::string json; | |
37 for (size_t i = 0; i < arg_list.size(); ++i) { | |
38 if (i > 0) | |
39 parameters += char16(','); | |
40 | |
41 base::JSONWriter::Write(arg_list[i], false, &json); | |
42 parameters += UTF8ToUTF16(json); | |
43 } | |
44 return ASCIIToUTF16(function_name) + | |
45 char16('(') + parameters + char16(')') + char16(';'); | |
46 } | |
47 | |
48 } | |
49 | |
50 WebUI::WebUI(WebContents* contents) | |
51 : hide_favicon_(false), | |
52 focus_location_bar_by_default_(false), | |
53 should_hide_url_(false), | |
54 link_transition_type_(content::PAGE_TRANSITION_LINK), | |
55 bindings_(content::BINDINGS_POLICY_WEB_UI), | |
56 web_contents_(contents) { | |
57 DCHECK(contents); | |
58 AddMessageHandler(new GenericHandler()); | |
59 } | |
60 | |
61 WebUI::~WebUI() { | |
62 // Delete the controller first, since it may also be keeping a pointer to some | |
63 // of the handlers and can call them at destruction. | |
64 controller_.reset(); | |
65 STLDeleteContainerPointers(handlers_.begin(), handlers_.end()); | |
66 } | |
67 | |
68 // WebUI, public: ------------------------------------------------------------- | |
69 | |
70 bool WebUI::OnMessageReceived(const IPC::Message& message) { | |
71 bool handled = true; | |
72 IPC_BEGIN_MESSAGE_MAP(WebUI, message) | |
73 IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend) | |
74 IPC_MESSAGE_UNHANDLED(handled = false) | |
75 IPC_END_MESSAGE_MAP() | |
76 return handled; | |
77 } | |
78 | |
79 void WebUI::OnWebUISend(const GURL& source_url, | |
80 const std::string& message, | |
81 const ListValue& args) { | |
82 if (!ChildProcessSecurityPolicy::GetInstance()-> | |
83 HasWebUIBindings(web_contents_->GetRenderProcessHost()->GetID())) { | |
84 NOTREACHED() << "Blocked unauthorized use of WebUIBindings."; | |
85 return; | |
86 } | |
87 | |
88 if (controller_->OverrideHandleWebUIMessage(source_url, message,args)) | |
89 return; | |
90 | |
91 // Look up the callback for this message. | |
92 MessageCallbackMap::const_iterator callback = | |
93 message_callbacks_.find(message); | |
94 if (callback != message_callbacks_.end()) { | |
95 // Forward this message and content on. | |
96 callback->second.Run(&args); | |
97 } | |
98 } | |
99 | |
100 void WebUI::RenderViewCreated(RenderViewHost* render_view_host) { | |
101 controller_->RenderViewCreated(render_view_host); | |
102 | |
103 // Do not attempt to set the toolkit property if WebUI is not enabled, e.g., | |
104 // the bookmarks manager page. | |
105 if (!(bindings_ & content::BINDINGS_POLICY_WEB_UI)) | |
106 return; | |
107 | |
108 #if defined(TOOLKIT_VIEWS) | |
109 render_view_host->SetWebUIProperty("toolkit", "views"); | |
110 #elif defined(TOOLKIT_GTK) | |
111 render_view_host->SetWebUIProperty("toolkit", "GTK"); | |
112 #endif // defined(TOOLKIT_VIEWS) | |
113 | |
114 // Let the WebUI know that we're looking for UI that's optimized for touch | |
115 // input. | |
116 // TODO(rbyers) Figure out the right model for enabling touch-optimized UI | |
117 // (http://crbug.com/105380). | |
118 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTouchOptimizedUI)) | |
119 render_view_host->SetWebUIProperty("touchOptimized", "true"); | |
120 } | |
121 | |
122 WebContents* WebUI::GetWebContents() const { | |
123 return web_contents_; | |
124 } | |
125 | |
126 bool WebUI::ShouldHideFavicon() const { | |
127 return hide_favicon_; | |
128 } | |
129 | |
130 void WebUI::HideFavicon() { | |
131 hide_favicon_ = true; | |
132 } | |
133 | |
134 bool WebUI::ShouldFocusLocationBarByDefault() const { | |
135 return focus_location_bar_by_default_; | |
136 } | |
137 | |
138 void WebUI::FocusLocationBarByDefault() { | |
139 focus_location_bar_by_default_ = true; | |
140 } | |
141 | |
142 bool WebUI::ShouldHideURL() const { | |
143 return should_hide_url_; | |
144 } | |
145 | |
146 void WebUI::HideURL() { | |
147 should_hide_url_ = true; | |
148 } | |
149 | |
150 const string16& WebUI::GetOverriddenTitle() const { | |
151 return overridden_title_; | |
152 } | |
153 | |
154 void WebUI::OverrideTitle(const string16& title) { | |
155 overridden_title_ = title; | |
156 } | |
157 | |
158 content::PageTransition WebUI::GetLinkTransitionType() const { | |
159 return link_transition_type_; | |
160 } | |
161 | |
162 void WebUI::SetLinkTransitionType(content::PageTransition type) { | |
163 link_transition_type_ = type; | |
164 } | |
165 | |
166 int WebUI::GetBindings() const { | |
167 return bindings_; | |
168 } | |
169 | |
170 void WebUI::SetBindings(int bindings) { | |
171 bindings_ = bindings; | |
172 } | |
173 | |
174 void WebUI::SetFrameXPath(const std::string& xpath) { | |
175 frame_xpath_ = xpath; | |
176 } | |
177 | |
178 WebUIController* WebUI::GetController() const { | |
179 return controller_.get(); | |
180 } | |
181 | |
182 void WebUI::SetController(WebUIController* controller) { | |
183 controller_.reset(controller); | |
184 } | |
185 | |
186 void WebUI::CallJavascriptFunction(const std::string& function_name) { | |
187 DCHECK(IsStringASCII(function_name)); | |
188 string16 javascript = ASCIIToUTF16(function_name + "();"); | |
189 ExecuteJavascript(javascript); | |
190 } | |
191 | |
192 void WebUI::CallJavascriptFunction(const std::string& function_name, | |
193 const Value& arg) { | |
194 DCHECK(IsStringASCII(function_name)); | |
195 std::vector<const Value*> args; | |
196 args.push_back(&arg); | |
197 ExecuteJavascript(GetJavascriptCall(function_name, args)); | |
198 } | |
199 | |
200 void WebUI::CallJavascriptFunction( | |
201 const std::string& function_name, | |
202 const Value& arg1, const Value& arg2) { | |
203 DCHECK(IsStringASCII(function_name)); | |
204 std::vector<const Value*> args; | |
205 args.push_back(&arg1); | |
206 args.push_back(&arg2); | |
207 ExecuteJavascript(GetJavascriptCall(function_name, args)); | |
208 } | |
209 | |
210 void WebUI::CallJavascriptFunction( | |
211 const std::string& function_name, | |
212 const Value& arg1, const Value& arg2, const Value& arg3) { | |
213 DCHECK(IsStringASCII(function_name)); | |
214 std::vector<const Value*> args; | |
215 args.push_back(&arg1); | |
216 args.push_back(&arg2); | |
217 args.push_back(&arg3); | |
218 ExecuteJavascript(GetJavascriptCall(function_name, args)); | |
219 } | |
220 | |
221 void WebUI::CallJavascriptFunction( | |
222 const std::string& function_name, | |
223 const Value& arg1, | |
224 const Value& arg2, | |
225 const Value& arg3, | |
226 const Value& arg4) { | |
227 DCHECK(IsStringASCII(function_name)); | |
228 std::vector<const Value*> args; | |
229 args.push_back(&arg1); | |
230 args.push_back(&arg2); | |
231 args.push_back(&arg3); | |
232 args.push_back(&arg4); | |
233 ExecuteJavascript(GetJavascriptCall(function_name, args)); | |
234 } | |
235 | |
236 void WebUI::CallJavascriptFunction( | |
237 const std::string& function_name, | |
238 const std::vector<const Value*>& args) { | |
239 DCHECK(IsStringASCII(function_name)); | |
240 ExecuteJavascript(GetJavascriptCall(function_name, args)); | |
241 } | |
242 | |
243 void WebUI::RegisterMessageCallback(const std::string &message, | |
244 const MessageCallback& callback) { | |
245 message_callbacks_.insert(std::make_pair(message, callback)); | |
246 } | |
247 | |
248 void WebUI::ProcessWebUIMessage(const GURL& source_url, | |
249 const std::string& message, | |
250 const base::ListValue& args) { | |
251 OnWebUISend(source_url, message, args); | |
252 } | |
253 | |
254 // WebUI, protected: ---------------------------------------------------------- | |
255 | |
256 void WebUI::AddMessageHandler(WebUIMessageHandler* handler) { | |
257 DCHECK(!handler->web_ui()); | |
258 handler->set_web_ui(this); | |
259 handler->RegisterMessages(); | |
260 handlers_.push_back(handler); | |
261 } | |
262 | |
263 void WebUI::ExecuteJavascript(const string16& javascript) { | |
264 web_contents_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( | |
265 ASCIIToUTF16(frame_xpath_), javascript); | |
266 } | |
OLD | NEW |