diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-20 21:05:37 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-20 21:05:37 +0000 |
commit | 7120f1327bcd3f0c33a983ee4a61c277747bb566 (patch) | |
tree | da00ed514530587dd14f76941063d34a4afcda88 /chrome/browser/extensions | |
parent | 6d33519bd4d811112cef3d64d9c346531d65ab9b (diff) | |
download | chromium_src-7120f1327bcd3f0c33a983ee4a61c277747bb566.zip chromium_src-7120f1327bcd3f0c33a983ee4a61c277747bb566.tar.gz chromium_src-7120f1327bcd3f0c33a983ee4a61c277747bb566.tar.bz2 |
Changed the extension.connect() API not to broadcast to all tabs. Added a
more specific tabs.connect(tabId) API to connect to a specific tab.
Also changed the ExtensionMessageService from a singleton to a Profile-owned object.
BUG=12461
TEST=no
Review URL: http://codereview.chromium.org/155707
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21102 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
8 files changed, 240 insertions, 188 deletions
diff --git a/chrome/browser/extensions/extension_bookmarks_module.cc b/chrome/browser/extensions/extension_bookmarks_module.cc index 9f978ef..2d7cbb3 100644 --- a/chrome/browser/extensions/extension_bookmarks_module.cc +++ b/chrome/browser/extensions/extension_bookmarks_module.cc @@ -154,8 +154,10 @@ void ExtensionBookmarkEventRouter::Observe(BookmarkModel* model) { void ExtensionBookmarkEventRouter::DispatchEvent(Profile *profile, const char* event_name, const std::string json_args) { - ExtensionMessageService::GetInstance(profile->GetRequestContext())-> - DispatchEventToRenderers(event_name, json_args); + if (profile->GetExtensionMessageService()) { + profile->GetExtensionMessageService()-> + DispatchEventToRenderers(event_name, json_args); + } } void ExtensionBookmarkEventRouter::Loaded(BookmarkModel* model) { diff --git a/chrome/browser/extensions/extension_browser_event_router.cc b/chrome/browser/extensions/extension_browser_event_router.cc index a689d32..1fcb3e7 100644 --- a/chrome/browser/extensions/extension_browser_event_router.cc +++ b/chrome/browser/extensions/extension_browser_event_router.cc @@ -85,8 +85,10 @@ ExtensionBrowserEventRouter* ExtensionBrowserEventRouter::GetInstance() { static void DispatchEvent(Profile* profile, const char* event_name, const std::string json_args) { - ExtensionMessageService::GetInstance(profile->GetRequestContext())-> - DispatchEventToRenderers(event_name, json_args); + if (profile->GetExtensionMessageService()) { + profile->GetExtensionMessageService()-> + DispatchEventToRenderers(event_name, json_args); + } } static void DispatchSimpleBrowserEvent(Profile* profile, diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 4b13d98..57a1212 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -189,13 +189,10 @@ ExtensionFunctionDispatcher::ExtensionFunctionDispatcher( ALLOW_THIS_IN_INITIALIZER_LIST(peer_(new Peer(this))) { all_instances()->insert(this); - // Ensure the message service is initialized. - ExtensionMessageService::GetInstance(profile()->GetRequestContext())->Init(); - // Notify the ExtensionProcessManager that the view was created. ExtensionProcessManager* epm = profile()->GetExtensionProcessManager(); epm->RegisterExtensionProcess(extension_id(), - render_view_host->process()->pid()); + render_view_host->process()->pid()); } ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { diff --git a/chrome/browser/extensions/extension_message_service.cc b/chrome/browser/extensions/extension_message_service.cc index 70bfda7..5424ad2 100644 --- a/chrome/browser/extensions/extension_message_service.cc +++ b/chrome/browser/extensions/extension_message_service.cc @@ -10,7 +10,9 @@ #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" #include "chrome/browser/renderer_host/render_process_host.h" #include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/browser/renderer_host/resource_message_filter.h" @@ -32,17 +34,25 @@ // Change even to odd and vice versa, to get the other side of a given channel. #define GET_OPPOSITE_PORT_ID(source_port_id) ((source_port_id) ^ 1) -namespace { -typedef std::map<URLRequestContext*, ExtensionMessageService*> InstanceMap; -struct SingletonData { - ~SingletonData() { - STLDeleteContainerPairSecondPointers(map.begin(), map.end()); - } - Lock lock; - InstanceMap map; +struct ExtensionMessageService::MessagePort { + IPC::Message::Sender* sender; + int routing_id; + + MessagePort(IPC::Message::Sender* sender = NULL, + int routing_id = MSG_ROUTING_CONTROL) : + sender(sender), routing_id(routing_id) {} +}; + +struct ExtensionMessageService::MessageChannel { + ExtensionMessageService::MessagePort opener; + ExtensionMessageService::MessagePort receiver; }; -static void DispatchOnConnect(IPC::Message::Sender* channel, int dest_port_id, + +namespace { + +static void DispatchOnConnect(const ExtensionMessageService::MessagePort& port, + int dest_port_id, const std::string& channel_name, const std::string& tab_json, const std::string& extension_id) { @@ -51,51 +61,39 @@ static void DispatchOnConnect(IPC::Message::Sender* channel, int 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)); + port.sender->Send(new ViewMsg_ExtensionMessageInvoke( + port.routing_id, ExtensionMessageService::kDispatchOnConnect, args)); } -static void DispatchOnDisconnect(IPC::Message::Sender* channel, - int source_port_id) { +static void DispatchOnDisconnect( + const ExtensionMessageService::MessagePort& port, int source_port_id) { ListValue args; args.Set(0, Value::CreateIntegerValue(source_port_id)); - channel->Send(new ViewMsg_ExtensionMessageInvoke( - ExtensionMessageService::kDispatchOnDisconnect, args)); + port.sender->Send(new ViewMsg_ExtensionMessageInvoke( + port.routing_id, ExtensionMessageService::kDispatchOnDisconnect, args)); } -static void DispatchOnMessage(IPC::Message::Sender* channel, +static void DispatchOnMessage(const ExtensionMessageService::MessagePort& port, const std::string& message, int source_port_id) { ListValue args; args.Set(0, Value::CreateStringValue(message)); args.Set(1, Value::CreateIntegerValue(source_port_id)); - channel->Send(new ViewMsg_ExtensionMessageInvoke( - ExtensionMessageService::kDispatchOnMessage, args)); + port.sender->Send(new ViewMsg_ExtensionMessageInvoke( + port.routing_id, ExtensionMessageService::kDispatchOnMessage, args)); } -static void DispatchEvent(IPC::Message::Sender* channel, +static void DispatchEvent(const ExtensionMessageService::MessagePort& port, const std::string& event_name, const std::string& event_args) { ListValue args; args.Set(0, Value::CreateStringValue(event_name)); args.Set(1, Value::CreateStringValue(event_args)); - channel->Send(new ViewMsg_ExtensionMessageInvoke( - ExtensionMessageService::kDispatchEvent, args)); -} - -static std::string GetChannelConnectEvent(const std::string& extension_id) { - return StringPrintf("channel-connect:%s", extension_id.c_str()); + port.sender->Send(new ViewMsg_ExtensionMessageInvoke( + port.routing_id, ExtensionMessageService::kDispatchEvent, args)); } } // namespace -// Since ExtensionMessageService is a collection of Singletons, we don't need to -// grab a reference to it when creating Tasks involving it. -template <> struct RunnableMethodTraits<ExtensionMessageService> { - static void RetainCallee(ExtensionMessageService*) {} - static void ReleaseCallee(ExtensionMessageService*) {} -}; - - const char ExtensionMessageService::kDispatchOnConnect[] = "Port.dispatchOnConnect"; const char ExtensionMessageService::kDispatchOnDisconnect[] = @@ -105,37 +103,29 @@ const char ExtensionMessageService::kDispatchOnMessage[] = const char ExtensionMessageService::kDispatchEvent[] = "Event.dispatchJSON"; -// static -ExtensionMessageService* ExtensionMessageService::GetInstance( - URLRequestContext* context) { - SingletonData* data = Singleton<SingletonData>::get(); - AutoLock lock(data->lock); +ExtensionMessageService::ExtensionMessageService(Profile* profile) + : ui_loop_(MessageLoop::current()), profile_(profile), next_port_id_(0) { + DCHECK_EQ(ui_loop_->type(), MessageLoop::TYPE_UI); - ExtensionMessageService* instance = data->map[context]; - if (!instance) { - instance = new ExtensionMessageService(); - data->map[context] = instance; - } - return instance; + registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::RENDER_VIEW_HOST_DELETED, + NotificationService::AllSources()); } -ExtensionMessageService::ExtensionMessageService() - : ui_loop_(NULL), initialized_(false), next_port_id_(0) { +ExtensionMessageService::~ExtensionMessageService() { } -void ExtensionMessageService::Init() { - DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); +void ExtensionMessageService::ProfileDestroyed() { + DCHECK_EQ(ui_loop_->type(), MessageLoop::TYPE_UI); - if (initialized_) - return; - initialized_ = true; + profile_ = NULL; - ui_loop_ = MessageLoop::current(); - - registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, - NotificationService::AllSources()); - registrar_.Add(this, NotificationType::RENDERER_PROCESS_CLOSED, - NotificationService::AllSources()); + // We remove notifications here because our destructor might be called on + // a non-UI thread. + registrar_.RemoveAll(); } void ExtensionMessageService::AddEventListener(std::string event_name, @@ -177,7 +167,6 @@ int ExtensionMessageService::OpenChannelToExtension( const std::string& channel_name, ResourceMessageFilter* source) { DCHECK_EQ(MessageLoop::current(), ChromeThread::GetMessageLoop(ChromeThread::IO)); - DCHECK(initialized_); // Create a channel ID for both sides of the channel. int port1_id = -1; @@ -187,51 +176,83 @@ int ExtensionMessageService::OpenChannelToExtension( // 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, port2_id, source->GetProcessId(), extension_id, + NewRunnableMethod(this, + &ExtensionMessageService::OpenChannelToExtensionOnUIThread, + source->GetProcessId(), routing_id, port2_id, 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_EQ(MessageLoop::current(), + ChromeThread::GetMessageLoop(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. + ui_loop_->PostTask(FROM_HERE, + NewRunnableMethod(this, + &ExtensionMessageService::OpenChannelToTabOnUIThread, + source->GetProcessId(), routing_id, port2_id, tab_id, extension_id, channel_name)); return port1_id; } -void ExtensionMessageService::OpenChannelOnUIThread( - int source_routing_id, int receivers_port_id, int source_process_id, +void ExtensionMessageService::OpenChannelToExtensionOnUIThread( + int source_process_id, int source_routing_id, int receiver_port_id, const std::string& extension_id, const std::string& channel_name) { + if (!profile_) + return; + RenderProcessHost* source = RenderProcessHost::FromID(source_process_id); - OpenChannelOnUIThreadImpl(source_routing_id, receivers_port_id, - source_process_id, source, extension_id, + MessagePort receiver( + profile_->GetExtensionProcessManager()->GetExtensionProcess(extension_id), + MSG_ROUTING_CONTROL); + OpenChannelOnUIThreadImpl(source, source_process_id, source_routing_id, + receiver, receiver_port_id, extension_id, channel_name); } -void ExtensionMessageService::OpenChannelOnUIThreadImpl( - int source_routing_id, int receivers_port_id, int source_process_id, - IPC::Message::Sender* source, const std::string& extension_id, +void ExtensionMessageService::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) { + RenderProcessHost* source = RenderProcessHost::FromID(source_process_id); + TabContents* contents; + MessagePort receiver; + if (ExtensionTabUtil::GetTabById(tab_id, source->profile(), + NULL, NULL, &contents, NULL)) { + receiver.sender = contents->render_view_host(); + receiver.routing_id = contents->render_view_host()->routing_id(); + } + OpenChannelOnUIThreadImpl(source, source_process_id, source_routing_id, + receiver, receiver_port_id, extension_id, + channel_name); +} + +void ExtensionMessageService::OpenChannelOnUIThreadImpl( + IPC::Message::Sender* source, int source_process_id, int source_routing_id, + const MessagePort& receiver, int receiver_port_id, + const std::string& extension_id, const std::string& channel_name) { DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); - if (!source) - return; // Source closed while task was in flight. + // TODO(mpcomplete): notify source if reciever doesn't exist + if (!source || !receiver.sender) + return; // Closed while in flight. linked_ptr<MessageChannel> channel(new MessageChannel); - channel->opener.insert(source); - - // Get the list of processes that are listening for this extension's channel - // connect event. - std::string event_name = GetChannelConnectEvent(extension_id); - std::set<int>& pids = listeners_[event_name]; - for (std::set<int>::iterator pid = pids.begin(); pid != pids.end(); ++pid) { - RenderProcessHost* renderer = RenderProcessHost::FromID(*pid); - if (!renderer) - continue; - channel->receivers.insert(renderer); - } - if (channel->receivers.empty()) { - // Either no one is listening, or all listeners have since closed. - // TODO(mpcomplete): should we notify the source? - return; - } + channel->opener = MessagePort(source, MSG_ROUTING_CONTROL); + channel->receiver = receiver; - channels_[GET_CHANNEL_ID(receivers_port_id)] = channel; + channels_[GET_CHANNEL_ID(receiver_port_id)] = channel; // Include info about the opener's tab (if it was a tab). std::string tab_json = "null"; @@ -242,20 +263,17 @@ void ExtensionMessageService::OpenChannelOnUIThreadImpl( JSONWriter::Write(tab_value, false, &tab_json); } - // Broadcast the connect event to the receivers. Give them the opener's - // port ID (the opener has the opposite port ID). - for (MessageChannel::Ports::iterator it = channel->receivers.begin(); - it != channel->receivers.end(); ++it) { - DispatchOnConnect(*it, receivers_port_id, channel_name, tab_json, - extension_id); - } + // Send the connect event to the receiver. Give it the opener's port ID (the + // opener has the opposite port ID). + DispatchOnConnect(receiver, receiver_port_id, channel_name, tab_json, + extension_id); } int ExtensionMessageService::OpenAutomationChannelToExtension( int source_process_id, int routing_id, const std::string& extension_id, IPC::Message::Sender* source) { DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); - DCHECK(initialized_); + DCHECK(profile_); int port1_id = -1; int port2_id = -1; @@ -267,8 +285,11 @@ 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, port2_id, source_process_id, - source, extension_id, ""); + MessagePort receiver( + profile_->GetExtensionProcessManager()->GetExtensionProcess(extension_id), + MSG_ROUTING_CONTROL); + OpenChannelOnUIThreadImpl(source, source_process_id, routing_id, receiver, + port2_id, extension_id, ""); return port1_id; } @@ -287,15 +308,10 @@ void ExtensionMessageService::CloseChannelImpl( DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); // Notify the other side. - MessageChannel::Ports* ports = - IS_OPENER_PORT_ID(closing_port_id) ? - &channel_iter->second->receivers : &channel_iter->second->opener; - - for (MessageChannel::Ports::iterator it = ports->begin(); - it != ports->end(); ++it) { - DispatchOnDisconnect(*it, GET_OPPOSITE_PORT_ID(closing_port_id)); - } + const MessagePort& port = IS_OPENER_PORT_ID(closing_port_id) ? + channel_iter->second->receiver : channel_iter->second->opener; + DispatchOnDisconnect(port, GET_OPPOSITE_PORT_ID(closing_port_id)); channels_.erase(channel_iter); } @@ -310,14 +326,10 @@ void ExtensionMessageService::PostMessageFromRenderer( // 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; + const MessagePort& port = IS_OPENER_PORT_ID(dest_port_id) ? + iter->second->opener : iter->second->receiver; - for (MessageChannel::Ports::iterator it = ports->begin(); - it != ports->end(); ++it) { - DispatchOnMessage(*it, message, dest_port_id); - } + DispatchOnMessage(port, message, dest_port_id); } void ExtensionMessageService::DispatchEventToRenderers( @@ -346,29 +358,41 @@ void ExtensionMessageService::Observe(NotificationType type, const NotificationDetails& details) { DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_UI); - DCHECK(type.value == NotificationType::RENDERER_PROCESS_TERMINATED || - type.value == NotificationType::RENDERER_PROCESS_CLOSED); - - RenderProcessHost* renderer = Source<RenderProcessHost>(source).ptr(); + switch (type.value) { + case NotificationType::RENDERER_PROCESS_TERMINATED: + case NotificationType::RENDERER_PROCESS_CLOSED: { + RenderProcessHost* renderer = Source<RenderProcessHost>(source).ptr(); + OnSenderClosed(renderer); + + // Remove this renderer from our listener maps. + for (ListenerMap::iterator it = listeners_.begin(); + it != listeners_.end(); ) { + ListenerMap::iterator current = it++; + current->second.erase(renderer->pid()); + if (current->second.empty()) + listeners_.erase(current); + } + break; + } + case NotificationType::RENDER_VIEW_HOST_DELETED: + OnSenderClosed(Details<RenderViewHost>(details).ptr()); + break; + default: + NOTREACHED(); + return; + } +} +void ExtensionMessageService::OnSenderClosed(IPC::Message::Sender* sender) { // Close any channels that share this renderer. We notify the opposite // port that his pair has closed. for (MessageChannelMap::iterator it = channels_.begin(); it != channels_.end(); ) { MessageChannelMap::iterator current = it++; - if (current->second->opener.count(renderer) > 0) { + if (current->second->opener.sender == sender) { CloseChannelImpl(current, GET_CHANNEL_OPENER_ID(current->first)); - } else if (current->second->receivers.count(renderer) > 0) { + } else if (current->second->receiver.sender == sender) { CloseChannelImpl(current, GET_CHANNEL_RECEIVERS_ID(current->first)); } } - - // Remove this renderer from our listener maps. - for (ListenerMap::iterator it = listeners_.begin(); - it != listeners_.end(); ) { - ListenerMap::iterator current = it++; - current->second.erase(renderer->pid()); - if (current->second.empty()) - listeners_.erase(current); - } } diff --git a/chrome/browser/extensions/extension_message_service.h b/chrome/browser/extensions/extension_message_service.h index 77970bc..832451d 100644 --- a/chrome/browser/extensions/extension_message_service.h +++ b/chrome/browser/extensions/extension_message_service.h @@ -11,36 +11,40 @@ #include "base/linked_ptr.h" #include "base/lock.h" +#include "base/ref_counted.h" #include "chrome/common/ipc_message.h" #include "chrome/common/notification_registrar.h" class MessageLoop; +class Profile; class RenderProcessHost; class ResourceMessageFilter; class URLRequestContext; // This class manages message and event passing between renderer processes. -// It maintains a list of processes that are listening to events (including -// messaging events), as well as a set of open channels. -// +// It maintains a list of processes that are listening to events and a set of +// open channels. +// // Messaging works this way: // - An extension-owned script context (like a toolstrip or a content script) -// adds an event listener to the "onConnect" event. We keep track here of a -// list of "listeners" that registered interest in receiving extension -// messages. -// - Another context calls "connect()" to open a channel to every listener -// owned by the same extension. This is a broadcast event, so every listener -// will get notified. +// adds an event listener to the "onConnect" event. +// - Another context calls "extension.connect()" to open a channel to the +// extension process, or an extension context calls "tabs.connect(tabId)" to +// open a channel to the content scripts for the given tab. The EMS notifies +// the target process/tab, which then calls the onConnect event in every +// context owned by the connecting extension in that process/tab. // - Once the channel is established, either side can call postMessage to send // a message to the opposite side of the channel, which may have multiple // listeners. // // Terminology: -// channel: connection between two ports (one side of which can have multiple -// listeners) -// port: one or more IPC::Message::Sender interfaces through which we -// communicate to process(es). These are generally RenderProcessHosts. -class ExtensionMessageService : public NotificationObserver { +// channel: connection between two ports +// 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. +class ExtensionMessageService : + public base::RefCountedThreadSafe<ExtensionMessageService>, + public NotificationObserver { public: // Javascript function name constants. static const char kDispatchOnConnect[]; @@ -49,16 +53,18 @@ class ExtensionMessageService : public NotificationObserver { static const char kDispatchEvent[]; static const char kDispatchError[]; - // Returns the message service for the given context. Messages can only - // be sent within a single context. - static ExtensionMessageService* GetInstance(URLRequestContext* context); - - ExtensionMessageService(); + // A messaging channel. Note that the opening port can be the same as the + // receiver, if an extension toolstrip wants to talk to its tab (for example). + struct MessageChannel; + struct MessagePort; // --- UI thread only: - // UI-thread specific initialization. Does nothing if called more than once. - void Init(); + ExtensionMessageService(Profile* profile); + ~ExtensionMessageService(); + + // Notification that our owning profile is going away. + void ProfileDestroyed(); // Add or remove |render_process_pid| as a listener for |event_name|. void AddEventListener(std::string event_name, int render_process_id); @@ -83,11 +89,6 @@ class ExtensionMessageService : public NotificationObserver { const std::string& extension_id, IPC::Message::Sender* source); - // NotificationObserver interface. - void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details); - // --- IO thread only: // Given an extension's ID, opens a channel between the given renderer "port" @@ -99,18 +100,16 @@ class ExtensionMessageService : public NotificationObserver { int OpenChannelToExtension(int routing_id, const std::string& extension_id, const std::string& channel_name, ResourceMessageFilter* source); - - private: - // A messaging channel. Since messages are broadcast, the channel can have - // multiple processes listening for messages. Note that the opening port - // can also be among the receivers, if an extension toolstrip wants to talk - // to its tab (for example). - struct MessageChannel { - typedef std::set<IPC::Message::Sender*> Ports; - Ports opener; // only 1 opener, but we use a set to simplify logic - Ports receivers; - }; + // 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); + + private: // A map of channel ID to its channel object. typedef std::map<int, linked_ptr<MessageChannel> > MessageChannelMap; @@ -127,15 +126,30 @@ class ExtensionMessageService : public NotificationObserver { // Handles channel creation and notifies the destinations that a channel was // opened. - void OpenChannelOnUIThread(int source_routing_id, - int source_port_id, int source_process_id, - const std::string& extension_id, const std::string& channel_name); + void OpenChannelToExtensionOnUIThread( + int source_process_id, int source_routing_id, int receiver_port_id, + const std::string& 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 OpenAutomationChannelToExtension. void OpenChannelOnUIThreadImpl( - int source_routing_id, int source_port_id, int source_process_id, - IPC::Message::Sender* source, const std::string& extension_id, - const std::string& channel_name); + IPC::Message::Sender* source, int source_process_id, int source_routing_id, + const MessagePort& receiver, int receiver_port_id, + const std::string& extension_id, const std::string& channel_name); + + // NotificationObserver interface. + void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + + // An IPC sender that might be in our list of channels has closed. + void OnSenderClosed(IPC::Message::Sender* sender); + + Profile* profile_; NotificationRegistrar registrar_; @@ -148,9 +162,6 @@ class ExtensionMessageService : public NotificationObserver { // --- UI or IO thread: - // True if Init has been called. - bool initialized_; - // For generating unique channel IDs. int next_port_id_; diff --git a/chrome/browser/extensions/extension_messages_unittest.cc b/chrome/browser/extensions/extension_messages_unittest.cc index c2e4540..7da3f38 100644 --- a/chrome/browser/extensions/extension_messages_unittest.cc +++ b/chrome/browser/extensions/extension_messages_unittest.cc @@ -17,14 +17,14 @@ static void DispatchOnConnect(int source_port_id, const std::string& name, args.Set(2, Value::CreateStringValue(tab_json)); args.Set(3, Value::CreateStringValue("")); // extension ID is empty for tests RendererExtensionBindings::Invoke( - ExtensionMessageService::kDispatchOnConnect, args); + ExtensionMessageService::kDispatchOnConnect, args, NULL); } static void DispatchOnDisconnect(int source_port_id) { ListValue args; args.Set(0, Value::CreateIntegerValue(source_port_id)); RendererExtensionBindings::Invoke( - ExtensionMessageService::kDispatchOnDisconnect, args); + ExtensionMessageService::kDispatchOnDisconnect, args, NULL); } static void DispatchOnMessage(const std::string& message, int source_port_id) { @@ -32,7 +32,7 @@ static void DispatchOnMessage(const std::string& message, int source_port_id) { args.Set(0, Value::CreateStringValue(message)); args.Set(1, Value::CreateIntegerValue(source_port_id)); RendererExtensionBindings::Invoke( - ExtensionMessageService::kDispatchOnMessage, args); + ExtensionMessageService::kDispatchOnMessage, args, NULL); } // Tests that the bindings for opening a channel to an extension and sending diff --git a/chrome/browser/extensions/extension_process_manager.cc b/chrome/browser/extensions/extension_process_manager.cc index 3018ea8..57b969b 100644 --- a/chrome/browser/extensions/extension_process_manager.cc +++ b/chrome/browser/extensions/extension_process_manager.cc @@ -85,7 +85,7 @@ ExtensionHost* ExtensionProcessManager::CreateBackgroundHost( } void ExtensionProcessManager::RegisterExtensionProcess( - std::string extension_id, int process_id) { + const std::string& extension_id, int process_id) { ProcessIDMap::const_iterator it = process_ids_.find(extension_id); if (it != process_ids_.end() && (*it).second == process_id) return; @@ -117,6 +117,17 @@ void ExtensionProcessManager::UnregisterExtensionProcess(int process_id) { } } +RenderProcessHost* ExtensionProcessManager::GetExtensionProcess( + const std::string& extension_id) { + ProcessIDMap::const_iterator it = process_ids_.find(extension_id); + if (it == process_ids_.end()) + return NULL; + + RenderProcessHost* rph = RenderProcessHost::FromID(it->second); + DCHECK(rph) << "We should have unregistered this host."; + return rph; +} + SiteInstance* ExtensionProcessManager::GetSiteInstanceForURL(const GURL& url) { return browsing_instance_->GetSiteInstanceForURL(url); } diff --git a/chrome/browser/extensions/extension_process_manager.h b/chrome/browser/extensions/extension_process_manager.h index e3d6b76..d2af74d 100644 --- a/chrome/browser/extensions/extension_process_manager.h +++ b/chrome/browser/extensions/extension_process_manager.h @@ -21,6 +21,7 @@ class ExtensionView; #endif class GURL; class Profile; +class RenderProcessHost; class SiteInstance; // Manages dynamic state of running Chromium extensions. There is one instance @@ -45,13 +46,17 @@ class ExtensionProcessManager : public NotificationObserver { // Returns the SiteInstance that the given URL belongs to. SiteInstance* GetSiteInstanceForURL(const GURL& url); - // Register an extension process by |extension_id| and specifying which + // Registers an extension process by |extension_id| and specifying which // |process_id| it belongs to. - void RegisterExtensionProcess(std::string extension_id, int process_id); + void RegisterExtensionProcess(const std::string& extension_id, + int process_id); - // Unregister an extension process with specified |process_id|. + // Unregisters an extension process with specified |process_id|. void UnregisterExtensionProcess(int process_id); + // Returns the process that the extension with the given ID is running in. + RenderProcessHost* GetExtensionProcess(const std::string& extension_id); + // NotificationObserver: virtual void Observe(NotificationType type, const NotificationSource& source, |