OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/event_router.h" | 5 #include "extensions/browser/event_router.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
15 #include "content/public/browser/child_process_security_policy.h" | |
15 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
17 #include "extensions/browser/api_activity_monitor.h" | 18 #include "extensions/browser/api_activity_monitor.h" |
18 #include "extensions/browser/extension_host.h" | 19 #include "extensions/browser/extension_host.h" |
19 #include "extensions/browser/extension_prefs.h" | 20 #include "extensions/browser/extension_prefs.h" |
20 #include "extensions/browser/extension_registry.h" | 21 #include "extensions/browser/extension_registry.h" |
21 #include "extensions/browser/extension_system.h" | 22 #include "extensions/browser/extension_system.h" |
22 #include "extensions/browser/extension_util.h" | 23 #include "extensions/browser/extension_util.h" |
23 #include "extensions/browser/extensions_browser_client.h" | 24 #include "extensions/browser/extensions_browser_client.h" |
24 #include "extensions/browser/lazy_background_task_queue.h" | 25 #include "extensions/browser/lazy_background_task_queue.h" |
(...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
505 incognito_context, extension, event)) { | 506 incognito_context, extension, event)) { |
506 already_dispatched->insert( | 507 already_dispatched->insert( |
507 std::make_pair(incognito_context, extension_id)); | 508 std::make_pair(incognito_context, extension_id)); |
508 } | 509 } |
509 } | 510 } |
510 } | 511 } |
511 | 512 |
512 void EventRouter::DispatchEventToProcess(const std::string& extension_id, | 513 void EventRouter::DispatchEventToProcess(const std::string& extension_id, |
513 content::RenderProcessHost* process, | 514 content::RenderProcessHost* process, |
514 const linked_ptr<Event>& event) { | 515 const linked_ptr<Event>& event) { |
516 BrowserContext* listener_context = process->GetBrowserContext(); | |
517 ProcessMap* process_map = ProcessMap::Get(listener_context); | |
518 | |
515 const Extension* extension = | 519 const Extension* extension = |
516 ExtensionRegistry::Get(browser_context_)->enabled_extensions().GetByID( | 520 ExtensionRegistry::Get(browser_context_)->enabled_extensions().GetByID( |
517 extension_id); | 521 extension_id); |
522 // NOTE: |extension| being NULL does not necessarily imply that this event | |
523 // shouldn't be dispatched. Events can be dispatched to WebUI as well. | |
518 | 524 |
519 // The extension could have been removed, but we do not unregister it until | 525 if (!extension && !extension_id.empty()) { |
520 // the extension process is unloaded. | 526 // Trying to dispatch an event to an extension that doesn't exist. The |
521 if (!extension) | 527 // extension could have been removed, but we do not unregister it until the |
522 return; | 528 // extension process is unloaded. |
523 | |
524 BrowserContext* listener_context = process->GetBrowserContext(); | |
525 ProcessMap* process_map = ProcessMap::Get(listener_context); | |
526 // If the event is privileged, only send to extension processes. Otherwise, | |
527 // it's OK to send to normal renderers (e.g., for content scripts). | |
528 if (!process_map->Contains(extension->id(), process->GetID()) && | |
529 !ExtensionAPI::GetSharedInstance()->IsAvailableInUntrustedContext( | |
530 event->event_name, extension)) { | |
531 return; | 529 return; |
532 } | 530 } |
533 | 531 |
534 // If the event is restricted to a URL, only dispatch if the extension has | 532 if (extension) { |
535 // permission for it (or if the event originated from itself). | 533 // Dispatching event to an extension. |
536 if (!event->event_url.is_empty() && | 534 // If the event is privileged, only send to extension processes. Otherwise, |
537 event->event_url.host() != extension->id() && | 535 // it's OK to send to normal renderers (e.g., for content scripts). |
538 !extension->permissions_data() | 536 if (!process_map->Contains(extension->id(), process->GetID()) && |
539 ->active_permissions() | 537 !ExtensionAPI::GetSharedInstance()->IsAvailableInUntrustedContext( |
540 ->HasEffectiveAccessToURL(event->event_url)) { | 538 event->event_name, extension)) { |
539 return; | |
540 } | |
541 | |
542 // If the event is restricted to a URL, only dispatch if the extension has | |
543 // permission for it (or if the event originated from itself). | |
544 if (!event->event_url.is_empty() && | |
545 event->event_url.host() != extension->id() && | |
546 !extension->permissions_data() | |
547 ->active_permissions() | |
548 ->HasEffectiveAccessToURL(event->event_url)) { | |
549 return; | |
550 } | |
551 | |
552 if (!CanDispatchEventToBrowserContext(listener_context, extension, event)) { | |
553 return; | |
554 } | |
555 } else if (content::ChildProcessSecurityPolicy::GetInstance() | |
556 ->HasWebUIBindings(process->GetID())) { | |
557 // Dispatching event to WebUI. | |
558 if (!ExtensionAPI::GetSharedInstance()->IsAvailableToWebUI( | |
Charlie Reis
2014/07/22 21:10:24
Good, I'm glad we have a whitelist of available AP
| |
559 event->event_name)) { | |
560 return; | |
561 } | |
562 } else { | |
563 // Dispatching event to a webpage - however, all such events (e.g. | |
564 // messaging) don't go through EventRouter so this should be impossible. | |
Charlie Reis
2014/07/22 21:10:24
"should be impossible:" Should this be a NOTREACHE
not at google - send to devlin
2014/07/22 23:42:32
NOTREACHED sgtm.
| |
541 return; | 565 return; |
542 } | 566 } |
543 | 567 |
544 if (!CanDispatchEventToBrowserContext(listener_context, extension, event)) | |
545 return; | |
546 | |
547 if (!event->will_dispatch_callback.is_null()) { | 568 if (!event->will_dispatch_callback.is_null()) { |
548 event->will_dispatch_callback.Run(listener_context, extension, | 569 event->will_dispatch_callback.Run( |
549 event->event_args.get()); | 570 listener_context, extension, event->event_args.get()); |
550 } | 571 } |
551 | 572 |
552 DispatchExtensionMessage(process, listener_context, extension->id(), | 573 DispatchExtensionMessage(process, |
553 event->event_name, event->event_args.get(), | 574 listener_context, |
554 event->user_gesture, event->filter_info); | 575 extension_id, |
555 IncrementInFlightEvents(listener_context, extension); | 576 event->event_name, |
577 event->event_args.get(), | |
578 event->user_gesture, | |
579 event->filter_info); | |
580 | |
581 if (extension) | |
582 IncrementInFlightEvents(listener_context, extension); | |
556 } | 583 } |
557 | 584 |
558 bool EventRouter::CanDispatchEventToBrowserContext( | 585 bool EventRouter::CanDispatchEventToBrowserContext( |
559 BrowserContext* context, | 586 BrowserContext* context, |
560 const Extension* extension, | 587 const Extension* extension, |
561 const linked_ptr<Event>& event) { | 588 const linked_ptr<Event>& event) { |
562 // Is this event from a different browser context than the renderer (ie, an | 589 // Is this event from a different browser context than the renderer (ie, an |
563 // incognito tab event sent to a normal process, or vice versa). | 590 // incognito tab event sent to a normal process, or vice versa). |
564 bool cross_incognito = event->restrict_to_browser_context && | 591 bool cross_incognito = event->restrict_to_browser_context && |
565 context != event->restrict_to_browser_context; | 592 context != event->restrict_to_browser_context; |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
771 } | 798 } |
772 | 799 |
773 EventListenerInfo::EventListenerInfo(const std::string& event_name, | 800 EventListenerInfo::EventListenerInfo(const std::string& event_name, |
774 const std::string& extension_id, | 801 const std::string& extension_id, |
775 content::BrowserContext* browser_context) | 802 content::BrowserContext* browser_context) |
776 : event_name(event_name), | 803 : event_name(event_name), |
777 extension_id(extension_id), | 804 extension_id(extension_id), |
778 browser_context(browser_context) {} | 805 browser_context(browser_context) {} |
779 | 806 |
780 } // namespace extensions | 807 } // namespace extensions |
OLD | NEW |