OLD | NEW |
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_manager.h" | 5 #include "extensions/renderer/script_injection_manager.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 case UserScript::UNDEFINED: | 46 case UserScript::UNDEFINED: |
47 case UserScript::RUN_DEFERRED: | 47 case UserScript::RUN_DEFERRED: |
48 case UserScript::BROWSER_DRIVEN: | 48 case UserScript::BROWSER_DRIVEN: |
49 case UserScript::RUN_LOCATION_LAST: | 49 case UserScript::RUN_LOCATION_LAST: |
50 break; | 50 break; |
51 } | 51 } |
52 NOTREACHED(); | 52 NOTREACHED(); |
53 return UserScript::RUN_LOCATION_LAST; | 53 return UserScript::RUN_LOCATION_LAST; |
54 } | 54 } |
55 | 55 |
56 | |
57 // TODO(hanxi): let ScriptInjection own an InjectionHost to avoid constructing | 56 // TODO(hanxi): let ScriptInjection own an InjectionHost to avoid constructing |
58 // an ExtensionInjectionHost many times. | 57 // an ExtensionInjectionHost many times. |
59 // Note: the ScriptInjection should be able to know when the backing extension | 58 // Note: the ScriptInjection should be able to know when the backing extension |
60 // is removed. | 59 // is removed. |
61 scoped_ptr<ExtensionInjectionHost> GetExtensionInjectionHost( | 60 scoped_ptr<ExtensionInjectionHost> GetExtensionInjectionHost( |
62 const std::string& extension_id, const ExtensionSet* extensions) { | 61 const std::string& extension_id, const ExtensionSet* extensions) { |
63 const Extension* extension = extensions->GetByID(extension_id); | 62 const Extension* extension = extensions->GetByID(extension_id); |
64 if (!extension) | 63 if (!extension) |
65 return scoped_ptr<ExtensionInjectionHost>(); | 64 return scoped_ptr<ExtensionInjectionHost>(); |
66 return scoped_ptr<ExtensionInjectionHost>( | 65 return scoped_ptr<ExtensionInjectionHost>( |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 void ScriptInjectionManager::RVOHelper::InvalidateFrame( | 234 void ScriptInjectionManager::RVOHelper::InvalidateFrame( |
236 blink::WebFrame* frame) { | 235 blink::WebFrame* frame) { |
237 pending_idle_frames_.erase(frame); | 236 pending_idle_frames_.erase(frame); |
238 manager_->InvalidateForFrame(frame); | 237 manager_->InvalidateForFrame(frame); |
239 } | 238 } |
240 | 239 |
241 ScriptInjectionManager::ScriptInjectionManager( | 240 ScriptInjectionManager::ScriptInjectionManager( |
242 const ExtensionSet* extensions, | 241 const ExtensionSet* extensions, |
243 UserScriptSetManager* user_script_set_manager) | 242 UserScriptSetManager* user_script_set_manager) |
244 : extensions_(extensions), | 243 : extensions_(extensions), |
245 injecting_scripts_(false), | |
246 user_script_set_manager_(user_script_set_manager), | 244 user_script_set_manager_(user_script_set_manager), |
247 user_script_set_manager_observer_(this) { | 245 user_script_set_manager_observer_(this) { |
248 user_script_set_manager_observer_.Add(user_script_set_manager_); | 246 user_script_set_manager_observer_.Add(user_script_set_manager_); |
249 } | 247 } |
250 | 248 |
251 ScriptInjectionManager::~ScriptInjectionManager() { | 249 ScriptInjectionManager::~ScriptInjectionManager() { |
252 } | 250 } |
253 | 251 |
254 void ScriptInjectionManager::OnRenderViewCreated( | 252 void ScriptInjectionManager::OnRenderViewCreated( |
255 content::RenderView* render_view) { | 253 content::RenderView* render_view) { |
256 rvo_helpers_.push_back(new RVOHelper(render_view, this)); | 254 rvo_helpers_.push_back(new RVOHelper(render_view, this)); |
257 } | 255 } |
258 | 256 |
| 257 void ScriptInjectionManager::OnInjectionFinished( |
| 258 ScriptInjection* injection) { |
| 259 ScopedVector<ScriptInjection>::iterator iter = |
| 260 std::find(running_injections_.begin(), |
| 261 running_injections_.end(), |
| 262 injection); |
| 263 if (iter != running_injections_.end()) |
| 264 running_injections_.erase(iter); |
| 265 } |
| 266 |
259 void ScriptInjectionManager::OnUserScriptsUpdated( | 267 void ScriptInjectionManager::OnUserScriptsUpdated( |
260 const std::set<std::string>& changed_extensions, | 268 const std::set<std::string>& changed_extensions, |
261 const std::vector<UserScript*>& scripts) { | 269 const std::vector<UserScript*>& scripts) { |
262 for (ScopedVector<ScriptInjection>::iterator iter = | 270 for (ScopedVector<ScriptInjection>::iterator iter = |
263 pending_injections_.begin(); | 271 pending_injections_.begin(); |
264 iter != pending_injections_.end();) { | 272 iter != pending_injections_.end();) { |
265 if (changed_extensions.count((*iter)->host_id().id()) > 0) | 273 if (changed_extensions.count((*iter)->host_id().id()) > 0) |
266 iter = pending_injections_.erase(iter); | 274 iter = pending_injections_.erase(iter); |
267 else | 275 else |
268 ++iter; | 276 ++iter; |
269 } | 277 } |
270 | |
271 // If we are currently injecting scripts, we need to make a note that these | |
272 // extensions were updated. | |
273 if (injecting_scripts_) { | |
274 invalidated_while_injecting_.insert(changed_extensions.begin(), | |
275 changed_extensions.end()); | |
276 } | |
277 } | 278 } |
278 | 279 |
279 void ScriptInjectionManager::RemoveObserver(RVOHelper* helper) { | 280 void ScriptInjectionManager::RemoveObserver(RVOHelper* helper) { |
280 for (ScopedVector<RVOHelper>::iterator iter = rvo_helpers_.begin(); | 281 for (ScopedVector<RVOHelper>::iterator iter = rvo_helpers_.begin(); |
281 iter != rvo_helpers_.end(); | 282 iter != rvo_helpers_.end(); |
282 ++iter) { | 283 ++iter) { |
283 if (*iter == helper) { | 284 if (*iter == helper) { |
284 rvo_helpers_.erase(iter); | 285 rvo_helpers_.erase(iter); |
285 break; | 286 break; |
286 } | 287 } |
287 } | 288 } |
288 } | 289 } |
289 | 290 |
290 void ScriptInjectionManager::InvalidateForFrame(blink::WebFrame* frame) { | 291 void ScriptInjectionManager::InvalidateForFrame(blink::WebFrame* frame) { |
291 for (ScopedVector<ScriptInjection>::iterator iter = | 292 for (ScopedVector<ScriptInjection>::iterator iter = |
292 pending_injections_.begin(); | 293 pending_injections_.begin(); |
293 iter != pending_injections_.end();) { | 294 iter != pending_injections_.end();) { |
294 if ((*iter)->web_frame() == frame) | 295 if ((*iter)->web_frame() == frame) |
295 iter = pending_injections_.erase(iter); | 296 iter = pending_injections_.erase(iter); |
296 else | 297 else |
297 ++iter; | 298 ++iter; |
298 } | 299 } |
299 | 300 |
300 frame_statuses_.erase(frame); | 301 frame_statuses_.erase(frame); |
301 } | 302 } |
302 | 303 |
303 bool ScriptInjectionManager::IsFrameValid(blink::WebFrame* frame) const { | |
304 return frame_statuses_.find(frame) != frame_statuses_.end(); | |
305 } | |
306 | |
307 void ScriptInjectionManager::StartInjectScripts( | 304 void ScriptInjectionManager::StartInjectScripts( |
308 blink::WebFrame* frame, UserScript::RunLocation run_location) { | 305 blink::WebFrame* frame, UserScript::RunLocation run_location) { |
309 FrameStatusMap::iterator iter = frame_statuses_.find(frame); | 306 FrameStatusMap::iterator iter = frame_statuses_.find(frame); |
310 // We also don't execute if we detect that the run location is somehow out of | 307 // We also don't execute if we detect that the run location is somehow out of |
311 // order. This can happen if: | 308 // order. This can happen if: |
312 // - The first run location reported for the frame isn't DOCUMENT_START, or | 309 // - The first run location reported for the frame isn't DOCUMENT_START, or |
313 // - The run location reported doesn't immediately follow the previous | 310 // - The run location reported doesn't immediately follow the previous |
314 // reported run location. | 311 // reported run location. |
315 // We don't want to run because extensions may have requirements that scripts | 312 // We don't want to run because extensions may have requirements that scripts |
316 // running in an earlier run location have run by the time a later script | 313 // running in an earlier run location have run by the time a later script |
(...skipping 10 matching lines...) Expand all Loading... |
327 InvalidateForFrame(frame); | 324 InvalidateForFrame(frame); |
328 return; | 325 return; |
329 } else if (iter != frame_statuses_.end() && iter->second >= run_location) { | 326 } else if (iter != frame_statuses_.end() && iter->second >= run_location) { |
330 // Certain run location signals (like DidCreateDocumentElement) can happen | 327 // Certain run location signals (like DidCreateDocumentElement) can happen |
331 // multiple times. Ignore the subsequent signals. | 328 // multiple times. Ignore the subsequent signals. |
332 return; | 329 return; |
333 } | 330 } |
334 | 331 |
335 // Otherwise, all is right in the world, and we can get on with the | 332 // Otherwise, all is right in the world, and we can get on with the |
336 // injections! | 333 // injections! |
337 | |
338 frame_statuses_[frame] = run_location; | 334 frame_statuses_[frame] = run_location; |
339 | 335 InjectScripts(frame, run_location); |
340 // If a content script injects blocking code (such as a javascript alert()), | |
341 // then there is a chance that we are running in a nested message loop, and | |
342 // shouldn't inject scripts right now (to avoid conflicts). | |
343 if (!injecting_scripts_) { | |
344 InjectScripts(frame, run_location); | |
345 // As above, we might have been blocked, but that means that, in the mean | |
346 // time, it's possible the frame advanced. Inject any scripts for run | |
347 // locations that were registered, but never ran. | |
348 while ((iter = frame_statuses_.find(frame)) != frame_statuses_.end() && | |
349 iter->second > run_location) { | |
350 run_location = NextRunLocation(run_location); | |
351 DCHECK_LE(run_location, UserScript::DOCUMENT_IDLE); | |
352 InjectScripts(frame, run_location); | |
353 } | |
354 } | |
355 } | 336 } |
356 | 337 |
357 void ScriptInjectionManager::InjectScripts( | 338 void ScriptInjectionManager::InjectScripts( |
358 blink::WebFrame* frame, | 339 blink::WebFrame* frame, |
359 UserScript::RunLocation run_location) { | 340 UserScript::RunLocation run_location) { |
360 DCHECK(!injecting_scripts_); | |
361 DCHECK(invalidated_while_injecting_.empty()); | |
362 base::AutoReset<bool>(&injecting_scripts_, true); | |
363 | |
364 // Find any injections that want to run on the given frame. | 341 // Find any injections that want to run on the given frame. |
365 // We create a separate vector for these because there is a chance that | |
366 // injected scripts can block, which can create a nested message loop. When | |
367 // this happens, other signals (like IPCs) can cause |pending_injections_| to | |
368 // be changed, so we don't want to risk that. | |
369 ScopedVector<ScriptInjection> frame_injections; | 342 ScopedVector<ScriptInjection> frame_injections; |
370 for (ScopedVector<ScriptInjection>::iterator iter = | 343 for (ScopedVector<ScriptInjection>::iterator iter = |
371 pending_injections_.begin(); | 344 pending_injections_.begin(); |
372 iter != pending_injections_.end();) { | 345 iter != pending_injections_.end();) { |
373 if ((*iter)->web_frame() == frame) { | 346 if ((*iter)->web_frame() == frame) { |
374 frame_injections.push_back(*iter); | 347 frame_injections.push_back(*iter); |
375 iter = pending_injections_.weak_erase(iter); | 348 iter = pending_injections_.weak_erase(iter); |
376 } else { | 349 } else { |
377 ++iter; | 350 ++iter; |
378 } | 351 } |
379 } | 352 } |
380 | 353 |
381 // Add any injections for user scripts. | 354 // Add any injections for user scripts. |
382 int tab_id = ExtensionHelper::Get(content::RenderView::FromWebView( | 355 int tab_id = ExtensionHelper::Get(content::RenderView::FromWebView( |
383 frame->top()->view()))->tab_id(); | 356 frame->top()->view()))->tab_id(); |
384 user_script_set_manager_->GetAllInjections( | 357 user_script_set_manager_->GetAllInjections( |
385 &frame_injections, frame, tab_id, run_location); | 358 &frame_injections, frame, tab_id, run_location); |
386 | 359 |
387 ScriptsRunInfo scripts_run_info; | 360 ScriptsRunInfo scripts_run_info; |
388 for (ScopedVector<ScriptInjection>::iterator iter = frame_injections.begin(); | 361 std::vector<ScriptInjection*> released_injections; |
389 iter != frame_injections.end();) { | 362 frame_injections.release(&released_injections); |
390 // If a blocking script was injected, there is potentially a possibility | 363 for (ScriptInjection* injection : released_injections) |
391 // that the frame has been invalidated in the time since. Check. | 364 TryToInject(make_scoped_ptr(injection), run_location, &scripts_run_info); |
392 if (!IsFrameValid(frame)) | 365 |
| 366 scripts_run_info.LogRun(frame, run_location); |
| 367 } |
| 368 |
| 369 void ScriptInjectionManager::TryToInject( |
| 370 scoped_ptr<ScriptInjection> injection, |
| 371 UserScript::RunLocation run_location, |
| 372 ScriptsRunInfo* scripts_run_info) { |
| 373 scoped_ptr<ExtensionInjectionHost> extension_injection_host = |
| 374 GetExtensionInjectionHost(injection->host_id().id(), extensions_); |
| 375 // Try to inject the script. If the injection is waiting (i.e., for |
| 376 // permission), add it to the list of pending injections. If the injection |
| 377 // has blocked, add it to the list of running injections. |
| 378 switch (injection->TryToInject( |
| 379 run_location, |
| 380 extension_injection_host.get(), |
| 381 scripts_run_info, |
| 382 this)) { |
| 383 case ScriptInjection::INJECTION_WAITING: |
| 384 pending_injections_.push_back(injection.release()); |
| 385 case ScriptInjection::INJECTION_BLOCKED: |
| 386 running_injections_.push_back(injection.release()); |
393 break; | 387 break; |
394 | 388 case ScriptInjection::INJECTION_FINISHED: |
395 const std::string& extension_id = (*iter)->host_id().id(); | 389 break; |
396 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | |
397 GetExtensionInjectionHost(extension_id, extensions_); | |
398 // Try to inject the script if the extension is not "dirty" (invalidated by | |
399 // an update). If the injection does not finish (i.e., it is waiting for | |
400 // permission), add it to the list of pending injections. | |
401 if (invalidated_while_injecting_.count(extension_id) == 0 && | |
402 !(*iter)->TryToInject(run_location, | |
403 extension_injection_host.get(), | |
404 &scripts_run_info)) { | |
405 pending_injections_.insert(pending_injections_.begin(), *iter); | |
406 iter = frame_injections.weak_erase(iter); | |
407 } else { | |
408 ++iter; | |
409 } | |
410 } | 390 } |
411 | |
412 if (IsFrameValid(frame)) | |
413 scripts_run_info.LogRun(frame, run_location); | |
414 | |
415 invalidated_while_injecting_.clear(); | |
416 } | 391 } |
417 | 392 |
418 void ScriptInjectionManager::HandleExecuteCode( | 393 void ScriptInjectionManager::HandleExecuteCode( |
419 const ExtensionMsg_ExecuteCode_Params& params, | 394 const ExtensionMsg_ExecuteCode_Params& params, |
420 content::RenderView* render_view) { | 395 content::RenderView* render_view) { |
421 // TODO(dcheng): Not sure how this can happen today. In an OOPI world, it | 396 // TODO(dcheng): Not sure how this can happen today. In an OOPI world, it |
422 // would indicate a logic error--the browser must direct this request to the | 397 // would indicate a logic error--the browser must direct this request to the |
423 // right renderer process to begin with. | 398 // right renderer process to begin with. |
424 blink::WebLocalFrame* main_frame = | 399 blink::WebLocalFrame* main_frame = |
425 render_view->GetWebView()->mainFrame()->toWebLocalFrame(); | 400 render_view->GetWebView()->mainFrame()->toWebLocalFrame(); |
(...skipping 11 matching lines...) Expand all Loading... |
437 scoped_ptr<ScriptInjector>( | 412 scoped_ptr<ScriptInjector>( |
438 new ProgrammaticScriptInjector(params, main_frame)), | 413 new ProgrammaticScriptInjector(params, main_frame)), |
439 main_frame, | 414 main_frame, |
440 HostID(HostID::EXTENSIONS, params.extension_id), | 415 HostID(HostID::EXTENSIONS, params.extension_id), |
441 static_cast<UserScript::RunLocation>(params.run_at), | 416 static_cast<UserScript::RunLocation>(params.run_at), |
442 ExtensionHelper::Get(render_view)->tab_id())); | 417 ExtensionHelper::Get(render_view)->tab_id())); |
443 | 418 |
444 ScriptsRunInfo scripts_run_info; | 419 ScriptsRunInfo scripts_run_info; |
445 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); | 420 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); |
446 | 421 |
447 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | 422 TryToInject( |
448 GetExtensionInjectionHost(injection->host_id().id(), extensions_); | 423 injection.Pass(), |
449 | 424 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, |
450 if (!injection->TryToInject( | 425 &scripts_run_info); |
451 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, | |
452 extension_injection_host.get(), | |
453 &scripts_run_info)) { | |
454 pending_injections_.push_back(injection.release()); | |
455 } | |
456 } | 426 } |
457 | 427 |
458 void ScriptInjectionManager::HandleExecuteDeclarativeScript( | 428 void ScriptInjectionManager::HandleExecuteDeclarativeScript( |
459 blink::WebFrame* web_frame, | 429 blink::WebFrame* web_frame, |
460 int tab_id, | 430 int tab_id, |
461 const ExtensionId& extension_id, | 431 const ExtensionId& extension_id, |
462 int script_id, | 432 int script_id, |
463 const GURL& url) { | 433 const GURL& url) { |
464 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | |
465 GetExtensionInjectionHost(extension_id, extensions_); | |
466 const Extension* extension = extensions_->GetByID(extension_id); | 434 const Extension* extension = extensions_->GetByID(extension_id); |
467 // TODO(dcheng): This function signature should really be a WebLocalFrame, | 435 // TODO(dcheng): This function signature should really be a WebLocalFrame, |
468 // rather than trying to coerce it here. | 436 // rather than trying to coerce it here. |
469 scoped_ptr<ScriptInjection> injection = | 437 scoped_ptr<ScriptInjection> injection = |
470 user_script_set_manager_->GetInjectionForDeclarativeScript( | 438 user_script_set_manager_->GetInjectionForDeclarativeScript( |
471 script_id, | 439 script_id, |
472 web_frame->toWebLocalFrame(), | 440 web_frame->toWebLocalFrame(), |
473 tab_id, | 441 tab_id, |
474 url, | 442 url, |
475 extension); | 443 extension); |
476 if (injection.get()) { | 444 if (injection.get()) { |
477 ScriptsRunInfo scripts_run_info; | 445 ScriptsRunInfo scripts_run_info; |
| 446 |
478 // TODO(markdittmer): Use return value of TryToInject for error handling. | 447 // TODO(markdittmer): Use return value of TryToInject for error handling. |
479 injection->TryToInject(UserScript::BROWSER_DRIVEN, | 448 TryToInject(injection.Pass(), |
480 extension_injection_host.get(), | 449 UserScript::BROWSER_DRIVEN, |
481 &scripts_run_info); | 450 &scripts_run_info); |
| 451 |
482 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); | 452 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); |
483 } | 453 } |
484 } | 454 } |
485 | 455 |
486 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { | 456 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { |
487 ScopedVector<ScriptInjection>::iterator iter = | 457 ScopedVector<ScriptInjection>::iterator iter = |
488 pending_injections_.begin(); | 458 pending_injections_.begin(); |
489 for (; iter != pending_injections_.end(); ++iter) { | 459 for (; iter != pending_injections_.end(); ++iter) { |
490 if ((*iter)->request_id() == request_id) | 460 if ((*iter)->request_id() == request_id) |
491 break; | 461 break; |
492 } | 462 } |
493 if (iter == pending_injections_.end()) | 463 if (iter == pending_injections_.end()) |
494 return; | 464 return; |
495 | 465 |
496 // At this point, because the request is present in pending_injections_, we | 466 // At this point, because the request is present in pending_injections_, we |
497 // know that this is the same page that issued the request (otherwise, | 467 // know that this is the same page that issued the request (otherwise, |
498 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be | 468 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be |
499 // cleared out). | 469 // cleared out). |
500 | 470 |
501 scoped_ptr<ScriptInjection> injection(*iter); | 471 scoped_ptr<ScriptInjection> injection(*iter); |
502 pending_injections_.weak_erase(iter); | 472 pending_injections_.weak_erase(iter); |
503 | 473 |
504 ScriptsRunInfo scripts_run_info; | |
505 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | 474 scoped_ptr<ExtensionInjectionHost> extension_injection_host = |
506 GetExtensionInjectionHost(injection->host_id().id(), extensions_); | 475 GetExtensionInjectionHost(injection->host_id().id(), extensions_); |
507 if (injection->OnPermissionGranted(extension_injection_host.get(), | 476 ScriptsRunInfo scripts_run_info; |
508 &scripts_run_info)) { | 477 ScriptInjection::InjectionResult res = injection->OnPermissionGranted( |
| 478 extension_injection_host.get(), &scripts_run_info); |
| 479 if (res == ScriptInjection::INJECTION_BLOCKED) |
| 480 running_injections_.push_back(injection.Pass()); |
| 481 if (extension_injection_host.get()) |
509 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); | 482 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); |
510 } | |
511 } | 483 } |
512 | 484 |
513 } // namespace extensions | 485 } // namespace extensions |
OLD | NEW |