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