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

Side by Side Diff: extensions/browser/event_router.cc

Issue 147923005: Split ExtensionSystem interface from ExtensionSystemImpl implementation, part 1. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: re^3base Created 6 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 | Annotate | Revision Log
« no previous file with comments | « extensions/browser/DEPS ('k') | extensions/browser/extension_system.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "chrome/browser/extensions/extension_host.h" 15 #include "chrome/browser/extensions/extension_host.h"
16 #include "chrome/browser/extensions/extension_system.h"
17 #include "chrome/browser/extensions/extension_util.h" 16 #include "chrome/browser/extensions/extension_util.h"
18 #include "chrome/common/extensions/extension_messages.h" 17 #include "chrome/common/extensions/extension_messages.h"
19 #include "content/public/browser/notification_service.h" 18 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
21 #include "extensions/browser/extension_prefs.h" 20 #include "extensions/browser/extension_prefs.h"
22 #include "extensions/browser/extension_registry.h" 21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/browser/extensions_browser_client.h" 23 #include "extensions/browser/extensions_browser_client.h"
24 #include "extensions/browser/lazy_background_task_queue.h" 24 #include "extensions/browser/lazy_background_task_queue.h"
25 #include "extensions/browser/process_manager.h" 25 #include "extensions/browser/process_manager.h"
26 #include "extensions/browser/process_map.h" 26 #include "extensions/browser/process_map.h"
27 #include "extensions/common/extension.h" 27 #include "extensions/common/extension.h"
28 #include "extensions/common/extension_api.h" 28 #include "extensions/common/extension_api.h"
29 #include "extensions/common/extension_urls.h" 29 #include "extensions/common/extension_urls.h"
30 #include "extensions/common/manifest_handlers/background_info.h" 30 #include "extensions/common/manifest_handlers/background_info.h"
31 #include "extensions/common/manifest_handlers/incognito_info.h" 31 #include "extensions/common/manifest_handlers/incognito_info.h"
32 32
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 BrowserThread::PostTask( 74 BrowserThread::PostTask(
75 BrowserThread::UI, 75 BrowserThread::UI,
76 FROM_HERE, 76 FROM_HERE,
77 base::Bind(&NotifyExtensionDispatchObserverOnUIThread, 77 base::Bind(&NotifyExtensionDispatchObserverOnUIThread,
78 browser_context_id, base::Passed(&details))); 78 browser_context_id, base::Passed(&details)));
79 } else { 79 } else {
80 BrowserContext* context = 80 BrowserContext* context =
81 reinterpret_cast<BrowserContext*>(browser_context_id); 81 reinterpret_cast<BrowserContext*>(browser_context_id);
82 if (!ExtensionsBrowserClient::Get()->IsValidContext(context)) 82 if (!ExtensionsBrowserClient::Get()->IsValidContext(context))
83 return; 83 return;
84 ExtensionSystem* extension_system = 84 ExtensionSystem* extension_system = ExtensionSystem::Get(context);
85 ExtensionSystem::GetForBrowserContext(context);
86 EventRouter* event_router = extension_system->event_router(); 85 EventRouter* event_router = extension_system->event_router();
87 if (!event_router) 86 if (!event_router)
88 return; 87 return;
89 if (event_router->event_dispatch_observer_) { 88 if (event_router->event_dispatch_observer_) {
90 event_router->event_dispatch_observer_->OnWillDispatchEvent( 89 event_router->event_dispatch_observer_->OnWillDispatchEvent(
91 details.Pass()); 90 details.Pass());
92 } 91 }
93 } 92 }
94 } 93 }
95 94
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 return util::CanCrossIncognito(extension, context); 565 return util::CanCrossIncognito(extension, context);
567 } 566 }
568 567
569 bool EventRouter::MaybeLoadLazyBackgroundPageToDispatchEvent( 568 bool EventRouter::MaybeLoadLazyBackgroundPageToDispatchEvent(
570 BrowserContext* context, 569 BrowserContext* context,
571 const Extension* extension, 570 const Extension* extension,
572 const linked_ptr<Event>& event) { 571 const linked_ptr<Event>& event) {
573 if (extension->is_ephemeral() && !event->can_load_ephemeral_apps) { 572 if (extension->is_ephemeral() && !event->can_load_ephemeral_apps) {
574 // Most events can only be dispatched to ephemeral apps that are already 573 // Most events can only be dispatched to ephemeral apps that are already
575 // running. 574 // running.
576 ProcessManager* pm = 575 ProcessManager* pm = ExtensionSystem::Get(context)->process_manager();
577 ExtensionSystem::GetForBrowserContext(context)->process_manager();
578 if (!pm->GetBackgroundHostForExtension(extension->id())) 576 if (!pm->GetBackgroundHostForExtension(extension->id()))
579 return false; 577 return false;
580 } 578 }
581 579
582 if (!CanDispatchEventToBrowserContext(context, extension, event)) 580 if (!CanDispatchEventToBrowserContext(context, extension, event))
583 return false; 581 return false;
584 582
585 LazyBackgroundTaskQueue* queue = ExtensionSystem::GetForBrowserContext( 583 LazyBackgroundTaskQueue* queue = ExtensionSystem::Get(
586 context)->lazy_background_task_queue(); 584 context)->lazy_background_task_queue();
587 if (queue->ShouldEnqueueTask(context, extension)) { 585 if (queue->ShouldEnqueueTask(context, extension)) {
588 linked_ptr<Event> dispatched_event(event); 586 linked_ptr<Event> dispatched_event(event);
589 587
590 // If there's a dispatch callback, call it now (rather than dispatch time) 588 // If there's a dispatch callback, call it now (rather than dispatch time)
591 // to avoid lifetime issues. Use a separate copy of the event args, so they 589 // to avoid lifetime issues. Use a separate copy of the event args, so they
592 // last until the event is dispatched. 590 // last until the event is dispatched.
593 if (!event->will_dispatch_callback.is_null()) { 591 if (!event->will_dispatch_callback.is_null()) {
594 dispatched_event.reset(event->DeepCopy()); 592 dispatched_event.reset(event->DeepCopy());
595 dispatched_event->will_dispatch_callback.Run( 593 dispatched_event->will_dispatch_callback.Run(
(...skipping 13 matching lines...) Expand all
609 607
610 // static 608 // static
611 void EventRouter::IncrementInFlightEventsOnUI( 609 void EventRouter::IncrementInFlightEventsOnUI(
612 void* browser_context_id, 610 void* browser_context_id,
613 const std::string& extension_id) { 611 const std::string& extension_id) {
614 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
615 BrowserContext* browser_context = 613 BrowserContext* browser_context =
616 reinterpret_cast<BrowserContext*>(browser_context_id); 614 reinterpret_cast<BrowserContext*>(browser_context_id);
617 if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context)) 615 if (!ExtensionsBrowserClient::Get()->IsValidContext(browser_context))
618 return; 616 return;
619 ExtensionSystem* extension_system = 617 ExtensionSystem* extension_system = ExtensionSystem::Get(browser_context);
620 ExtensionSystem::GetForBrowserContext(browser_context);
621 EventRouter* event_router = extension_system->event_router(); 618 EventRouter* event_router = extension_system->event_router();
622 if (!event_router) 619 if (!event_router)
623 return; 620 return;
624 const Extension* extension = 621 const Extension* extension =
625 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID( 622 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID(
626 extension_id); 623 extension_id);
627 if (!extension) 624 if (!extension)
628 return; 625 return;
629 event_router->IncrementInFlightEvents(browser_context, extension); 626 event_router->IncrementInFlightEvents(browser_context, extension);
630 } 627 }
631 628
632 void EventRouter::IncrementInFlightEvents(BrowserContext* context, 629 void EventRouter::IncrementInFlightEvents(BrowserContext* context,
633 const Extension* extension) { 630 const Extension* extension) {
634 // Only increment in-flight events if the lazy background page is active, 631 // Only increment in-flight events if the lazy background page is active,
635 // because that's the only time we'll get an ACK. 632 // because that's the only time we'll get an ACK.
636 if (BackgroundInfo::HasLazyBackgroundPage(extension)) { 633 if (BackgroundInfo::HasLazyBackgroundPage(extension)) {
637 ProcessManager* pm = 634 ProcessManager* pm = ExtensionSystem::Get(context)->process_manager();
638 ExtensionSystem::GetForBrowserContext(context)->process_manager();
639 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension->id()); 635 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension->id());
640 if (host) 636 if (host)
641 pm->IncrementLazyKeepaliveCount(extension); 637 pm->IncrementLazyKeepaliveCount(extension);
642 } 638 }
643 } 639 }
644 640
645 void EventRouter::OnEventAck(BrowserContext* context, 641 void EventRouter::OnEventAck(BrowserContext* context,
646 const std::string& extension_id) { 642 const std::string& extension_id) {
647 ProcessManager* pm = 643 ProcessManager* pm = ExtensionSystem::Get(context)->process_manager();
648 ExtensionSystem::GetForBrowserContext(context)->process_manager();
649 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id); 644 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id);
650 // The event ACK is routed to the background host, so this should never be 645 // The event ACK is routed to the background host, so this should never be
651 // NULL. 646 // NULL.
652 CHECK(host); 647 CHECK(host);
653 // TODO(mpcomplete): We should never get this message unless 648 // TODO(mpcomplete): We should never get this message unless
654 // HasLazyBackgroundPage is true. Find out why we're getting it anyway. 649 // HasLazyBackgroundPage is true. Find out why we're getting it anyway.
655 if (host->extension() && 650 if (host->extension() &&
656 BackgroundInfo::HasLazyBackgroundPage(host->extension())) 651 BackgroundInfo::HasLazyBackgroundPage(host->extension()))
657 pm->DecrementLazyKeepaliveCount(host->extension()); 652 pm->DecrementLazyKeepaliveCount(host->extension());
658 } 653 }
(...skipping 21 matching lines...) Expand all
680 // Remove all event listeners associated with this renderer. 675 // Remove all event listeners associated with this renderer.
681 listeners_.RemoveListenersForProcess(renderer); 676 listeners_.RemoveListenersForProcess(renderer);
682 break; 677 break;
683 } 678 }
684 case chrome::NOTIFICATION_EXTENSION_ENABLED: { 679 case chrome::NOTIFICATION_EXTENSION_ENABLED: {
685 // If the extension has a lazy background page, make sure it gets loaded 680 // If the extension has a lazy background page, make sure it gets loaded
686 // to register the events the extension is interested in. 681 // to register the events the extension is interested in.
687 const Extension* extension = 682 const Extension* extension =
688 content::Details<const Extension>(details).ptr(); 683 content::Details<const Extension>(details).ptr();
689 if (BackgroundInfo::HasLazyBackgroundPage(extension)) { 684 if (BackgroundInfo::HasLazyBackgroundPage(extension)) {
690 LazyBackgroundTaskQueue* queue = ExtensionSystem::GetForBrowserContext( 685 LazyBackgroundTaskQueue* queue = ExtensionSystem::Get(
691 browser_context_)->lazy_background_task_queue(); 686 browser_context_)->lazy_background_task_queue();
692 queue->AddPendingTask(browser_context_, extension->id(), 687 queue->AddPendingTask(browser_context_, extension->id(),
693 base::Bind(&DoNothing)); 688 base::Bind(&DoNothing));
694 } 689 }
695 break; 690 break;
696 } 691 }
697 case chrome::NOTIFICATION_EXTENSION_LOADED: { 692 case chrome::NOTIFICATION_EXTENSION_LOADED: {
698 // Add all registered lazy listeners to our cache. 693 // Add all registered lazy listeners to our cache.
699 const Extension* extension = 694 const Extension* extension =
700 content::Details<const Extension>(details).ptr(); 695 content::Details<const Extension>(details).ptr();
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 EventDispatchInfo::EventDispatchInfo(const std::string& extension_id, 776 EventDispatchInfo::EventDispatchInfo(const std::string& extension_id,
782 const std::string& event_name, 777 const std::string& event_name,
783 scoped_ptr<ListValue> event_args) 778 scoped_ptr<ListValue> event_args)
784 : extension_id(extension_id), 779 : extension_id(extension_id),
785 event_name(event_name), 780 event_name(event_name),
786 event_args(event_args.Pass()) {} 781 event_args(event_args.Pass()) {}
787 782
788 EventDispatchInfo::~EventDispatchInfo() {} 783 EventDispatchInfo::~EventDispatchInfo() {}
789 784
790 } // namespace extensions 785 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/DEPS ('k') | extensions/browser/extension_system.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698