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 | |
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) { | |
63 const Extension* extension = extensions->GetByID(extension_id); | |
64 if (!extension) | |
65 return scoped_ptr<ExtensionInjectionHost>(); | |
66 return scoped_ptr<ExtensionInjectionHost>( | |
67 new ExtensionInjectionHost(extension)); | |
68 } | |
69 | |
70 } // namespace | 56 } // namespace |
71 | 57 |
72 class ScriptInjectionManager::RVOHelper : public content::RenderViewObserver { | 58 class ScriptInjectionManager::RVOHelper : public content::RenderViewObserver { |
73 public: | 59 public: |
74 RVOHelper(content::RenderView* render_view, ScriptInjectionManager* manager); | 60 RVOHelper(content::RenderView* render_view, ScriptInjectionManager* manager); |
75 ~RVOHelper() override; | 61 ~RVOHelper() override; |
76 | 62 |
77 private: | 63 private: |
78 // RenderViewObserver implementation. | 64 // RenderViewObserver implementation. |
79 bool OnMessageReceived(const IPC::Message& message) override; | 65 bool OnMessageReceived(const IPC::Message& message) override; |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 } | 235 } |
250 | 236 |
251 ScriptInjectionManager::~ScriptInjectionManager() { | 237 ScriptInjectionManager::~ScriptInjectionManager() { |
252 } | 238 } |
253 | 239 |
254 void ScriptInjectionManager::OnRenderViewCreated( | 240 void ScriptInjectionManager::OnRenderViewCreated( |
255 content::RenderView* render_view) { | 241 content::RenderView* render_view) { |
256 rvo_helpers_.push_back(new RVOHelper(render_view, this)); | 242 rvo_helpers_.push_back(new RVOHelper(render_view, this)); |
257 } | 243 } |
258 | 244 |
245 void ScriptInjectionManager::OnExtensionUnloaded( | |
246 const std::string& extension_id) { | |
247 for (auto iter = pending_injections_.begin(); | |
248 iter != pending_injections_.end();) { | |
249 if ((*iter)->host_id().id() == extension_id) { | |
250 (*iter)->OnHostRemoved(); | |
251 iter = pending_injections_.erase(iter); | |
252 } | |
253 else { | |
Devlin
2015/03/02 23:14:57
Bracketing.
Xi Han
2015/03/03 00:03:31
Miss the indent part. Done.
| |
254 ++iter; | |
255 } | |
256 } | |
257 // If we are currently injection scripts, we need to make a note that this | |
258 // extension is "dirty" (invalidated). | |
259 if (injecting_scripts_) | |
260 invalidated_while_injecting_.insert(extension_id); | |
261 } | |
262 | |
259 void ScriptInjectionManager::OnUserScriptsUpdated( | 263 void ScriptInjectionManager::OnUserScriptsUpdated( |
260 const std::set<std::string>& changed_extensions, | 264 const std::set<std::string>& changed_extensions, |
261 const std::vector<UserScript*>& scripts) { | 265 const std::vector<UserScript*>& scripts) { |
262 for (ScopedVector<ScriptInjection>::iterator iter = | 266 for (ScopedVector<ScriptInjection>::iterator iter = |
263 pending_injections_.begin(); | 267 pending_injections_.begin(); |
264 iter != pending_injections_.end();) { | 268 iter != pending_injections_.end();) { |
265 if (changed_extensions.count((*iter)->host_id().id()) > 0) | 269 if (changed_extensions.count((*iter)->host_id().id()) > 0) |
266 iter = pending_injections_.erase(iter); | 270 iter = pending_injections_.erase(iter); |
267 else | 271 else |
268 ++iter; | 272 ++iter; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 &frame_injections, frame, tab_id, run_location); | 389 &frame_injections, frame, tab_id, run_location); |
386 | 390 |
387 ScriptsRunInfo scripts_run_info; | 391 ScriptsRunInfo scripts_run_info; |
388 for (ScopedVector<ScriptInjection>::iterator iter = frame_injections.begin(); | 392 for (ScopedVector<ScriptInjection>::iterator iter = frame_injections.begin(); |
389 iter != frame_injections.end();) { | 393 iter != frame_injections.end();) { |
390 // If a blocking script was injected, there is potentially a possibility | 394 // If a blocking script was injected, there is potentially a possibility |
391 // that the frame has been invalidated in the time since. Check. | 395 // that the frame has been invalidated in the time since. Check. |
392 if (!IsFrameValid(frame)) | 396 if (!IsFrameValid(frame)) |
393 break; | 397 break; |
394 | 398 |
395 const std::string& extension_id = (*iter)->host_id().id(); | 399 // Try to inject the script if the injection host is not "dirty" |
396 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | 400 // (invalidated by an update). If the injection does not finish |
397 GetExtensionInjectionHost(extension_id, extensions_); | 401 // (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 | 402 // injections. |
399 // an update). If the injection does not finish (i.e., it is waiting for | 403 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, | 404 !(*iter)->TryToInject(run_location, |
403 extension_injection_host.get(), | |
404 &scripts_run_info)) { | 405 &scripts_run_info)) { |
405 pending_injections_.insert(pending_injections_.begin(), *iter); | 406 pending_injections_.insert(pending_injections_.begin(), *iter); |
406 iter = frame_injections.weak_erase(iter); | 407 iter = frame_injections.weak_erase(iter); |
407 } else { | 408 } else { |
408 ++iter; | 409 ++iter; |
409 } | 410 } |
410 } | 411 } |
411 | 412 |
412 if (IsFrameValid(frame)) | 413 if (IsFrameValid(frame)) |
413 scripts_run_info.LogRun(frame, run_location); | 414 scripts_run_info.LogRun(frame, run_location); |
(...skipping 12 matching lines...) Expand all Loading... | |
426 if (!main_frame) { | 427 if (!main_frame) { |
427 render_view->Send( | 428 render_view->Send( |
428 new ExtensionHostMsg_ExecuteCodeFinished(render_view->GetRoutingID(), | 429 new ExtensionHostMsg_ExecuteCodeFinished(render_view->GetRoutingID(), |
429 params.request_id, | 430 params.request_id, |
430 "No main frame", | 431 "No main frame", |
431 GURL(std::string()), | 432 GURL(std::string()), |
432 base::ListValue())); | 433 base::ListValue())); |
433 return; | 434 return; |
434 } | 435 } |
435 | 436 |
437 scoped_ptr<const ExtensionInjectionHost> extension_injection_host = | |
438 ExtensionInjectionHost::Create(params.extension_id, extensions_); | |
439 | |
440 if (!extension_injection_host) | |
441 return; | |
442 | |
436 scoped_ptr<ScriptInjection> injection(new ScriptInjection( | 443 scoped_ptr<ScriptInjection> injection(new ScriptInjection( |
437 scoped_ptr<ScriptInjector>( | 444 scoped_ptr<ScriptInjector>( |
438 new ProgrammaticScriptInjector(params, main_frame)), | 445 new ProgrammaticScriptInjector(params, main_frame)), |
439 main_frame, | 446 main_frame, |
440 HostID(HostID::EXTENSIONS, params.extension_id), | 447 extension_injection_host.Pass(), |
441 static_cast<UserScript::RunLocation>(params.run_at), | 448 static_cast<UserScript::RunLocation>(params.run_at), |
442 ExtensionHelper::Get(render_view)->tab_id())); | 449 ExtensionHelper::Get(render_view)->tab_id())); |
443 | 450 |
444 ScriptsRunInfo scripts_run_info; | 451 ScriptsRunInfo scripts_run_info; |
445 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); | 452 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); |
446 | 453 |
447 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | |
448 GetExtensionInjectionHost(injection->host_id().id(), extensions_); | |
449 | |
450 if (!injection->TryToInject( | 454 if (!injection->TryToInject( |
451 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, | 455 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, |
452 extension_injection_host.get(), | |
453 &scripts_run_info)) { | 456 &scripts_run_info)) { |
454 pending_injections_.push_back(injection.release()); | 457 pending_injections_.push_back(injection.release()); |
455 } | 458 } |
456 } | 459 } |
457 | 460 |
458 void ScriptInjectionManager::HandleExecuteDeclarativeScript( | 461 void ScriptInjectionManager::HandleExecuteDeclarativeScript( |
459 blink::WebFrame* web_frame, | 462 blink::WebFrame* web_frame, |
460 int tab_id, | 463 int tab_id, |
461 const ExtensionId& extension_id, | 464 const ExtensionId& extension_id, |
462 int script_id, | 465 int script_id, |
463 const GURL& url) { | 466 const GURL& url) { |
464 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | |
465 GetExtensionInjectionHost(extension_id, extensions_); | |
466 const Extension* extension = extensions_->GetByID(extension_id); | |
467 // TODO(dcheng): This function signature should really be a WebLocalFrame, | 467 // TODO(dcheng): This function signature should really be a WebLocalFrame, |
468 // rather than trying to coerce it here. | 468 // rather than trying to coerce it here. |
469 scoped_ptr<ScriptInjection> injection = | 469 scoped_ptr<ScriptInjection> injection = |
470 user_script_set_manager_->GetInjectionForDeclarativeScript( | 470 user_script_set_manager_->GetInjectionForDeclarativeScript( |
471 script_id, | 471 script_id, |
472 web_frame->toWebLocalFrame(), | 472 web_frame->toWebLocalFrame(), |
473 tab_id, | 473 tab_id, |
474 url, | 474 url, |
475 extension); | 475 extension_id); |
476 if (injection.get()) { | 476 if (injection) { |
477 ScriptsRunInfo scripts_run_info; | 477 ScriptsRunInfo scripts_run_info; |
478 // TODO(markdittmer): Use return value of TryToInject for error handling. | 478 // TODO(markdittmer): Use return value of TryToInject for error handling. |
479 injection->TryToInject(UserScript::BROWSER_DRIVEN, | 479 injection->TryToInject(UserScript::BROWSER_DRIVEN, |
480 extension_injection_host.get(), | |
481 &scripts_run_info); | 480 &scripts_run_info); |
482 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); | 481 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); |
483 } | 482 } |
484 } | 483 } |
485 | 484 |
486 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { | 485 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { |
487 ScopedVector<ScriptInjection>::iterator iter = | 486 ScopedVector<ScriptInjection>::iterator iter = |
488 pending_injections_.begin(); | 487 pending_injections_.begin(); |
489 for (; iter != pending_injections_.end(); ++iter) { | 488 for (; iter != pending_injections_.end(); ++iter) { |
490 if ((*iter)->request_id() == request_id) | 489 if ((*iter)->request_id() == request_id) { |
490 DCHECK((*iter)->host_id().type() == HostID::EXTENSIONS); | |
491 break; | 491 break; |
492 } | |
492 } | 493 } |
493 if (iter == pending_injections_.end()) | 494 if (iter == pending_injections_.end()) |
494 return; | 495 return; |
495 | 496 |
496 // At this point, because the request is present in pending_injections_, we | 497 // 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, | 498 // know that this is the same page that issued the request (otherwise, |
498 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be | 499 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be |
499 // cleared out). | 500 // cleared out). |
500 | 501 |
501 scoped_ptr<ScriptInjection> injection(*iter); | 502 scoped_ptr<ScriptInjection> injection(*iter); |
502 pending_injections_.weak_erase(iter); | 503 pending_injections_.weak_erase(iter); |
503 | 504 |
504 ScriptsRunInfo scripts_run_info; | 505 ScriptsRunInfo scripts_run_info; |
505 scoped_ptr<ExtensionInjectionHost> extension_injection_host = | 506 if (injection->OnPermissionGranted(&scripts_run_info)) { |
506 GetExtensionInjectionHost(injection->host_id().id(), extensions_); | |
507 if (injection->OnPermissionGranted(extension_injection_host.get(), | |
508 &scripts_run_info)) { | |
509 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); | 507 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); |
510 } | 508 } |
511 } | 509 } |
512 | 510 |
513 } // namespace extensions | 511 } // namespace extensions |
OLD | NEW |