Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: content/browser/webui/web_ui_impl.cc

Issue 1896463003: WebUI: Add JavaScript lifecycle-control to WebUIMessageHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_impl.h" 5 #include "content/browser/webui/web_ui_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/debug/dump_without_crashing.h" 9 #include "base/debug/dump_without_crashing.h"
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 } 137 }
138 138
139 WebUIController* WebUIImpl::GetController() const { 139 WebUIController* WebUIImpl::GetController() const {
140 return controller_.get(); 140 return controller_.get();
141 } 141 }
142 142
143 void WebUIImpl::SetController(WebUIController* controller) { 143 void WebUIImpl::SetController(WebUIController* controller) {
144 controller_.reset(controller); 144 controller_.reset(controller);
145 } 145 }
146 146
147 bool WebUIImpl::CanCallJavascript() {
148 RenderFrameHost* target_frame = TargetFrame();
149 return target_frame &&
150 (ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings(
151 target_frame->GetProcess()->GetID()) ||
152 // It's possible to load about:blank in a Web UI renderer.
153 // See http://crbug.com/42547
154 target_frame->GetLastCommittedURL().spec() == url::kAboutBlankURL);
155 }
156
147 void WebUIImpl::CallJavascriptFunction(const std::string& function_name) { 157 void WebUIImpl::CallJavascriptFunction(const std::string& function_name) {
148 DCHECK(base::IsStringASCII(function_name)); 158 DCHECK(base::IsStringASCII(function_name));
149 base::string16 javascript = base::ASCIIToUTF16(function_name + "();"); 159 base::string16 javascript = base::ASCIIToUTF16(function_name + "();");
150 ExecuteJavascript(javascript); 160 ExecuteJavascript(javascript);
151 } 161 }
152 162
153 void WebUIImpl::CallJavascriptFunction(const std::string& function_name, 163 void WebUIImpl::CallJavascriptFunction(const std::string& function_name,
154 const base::Value& arg) { 164 const base::Value& arg) {
155 DCHECK(base::IsStringASCII(function_name)); 165 DCHECK(base::IsStringASCII(function_name));
156 std::vector<const base::Value*> args; 166 std::vector<const base::Value*> args;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 // WebUIImpl, protected: ------------------------------------------------------- 236 // WebUIImpl, protected: -------------------------------------------------------
227 237
228 void WebUIImpl::AddMessageHandler(WebUIMessageHandler* handler) { 238 void WebUIImpl::AddMessageHandler(WebUIMessageHandler* handler) {
229 DCHECK(!handler->web_ui()); 239 DCHECK(!handler->web_ui());
230 handler->set_web_ui(this); 240 handler->set_web_ui(this);
231 handler->RegisterMessages(); 241 handler->RegisterMessages();
232 handlers_.push_back(handler); 242 handlers_.push_back(handler);
233 } 243 }
234 244
235 void WebUIImpl::ExecuteJavascript(const base::string16& javascript) { 245 void WebUIImpl::ExecuteJavascript(const base::string16& javascript) {
236 RenderFrameHost* target_frame = TargetFrame(); 246 // Silently ignore the request. Would be nice to clean-up WebUI so we
237 if (target_frame) { 247 // could turn this into a CHECK(). http://crbug.com/516690.
238 if (!(ChildProcessSecurityPolicyImpl::GetInstance()->HasWebUIBindings( 248 if (!CanCallJavascript())
239 target_frame->GetProcess()->GetID()) || 249 return;
240 // It's possible to load about:blank in a Web UI renderer. 250
241 // See http://crbug.com/42547 251 TargetFrame()->ExecuteJavaScript(javascript);
242 target_frame->GetLastCommittedURL().spec() == url::kAboutBlankURL)) {
243 // Silently ignore the request. Would be nice to clean-up WebUI so we
244 // could turn this into a CHECK(). http://crbug.com/516690.
245 return;
246 }
247 target_frame->ExecuteJavaScript(javascript);
248 }
249 } 252 }
250 253
251 RenderFrameHost* WebUIImpl::TargetFrame() { 254 RenderFrameHost* WebUIImpl::TargetFrame() {
252 if (frame_name_.empty()) 255 if (frame_name_.empty())
253 return web_contents_->GetMainFrame(); 256 return web_contents_->GetMainFrame();
254 257
255 std::set<RenderFrameHost*> frame_set; 258 std::set<RenderFrameHost*> frame_set;
256 web_contents_->ForEachFrame(base::Bind(&WebUIImpl::AddToSetIfFrameNameMatches, 259 web_contents_->ForEachFrame(base::Bind(&WebUIImpl::AddToSetIfFrameNameMatches,
257 base::Unretained(this), 260 base::Unretained(this),
258 &frame_set)); 261 &frame_set));
259 262
260 // It happens that some sub-pages attempt to send JavaScript messages before 263 // It happens that some sub-pages attempt to send JavaScript messages before
261 // their frames are loaded. 264 // their frames are loaded.
262 DCHECK_GE(1U, frame_set.size()); 265 DCHECK_GE(1U, frame_set.size());
263 if (frame_set.empty()) 266 if (frame_set.empty())
264 return NULL; 267 return NULL;
265 return *frame_set.begin(); 268 return *frame_set.begin();
266 } 269 }
267 270
268 void WebUIImpl::AddToSetIfFrameNameMatches( 271 void WebUIImpl::AddToSetIfFrameNameMatches(
269 std::set<RenderFrameHost*>* frame_set, 272 std::set<RenderFrameHost*>* frame_set,
270 RenderFrameHost* host) { 273 RenderFrameHost* host) {
271 if (host->GetFrameName() == frame_name_) 274 if (host->GetFrameName() == frame_name_)
272 frame_set->insert(host); 275 frame_set->insert(host);
273 } 276 }
274 277
275 } // namespace content 278 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698