Chromium Code Reviews| 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 | 56 scoped_ptr<const ExtensionInjectionHost> GetExtensionInjectionHost( |
| 57 // TODO(hanxi): let ScriptInjection own an InjectionHost to avoid constructing | |
| 58 // an ExtensionInjectionHost many times. | |
| 59 // Note: the ScriptInjection should be able to know when the backing extension | |
| 60 // is removed. | |
| 61 scoped_ptr<ExtensionInjectionHost> GetExtensionInjectionHost( | |
| 62 const std::string& extension_id, const ExtensionSet* extensions) { | 57 const std::string& extension_id, const ExtensionSet* extensions) { |
| 63 const Extension* extension = extensions->GetByID(extension_id); | 58 const Extension* extension = extensions->GetByID(extension_id); |
| 64 if (!extension) | 59 if (!extension) |
| 65 return scoped_ptr<ExtensionInjectionHost>(); | 60 return scoped_ptr<const ExtensionInjectionHost>(); |
| 66 return scoped_ptr<ExtensionInjectionHost>( | 61 return scoped_ptr<const ExtensionInjectionHost>( |
| 67 new ExtensionInjectionHost(extension)); | 62 new ExtensionInjectionHost(extension)); |
| 68 } | 63 } |
| 69 | 64 |
| 70 } // namespace | 65 } // namespace |
| 71 | 66 |
| 72 class ScriptInjectionManager::RVOHelper : public content::RenderViewObserver { | 67 class ScriptInjectionManager::RVOHelper : public content::RenderViewObserver { |
| 73 public: | 68 public: |
| 74 RVOHelper(content::RenderView* render_view, ScriptInjectionManager* manager); | 69 RVOHelper(content::RenderView* render_view, ScriptInjectionManager* manager); |
| 75 ~RVOHelper() override; | 70 ~RVOHelper() override; |
| 76 | 71 |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 &frame_injections, frame, tab_id, run_location); | 380 &frame_injections, frame, tab_id, run_location); |
| 386 | 381 |
| 387 ScriptsRunInfo scripts_run_info; | 382 ScriptsRunInfo scripts_run_info; |
| 388 for (ScopedVector<ScriptInjection>::iterator iter = frame_injections.begin(); | 383 for (ScopedVector<ScriptInjection>::iterator iter = frame_injections.begin(); |
| 389 iter != frame_injections.end();) { | 384 iter != frame_injections.end();) { |
| 390 // If a blocking script was injected, there is potentially a possibility | 385 // If a blocking script was injected, there is potentially a possibility |
| 391 // that the frame has been invalidated in the time since. Check. | 386 // that the frame has been invalidated in the time since. Check. |
| 392 if (!IsFrameValid(frame)) | 387 if (!IsFrameValid(frame)) |
| 393 break; | 388 break; |
| 394 | 389 |
| 395 const std::string& extension_id = (*iter)->host_id().id(); | 390 // Try to inject the script if the injection host is not "dirty" |
| 396 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | 391 // (invalidated by an update). If the injection does not finish |
| 397 GetExtensionInjectionHost(extension_id, extensions_); | 392 // (i.e., it is waiting for permission), add it to the list of pending |
| 398 // Try to inject the script if the extension is not "dirty" (invalidated by | 393 // injections. |
| 399 // an update). If the injection does not finish (i.e., it is waiting for | 394 if (invalidated_while_injecting_.count((*iter)->host_id().id()) == 0 && |
| 400 // permission), add it to the list of pending injections. | |
| 401 if (invalidated_while_injecting_.count(extension_id) == 0 && | |
| 402 !(*iter)->TryToInject(run_location, | 395 !(*iter)->TryToInject(run_location, |
| 403 extension_injection_host.get(), | 396 (*iter)->GetInjectionHost(), |
|
Devlin
2015/02/17 18:56:52
This is a bit silly - the ScriptInjection owns the
Xi Han
2015/02/18 21:11:46
Removed, and also updated some other functions tha
| |
| 404 &scripts_run_info)) { | 397 &scripts_run_info)) { |
| 405 pending_injections_.insert(pending_injections_.begin(), *iter); | 398 pending_injections_.insert(pending_injections_.begin(), *iter); |
| 406 iter = frame_injections.weak_erase(iter); | 399 iter = frame_injections.weak_erase(iter); |
| 407 } else { | 400 } else { |
| 408 ++iter; | 401 ++iter; |
| 409 } | 402 } |
| 410 } | 403 } |
| 411 | 404 |
| 412 if (IsFrameValid(frame)) | 405 if (IsFrameValid(frame)) |
| 413 scripts_run_info.LogRun(frame, run_location); | 406 scripts_run_info.LogRun(frame, run_location); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 426 if (!main_frame) { | 419 if (!main_frame) { |
| 427 render_view->Send( | 420 render_view->Send( |
| 428 new ExtensionHostMsg_ExecuteCodeFinished(render_view->GetRoutingID(), | 421 new ExtensionHostMsg_ExecuteCodeFinished(render_view->GetRoutingID(), |
| 429 params.request_id, | 422 params.request_id, |
| 430 "No main frame", | 423 "No main frame", |
| 431 GURL(std::string()), | 424 GURL(std::string()), |
| 432 base::ListValue())); | 425 base::ListValue())); |
| 433 return; | 426 return; |
| 434 } | 427 } |
| 435 | 428 |
| 429 scoped_ptr<const ExtensionInjectionHost> extension_injection_host = | |
| 430 GetExtensionInjectionHost(params.extension_id, extensions_); | |
| 431 | |
| 432 if (!extension_injection_host) | |
| 433 return; | |
| 434 | |
| 436 scoped_ptr<ScriptInjection> injection(new ScriptInjection( | 435 scoped_ptr<ScriptInjection> injection(new ScriptInjection( |
| 437 scoped_ptr<ScriptInjector>( | 436 scoped_ptr<ScriptInjector>( |
| 438 new ProgrammaticScriptInjector(params, main_frame)), | 437 new ProgrammaticScriptInjector(params, main_frame)), |
| 439 main_frame, | 438 main_frame, |
| 440 HostID(HostID::EXTENSIONS, params.extension_id), | 439 extension_injection_host.Pass(), |
| 441 params.is_web_view ? UserScript::ConsumerInstanceType::WEBVIEW | 440 params.is_web_view ? UserScript::ConsumerInstanceType::WEBVIEW |
| 442 : UserScript::ConsumerInstanceType::TAB, | 441 : UserScript::ConsumerInstanceType::TAB, |
| 443 static_cast<UserScript::RunLocation>(params.run_at), | 442 static_cast<UserScript::RunLocation>(params.run_at), |
| 444 ExtensionHelper::Get(render_view)->tab_id())); | 443 ExtensionHelper::Get(render_view)->tab_id())); |
| 445 | 444 |
| 446 ScriptsRunInfo scripts_run_info; | 445 ScriptsRunInfo scripts_run_info; |
| 447 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); | 446 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); |
| 448 | 447 |
| 449 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | |
| 450 GetExtensionInjectionHost(injection->host_id().id(), extensions_); | |
| 451 | |
| 452 if (!injection->TryToInject( | 448 if (!injection->TryToInject( |
| 453 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, | 449 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, |
| 454 extension_injection_host.get(), | 450 injection->GetInjectionHost(), |
| 455 &scripts_run_info)) { | 451 &scripts_run_info)) { |
| 456 pending_injections_.push_back(injection.release()); | 452 pending_injections_.push_back(injection.release()); |
| 457 } | 453 } |
| 458 } | 454 } |
| 459 | 455 |
| 460 void ScriptInjectionManager::HandleExecuteDeclarativeScript( | 456 void ScriptInjectionManager::HandleExecuteDeclarativeScript( |
| 461 blink::WebFrame* web_frame, | 457 blink::WebFrame* web_frame, |
| 462 int tab_id, | 458 int tab_id, |
| 463 const ExtensionId& extension_id, | 459 const ExtensionId& extension_id, |
| 464 int script_id, | 460 int script_id, |
| 465 const GURL& url) { | 461 const GURL& url) { |
| 466 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | 462 scoped_ptr<const ExtensionInjectionHost> extension_injection_host = |
| 467 GetExtensionInjectionHost(extension_id, extensions_); | 463 GetExtensionInjectionHost(extension_id, extensions_); |
| 468 const Extension* extension = extensions_->GetByID(extension_id); | |
| 469 // TODO(dcheng): This function signature should really be a WebLocalFrame, | 464 // TODO(dcheng): This function signature should really be a WebLocalFrame, |
| 470 // rather than trying to coerce it here. | 465 // rather than trying to coerce it here. |
| 471 scoped_ptr<ScriptInjection> injection = | 466 scoped_ptr<ScriptInjection> injection = |
| 472 user_script_set_manager_->GetInjectionForDeclarativeScript( | 467 user_script_set_manager_->GetInjectionForDeclarativeScript( |
| 473 script_id, | 468 script_id, |
| 474 web_frame->toWebLocalFrame(), | 469 web_frame->toWebLocalFrame(), |
| 475 tab_id, | 470 tab_id, |
| 476 url, | 471 url, |
| 477 extension); | 472 extension_injection_host.get()); |
| 478 if (injection.get()) { | 473 if (injection.get()) { |
| 479 ScriptsRunInfo scripts_run_info; | 474 ScriptsRunInfo scripts_run_info; |
| 480 // TODO(markdittmer): Use return value of TryToInject for error handling. | 475 // TODO(markdittmer): Use return value of TryToInject for error handling. |
| 481 injection->TryToInject(UserScript::BROWSER_DRIVEN, | 476 injection->TryToInject(UserScript::BROWSER_DRIVEN, |
| 482 extension_injection_host.get(), | 477 extension_injection_host.get(), |
| 483 &scripts_run_info); | 478 &scripts_run_info); |
| 484 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); | 479 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); |
| 485 } | 480 } |
| 486 } | 481 } |
| 487 | 482 |
| 488 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { | 483 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { |
| 489 ScopedVector<ScriptInjection>::iterator iter = | 484 ScopedVector<ScriptInjection>::iterator iter = |
| 490 pending_injections_.begin(); | 485 pending_injections_.begin(); |
| 491 for (; iter != pending_injections_.end(); ++iter) { | 486 for (; iter != pending_injections_.end(); ++iter) { |
| 492 if ((*iter)->request_id() == request_id) | 487 if ((*iter)->request_id() == request_id && |
| 488 (*iter)->host_id().type() == HostID::EXTENSIONS) | |
| 493 break; | 489 break; |
| 494 } | 490 } |
| 495 if (iter == pending_injections_.end()) | 491 if (iter == pending_injections_.end()) |
| 496 return; | 492 return; |
| 497 | 493 |
| 498 // At this point, because the request is present in pending_injections_, we | 494 // At this point, because the request is present in pending_injections_, we |
| 499 // know that this is the same page that issued the request (otherwise, | 495 // know that this is the same page that issued the request (otherwise, |
| 500 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be | 496 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be |
| 501 // cleared out). | 497 // cleared out). |
| 502 | 498 |
| 503 scoped_ptr<ScriptInjection> injection(*iter); | 499 scoped_ptr<ScriptInjection> injection(*iter); |
| 504 pending_injections_.weak_erase(iter); | 500 pending_injections_.weak_erase(iter); |
| 505 | 501 |
| 506 ScriptsRunInfo scripts_run_info; | 502 ScriptsRunInfo scripts_run_info; |
| 507 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | 503 scoped_ptr<const ExtensionInjectionHost> extension_injection_host = |
| 508 GetExtensionInjectionHost(injection->host_id().id(), extensions_); | 504 GetExtensionInjectionHost(injection->host_id().id(), extensions_); |
| 509 if (injection->OnPermissionGranted(extension_injection_host.get(), | 505 if (injection->OnPermissionGranted(extension_injection_host.get(), |
| 510 &scripts_run_info)) { | 506 &scripts_run_info)) { |
| 511 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); | 507 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); |
| 512 } | 508 } |
| 513 } | 509 } |
| 514 | 510 |
| 515 } // namespace extensions | 511 } // namespace extensions |
| OLD | NEW |