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" |
| 11 #include "content/public/renderer/render_thread.h" | 11 #include "content/public/renderer/render_thread.h" |
| 12 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
| 13 #include "content/public/renderer/render_view_observer.h" | 13 #include "content/public/renderer/render_view_observer.h" |
| 14 #include "extensions/common/extension.h" | 14 #include "extensions/common/extension.h" |
| 15 #include "extensions/common/extension_consumer.h" | |
| 15 #include "extensions/common/extension_messages.h" | 16 #include "extensions/common/extension_messages.h" |
| 16 #include "extensions/common/extension_set.h" | 17 #include "extensions/common/extension_set.h" |
| 17 #include "extensions/renderer/extension_helper.h" | 18 #include "extensions/renderer/extension_helper.h" |
| 18 #include "extensions/renderer/programmatic_script_injector.h" | 19 #include "extensions/renderer/programmatic_script_injector.h" |
| 19 #include "extensions/renderer/script_injection.h" | 20 #include "extensions/renderer/script_injection.h" |
| 20 #include "extensions/renderer/scripts_run_info.h" | 21 #include "extensions/renderer/scripts_run_info.h" |
| 21 #include "ipc/ipc_message_macros.h" | 22 #include "ipc/ipc_message_macros.h" |
| 22 #include "third_party/WebKit/public/web/WebDocument.h" | 23 #include "third_party/WebKit/public/web/WebDocument.h" |
| 23 #include "third_party/WebKit/public/web/WebFrame.h" | 24 #include "third_party/WebKit/public/web/WebFrame.h" |
| 24 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 25 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 content::RenderView* render_view) { | 241 content::RenderView* render_view) { |
| 241 rvo_helpers_.push_back(new RVOHelper(render_view, this)); | 242 rvo_helpers_.push_back(new RVOHelper(render_view, this)); |
| 242 } | 243 } |
| 243 | 244 |
| 244 void ScriptInjectionManager::OnUserScriptsUpdated( | 245 void ScriptInjectionManager::OnUserScriptsUpdated( |
| 245 const std::set<std::string>& changed_extensions, | 246 const std::set<std::string>& changed_extensions, |
| 246 const std::vector<UserScript*>& scripts) { | 247 const std::vector<UserScript*>& scripts) { |
| 247 for (ScopedVector<ScriptInjection>::iterator iter = | 248 for (ScopedVector<ScriptInjection>::iterator iter = |
| 248 pending_injections_.begin(); | 249 pending_injections_.begin(); |
| 249 iter != pending_injections_.end();) { | 250 iter != pending_injections_.end();) { |
| 250 if (changed_extensions.count((*iter)->extension_id()) > 0) | 251 if (changed_extensions.count((*iter)->host_id().id()) > 0) |
| 251 iter = pending_injections_.erase(iter); | 252 iter = pending_injections_.erase(iter); |
| 252 else | 253 else |
| 253 ++iter; | 254 ++iter; |
| 254 } | 255 } |
| 255 | 256 |
| 256 // If we are currently injecting scripts, we need to make a note that these | 257 // If we are currently injecting scripts, we need to make a note that these |
| 257 // extensions were updated. | 258 // extensions were updated. |
| 258 if (injecting_scripts_) { | 259 if (injecting_scripts_) { |
| 259 invalidated_while_injecting_.insert(changed_extensions.begin(), | 260 invalidated_while_injecting_.insert(changed_extensions.begin(), |
| 260 changed_extensions.end()); | 261 changed_extensions.end()); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 &frame_injections, frame, tab_id, run_location); | 371 &frame_injections, frame, tab_id, run_location); |
| 371 | 372 |
| 372 ScriptsRunInfo scripts_run_info; | 373 ScriptsRunInfo scripts_run_info; |
| 373 for (ScopedVector<ScriptInjection>::iterator iter = frame_injections.begin(); | 374 for (ScopedVector<ScriptInjection>::iterator iter = frame_injections.begin(); |
| 374 iter != frame_injections.end();) { | 375 iter != frame_injections.end();) { |
| 375 // If a blocking script was injected, there is potentially a possibility | 376 // If a blocking script was injected, there is potentially a possibility |
| 376 // that the frame has been invalidated in the time since. Check. | 377 // that the frame has been invalidated in the time since. Check. |
| 377 if (!IsFrameValid(frame)) | 378 if (!IsFrameValid(frame)) |
| 378 break; | 379 break; |
| 379 | 380 |
| 381 const std::string extension_id = (*iter)->host_id().id(); | |
| 382 scoped_ptr<ExtensionConsumer> extension_consumer( | |
| 383 new ExtensionConsumer( | |
|
Devlin
2015/02/04 17:01:14
This breaks.
ScriptInjection checks that |extensi
Xi Han
2015/02/05 16:06:21
You are absolutely right. In ScriptInjection class
| |
| 384 const_cast<Extension*>(extensions_->GetByID(extension_id)), | |
| 385 HostID(HostID::EXTENSIONS, extension_id))); | |
| 380 // Try to inject the script if the extension is not "dirty" (invalidated by | 386 // Try to inject the script if the extension is not "dirty" (invalidated by |
| 381 // an update). If the injection does not finish (i.e., it is waiting for | 387 // an update). If the injection does not finish (i.e., it is waiting for |
| 382 // permission), add it to the list of pending injections. | 388 // permission), add it to the list of pending injections. |
| 383 if (invalidated_while_injecting_.count((*iter)->extension_id()) == 0 && | 389 if (invalidated_while_injecting_.count(extension_id) == 0 && |
| 384 !(*iter)->TryToInject(run_location, | 390 !(*iter)->TryToInject(run_location, |
| 385 extensions_->GetByID((*iter)->extension_id()), | 391 extension_consumer.get(), |
| 386 &scripts_run_info)) { | 392 &scripts_run_info)) { |
| 387 pending_injections_.insert(pending_injections_.begin(), *iter); | 393 pending_injections_.insert(pending_injections_.begin(), *iter); |
| 388 iter = frame_injections.weak_erase(iter); | 394 iter = frame_injections.weak_erase(iter); |
| 389 } else { | 395 } else { |
| 390 ++iter; | 396 ++iter; |
| 391 } | 397 } |
| 392 } | 398 } |
| 393 | 399 |
| 394 if (IsFrameValid(frame)) | 400 if (IsFrameValid(frame)) |
| 395 scripts_run_info.LogRun(frame, run_location); | 401 scripts_run_info.LogRun(frame, run_location); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 412 "No main frame", | 418 "No main frame", |
| 413 GURL(std::string()), | 419 GURL(std::string()), |
| 414 base::ListValue())); | 420 base::ListValue())); |
| 415 return; | 421 return; |
| 416 } | 422 } |
| 417 | 423 |
| 418 scoped_ptr<ScriptInjection> injection(new ScriptInjection( | 424 scoped_ptr<ScriptInjection> injection(new ScriptInjection( |
| 419 scoped_ptr<ScriptInjector>( | 425 scoped_ptr<ScriptInjector>( |
| 420 new ProgrammaticScriptInjector(params, main_frame)), | 426 new ProgrammaticScriptInjector(params, main_frame)), |
| 421 main_frame, | 427 main_frame, |
| 422 params.extension_id, | 428 HostID(HostID::EXTENSIONS, params.extension_id), |
| 429 params.is_web_view ? params.instance_id : Host::kDefaultInstanceId, | |
| 423 static_cast<UserScript::RunLocation>(params.run_at), | 430 static_cast<UserScript::RunLocation>(params.run_at), |
| 424 ExtensionHelper::Get(render_view)->tab_id())); | 431 ExtensionHelper::Get(render_view)->tab_id())); |
| 425 | 432 |
| 426 ScriptsRunInfo scripts_run_info; | 433 ScriptsRunInfo scripts_run_info; |
| 427 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); | 434 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); |
| 435 | |
| 436 const std::string& extension_id = injection->host_id().id(); | |
| 437 scoped_ptr<ExtensionConsumer> extension_consumer(new ExtensionConsumer( | |
| 438 const_cast<Extension*>(extensions_->GetByID(extension_id)), | |
| 439 HostID(HostID::EXTENSIONS, extension_id))); | |
| 440 | |
| 428 if (!injection->TryToInject( | 441 if (!injection->TryToInject( |
| 429 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, | 442 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, |
| 430 extensions_->GetByID(injection->extension_id()), | 443 extension_consumer.get(), |
| 431 &scripts_run_info)) { | 444 &scripts_run_info)) { |
| 432 pending_injections_.push_back(injection.release()); | 445 pending_injections_.push_back(injection.release()); |
| 433 } | 446 } |
| 434 } | 447 } |
| 435 | 448 |
| 436 void ScriptInjectionManager::HandleExecuteDeclarativeScript( | 449 void ScriptInjectionManager::HandleExecuteDeclarativeScript( |
| 437 blink::WebFrame* web_frame, | 450 blink::WebFrame* web_frame, |
| 438 int tab_id, | 451 int tab_id, |
| 439 const ExtensionId& extension_id, | 452 const ExtensionId& extension_id, |
| 440 int script_id, | 453 int script_id, |
| 441 const GURL& url) { | 454 const GURL& url) { |
| 442 const Extension* extension = extensions_->GetByID(extension_id); | 455 const Extension* extension = extensions_->GetByID(extension_id); |
| 456 scoped_ptr<ExtensionConsumer> extension_consumer( | |
| 457 new ExtensionConsumer(const_cast<Extension*>(extension), | |
| 458 HostID(HostID::EXTENSIONS, extension_id))); | |
| 443 // TODO(dcheng): This function signature should really be a WebLocalFrame, | 459 // TODO(dcheng): This function signature should really be a WebLocalFrame, |
| 444 // rather than trying to coerce it here. | 460 // rather than trying to coerce it here. |
| 445 scoped_ptr<ScriptInjection> injection = | 461 scoped_ptr<ScriptInjection> injection = |
| 446 user_script_set_manager_->GetInjectionForDeclarativeScript( | 462 user_script_set_manager_->GetInjectionForDeclarativeScript( |
| 447 script_id, | 463 script_id, |
| 448 web_frame->toWebLocalFrame(), | 464 web_frame->toWebLocalFrame(), |
| 449 tab_id, | 465 tab_id, |
| 450 url, | 466 url, |
| 451 extension); | 467 extension); |
| 452 if (injection.get()) { | 468 if (injection.get()) { |
| 453 ScriptsRunInfo scripts_run_info; | 469 ScriptsRunInfo scripts_run_info; |
| 454 // TODO(markdittmer): Use return value of TryToInject for error handling. | 470 // TODO(markdittmer): Use return value of TryToInject for error handling. |
| 455 injection->TryToInject(UserScript::BROWSER_DRIVEN, | 471 injection->TryToInject(UserScript::BROWSER_DRIVEN, |
| 456 extension, | 472 extension_consumer.get(), |
| 457 &scripts_run_info); | 473 &scripts_run_info); |
| 458 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); | 474 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); |
| 459 } | 475 } |
| 460 } | 476 } |
| 461 | 477 |
| 462 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { | 478 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { |
| 463 ScopedVector<ScriptInjection>::iterator iter = | 479 ScopedVector<ScriptInjection>::iterator iter = |
| 464 pending_injections_.begin(); | 480 pending_injections_.begin(); |
| 465 for (; iter != pending_injections_.end(); ++iter) { | 481 for (; iter != pending_injections_.end(); ++iter) { |
| 466 if ((*iter)->request_id() == request_id) | 482 if ((*iter)->request_id() == request_id) |
| 467 break; | 483 break; |
| 468 } | 484 } |
| 469 if (iter == pending_injections_.end()) | 485 if (iter == pending_injections_.end()) |
| 470 return; | 486 return; |
| 471 | 487 |
| 472 // At this point, because the request is present in pending_injections_, we | 488 // At this point, because the request is present in pending_injections_, we |
| 473 // know that this is the same page that issued the request (otherwise, | 489 // know that this is the same page that issued the request (otherwise, |
| 474 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be | 490 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be |
| 475 // cleared out). | 491 // cleared out). |
| 476 | 492 |
| 477 scoped_ptr<ScriptInjection> injection(*iter); | 493 scoped_ptr<ScriptInjection> injection(*iter); |
| 478 pending_injections_.weak_erase(iter); | 494 pending_injections_.weak_erase(iter); |
| 479 | 495 |
| 480 ScriptsRunInfo scripts_run_info; | 496 ScriptsRunInfo scripts_run_info; |
| 481 if (injection->OnPermissionGranted(extensions_->GetByID( | 497 const std::string& extension_id = injection->host_id().id(); |
| 482 injection->extension_id()), | 498 scoped_ptr<ExtensionConsumer> extension_consumer(new ExtensionConsumer( |
| 499 const_cast<Extension*>(extensions_->GetByID(extension_id)), | |
| 500 HostID(HostID::EXTENSIONS, extension_id))); | |
| 501 if (injection->OnPermissionGranted(extension_consumer.get(), | |
| 483 &scripts_run_info)) { | 502 &scripts_run_info)) { |
| 484 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); | 503 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); |
| 485 } | 504 } |
| 486 } | 505 } |
| 487 | 506 |
| 488 } // namespace extensions | 507 } // namespace extensions |
| OLD | NEW |