| 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_API_MESSAGING_MESSAGE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_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/scoped_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | |
| 16 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
| 18 | 16 |
| 19 class Profile; | 17 class Profile; |
| 20 | 18 |
| 21 namespace content { | 19 namespace content { |
| 22 class RenderProcessHost; | 20 class RenderProcessHost; |
| 23 class WebContents; | 21 class WebContents; |
| 24 } | 22 } |
| 25 | 23 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 41 // context owned by the connecting extension in that process/tab. | 39 // context owned by the connecting extension in that process/tab. |
| 42 // - Once the channel is established, either side can call postMessage to send | 40 // - Once the channel is established, either side can call postMessage to send |
| 43 // a message to the opposite side of the channel, which may have multiple | 41 // a message to the opposite side of the channel, which may have multiple |
| 44 // listeners. | 42 // listeners. |
| 45 // | 43 // |
| 46 // Terminology: | 44 // Terminology: |
| 47 // channel: connection between two ports | 45 // channel: connection between two ports |
| 48 // port: an IPC::Message::Process interface and an optional routing_id (in the | 46 // port: an IPC::Message::Process interface and an optional routing_id (in the |
| 49 // case that the port is a tab). The Process is usually either a | 47 // case that the port is a tab). The Process is usually either a |
| 50 // RenderProcessHost or a RenderViewHost. | 48 // RenderProcessHost or a RenderViewHost. |
| 51 class MessageService : public content::NotificationObserver, | 49 class MessageService : public content::NotificationObserver { |
| 52 public NativeMessageProcessHost::Client { | |
| 53 public: | 50 public: |
| 54 // A messaging channel. Note that the opening port can be the same as the | 51 // A messaging channel. Note that the opening port can be the same as the |
| 55 // receiver, if an extension background page wants to talk to its tab (for | 52 // receiver, if an extension background page wants to talk to its tab (for |
| 56 // example). | 53 // example). |
| 57 struct MessageChannel; | 54 struct MessageChannel; |
| 58 | 55 struct MessagePort; |
| 59 // One side of the communication handled by extensions::MessageService. | |
| 60 class MessagePort { | |
| 61 public: | |
| 62 virtual ~MessagePort() {} | |
| 63 // Notify the port that the channel has been opened. | |
| 64 virtual void DispatchOnConnect(int dest_port_id, | |
| 65 const std::string& channel_name, | |
| 66 const std::string& tab_json, | |
| 67 const std::string& source_extension_id, | |
| 68 const std::string& target_extension_id) {} | |
| 69 | |
| 70 // Notify the port that the channel has been closed. | |
| 71 virtual void DispatchOnDisconnect(int source_port_id, | |
| 72 bool connection_error) {} | |
| 73 | |
| 74 // Dispatch a message to this end of the communication. | |
| 75 virtual void DispatchOnMessage(const std::string& message, | |
| 76 int target_port_id) = 0; | |
| 77 | |
| 78 // MessagPorts that target extensions will need to adjust their keepalive | |
| 79 // counts for their lazy background page. | |
| 80 virtual void IncrementLazyKeepaliveCount() {} | |
| 81 virtual void DecrementLazyKeepaliveCount() {} | |
| 82 | |
| 83 // Get the RenderProcessHost (if any) associated with the port. | |
| 84 virtual content::RenderProcessHost* GetRenderProcessHost(); | |
| 85 | |
| 86 protected: | |
| 87 MessagePort() {} | |
| 88 | |
| 89 private: | |
| 90 DISALLOW_COPY_AND_ASSIGN(MessagePort); | |
| 91 }; | |
| 92 | 56 |
| 93 // Allocates a pair of port ids. | 57 // Allocates a pair of port ids. |
| 94 // NOTE: this can be called from any thread. | 58 // NOTE: this can be called from any thread. |
| 95 static void AllocatePortIdPair(int* port1, int* port2); | 59 static void AllocatePortIdPair(int* port1, int* port2); |
| 96 | 60 |
| 97 explicit MessageService(LazyBackgroundTaskQueue* queue); | 61 explicit MessageService(LazyBackgroundTaskQueue* queue); |
| 98 virtual ~MessageService(); | 62 virtual ~MessageService(); |
| 99 | 63 |
| 100 // Given an extension's ID, opens a channel between the given renderer "port" | 64 // Given an extension's ID, opens a channel between the given renderer "port" |
| 101 // and every listening context owned by that extension. |channel_name| is | 65 // and every listening context owned by that extension. |channel_name| is |
| 102 // an optional identifier for use by extension developers. | 66 // an optional identifier for use by extension developers. |
| 103 void OpenChannelToExtension( | 67 void OpenChannelToExtension( |
| 104 int source_process_id, int source_routing_id, int receiver_port_id, | 68 int source_process_id, int source_routing_id, int receiver_port_id, |
| 105 const std::string& source_extension_id, | 69 const std::string& source_extension_id, |
| 106 const std::string& target_extension_id, | 70 const std::string& target_extension_id, |
| 107 const std::string& channel_name); | 71 const std::string& channel_name); |
| 108 | 72 |
| 109 // Same as above, but opens a channel to the tab with the given ID. Messages | 73 // Same as above, but opens a channel to the tab with the given ID. Messages |
| 110 // are restricted to that tab, so if there are multiple tabs in that process, | 74 // are restricted to that tab, so if there are multiple tabs in that process, |
| 111 // only the targeted tab will receive messages. | 75 // only the targeted tab will receive messages. |
| 112 void OpenChannelToTab( | 76 void OpenChannelToTab( |
| 113 int source_process_id, int source_routing_id, int receiver_port_id, | 77 int source_process_id, int source_routing_id, int receiver_port_id, |
| 114 int tab_id, const std::string& extension_id, | 78 int tab_id, const std::string& extension_id, |
| 115 const std::string& channel_name); | 79 const std::string& channel_name); |
| 116 | 80 |
| 117 void OpenChannelToNativeApp( | |
| 118 int source_process_id, | |
| 119 int source_routing_id, | |
| 120 int receiver_port_id, | |
| 121 const std::string& source_extension_id, | |
| 122 const std::string& native_app_name, | |
| 123 const std::string& channel_name, | |
| 124 const std::string& connect_message); | |
| 125 | |
| 126 // Should be called on the UI thread. | |
| 127 void FinalizeOpenChannelToNativeApp( | |
| 128 int receiver_port_id, | |
| 129 const std::string& channel_name, | |
| 130 scoped_ptr<MessageChannel> channel, | |
| 131 const std::string& tab_json, | |
| 132 NativeMessageProcessHost::ScopedHost native_process); | |
| 133 | |
| 134 // Closes the message channel associated with the given port, and notifies | 81 // Closes the message channel associated with the given port, and notifies |
| 135 // the other side. | 82 // the other side. |
| 136 virtual void CloseChannel(int port_id, bool connection_error) OVERRIDE; | 83 void CloseChannel(int port_id, bool connection_error); |
| 137 | 84 |
| 138 // Sends a message to the given port. | 85 // Sends a message from a renderer to the given port. |
| 139 void PostMessage(int port_id, const std::string& message); | 86 void PostMessageFromRenderer(int port_id, const std::string& message); |
| 140 | |
| 141 // NativeMessageProcessHost::Client | |
| 142 virtual void PostMessageFromNativeProcess( | |
| 143 int port_id, const std::string& message) OVERRIDE; | |
| 144 | 87 |
| 145 private: | 88 private: |
| 146 friend class MockMessageService; | 89 friend class MockMessageService; |
| 147 struct OpenChannelParams; | 90 struct OpenChannelParams; |
| 148 | 91 |
| 149 // A map of channel ID to its channel object. | 92 // A map of channel ID to its channel object. |
| 150 typedef std::map<int, MessageChannel*> MessageChannelMap; | 93 typedef std::map<int, MessageChannel*> MessageChannelMap; |
| 151 | 94 |
| 152 // A map of channel ID to information about the extension that is waiting | 95 // A map of channel ID to information about the extension that is waiting |
| 153 // for that channel to open. Used for lazy background pages. | 96 // for that channel to open. Used for lazy background pages. |
| 154 typedef std::string ExtensionID; | 97 typedef std::string ExtensionID; |
| 155 typedef std::pair<Profile*, ExtensionID> PendingChannel; | 98 typedef std::pair<Profile*, ExtensionID> PendingChannel; |
| 156 typedef std::map<int, PendingChannel> PendingChannelMap; | 99 typedef std::map<int, PendingChannel> PendingChannelMap; |
| 157 | 100 |
| 158 // Common among OpenChannel* variants. | 101 // Common among OpenChannel* variants. |
| 159 bool OpenChannelImpl(scoped_ptr<OpenChannelParams> params); | 102 bool OpenChannelImpl(const OpenChannelParams& params); |
| 160 | 103 |
| 161 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, | 104 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, |
| 162 int port_id, bool connection_error, | 105 int port_id, bool connection_error, |
| 163 bool notify_other_port); | 106 bool notify_other_port); |
| 164 | 107 |
| 165 // Have MessageService take ownership of |channel|, and remove any pending | |
| 166 // channels with the same id. | |
| 167 void AddChannel(MessageChannel* channel, int receiver_port_id); | |
| 168 | |
| 169 // content::NotificationObserver interface. | 108 // content::NotificationObserver interface. |
| 170 virtual void Observe(int type, | 109 virtual void Observe(int type, |
| 171 const content::NotificationSource& source, | 110 const content::NotificationSource& source, |
| 172 const content::NotificationDetails& details) OVERRIDE; | 111 const content::NotificationDetails& details) OVERRIDE; |
| 173 | 112 |
| 174 // A process that might be in our list of channels has closed. | 113 // A process that might be in our list of channels has closed. |
| 175 void OnProcessClosed(content::RenderProcessHost* process); | 114 void OnProcessClosed(content::RenderProcessHost* process); |
| 176 | 115 |
| 177 // Potentially registers a pending task with the LazyBackgroundTaskQueue | 116 // Potentially registers a pending task with the LazyBackgroundTaskQueue |
| 178 // to open a channel. Returns true if a task was queued. | 117 // to open a channel. Returns true if a task was queued. |
| 179 // Takes ownership of |params| if true is returned. | |
| 180 bool MaybeAddPendingOpenChannelTask(Profile* profile, | 118 bool MaybeAddPendingOpenChannelTask(Profile* profile, |
| 181 OpenChannelParams* params); | 119 const OpenChannelParams& params); |
| 182 | 120 |
| 183 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an | 121 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an |
| 184 // ExtensionHost to its task callbacks, though some of our callbacks don't | 122 // ExtensionHost to its task callbacks, though some of our callbacks don't |
| 185 // use that argument. | 123 // use that argument. |
| 186 void PendingOpenChannel(scoped_ptr<OpenChannelParams> params, | 124 void PendingOpenChannel(const OpenChannelParams& params, |
| 187 int source_process_id, | 125 int source_process_id, |
| 188 extensions::ExtensionHost* host); | 126 extensions::ExtensionHost* host); |
| 189 void PendingCloseChannel(int port_id, | 127 void PendingCloseChannel(int port_id, |
| 190 bool connection_error, | 128 bool connection_error, |
| 191 extensions::ExtensionHost* host) { | 129 extensions::ExtensionHost* host) { |
| 192 if (host) | 130 if (host) |
| 193 CloseChannel(port_id, connection_error); | 131 CloseChannel(port_id, connection_error); |
| 194 } | 132 } |
| 195 void PendingPostMessage(int port_id, | 133 void PendingPostMessage(int port_id, |
| 196 const std::string& message, | 134 const std::string& message, |
| 197 extensions::ExtensionHost* host) { | 135 extensions::ExtensionHost* host) { |
| 198 if (host) | 136 if (host) |
| 199 PostMessage(port_id, message); | 137 PostMessageFromRenderer(port_id, message); |
| 200 } | 138 } |
| 201 | 139 |
| 202 content::NotificationRegistrar registrar_; | 140 content::NotificationRegistrar registrar_; |
| 203 MessageChannelMap channels_; | 141 MessageChannelMap channels_; |
| 204 PendingChannelMap pending_channels_; | 142 PendingChannelMap pending_channels_; |
| 205 | 143 |
| 206 // Weak pointer. Guaranteed to outlive this class. | 144 // Weak pointer. Guaranteed to outlive this class. |
| 207 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 145 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
| 208 | 146 |
| 209 base::WeakPtrFactory<MessageService> weak_factory_; | |
| 210 | |
| 211 DISALLOW_COPY_AND_ASSIGN(MessageService); | 147 DISALLOW_COPY_AND_ASSIGN(MessageService); |
| 212 }; | 148 }; |
| 213 | 149 |
| 214 } // namespace extensions | 150 } // namespace extensions |
| 215 | 151 |
| 216 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 152 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
| OLD | NEW |