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

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

Issue 885493007: Refactoring: de-couple Extensions from "script injection System" [render side] : 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Devlin@'s comments. Created 5 years, 10 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_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 20 matching lines...) Expand all
45 case UserScript::UNDEFINED: 46 case UserScript::UNDEFINED:
46 case UserScript::RUN_DEFERRED: 47 case UserScript::RUN_DEFERRED:
47 case UserScript::BROWSER_DRIVEN: 48 case UserScript::BROWSER_DRIVEN:
48 case UserScript::RUN_LOCATION_LAST: 49 case UserScript::RUN_LOCATION_LAST:
49 break; 50 break;
50 } 51 }
51 NOTREACHED(); 52 NOTREACHED();
52 return UserScript::RUN_LOCATION_LAST; 53 return UserScript::RUN_LOCATION_LAST;
53 } 54 }
54 55
56 scoped_ptr<ExtensionConsumer> GetExtensionConsumer(
57 const std::string& extension_id, const ExtensionSet* extensions) {
58 const Extension* extension = extensions->GetByID(extension_id);
59 return scoped_ptr<ExtensionConsumer>(new ExtensionConsumer(
60 extension, HostID(HostID::EXTENSIONS, extension_id)));
61 }
62
55 } // namespace 63 } // namespace
56 64
57 class ScriptInjectionManager::RVOHelper : public content::RenderViewObserver { 65 class ScriptInjectionManager::RVOHelper : public content::RenderViewObserver {
58 public: 66 public:
59 RVOHelper(content::RenderView* render_view, ScriptInjectionManager* manager); 67 RVOHelper(content::RenderView* render_view, ScriptInjectionManager* manager);
60 ~RVOHelper() override; 68 ~RVOHelper() override;
61 69
62 private: 70 private:
63 // RenderViewObserver implementation. 71 // RenderViewObserver implementation.
64 bool OnMessageReceived(const IPC::Message& message) override; 72 bool OnMessageReceived(const IPC::Message& message) override;
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 content::RenderView* render_view) { 248 content::RenderView* render_view) {
241 rvo_helpers_.push_back(new RVOHelper(render_view, this)); 249 rvo_helpers_.push_back(new RVOHelper(render_view, this));
242 } 250 }
243 251
244 void ScriptInjectionManager::OnUserScriptsUpdated( 252 void ScriptInjectionManager::OnUserScriptsUpdated(
245 const std::set<std::string>& changed_extensions, 253 const std::set<std::string>& changed_extensions,
246 const std::vector<UserScript*>& scripts) { 254 const std::vector<UserScript*>& scripts) {
247 for (ScopedVector<ScriptInjection>::iterator iter = 255 for (ScopedVector<ScriptInjection>::iterator iter =
248 pending_injections_.begin(); 256 pending_injections_.begin();
249 iter != pending_injections_.end();) { 257 iter != pending_injections_.end();) {
250 if (changed_extensions.count((*iter)->extension_id()) > 0) 258 if (changed_extensions.count((*iter)->host_id().id()) > 0)
251 iter = pending_injections_.erase(iter); 259 iter = pending_injections_.erase(iter);
252 else 260 else
253 ++iter; 261 ++iter;
254 } 262 }
255 263
256 // If we are currently injecting scripts, we need to make a note that these 264 // If we are currently injecting scripts, we need to make a note that these
257 // extensions were updated. 265 // extensions were updated.
258 if (injecting_scripts_) { 266 if (injecting_scripts_) {
259 invalidated_while_injecting_.insert(changed_extensions.begin(), 267 invalidated_while_injecting_.insert(changed_extensions.begin(),
260 changed_extensions.end()); 268 changed_extensions.end());
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 &frame_injections, frame, tab_id, run_location); 378 &frame_injections, frame, tab_id, run_location);
371 379
372 ScriptsRunInfo scripts_run_info; 380 ScriptsRunInfo scripts_run_info;
373 for (ScopedVector<ScriptInjection>::iterator iter = frame_injections.begin(); 381 for (ScopedVector<ScriptInjection>::iterator iter = frame_injections.begin();
374 iter != frame_injections.end();) { 382 iter != frame_injections.end();) {
375 // If a blocking script was injected, there is potentially a possibility 383 // If a blocking script was injected, there is potentially a possibility
376 // that the frame has been invalidated in the time since. Check. 384 // that the frame has been invalidated in the time since. Check.
377 if (!IsFrameValid(frame)) 385 if (!IsFrameValid(frame))
378 break; 386 break;
379 387
388 const std::string extension_id = (*iter)->host_id().id();
389 scoped_ptr<ExtensionConsumer> extension_consumer = GetExtensionConsumer(
390 extension_id, extensions_);
380 // Try to inject the script if the extension is not "dirty" (invalidated by 391 // 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 392 // an update). If the injection does not finish (i.e., it is waiting for
382 // permission), add it to the list of pending injections. 393 // permission), add it to the list of pending injections.
383 if (invalidated_while_injecting_.count((*iter)->extension_id()) == 0 && 394 if (invalidated_while_injecting_.count(extension_id) == 0 &&
384 !(*iter)->TryToInject(run_location, 395 !(*iter)->TryToInject(run_location,
385 extensions_->GetByID((*iter)->extension_id()), 396 extension_consumer.get(),
386 &scripts_run_info)) { 397 &scripts_run_info)) {
387 pending_injections_.insert(pending_injections_.begin(), *iter); 398 pending_injections_.insert(pending_injections_.begin(), *iter);
388 iter = frame_injections.weak_erase(iter); 399 iter = frame_injections.weak_erase(iter);
389 } else { 400 } else {
390 ++iter; 401 ++iter;
391 } 402 }
392 } 403 }
393 404
394 if (IsFrameValid(frame)) 405 if (IsFrameValid(frame))
395 scripts_run_info.LogRun(frame, run_location); 406 scripts_run_info.LogRun(frame, run_location);
(...skipping 16 matching lines...) Expand all
412 "No main frame", 423 "No main frame",
413 GURL(std::string()), 424 GURL(std::string()),
414 base::ListValue())); 425 base::ListValue()));
415 return; 426 return;
416 } 427 }
417 428
418 scoped_ptr<ScriptInjection> injection(new ScriptInjection( 429 scoped_ptr<ScriptInjection> injection(new ScriptInjection(
419 scoped_ptr<ScriptInjector>( 430 scoped_ptr<ScriptInjector>(
420 new ProgrammaticScriptInjector(params, main_frame)), 431 new ProgrammaticScriptInjector(params, main_frame)),
421 main_frame, 432 main_frame,
422 params.extension_id, 433 HostID(HostID::EXTENSIONS, params.extension_id),
434 params.is_web_view ? params.instance_id : Host::kDefaultInstanceId,
423 static_cast<UserScript::RunLocation>(params.run_at), 435 static_cast<UserScript::RunLocation>(params.run_at),
424 ExtensionHelper::Get(render_view)->tab_id())); 436 ExtensionHelper::Get(render_view)->tab_id()));
425 437
426 ScriptsRunInfo scripts_run_info; 438 ScriptsRunInfo scripts_run_info;
427 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame); 439 FrameStatusMap::const_iterator iter = frame_statuses_.find(main_frame);
440
441 scoped_ptr<ExtensionConsumer> extension_consumer = GetExtensionConsumer(
442 injection->host_id().id(), extensions_);
443
428 if (!injection->TryToInject( 444 if (!injection->TryToInject(
429 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second, 445 iter == frame_statuses_.end() ? UserScript::UNDEFINED : iter->second,
430 extensions_->GetByID(injection->extension_id()), 446 extension_consumer.get(),
431 &scripts_run_info)) { 447 &scripts_run_info)) {
432 pending_injections_.push_back(injection.release()); 448 pending_injections_.push_back(injection.release());
433 } 449 }
434 } 450 }
435 451
436 void ScriptInjectionManager::HandleExecuteDeclarativeScript( 452 void ScriptInjectionManager::HandleExecuteDeclarativeScript(
437 blink::WebFrame* web_frame, 453 blink::WebFrame* web_frame,
438 int tab_id, 454 int tab_id,
439 const ExtensionId& extension_id, 455 const ExtensionId& extension_id,
440 int script_id, 456 int script_id,
441 const GURL& url) { 457 const GURL& url) {
442 const Extension* extension = extensions_->GetByID(extension_id); 458 const Extension* extension = extensions_->GetByID(extension_id);
459 scoped_ptr<ExtensionConsumer> extension_consumer(new ExtensionConsumer(
460 const_cast<Extension*>(extension),
461 HostID(HostID::EXTENSIONS, extension_id)));
462
443 // TODO(dcheng): This function signature should really be a WebLocalFrame, 463 // TODO(dcheng): This function signature should really be a WebLocalFrame,
444 // rather than trying to coerce it here. 464 // rather than trying to coerce it here.
445 scoped_ptr<ScriptInjection> injection = 465 scoped_ptr<ScriptInjection> injection =
446 user_script_set_manager_->GetInjectionForDeclarativeScript( 466 user_script_set_manager_->GetInjectionForDeclarativeScript(
447 script_id, 467 script_id,
448 web_frame->toWebLocalFrame(), 468 web_frame->toWebLocalFrame(),
449 tab_id, 469 tab_id,
450 url, 470 url,
451 extension); 471 extension);
452 if (injection.get()) { 472 if (injection.get()) {
453 ScriptsRunInfo scripts_run_info; 473 ScriptsRunInfo scripts_run_info;
454 // TODO(markdittmer): Use return value of TryToInject for error handling. 474 // TODO(markdittmer): Use return value of TryToInject for error handling.
455 injection->TryToInject(UserScript::BROWSER_DRIVEN, 475 injection->TryToInject(UserScript::BROWSER_DRIVEN,
456 extension, 476 extension_consumer.get(),
457 &scripts_run_info); 477 &scripts_run_info);
458 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN); 478 scripts_run_info.LogRun(web_frame, UserScript::BROWSER_DRIVEN);
459 } 479 }
460 } 480 }
461 481
462 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) { 482 void ScriptInjectionManager::HandlePermitScriptInjection(int64 request_id) {
463 ScopedVector<ScriptInjection>::iterator iter = 483 ScopedVector<ScriptInjection>::iterator iter =
464 pending_injections_.begin(); 484 pending_injections_.begin();
465 for (; iter != pending_injections_.end(); ++iter) { 485 for (; iter != pending_injections_.end(); ++iter) {
466 if ((*iter)->request_id() == request_id) 486 if ((*iter)->request_id() == request_id)
467 break; 487 break;
468 } 488 }
469 if (iter == pending_injections_.end()) 489 if (iter == pending_injections_.end())
470 return; 490 return;
471 491
472 // At this point, because the request is present in pending_injections_, we 492 // 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, 493 // know that this is the same page that issued the request (otherwise,
474 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be 494 // RVOHelper's DidStartProvisionalLoad callback would have caused it to be
475 // cleared out). 495 // cleared out).
476 496
477 scoped_ptr<ScriptInjection> injection(*iter); 497 scoped_ptr<ScriptInjection> injection(*iter);
478 pending_injections_.weak_erase(iter); 498 pending_injections_.weak_erase(iter);
479 499
480 ScriptsRunInfo scripts_run_info; 500 ScriptsRunInfo scripts_run_info;
481 if (injection->OnPermissionGranted(extensions_->GetByID( 501 scoped_ptr<ExtensionConsumer> extension_consumer = GetExtensionConsumer(
482 injection->extension_id()), 502 injection->host_id().id(), extensions_);
503 if (injection->OnPermissionGranted(extension_consumer.get(),
483 &scripts_run_info)) { 504 &scripts_run_info)) {
484 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED); 505 scripts_run_info.LogRun(injection->web_frame(), UserScript::RUN_DEFERRED);
485 } 506 }
486 } 507 }
487 508
488 } // namespace extensions 509 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698