diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-13 20:38:56 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-13 20:38:56 +0000 |
commit | 2933580fcac85dbd0ad6eb5ccb5fe88fef40a128 (patch) | |
tree | d79b615ee353f0f6e90668e00aae824894853f04 /chrome/browser/extensions/extension_message_service.cc | |
parent | cd7b299fd6053afcd625ce36f0e51f94c83b182c (diff) | |
download | chromium_src-2933580fcac85dbd0ad6eb5ccb5fe88fef40a128.zip chromium_src-2933580fcac85dbd0ad6eb5ccb5fe88fef40a128.tar.gz chromium_src-2933580fcac85dbd0ad6eb5ccb5fe88fef40a128.tar.bz2 |
Add an optional channel name to the extension message connect event.
Also changed the way port IDs work internally to reduce confusion. Each end of the port has its own ID, not the ID of its opposite end.
BUG=13706
TEST=no
Review URL: http://codereview.chromium.org/155381
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20519 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_message_service.cc')
-rw-r--r-- | chrome/browser/extensions/extension_message_service.cc | 52 |
1 files changed, 30 insertions, 22 deletions
diff --git a/chrome/browser/extensions/extension_message_service.cc b/chrome/browser/extensions/extension_message_service.cc index 78dd1f2..70bfda7 100644 --- a/chrome/browser/extensions/extension_message_service.cc +++ b/chrome/browser/extensions/extension_message_service.cc @@ -42,13 +42,15 @@ struct SingletonData { InstanceMap map; }; -static void DispatchOnConnect(IPC::Message::Sender* channel, int source_port_id, +static void DispatchOnConnect(IPC::Message::Sender* channel, int dest_port_id, + const std::string& channel_name, const std::string& tab_json, const std::string& extension_id) { ListValue args; - args.Set(0, Value::CreateIntegerValue(source_port_id)); - args.Set(1, Value::CreateStringValue(tab_json)); - args.Set(2, Value::CreateStringValue(extension_id)); + args.Set(0, Value::CreateIntegerValue(dest_port_id)); + args.Set(1, Value::CreateStringValue(channel_name)); + args.Set(2, Value::CreateStringValue(tab_json)); + args.Set(3, Value::CreateStringValue(extension_id)); channel->Send(new ViewMsg_ExtensionMessageInvoke( ExtensionMessageService::kDispatchOnConnect, args)); } @@ -172,7 +174,7 @@ void ExtensionMessageService::AllocatePortIdPair(int* port1, int* port2) { int ExtensionMessageService::OpenChannelToExtension( int routing_id, const std::string& extension_id, - ResourceMessageFilter* source) { + const std::string& channel_name, ResourceMessageFilter* source) { DCHECK_EQ(MessageLoop::current(), ChromeThread::GetMessageLoop(ChromeThread::IO)); DCHECK(initialized_); @@ -182,24 +184,29 @@ int ExtensionMessageService::OpenChannelToExtension( 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. ui_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, &ExtensionMessageService::OpenChannelOnUIThread, - routing_id, port1_id, source->GetProcessId(), extension_id)); + routing_id, port2_id, source->GetProcessId(), extension_id, + channel_name)); - return port2_id; + return port1_id; } void ExtensionMessageService::OpenChannelOnUIThread( - int source_routing_id, int source_port_id, int source_process_id, - const std::string& extension_id) { + int source_routing_id, int receivers_port_id, int source_process_id, + const std::string& extension_id, const std::string& channel_name) { RenderProcessHost* source = RenderProcessHost::FromID(source_process_id); - OpenChannelOnUIThreadImpl(source_routing_id, source_port_id, - source_process_id, source, extension_id); + OpenChannelOnUIThreadImpl(source_routing_id, receivers_port_id, + source_process_id, source, extension_id, + channel_name); } void ExtensionMessageService::OpenChannelOnUIThreadImpl( - int source_routing_id, int source_port_id, int source_process_id, - IPC::Message::Sender* source, const std::string& extension_id) { + int source_routing_id, int receivers_port_id, int source_process_id, + IPC::Message::Sender* source, const std::string& extension_id, + const std::string& channel_name) { DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); if (!source) @@ -224,7 +231,7 @@ void ExtensionMessageService::OpenChannelOnUIThreadImpl( return; } - channels_[GET_CHANNEL_ID(source_port_id)] = channel; + channels_[GET_CHANNEL_ID(receivers_port_id)] = channel; // Include info about the opener's tab (if it was a tab). std::string tab_json = "null"; @@ -239,7 +246,8 @@ void ExtensionMessageService::OpenChannelOnUIThreadImpl( // port ID (the opener has the opposite port ID). for (MessageChannel::Ports::iterator it = channel->receivers.begin(); it != channel->receivers.end(); ++it) { - DispatchOnConnect(*it, source_port_id, tab_json, extension_id); + DispatchOnConnect(*it, receivers_port_id, channel_name, tab_json, + extension_id); } } @@ -259,10 +267,10 @@ int ExtensionMessageService::OpenAutomationChannelToExtension( // This isn't really appropriate here, the originating tab // information should be supplied by the caller for // automation-initiated ports. - OpenChannelOnUIThreadImpl(routing_id, port1_id, source_process_id, - source, extension_id); + OpenChannelOnUIThreadImpl(routing_id, port2_id, source_process_id, + source, extension_id, ""); - return port2_id; + return port1_id; } void ExtensionMessageService::CloseChannel(int port_id) { @@ -292,23 +300,23 @@ void ExtensionMessageService::CloseChannelImpl( } void ExtensionMessageService::PostMessageFromRenderer( - int dest_port_id, const std::string& message) { + int source_port_id, const std::string& message) { DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); MessageChannelMap::iterator iter = - channels_.find(GET_CHANNEL_ID(dest_port_id)); + channels_.find(GET_CHANNEL_ID(source_port_id)); if (iter == channels_.end()) return; // Figure out which port the ID corresponds to. + int dest_port_id = GET_OPPOSITE_PORT_ID(source_port_id); MessageChannel::Ports* ports = IS_OPENER_PORT_ID(dest_port_id) ? &iter->second->opener : &iter->second->receivers; - int source_port_id = GET_OPPOSITE_PORT_ID(dest_port_id); for (MessageChannel::Ports::iterator it = ports->begin(); it != ports->end(); ++it) { - DispatchOnMessage(*it, message, source_port_id); + DispatchOnMessage(*it, message, dest_port_id); } } |