Chromium Code Reviews| 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 #ifndef CHROME_BROWSER_EXTENSIONS_MESSAGE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_MESSAGE_SERVICE_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_MESSAGE_SERVICE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_MESSAGE_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | |
| 14 #include "content/public/browser/notification_observer.h" | 15 #include "content/public/browser/notification_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" | 16 #include "content/public/browser/notification_registrar.h" |
| 16 | 17 |
| 17 class Profile; | 18 class Profile; |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 class RenderProcessHost; | 21 class RenderProcessHost; |
| 21 class WebContents; | 22 class WebContents; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace extensions { | 25 namespace extensions { |
| 25 class ExtensionHost; | 26 class ExtensionHost; |
| 26 class LazyBackgroundTaskQueue; | 27 class LazyBackgroundTaskQueue; |
| 28 class NativeMessageProcess; | |
| 27 | 29 |
| 28 // This class manages message and event passing between renderer processes. | 30 // This class manages message and event passing between renderer processes. |
| 29 // It maintains a list of processes that are listening to events and a set of | 31 // It maintains a list of processes that are listening to events and a set of |
| 30 // open channels. | 32 // open channels. |
| 31 // | 33 // |
| 32 // Messaging works this way: | 34 // Messaging works this way: |
| 33 // - An extension-owned script context (like a background page or a content | 35 // - An extension-owned script context (like a background page or a content |
| 34 // script) adds an event listener to the "onConnect" event. | 36 // script) adds an event listener to the "onConnect" event. |
| 35 // - Another context calls "extension.connect()" to open a channel to the | 37 // - Another context calls "extension.connect()" to open a channel to the |
| 36 // extension process, or an extension context calls "tabs.connect(tabId)" to | 38 // extension process, or an extension context calls "tabs.connect(tabId)" to |
| 37 // open a channel to the content scripts for the given tab. The EMS notifies | 39 // open a channel to the content scripts for the given tab. The EMS notifies |
| 38 // the target process/tab, which then calls the onConnect event in every | 40 // the target process/tab, which then calls the onConnect event in every |
| 39 // context owned by the connecting extension in that process/tab. | 41 // context owned by the connecting extension in that process/tab. |
| 40 // - Once the channel is established, either side can call postMessage to send | 42 // - Once the channel is established, either side can call postMessage to send |
| 41 // a message to the opposite side of the channel, which may have multiple | 43 // a message to the opposite side of the channel, which may have multiple |
| 42 // listeners. | 44 // listeners. |
| 43 // | 45 // |
| 44 // Terminology: | 46 // Terminology: |
| 45 // channel: connection between two ports | 47 // channel: connection between two ports |
| 46 // port: an IPC::Message::Process interface and an optional routing_id (in the | 48 // port: an IPC::Message::Process interface and an optional routing_id (in the |
| 47 // case that the port is a tab). The Process is usually either a | 49 // case that the port is a tab). The Process is usually either a |
| 48 // RenderProcessHost or a RenderViewHost. | 50 // RenderProcessHost or a RenderViewHost. |
| 49 class MessageService : public content::NotificationObserver { | 51 class MessageService : public content::NotificationObserver { |
| 50 public: | 52 public: |
| 51 // A messaging channel. Note that the opening port can be the same as the | 53 // A messaging channel. Note that the opening port can be the same as the |
| 52 // receiver, if an extension background page wants to talk to its tab (for | 54 // receiver, if an extension background page wants to talk to its tab (for |
| 53 // example). | 55 // example). |
| 54 struct MessageChannel; | 56 struct MessageChannel; |
| 55 struct MessagePort; | |
| 56 | 57 |
| 57 // Allocates a pair of port ids. | 58 // Allocates a pair of port ids. |
| 58 // NOTE: this can be called from any thread. | 59 // NOTE: this can be called from any thread. |
| 59 static void AllocatePortIdPair(int* port1, int* port2); | 60 static void AllocatePortIdPair(int* port1, int* port2); |
| 60 | 61 |
| 61 explicit MessageService(LazyBackgroundTaskQueue* queue); | 62 explicit MessageService(LazyBackgroundTaskQueue* queue); |
| 62 virtual ~MessageService(); | 63 virtual ~MessageService(); |
| 63 | 64 |
| 64 // Given an extension's ID, opens a channel between the given renderer "port" | 65 // Given an extension's ID, opens a channel between the given renderer "port" |
| 65 // and every listening context owned by that extension. |channel_name| is | 66 // and every listening context owned by that extension. |channel_name| is |
| 66 // an optional identifier for use by extension developers. | 67 // an optional identifier for use by extension developers. |
| 67 void OpenChannelToExtension( | 68 void OpenChannelToExtension( |
| 68 int source_process_id, int source_routing_id, int receiver_port_id, | 69 int source_process_id, int source_routing_id, int receiver_port_id, |
| 69 const std::string& source_extension_id, | 70 const std::string& source_extension_id, |
| 70 const std::string& target_extension_id, | 71 const std::string& target_extension_id, |
| 71 const std::string& channel_name); | 72 const std::string& channel_name); |
| 72 | 73 |
| 73 // Same as above, but opens a channel to the tab with the given ID. Messages | 74 // Same as above, but opens a channel to the tab with the given ID. Messages |
| 74 // are restricted to that tab, so if there are multiple tabs in that process, | 75 // are restricted to that tab, so if there are multiple tabs in that process, |
| 75 // only the targeted tab will receive messages. | 76 // only the targeted tab will receive messages. |
| 76 void OpenChannelToTab( | 77 void OpenChannelToTab( |
| 77 int source_process_id, int source_routing_id, int receiver_port_id, | 78 int source_process_id, int source_routing_id, int receiver_port_id, |
| 78 int tab_id, const std::string& extension_id, | 79 int tab_id, const std::string& extension_id, |
| 79 const std::string& channel_name); | 80 const std::string& channel_name); |
| 80 | 81 |
| 82 void OpenChannelToNativeApp( | |
| 83 int source_process_id, int source_routing_id, int receiver_port_id, | |
|
Aaron Boodman
2012/08/15 03:39:21
Nit: Some of these lines could be combined. Either
Matt Perry
2012/08/16 00:01:36
Chromium style is actually to put each arg on its
eaugusti
2012/08/21 23:08:34
Done.
| |
| 84 const std::string& source_extension_id, | |
| 85 const std::string& native_app_name, | |
| 86 const std::string& channel_name, | |
| 87 const std::string& connect_message); | |
| 88 // Should be called on the UI thread. | |
|
Aaron Boodman
2012/08/15 03:39:21
Nit: insert blank line
eaugusti
2012/08/21 23:08:34
Done.
| |
| 89 void FinalizeOpenChannelToNativeApp( | |
| 90 int receiver_port_id, | |
| 91 const std::string& channel_name, | |
| 92 scoped_ptr<MessageChannel> channel, | |
| 93 const std::string& tab_json, | |
| 94 NativeMessageProcess* native_process); | |
| 95 | |
| 81 // Closes the message channel associated with the given port, and notifies | 96 // Closes the message channel associated with the given port, and notifies |
| 82 // the other side. | 97 // the other side. |
| 83 void CloseChannel(int port_id, bool connection_error); | 98 void CloseChannel(int port_id, bool connection_error); |
| 84 | 99 |
| 85 // Sends a message from a renderer to the given port. | 100 // Sends a message from a renderer to the given port. |
| 86 void PostMessageFromRenderer(int port_id, const std::string& message); | 101 void PostMessageFromRenderer(int port_id, const std::string& message); |
| 87 | 102 |
| 88 private: | 103 private: |
| 89 friend class MockMessageService; | 104 friend class MockMessageService; |
| 90 struct OpenChannelParams; | 105 struct OpenChannelParams; |
| 91 | 106 |
| 92 // A map of channel ID to its channel object. | 107 // A map of channel ID to its channel object. |
| 93 typedef std::map<int, MessageChannel*> MessageChannelMap; | 108 typedef std::map<int, MessageChannel*> MessageChannelMap; |
| 94 | 109 |
| 95 // A map of channel ID to information about the extension that is waiting | 110 // A map of channel ID to information about the extension that is waiting |
| 96 // for that channel to open. Used for lazy background pages. | 111 // for that channel to open. Used for lazy background pages. |
| 97 typedef std::string ExtensionID; | 112 typedef std::string ExtensionID; |
| 98 typedef std::pair<Profile*, ExtensionID> PendingChannel; | 113 typedef std::pair<Profile*, ExtensionID> PendingChannel; |
| 99 typedef std::map<int, PendingChannel> PendingChannelMap; | 114 typedef std::map<int, PendingChannel> PendingChannelMap; |
| 100 | 115 |
| 101 // Common among OpenChannel* variants. | 116 // Common among OpenChannel* variants. |
| 102 bool OpenChannelImpl(const OpenChannelParams& params); | 117 bool OpenChannelImpl(scoped_ptr<OpenChannelParams> params); |
| 103 | 118 |
| 104 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, | 119 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, |
| 105 int port_id, bool connection_error, | 120 int port_id, bool connection_error, |
| 106 bool notify_other_port); | 121 bool notify_other_port); |
| 107 | 122 |
| 123 // Have MessageService take ownership of |channel|, and remove any pending | |
| 124 // channels with the same id. | |
| 125 void AddChannel(MessageChannel* channel, int receiver_port_id); | |
| 126 | |
| 108 // content::NotificationObserver interface. | 127 // content::NotificationObserver interface. |
| 109 virtual void Observe(int type, | 128 virtual void Observe(int type, |
| 110 const content::NotificationSource& source, | 129 const content::NotificationSource& source, |
| 111 const content::NotificationDetails& details) OVERRIDE; | 130 const content::NotificationDetails& details) OVERRIDE; |
| 112 | 131 |
| 113 // A process that might be in our list of channels has closed. | 132 // A process that might be in our list of channels has closed. |
| 114 void OnProcessClosed(content::RenderProcessHost* process); | 133 void OnProcessClosed(content::RenderProcessHost* process); |
| 115 | 134 |
| 116 // Potentially registers a pending task with the LazyBackgroundTaskQueue | 135 // Potentially registers a pending task with the LazyBackgroundTaskQueue |
| 117 // to open a channel. Returns true if a task was queued. | 136 // to open a channel. Returns true if a task was queued. |
| 137 // Takes ownership of |params| if true is returned. | |
| 118 bool MaybeAddPendingOpenChannelTask(Profile* profile, | 138 bool MaybeAddPendingOpenChannelTask(Profile* profile, |
| 119 const OpenChannelParams& params); | 139 OpenChannelParams* params); |
| 120 | 140 |
| 121 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an | 141 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an |
| 122 // ExtensionHost to its task callbacks, though some of our callbacks don't | 142 // ExtensionHost to its task callbacks, though some of our callbacks don't |
| 123 // use that argument. | 143 // use that argument. |
| 124 void PendingOpenChannel(const OpenChannelParams& params, | 144 void PendingOpenChannel(scoped_ptr<OpenChannelParams> params, |
| 125 int source_process_id, | 145 int source_process_id, |
| 126 extensions::ExtensionHost* host); | 146 extensions::ExtensionHost* host); |
| 127 void PendingCloseChannel(int port_id, | 147 void PendingCloseChannel(int port_id, |
| 128 bool connection_error, | 148 bool connection_error, |
| 129 extensions::ExtensionHost* host) { | 149 extensions::ExtensionHost* host) { |
| 130 if (host) | 150 if (host) |
| 131 CloseChannel(port_id, connection_error); | 151 CloseChannel(port_id, connection_error); |
| 132 } | 152 } |
| 133 void PendingPostMessage(int port_id, | 153 void PendingPostMessage(int port_id, |
| 134 const std::string& message, | 154 const std::string& message, |
| 135 extensions::ExtensionHost* host) { | 155 extensions::ExtensionHost* host) { |
| 136 if (host) | 156 if (host) |
| 137 PostMessageFromRenderer(port_id, message); | 157 PostMessageFromRenderer(port_id, message); |
| 138 } | 158 } |
| 139 | 159 |
| 140 content::NotificationRegistrar registrar_; | 160 content::NotificationRegistrar registrar_; |
| 141 MessageChannelMap channels_; | 161 MessageChannelMap channels_; |
| 142 PendingChannelMap pending_channels_; | 162 PendingChannelMap pending_channels_; |
| 143 | 163 |
| 144 // Weak pointer. Guaranteed to outlive this class. | 164 // Weak pointer. Guaranteed to outlive this class. |
| 145 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 165 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
| 146 | 166 |
| 167 base::WeakPtrFactory<MessageService> weak_factory_; | |
| 168 | |
| 147 DISALLOW_COPY_AND_ASSIGN(MessageService); | 169 DISALLOW_COPY_AND_ASSIGN(MessageService); |
| 148 }; | 170 }; |
| 149 | 171 |
| 150 } // namespace extensions | 172 } // namespace extensions |
| 151 | 173 |
| 152 #endif // CHROME_BROWSER_EXTENSIONS_MESSAGE_SERVICE_H_ | 174 #endif // CHROME_BROWSER_EXTENSIONS_MESSAGE_SERVICE_H_ |
| OLD | NEW |