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

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

Powered by Google App Engine
This is Rietveld 408576698