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

Side by Side Diff: extensions/renderer/script_injection.cc

Issue 2213603002: Prevent duplicate content script injection defined in manifest.json (reland) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed patch 7 code review comments Created 4 years, 3 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/renderer/script_injection.h" 5 #include "extensions/renderer/script_injection.h"
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 ScriptInjector::InjectFailureReason reason) { 206 ScriptInjector::InjectFailureReason reason) {
207 complete_ = true; 207 complete_ = true;
208 injector_->OnWillNotInject(reason, render_frame_); 208 injector_->OnWillNotInject(reason, render_frame_);
209 } 209 }
210 210
211 ScriptInjection::InjectionResult ScriptInjection::Inject( 211 ScriptInjection::InjectionResult ScriptInjection::Inject(
212 ScriptsRunInfo* scripts_run_info) { 212 ScriptsRunInfo* scripts_run_info) {
213 DCHECK(injection_host_); 213 DCHECK(injection_host_);
214 DCHECK(scripts_run_info); 214 DCHECK(scripts_run_info);
215 DCHECK(!complete_); 215 DCHECK(!complete_);
216 bool should_inject_js = injector_->ShouldInjectJs(
217 run_location_, scripts_run_info->executing_scripts);
218 bool should_inject_css = injector_->ShouldInjectCss(
219 run_location_, scripts_run_info->executing_scripts);
216 220
217 bool should_inject_js = injector_->ShouldInjectJs(run_location_); 221 // This can happen if the extension specified a script to
218 bool should_inject_css = injector_->ShouldInjectCss(run_location_); 222 // be run in multiple rules, and the script has already run.
219 DCHECK(should_inject_js || should_inject_css); 223 // See crbug.com/631247.
224 if (!should_inject_js && !should_inject_css) {
225 return INJECTION_FINISHED;
226 }
220 227
221 if (should_inject_js) 228 if (should_inject_js)
222 InjectJs(); 229 InjectJs(&(scripts_run_info->executing_scripts),
230 &(scripts_run_info->num_js));
223 if (should_inject_css) 231 if (should_inject_css)
224 InjectCss(); 232 InjectCss(&(scripts_run_info->executing_scripts),
233 &(scripts_run_info->num_js));
Devlin 2016/09/06 17:21:13 shouldn't this be num_css?
catmullings 2016/09/07 01:00:35 Yup! Thanks for the catch.
catmullings 2016/09/07 01:00:35 Done.
225 234
226 complete_ = did_inject_js_ || !should_inject_js; 235 complete_ = did_inject_js_ || !should_inject_js;
227 236
228 injector_->GetRunInfo(scripts_run_info, run_location_);
229
230 if (complete_) { 237 if (complete_) {
231 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, 238 injector_->OnInjectionComplete(std::move(execution_result_), run_location_,
232 render_frame_); 239 render_frame_);
233 } else { 240 } else {
234 ++scripts_run_info->num_blocking_js; 241 ++scripts_run_info->num_blocking_js;
235 } 242 }
236 243
237 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED; 244 return complete_ ? INJECTION_FINISHED : INJECTION_BLOCKED;
238 } 245 }
239 246
240 void ScriptInjection::InjectJs() { 247 void ScriptInjection::InjectJs(
248 std::map<std::string, std::set<std::string>>* executing_scripts,
249 size_t* num_injected_js_script) {
241 DCHECK(!did_inject_js_); 250 DCHECK(!did_inject_js_);
242 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); 251 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame();
243 std::vector<blink::WebScriptSource> sources = 252 std::vector<blink::WebScriptSource> sources = injector_->GetJsSources(
244 injector_->GetJsSources(run_location_); 253 run_location_, executing_scripts, num_injected_js_script);
254 DCHECK(!sources.empty());
245 bool in_main_world = injector_->ShouldExecuteInMainWorld(); 255 bool in_main_world = injector_->ShouldExecuteInMainWorld();
246 int world_id = in_main_world 256 int world_id = in_main_world
247 ? DOMActivityLogger::kMainWorldId 257 ? DOMActivityLogger::kMainWorldId
248 : GetIsolatedWorldIdForInstance(injection_host_.get(), 258 : GetIsolatedWorldIdForInstance(injection_host_.get(),
249 web_frame); 259 web_frame);
250 bool is_user_gesture = injector_->IsUserGesture(); 260 bool is_user_gesture = injector_->IsUserGesture();
251 261
252 std::unique_ptr<blink::WebScriptExecutionCallback> callback( 262 std::unique_ptr<blink::WebScriptExecutionCallback> callback(
253 new ScriptInjectionCallback( 263 new ScriptInjectionCallback(
254 base::Bind(&ScriptInjection::OnJsInjectionCompleted, 264 base::Bind(&ScriptInjection::OnJsInjectionCompleted,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 // If |async_completion_callback_| is set, it means the script finished 314 // If |async_completion_callback_| is set, it means the script finished
305 // asynchronously, and we should run it. 315 // asynchronously, and we should run it.
306 if (!async_completion_callback_.is_null()) { 316 if (!async_completion_callback_.is_null()) {
307 injector_->OnInjectionComplete(std::move(execution_result_), run_location_, 317 injector_->OnInjectionComplete(std::move(execution_result_), run_location_,
308 render_frame_); 318 render_frame_);
309 // Warning: this object can be destroyed after this line! 319 // Warning: this object can be destroyed after this line!
310 async_completion_callback_.Run(this); 320 async_completion_callback_.Run(this);
311 } 321 }
312 } 322 }
313 323
314 void ScriptInjection::InjectCss() { 324 void ScriptInjection::InjectCss(
315 std::vector<blink::WebString> css_sources = 325 std::map<std::string, std::set<std::string>>* executing_scripts,
316 injector_->GetCssSources(run_location_); 326 size_t* num_injected_css_scripts) {
327 std::vector<blink::WebString> css_sources = injector_->GetCssSources(
328 run_location_, executing_scripts, num_injected_css_scripts);
317 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); 329 blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame();
318 for (const blink::WebString& css : css_sources) 330 for (const blink::WebString& css : css_sources)
319 web_frame->document().insertStyleSheet(css); 331 web_frame->document().insertStyleSheet(css);
320 } 332 }
321 333
322 } // namespace extensions 334 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698