diff options
10 files changed, 132 insertions, 262 deletions
diff --git a/chrome/browser/automation/extension_port_container.cc b/chrome/browser/automation/extension_port_container.cc index e93f42c..2c80844 100644 --- a/chrome/browser/automation/extension_port_container.cc +++ b/chrome/browser/automation/extension_port_container.cc @@ -36,11 +36,6 @@ ExtensionPortContainer::~ExtensionPortContainer() { if (port_id_ != -1) service_->CloseChannel(port_id_); - - NotificationService::current()->Notify( - NotificationType::EXTENSION_PORT_DELETED_DEBUG, - Source<IPC::Message::Sender>(this), - NotificationService::NoDetails()); } bool ExtensionPortContainer::PostResponseToExternalPort( diff --git a/chrome/browser/debugger/extension_ports_remote_service.cc b/chrome/browser/debugger/extension_ports_remote_service.cc index ad9f3b6..d92a043 100644 --- a/chrome/browser/debugger/extension_ports_remote_service.cc +++ b/chrome/browser/debugger/extension_ports_remote_service.cc @@ -137,10 +137,6 @@ ExtensionPortsRemoteService::ExtensionPortsRemoteService( } ExtensionPortsRemoteService::~ExtensionPortsRemoteService() { - NotificationService::current()->Notify( - NotificationType::EXTENSION_PORT_DELETED_DEBUG, - Source<IPC::Message::Sender>(this), - NotificationService::NoDetails()); } void ExtensionPortsRemoteService::HandleMessage( diff --git a/chrome/browser/extensions/extension_menu_manager_unittest.cc b/chrome/browser/extensions/extension_menu_manager_unittest.cc index c0ad8d9..f074b4a 100644 --- a/chrome/browser/extensions/extension_menu_manager_unittest.cc +++ b/chrome/browser/extensions/extension_menu_manager_unittest.cc @@ -9,6 +9,7 @@ #include "base/scoped_temp_dir.h" #include "base/scoped_vector.h" #include "base/values.h" +#include "chrome/browser/chrome_thread.h" #include "chrome/browser/extensions/extension_menu_manager.h" #include "chrome/browser/extensions/extension_message_service.h" #include "chrome/browser/extensions/test_extension_prefs.h" diff --git a/chrome/browser/extensions/extension_message_service.cc b/chrome/browser/extensions/extension_message_service.cc index bdd5620..c4189b1 100644 --- a/chrome/browser/extensions/extension_message_service.cc +++ b/chrome/browser/extensions/extension_message_service.cc @@ -4,12 +4,11 @@ #include "chrome/browser/extensions/extension_message_service.h" +#include "base/atomic_sequence_num.h" #include "base/json/json_writer.h" -#include "base/singleton.h" #include "base/stl_util-inl.h" #include "base/values.h" #include "chrome/browser/child_process_security_policy.h" -#include "chrome/browser/chrome_thread.h" #include "chrome/browser/extensions/extension_process_manager.h" #include "chrome/browser/extensions/extension_tabs_module.h" #include "chrome/browser/profile.h" @@ -37,15 +36,9 @@ struct ExtensionMessageService::MessagePort { IPC::Message::Sender* sender; int routing_id; - // TODO(mpcomplete): remove this when I track down the crasher. Hopefully - // this guy will show up in some stack traces and potentially give some - // insight. - // http://code.google.com/p/chromium/issues/detail?id=21201 - int debug_info; - MessagePort(IPC::Message::Sender* sender = NULL, int routing_id = MSG_ROUTING_CONTROL) : - sender(sender), routing_id(routing_id), debug_info(0) {} + sender(sender), routing_id(routing_id) {} }; struct ExtensionMessageService::MessageChannel { @@ -53,9 +46,19 @@ struct ExtensionMessageService::MessageChannel { ExtensionMessageService::MessagePort receiver; }; +const char ExtensionMessageService::kDispatchOnConnect[] = + "Port.dispatchOnConnect"; +const char ExtensionMessageService::kDispatchOnDisconnect[] = + "Port.dispatchOnDisconnect"; +const char ExtensionMessageService::kDispatchOnMessage[] = + "Port.dispatchOnMessage"; +const char ExtensionMessageService::kDispatchEvent[] = + "Event.dispatchJSON"; namespace { +static base::AtomicSequenceNumber g_next_channel_id(base::LINKER_INITIALIZED); + static void DispatchOnConnect(const ExtensionMessageService::MessagePort& port, int dest_port_id, const std::string& channel_name, @@ -105,15 +108,6 @@ static void DispatchEvent(const ExtensionMessageService::MessagePort& port, } // namespace -const char ExtensionMessageService::kDispatchOnConnect[] = - "Port.dispatchOnConnect"; -const char ExtensionMessageService::kDispatchOnDisconnect[] = - "Port.dispatchOnDisconnect"; -const char ExtensionMessageService::kDispatchOnMessage[] = - "Port.dispatchOnMessage"; -const char ExtensionMessageService::kDispatchEvent[] = - "Event.dispatchJSON"; - // static std::string ExtensionMessageService::GetPerExtensionEventName( const std::string& event_name, const std::string& extension_id) { @@ -122,10 +116,28 @@ std::string ExtensionMessageService::GetPerExtensionEventName( return event_name + "/" + extension_id; } +// static +void ExtensionMessageService::AllocatePortIdPair(int* port1, int* port2) { + int channel_id = g_next_channel_id.GetNext(); + int port1_id = channel_id * 2; + int port2_id = channel_id * 2 + 1; + + // Sanity checks to make sure our channel<->port converters are correct. + DCHECK(IS_OPENER_PORT_ID(port1_id)); + DCHECK(GET_OPPOSITE_PORT_ID(port1_id) == port2_id); + DCHECK(GET_OPPOSITE_PORT_ID(port2_id) == port1_id); + DCHECK(GET_CHANNEL_ID(port1_id) == GET_CHANNEL_ID(port2_id)); + DCHECK(GET_CHANNEL_ID(port1_id) == channel_id); + DCHECK(GET_CHANNEL_OPENER_ID(channel_id) == port1_id); + DCHECK(GET_CHANNEL_RECEIVERS_ID(channel_id) == port2_id); + + *port1 = port1_id; + *port2 = port2_id; +} + ExtensionMessageService::ExtensionMessageService(Profile* profile) : profile_(profile), - extension_devtools_manager_(NULL), - next_port_id_(0) { + extension_devtools_manager_(NULL) { registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, NotificationService::AllSources()); registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, @@ -143,10 +155,8 @@ ExtensionMessageService::~ExtensionMessageService() { void ExtensionMessageService::ProfileDestroyed() { profile_ = NULL; - if (!registrar_.IsEmpty()) { - CHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + if (!registrar_.IsEmpty()) registrar_.RemoveAll(); - } } void ExtensionMessageService::AddEventListener(const std::string& event_name, @@ -157,7 +167,6 @@ void ExtensionMessageService::AddEventListener(const std::string& event_name, if (!rph || rph->ListenersIterator().IsAtEnd()) return; - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); DCHECK_EQ(listeners_[event_name].count(render_process_id), 0u) << event_name; listeners_[event_name].insert(render_process_id); @@ -174,7 +183,6 @@ void ExtensionMessageService::RemoveEventListener(const std::string& event_name, if (!rph || rph->ListenersIterator().IsAtEnd()) return; - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); DCHECK_EQ(listeners_[event_name].count(render_process_id), 1u) << " PID=" << render_process_id << " event=" << event_name; listeners_[event_name].erase(render_process_id); @@ -191,74 +199,7 @@ bool ExtensionMessageService::HasEventListener( !listeners_[event_name].empty()); } -void ExtensionMessageService::AllocatePortIdPair(int* port1, int* port2) { - AutoLock lock(next_port_id_lock_); - - // TODO(mpcomplete): what happens when this wraps? - int port1_id = next_port_id_++; - int port2_id = next_port_id_++; - - DCHECK(IS_OPENER_PORT_ID(port1_id)); - DCHECK(GET_OPPOSITE_PORT_ID(port1_id) == port2_id); - DCHECK(GET_OPPOSITE_PORT_ID(port2_id) == port1_id); - DCHECK(GET_CHANNEL_ID(port1_id) == GET_CHANNEL_ID(port2_id)); - - int channel_id = GET_CHANNEL_ID(port1_id); - DCHECK(GET_CHANNEL_OPENER_ID(channel_id) == port1_id); - DCHECK(GET_CHANNEL_RECEIVERS_ID(channel_id) == port2_id); - - *port1 = port1_id; - *port2 = port2_id; -} - -int ExtensionMessageService::OpenChannelToExtension( - int routing_id, const std::string& source_extension_id, - const std::string& target_extension_id, - const std::string& channel_name, ResourceMessageFilter* source) { - DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); - - // Create a channel ID for both sides of the channel. - int port1_id = -1; - int port2_id = -1; - AllocatePortIdPair(&port1_id, &port2_id); - - // Each side of the port is given his own port ID. When they send messages, - // we convert to the opposite port ID. See PostMessageFromRenderer. - ChromeThread::PostTask( - ChromeThread::UI, FROM_HERE, - NewRunnableMethod( - this, &ExtensionMessageService::OpenChannelToExtensionOnUIThread, - source->id(), routing_id, port2_id, source_extension_id, - target_extension_id, channel_name)); - - return port1_id; -} - -int ExtensionMessageService::OpenChannelToTab(int routing_id, - int tab_id, - const std::string& extension_id, - const std::string& channel_name, - ResourceMessageFilter* source) { - DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); - - // Create a channel ID for both sides of the channel. - int port1_id = -1; - int port2_id = -1; - AllocatePortIdPair(&port1_id, &port2_id); - - // Each side of the port is given his own port ID. When they send messages, - // we convert to the opposite port ID. See PostMessageFromRenderer. - ChromeThread::PostTask( - ChromeThread::UI, FROM_HERE, - NewRunnableMethod( - this, &ExtensionMessageService::OpenChannelToTabOnUIThread, - source->id(), routing_id, port2_id, tab_id, extension_id, - channel_name)); - - return port1_id; -} - -void ExtensionMessageService::OpenChannelToExtensionOnUIThread( +void ExtensionMessageService::OpenChannelToExtension( int source_process_id, int source_routing_id, int receiver_port_id, const std::string& source_extension_id, const std::string& target_extension_id, @@ -274,7 +215,6 @@ void ExtensionMessageService::OpenChannelToExtensionOnUIThread( profile_->GetExtensionProcessManager()->GetExtensionProcess( target_extension_id), MSG_ROUTING_CONTROL); - receiver.debug_info = 1; TabContents* source_contents = tab_util::GetTabContentsByID( source_process_id, source_routing_id); @@ -286,13 +226,11 @@ void ExtensionMessageService::OpenChannelToExtensionOnUIThread( base::JSONWriter::Write(tab_value.get(), false, &tab_json); } - OpenChannelOnUIThreadImpl(source, tab_json, - receiver, receiver_port_id, - source_extension_id, target_extension_id, - channel_name); + OpenChannelImpl(source, tab_json, receiver, receiver_port_id, + source_extension_id, target_extension_id, channel_name); } -void ExtensionMessageService::OpenChannelToTabOnUIThread( +void ExtensionMessageService::OpenChannelToTab( int source_process_id, int source_routing_id, int receiver_port_id, int tab_id, const std::string& extension_id, const std::string& channel_name) { @@ -302,12 +240,10 @@ void ExtensionMessageService::OpenChannelToTabOnUIThread( TabContents* contents = NULL; MessagePort receiver; - receiver.debug_info = 2; if (ExtensionTabUtil::GetTabById(tab_id, source->profile(), true, NULL, NULL, &contents, NULL)) { receiver.sender = contents->render_view_host(); receiver.routing_id = contents->render_view_host()->routing_id(); - receiver.debug_info = 3; } if (contents && contents->controller().needs_reload()) { @@ -329,20 +265,17 @@ void ExtensionMessageService::OpenChannelToTabOnUIThread( base::JSONWriter::Write(tab_value.get(), false, &tab_json); } - OpenChannelOnUIThreadImpl(source, tab_json, - receiver, receiver_port_id, - extension_id, extension_id, channel_name); + OpenChannelImpl(source, tab_json, receiver, receiver_port_id, + extension_id, extension_id, channel_name); } -bool ExtensionMessageService::OpenChannelOnUIThreadImpl( +bool ExtensionMessageService::OpenChannelImpl( IPC::Message::Sender* source, const std::string& tab_json, const MessagePort& receiver, int receiver_port_id, const std::string& source_extension_id, const std::string& target_extension_id, const std::string& channel_name) { - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); - // TODO(mpcomplete): notify source if receiver doesn't exist if (!source) return false; // Closed while in flight. @@ -380,7 +313,6 @@ bool ExtensionMessageService::OpenChannelOnUIThreadImpl( int ExtensionMessageService::OpenSpecialChannelToExtension( const std::string& extension_id, const std::string& channel_name, const std::string& tab_json, IPC::Message::Sender* source) { - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); DCHECK(profile_); int port1_id = -1; @@ -392,10 +324,8 @@ int ExtensionMessageService::OpenSpecialChannelToExtension( profile_->GetExtensionProcessManager()-> GetExtensionProcess(extension_id), MSG_ROUTING_CONTROL); - receiver.debug_info = 4; - if (!OpenChannelOnUIThreadImpl( - source, tab_json, receiver, port2_id, extension_id, extension_id, - channel_name)) + if (!OpenChannelImpl(source, tab_json, receiver, port2_id, + extension_id, extension_id, channel_name)) return -1; return port1_id; @@ -404,7 +334,6 @@ int ExtensionMessageService::OpenSpecialChannelToExtension( int ExtensionMessageService::OpenSpecialChannelToTab( const std::string& extension_id, const std::string& channel_name, TabContents* target_tab_contents, IPC::Message::Sender* source) { - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); DCHECK(target_tab_contents); if (target_tab_contents->controller().needs_reload()) { @@ -420,18 +349,14 @@ int ExtensionMessageService::OpenSpecialChannelToTab( MessagePort receiver( target_tab_contents->render_view_host(), target_tab_contents->render_view_host()->routing_id()); - receiver.debug_info = 5; - if (!OpenChannelOnUIThreadImpl(source, "null", - receiver, port2_id, - extension_id, extension_id, channel_name)) + if (!OpenChannelImpl(source, "null", receiver, port2_id, + extension_id, extension_id, channel_name)) return -1; return port1_id; } void ExtensionMessageService::CloseChannel(int port_id) { - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); - // Note: The channel might be gone already, if the other side closed first. MessageChannelMap::iterator it = channels_.find(GET_CHANNEL_ID(port_id)); if (it != channels_.end()) @@ -441,8 +366,6 @@ void ExtensionMessageService::CloseChannel(int port_id) { void ExtensionMessageService::CloseChannelImpl( MessageChannelMap::iterator channel_iter, int closing_port_id, bool notify_other_port) { - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); - // Notify the other side. const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ? channel_iter->second->receiver : channel_iter->second->opener; @@ -455,8 +378,6 @@ void ExtensionMessageService::CloseChannelImpl( void ExtensionMessageService::PostMessageFromRenderer( int source_port_id, const std::string& message) { - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); - MessageChannelMap::iterator iter = channels_.find(GET_CHANNEL_ID(source_port_id)); if (iter == channels_.end()) @@ -473,7 +394,6 @@ void ExtensionMessageService::PostMessageFromRenderer( void ExtensionMessageService::DispatchEventToRenderers( const std::string& event_name, const std::string& event_args, bool has_incognito_data, const GURL& event_url) { - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); ListenerMap::iterator it = listeners_.find(event_name); if (it == listeners_.end()) return; @@ -507,8 +427,6 @@ void ExtensionMessageService::DispatchEventToExtension( void ExtensionMessageService::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); - switch (type.value) { case NotificationType::RENDERER_PROCESS_TERMINATED: case NotificationType::RENDERER_PROCESS_CLOSED: { @@ -527,26 +445,6 @@ void ExtensionMessageService::Observe(NotificationType type, case NotificationType::RENDER_VIEW_HOST_DELETED: OnSenderClosed(Details<RenderViewHost>(details).ptr()); break; - - // We should already have removed this guy from our channel map by this - // point. - case NotificationType::EXTENSION_PORT_DELETED_DEBUG: { - IPC::Message::Sender* sender = - Details<IPC::Message::Sender>(details).ptr(); - for (MessageChannelMap::iterator it = channels_.begin(); - it != channels_.end(); ) { - MessageChannelMap::iterator current = it++; - int debug_info = current->second->receiver.debug_info; - if (current->second->opener.sender == sender) { - LOG(FATAL) << "Shouldn't happen:" << debug_info; - } else if (current->second->receiver.sender == sender) { - LOG(FATAL) << "Shouldn't happen either: " << debug_info; - } - } - OnSenderClosed(sender); - break; - } - default: NOTREACHED(); return; @@ -561,11 +459,8 @@ void ExtensionMessageService::OnSenderClosed(IPC::Message::Sender* sender) { MessageChannelMap::iterator current = it++; // If both sides are the same renderer, and it is closing, there is no // "other" port, so there's no need to notify it. - int debug_info = current->second->receiver.debug_info; - bool debug_check = debug_info == 4 || debug_info == 5; bool notify_other_port = - current->second->opener.sender != current->second->receiver.sender || - debug_check; + current->second->opener.sender != current->second->receiver.sender; if (current->second->opener.sender == sender) { CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first), diff --git a/chrome/browser/extensions/extension_message_service.h b/chrome/browser/extensions/extension_message_service.h index 40050f1..e1f5611 100644 --- a/chrome/browser/extensions/extension_message_service.h +++ b/chrome/browser/extensions/extension_message_service.h @@ -10,17 +10,13 @@ #include <set> #include <string> -#include "base/lock.h" #include "base/ref_counted.h" #include "chrome/common/notification_registrar.h" -#include "chrome/browser/chrome_thread.h" #include "chrome/browser/extensions/extension_devtools_manager.h" #include "ipc/ipc_message.h" class GURL; class Profile; -class RenderProcessHost; -class ResourceMessageFilter; class TabContents; // This class manages message and event passing between renderer processes. @@ -44,9 +40,11 @@ class TabContents; // port: an IPC::Message::Sender interface and an optional routing_id (in the // case that the port is a tab). The Sender is usually either a // RenderProcessHost or a RenderViewHost. + +// TODO(mpcomplete): Remove refcounting and make Profile the sole owner of this +// class. Then we can get rid of ProfileDestroyed(). class ExtensionMessageService - : public base::RefCountedThreadSafe< - ExtensionMessageService, ChromeThread::DeleteOnUIThread>, + : public base::RefCounted<ExtensionMessageService>, public NotificationObserver { public: // Javascript function name constants. @@ -65,7 +63,9 @@ class ExtensionMessageService static std::string GetPerExtensionEventName(const std::string& event_name, const std::string& extension_id); - // --- UI thread only: + // Allocates a pair of port ids. + // NOTE: this can be called from any thread. + static void AllocatePortIdPair(int* port1, int* port2); explicit ExtensionMessageService(Profile* profile); @@ -80,13 +80,6 @@ class ExtensionMessageService // Returns true if there is at least one listener for the given event. bool HasEventListener(const std::string& event_name); - // Closes the message channel associated with the given port, and notifies - // the other side. - void CloseChannel(int port_id); - - // Sends a message from a renderer to the given port. - void PostMessageFromRenderer(int port_id, const std::string& message); - // Send an event to every registered extension renderer. If // |has_incognito_data| is true, the event is only sent to extension with the // permission to access incognito data. If |event_url| is not empty, the @@ -102,6 +95,23 @@ class ExtensionMessageService const std::string& event_name, const std::string& event_args, bool has_incognito_data, const GURL& event_url); + // Given an extension's ID, opens a channel between the given renderer "port" + // and every listening context owned by that extension. |channel_name| is + // an optional identifier for use by extension developers. + void OpenChannelToExtension( + int source_process_id, int source_routing_id, int receiver_port_id, + const std::string& source_extension_id, + const std::string& target_extension_id, + const std::string& channel_name); + + // Same as above, but opens a channel to the tab with the given ID. Messages + // are restricted to that tab, so if there are multiple tabs in that process, + // only the targeted tab will receive messages. + void OpenChannelToTab( + int source_process_id, int source_routing_id, int receiver_port_id, + int tab_id, const std::string& extension_id, + const std::string& channel_name); + // Given an extension ID, opens a channel between the given // automation "port" or DevTools service and that extension. the // channel will be open to the extension process hosting the @@ -124,31 +134,15 @@ class ExtensionMessageService const std::string& extension_id, const std::string& channel_name, TabContents* target_tab_contents, IPC::Message::Sender* source); - // --- IO thread only: - - // Given an extension's ID, opens a channel between the given renderer "port" - // and every listening context owned by that extension. Returns a port ID - // to be used for posting messages between the processes. |channel_name| is - // an optional identifier for use by extension developers. - // This runs on the IO thread so that it can be used in a synchronous IPC - // message. - int OpenChannelToExtension(int routing_id, - const std::string& source_extension_id, - const std::string& target_extension_id, - const std::string& channel_name, - ResourceMessageFilter* source); + // Closes the message channel associated with the given port, and notifies + // the other side. + void CloseChannel(int port_id); - // Same as above, but opens a channel to the tab with the given ID. Messages - // are restricted to that tab, so if there are multiple tabs in that process, - // only the targeted tab will receive messages. - int OpenChannelToTab(int routing_id, int tab_id, - const std::string& extension_id, - const std::string& channel_name, - ResourceMessageFilter* source); + // Sends a message from a renderer to the given port. + void PostMessageFromRenderer(int port_id, const std::string& message); private: - friend class ChromeThread; - friend class DeleteTask<ExtensionMessageService>; + friend class base::RefCounted<ExtensionMessageService>; friend class MockExtensionMessageService; // A map of channel ID to its channel object. @@ -156,30 +150,8 @@ class ExtensionMessageService virtual ~ExtensionMessageService(); - // Allocates a pair of port ids. - // NOTE: this can be called from any thread. - void AllocatePortIdPair(int* port1, int* port2); - - void CloseChannelImpl(MessageChannelMap::iterator channel_iter, int port_id, - bool notify_other_port); - - // --- UI thread only: - - // Handles channel creation and notifies the destinations that a channel was - // opened. - void OpenChannelToExtensionOnUIThread( - int source_process_id, int source_routing_id, int receiver_port_id, - const std::string& source_extension_id, - const std::string& target_extension_id, - const std::string& channel_name); - - void OpenChannelToTabOnUIThread( - int source_process_id, int source_routing_id, int receiver_port_id, - int tab_id, const std::string& extension_id, - const std::string& channel_name); - - // Common between OpenChannelOnUIThread and OpenSpecialChannelToExtension. - bool OpenChannelOnUIThreadImpl( + // Common among Open(Special)Channel* variants. + bool OpenChannelImpl( IPC::Message::Sender* source, const std::string& tab_json, const MessagePort& receiver, int receiver_port_id, @@ -187,6 +159,9 @@ class ExtensionMessageService const std::string& target_extension_id, const std::string& channel_name); + void CloseChannelImpl(MessageChannelMap::iterator channel_iter, int port_id, + bool notify_other_port); + // NotificationObserver interface. void Observe(NotificationType type, const NotificationSource& source, @@ -208,15 +183,6 @@ class ExtensionMessageService typedef std::map<std::string, std::set<int> > ListenerMap; ListenerMap listeners_; - // --- UI or IO thread: - - // For generating unique channel IDs. - int next_port_id_; - - // Protects the next_port_id_ variable, since it can be - // used on the IO thread or the UI thread. - Lock next_port_id_lock_; - DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService); }; diff --git a/chrome/browser/renderer_host/browser_render_process_host.cc b/chrome/browser/renderer_host/browser_render_process_host.cc index d41c9c3..148fa6b 100644 --- a/chrome/browser/renderer_host/browser_render_process_host.cc +++ b/chrome/browser/renderer_host/browser_render_process_host.cc @@ -241,11 +241,6 @@ BrowserRenderProcessHost::~BrowserRenderProcessHost() { audio_renderer_host_->Destroy(); ClearTransportDIBCache(); - - NotificationService::current()->Notify( - NotificationType::EXTENSION_PORT_DELETED_DEBUG, - Source<IPC::Message::Sender>(this), - NotificationService::NoDetails()); } bool BrowserRenderProcessHost::Init(bool is_extensions_process) { diff --git a/chrome/browser/renderer_host/render_view_host.cc b/chrome/browser/renderer_host/render_view_host.cc index 0ba65d6..a477c92 100644 --- a/chrome/browser/renderer_host/render_view_host.cc +++ b/chrome/browser/renderer_host/render_view_host.cc @@ -145,11 +145,6 @@ RenderViewHost::~RenderViewHost() { // Be sure to clean up any leftover state from cross-site requests. Singleton<CrossSiteRequestManager>()->SetHasPendingCrossSiteRequest( process()->id(), routing_id(), false); - - NotificationService::current()->Notify( - NotificationType::EXTENSION_PORT_DELETED_DEBUG, - Source<IPC::Message::Sender>(this), - NotificationService::NoDetails()); } bool RenderViewHost::CreateRenderView(const string16& frame_name) { diff --git a/chrome/browser/renderer_host/resource_message_filter.cc b/chrome/browser/renderer_host/resource_message_filter.cc index e7985a0..797ed3c 100644 --- a/chrome/browser/renderer_host/resource_message_filter.cc +++ b/chrome/browser/renderer_host/resource_message_filter.cc @@ -221,7 +221,6 @@ ResourceMessageFilter::ResourceMessageFilter( ALLOW_THIS_IN_INITIALIZER_LIST(resolve_proxy_msg_helper_(this, NULL)), media_request_context_(profile->GetRequestContextForMedia()), extensions_request_context_(profile->GetRequestContextForExtensions()), - extensions_message_service_(profile->GetExtensionMessageService()), render_widget_helper_(render_widget_helper), audio_renderer_host_(audio_renderer_host), appcache_dispatcher_host_( @@ -1266,24 +1265,52 @@ void ResourceMessageFilter::OnOpenChannelToExtension( int routing_id, const std::string& source_extension_id, const std::string& target_extension_id, const std::string& channel_name, int* port_id) { - if (extensions_message_service_.get()) { - *port_id = extensions_message_service_-> - OpenChannelToExtension(routing_id, source_extension_id, - target_extension_id, channel_name, this); - } else { - *port_id = -1; - } + int port2_id; + ExtensionMessageService::AllocatePortIdPair(port_id, &port2_id); + + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, + NewRunnableMethod( + this, &ResourceMessageFilter::OpenChannelToExtensionOnUIThread, + id(), routing_id, port2_id, source_extension_id, + target_extension_id, channel_name)); +} + +void ResourceMessageFilter::OpenChannelToExtensionOnUIThread( + int source_process_id, int source_routing_id, + int receiver_port_id, + const std::string& source_extension_id, + const std::string& target_extension_id, + const std::string& channel_name) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + profile_->GetExtensionMessageService()->OpenChannelToExtension( + source_process_id, source_routing_id, receiver_port_id, + source_extension_id, target_extension_id, channel_name); } void ResourceMessageFilter::OnOpenChannelToTab( int routing_id, int tab_id, const std::string& extension_id, const std::string& channel_name, int* port_id) { - if (extensions_message_service_.get()) { - *port_id = extensions_message_service_-> - OpenChannelToTab(routing_id, tab_id, extension_id, channel_name, this); - } else { - *port_id = -1; - } + int port2_id; + ExtensionMessageService::AllocatePortIdPair(port_id, &port2_id); + + ChromeThread::PostTask( + ChromeThread::UI, FROM_HERE, + NewRunnableMethod( + this, &ResourceMessageFilter::OpenChannelToTabOnUIThread, + id(), routing_id, port2_id, tab_id, extension_id, channel_name)); +} + +void ResourceMessageFilter::OpenChannelToTabOnUIThread( + int source_process_id, int source_routing_id, + int receiver_port_id, + int tab_id, + const std::string& extension_id, + const std::string& channel_name) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + profile_->GetExtensionMessageService()->OpenChannelToTab( + source_process_id, source_routing_id, receiver_port_id, + tab_id, extension_id, channel_name); } bool ResourceMessageFilter::CheckBenchmarkingEnabled() const { diff --git a/chrome/browser/renderer_host/resource_message_filter.h b/chrome/browser/renderer_host/resource_message_filter.h index ebd94a6..16e329b 100644 --- a/chrome/browser/renderer_host/resource_message_filter.h +++ b/chrome/browser/renderer_host/resource_message_filter.h @@ -35,7 +35,6 @@ class AudioRendererHost; class ChromeURLRequestContext; class DatabaseDispatcherHost; class DOMStorageDispatcherHost; -class ExtensionMessageService; struct FontDescriptor; class GeolocationDispatcherHost; class HostZoomMap; @@ -325,9 +324,19 @@ class ResourceMessageFilter : public IPC::ChannelProxy::MessageFilter, const std::string& source_extension_id, const std::string& target_extension_id, const std::string& channel_name, int* port_id); + void OpenChannelToExtensionOnUIThread(int source_process_id, + int source_routing_id, + int receiver_port_id, + const std::string& source_extension_id, + const std::string& target_extension_id, + const std::string& channel_name); void OnOpenChannelToTab(int routing_id, int tab_id, const std::string& extension_id, const std::string& channel_name, int* port_id); + void OpenChannelToTabOnUIThread(int source_process_id, int source_routing_id, + int receiver_port_id, + int tab_id, const std::string& extension_id, + const std::string& channel_name); void OnCloseCurrentConnections(); void OnSetCacheMode(bool enabled); @@ -419,9 +428,6 @@ class ResourceMessageFilter : public IPC::ChannelProxy::MessageFilter, // A request context that holds a cookie store for chrome-extension URLs. scoped_refptr<URLRequestContextGetter> extensions_request_context_; - // Used for routing extension messages. - scoped_refptr<ExtensionMessageService> extensions_message_service_; - scoped_refptr<RenderWidgetHelper> render_widget_helper_; // Object that should take care of audio related resource requests. diff --git a/chrome/common/notification_type.h b/chrome/common/notification_type.h index 94b1fae..08bc47f 100644 --- a/chrome/common/notification_type.h +++ b/chrome/common/notification_type.h @@ -900,12 +900,6 @@ class NotificationType { // object. EXTENSION_PREF_CHANGED, - // Debugging --------------------------------------------------------------- - - // TODO(mpcomplete): Sent to diagnose a bug. Remove when fixed. - // http://code.google.com/p/chromium/issues/detail?id=21201 - EXTENSION_PORT_DELETED_DEBUG, - // Desktop Notifications --------------------------------------------------- // This notification is sent when a balloon is connected to a renderer |