| 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 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "chrome/browser/extensions/api/messaging/message_property_provider.h" | 17 #include "chrome/browser/extensions/api/messaging/message_property_provider.h" |
| 18 #include "content/public/browser/notification_observer.h" | |
| 19 #include "content/public/browser/notification_registrar.h" | |
| 20 #include "extensions/browser/api/messaging/native_message_host.h" | 18 #include "extensions/browser/api/messaging/native_message_host.h" |
| 21 #include "extensions/browser/browser_context_keyed_api_factory.h" | 19 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 22 #include "extensions/common/api/messaging/message.h" | 20 #include "extensions/common/api/messaging/message.h" |
| 23 | 21 |
| 24 class GURL; | 22 class GURL; |
| 25 class Profile; | 23 class Profile; |
| 26 | 24 |
| 27 namespace content { | 25 namespace content { |
| 28 class BrowserContext; | 26 class BrowserContext; |
| 29 class RenderProcessHost; | |
| 30 class WebContents; | |
| 31 } | 27 } |
| 32 | 28 |
| 33 namespace extensions { | 29 namespace extensions { |
| 34 class Extension; | 30 class Extension; |
| 35 class ExtensionHost; | 31 class ExtensionHost; |
| 36 class LazyBackgroundTaskQueue; | 32 class LazyBackgroundTaskQueue; |
| 37 | 33 |
| 38 // This class manages message and event passing between renderer processes. | 34 // This class manages message and event passing between renderer processes. |
| 39 // It maintains a list of processes that are listening to events and a set of | 35 // It maintains a list of processes that are listening to events and a set of |
| 40 // open channels. | 36 // open channels. |
| 41 // | 37 // |
| 42 // Messaging works this way: | 38 // Messaging works this way: |
| 43 // - An extension-owned script context (like a background page or a content | 39 // - An extension-owned script context (like a background page or a content |
| 44 // script) adds an event listener to the "onConnect" event. | 40 // script) adds an event listener to the "onConnect" event. |
| 45 // - Another context calls "runtime.connect()" to open a channel to the | 41 // - Another context calls "runtime.connect()" to open a channel to the |
| 46 // extension process, or an extension context calls "tabs.connect(tabId)" to | 42 // extension process, or an extension context calls "tabs.connect(tabId)" to |
| 47 // open a channel to the content scripts for the given tab. The EMS notifies | 43 // open a channel to the content scripts for the given tab. The EMS notifies |
| 48 // the target process/tab, which then calls the onConnect event in every | 44 // the target process/tab, which then calls the onConnect event in every |
| 49 // context owned by the connecting extension in that process/tab. | 45 // context owned by the connecting extension in that process/tab. |
| 50 // - Once the channel is established, either side can call postMessage to send | 46 // - Once the channel is established, either side can call postMessage to send |
| 51 // a message to the opposite side of the channel, which may have multiple | 47 // a message to the opposite side of the channel, which may have multiple |
| 52 // listeners. | 48 // listeners. |
| 53 // | 49 // |
| 54 // Terminology: | 50 // Terminology: |
| 55 // channel: connection between two ports | 51 // channel: connection between two ports |
| 56 // port: an IPC::Message::Process interface and an optional routing_id (in the | 52 // port: One sender or receivers tied to one or more RenderFrameHost instances. |
| 57 // case that the port is a tab). The Process is usually either a | 53 class MessageService : public BrowserContextKeyedAPI { |
| 58 // RenderProcessHost or a RenderViewHost. | |
| 59 class MessageService : public BrowserContextKeyedAPI, | |
| 60 public content::NotificationObserver { | |
| 61 public: | 54 public: |
| 62 // A messaging channel. Note that the opening port can be the same as the | 55 // A messaging channel. Note that the opening port can be the same as the |
| 63 // receiver, if an extension background page wants to talk to its tab (for | 56 // receiver, if an extension background page wants to talk to its tab (for |
| 64 // example). | 57 // example). |
| 65 struct MessageChannel; | 58 struct MessageChannel; |
| 66 | 59 |
| 67 // One side of the communication handled by extensions::MessageService. | 60 // One side of the communication handled by extensions::MessageService. |
| 68 class MessagePort { | 61 class MessagePort { |
| 69 public: | 62 public: |
| 70 virtual ~MessagePort() {} | 63 virtual ~MessagePort() {} |
| 71 // Notify the port that the channel has been opened. | 64 // Notify the port that the channel has been opened. |
| 72 virtual void DispatchOnConnect(int dest_port_id, | 65 virtual void DispatchOnConnect(const std::string& channel_name, |
| 73 const std::string& channel_name, | |
| 74 scoped_ptr<base::DictionaryValue> source_tab, | 66 scoped_ptr<base::DictionaryValue> source_tab, |
| 75 int source_frame_id, | 67 int source_frame_id, |
| 76 int target_tab_id, | |
| 77 int target_frame_id, | |
| 78 int guest_process_id, | 68 int guest_process_id, |
| 79 int guest_render_frame_routing_id, | 69 int guest_render_frame_routing_id, |
| 80 const std::string& source_extension_id, | 70 const std::string& source_extension_id, |
| 81 const std::string& target_extension_id, | 71 const std::string& target_extension_id, |
| 82 const GURL& source_url, | 72 const GURL& source_url, |
| 83 const std::string& tls_channel_id) {} | 73 const std::string& tls_channel_id) {} |
| 84 | 74 |
| 85 // Notify the port that the channel has been closed. If |error_message| is | 75 // Notify the port that the channel has been closed. If |error_message| is |
| 86 // non-empty, it indicates an error occurred while opening the connection. | 76 // non-empty, it indicates an error occurred while opening the connection. |
| 87 virtual void DispatchOnDisconnect(int source_port_id, | 77 virtual void DispatchOnDisconnect(const std::string& error_message) {} |
| 88 const std::string& error_message) {} | |
| 89 | 78 |
| 90 // Dispatch a message to this end of the communication. | 79 // Dispatch a message to this end of the communication. |
| 91 virtual void DispatchOnMessage(const Message& message, | 80 virtual void DispatchOnMessage(const Message& message) = 0; |
| 92 int target_port_id) = 0; | |
| 93 | 81 |
| 94 // MessagPorts that target extensions will need to adjust their keepalive | 82 // Mark the port as opened by the specific frame. |
| 83 virtual void OpenPort(int process_id, int routing_id) {} |
| 84 |
| 85 // Close the port for the given frame. |
| 86 virtual void ClosePort(int process_id, int routing_id) {} |
| 87 |
| 88 // MessagePorts that target extensions will need to adjust their keepalive |
| 95 // counts for their lazy background page. | 89 // counts for their lazy background page. |
| 96 virtual void IncrementLazyKeepaliveCount() {} | 90 virtual void IncrementLazyKeepaliveCount() {} |
| 97 virtual void DecrementLazyKeepaliveCount() {} | 91 virtual void DecrementLazyKeepaliveCount() {} |
| 98 | 92 |
| 99 // Get the RenderProcessHost (if any) associated with the port. | |
| 100 virtual content::RenderProcessHost* GetRenderProcessHost(); | |
| 101 | |
| 102 protected: | 93 protected: |
| 103 MessagePort() {} | 94 MessagePort() {} |
| 104 | 95 |
| 105 private: | 96 private: |
| 106 DISALLOW_COPY_AND_ASSIGN(MessagePort); | 97 DISALLOW_COPY_AND_ASSIGN(MessagePort); |
| 107 }; | 98 }; |
| 108 | 99 |
| 109 enum PolicyPermission { | 100 enum PolicyPermission { |
| 110 DISALLOW, // The host is not allowed. | 101 DISALLOW, // The host is not allowed. |
| 111 ALLOW_SYSTEM_ONLY, // Allowed only when installed on system level. | 102 ALLOW_SYSTEM_ONLY, // Allowed only when installed on system level. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 137 const std::string& source_extension_id, | 128 const std::string& source_extension_id, |
| 138 const std::string& target_extension_id, | 129 const std::string& target_extension_id, |
| 139 const GURL& source_url, | 130 const GURL& source_url, |
| 140 const std::string& channel_name, | 131 const std::string& channel_name, |
| 141 bool include_tls_channel_id); | 132 bool include_tls_channel_id); |
| 142 | 133 |
| 143 // Same as above, but opens a channel to the tab with the given ID. Messages | 134 // Same as above, but opens a channel to the tab with the given ID. Messages |
| 144 // are restricted to that tab, so if there are multiple tabs in that process, | 135 // are restricted to that tab, so if there are multiple tabs in that process, |
| 145 // only the targeted tab will receive messages. | 136 // only the targeted tab will receive messages. |
| 146 void OpenChannelToTab(int source_process_id, | 137 void OpenChannelToTab(int source_process_id, |
| 138 int source_routing_id, |
| 147 int receiver_port_id, | 139 int receiver_port_id, |
| 148 int tab_id, | 140 int tab_id, |
| 149 int frame_id, | 141 int frame_id, |
| 150 const std::string& extension_id, | 142 const std::string& extension_id, |
| 151 const std::string& channel_name); | 143 const std::string& channel_name); |
| 152 | 144 |
| 153 void OpenChannelToNativeApp( | 145 void OpenChannelToNativeApp( |
| 154 int source_process_id, | 146 int source_process_id, |
| 155 int source_routing_id, | 147 int source_routing_id, |
| 156 int receiver_port_id, | 148 int receiver_port_id, |
| 157 const std::string& source_extension_id, | 149 const std::string& source_extension_id, |
| 158 const std::string& native_app_name); | 150 const std::string& native_app_name); |
| 159 | 151 |
| 152 // Acknowledges open port in the frame identified by (process_id, routing_id). |
| 153 void OpenPort(int port_id, int process_id, int routing_id); |
| 154 |
| 155 // Closes the given port in the given frame. If this was the last frame or if |
| 156 // |force_close| is true, then the other side is closed as well. |
| 157 void ClosePort(int port_id, int process_id, int routing_id, bool force_close); |
| 158 |
| 160 // Closes the message channel associated with the given port, and notifies | 159 // Closes the message channel associated with the given port, and notifies |
| 161 // the other side. | 160 // the other side. |
| 162 void CloseChannel(int port_id, const std::string& error_message); | 161 void CloseChannel(int port_id, const std::string& error_message); |
| 163 | 162 |
| 164 // Enqueues a message on a pending channel, or sends a message to the given | 163 // Enqueues a message on a pending channel, or sends a message to the given |
| 165 // port if the channel isn't pending. | 164 // port if the channel isn't pending. |
| 166 void PostMessage(int port_id, const Message& message); | 165 void PostMessage(int port_id, const Message& message); |
| 167 | 166 |
| 168 private: | 167 private: |
| 169 friend class MockMessageService; | 168 friend class MockMessageService; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 | 200 |
| 202 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, | 201 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, |
| 203 int port_id, | 202 int port_id, |
| 204 const std::string& error_message, | 203 const std::string& error_message, |
| 205 bool notify_other_port); | 204 bool notify_other_port); |
| 206 | 205 |
| 207 // Have MessageService take ownership of |channel|, and remove any pending | 206 // Have MessageService take ownership of |channel|, and remove any pending |
| 208 // channels with the same id. | 207 // channels with the same id. |
| 209 void AddChannel(MessageChannel* channel, int receiver_port_id); | 208 void AddChannel(MessageChannel* channel, int receiver_port_id); |
| 210 | 209 |
| 211 // content::NotificationObserver interface. | |
| 212 void Observe(int type, | |
| 213 const content::NotificationSource& source, | |
| 214 const content::NotificationDetails& details) override; | |
| 215 | |
| 216 // A process that might be in our list of channels has closed. | |
| 217 void OnProcessClosed(content::RenderProcessHost* process); | |
| 218 | |
| 219 // If the channel is being opened from an incognito tab the user must allow | 210 // If the channel is being opened from an incognito tab the user must allow |
| 220 // the connection. | 211 // the connection. |
| 221 void OnOpenChannelAllowed(scoped_ptr<OpenChannelParams> params, bool allowed); | 212 void OnOpenChannelAllowed(scoped_ptr<OpenChannelParams> params, bool allowed); |
| 222 void GotChannelID(scoped_ptr<OpenChannelParams> params, | 213 void GotChannelID(scoped_ptr<OpenChannelParams> params, |
| 223 const std::string& tls_channel_id); | 214 const std::string& tls_channel_id); |
| 224 | 215 |
| 225 // Enqueues a message on a pending channel. | 216 // Enqueues a message on a pending channel. |
| 226 void EnqueuePendingMessage(int port_id, int channel_id, | 217 void EnqueuePendingMessage(int port_id, int channel_id, |
| 227 const Message& message); | 218 const Message& message); |
| 228 | 219 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 259 } | 250 } |
| 260 void PendingLazyBackgroundPagePostMessage(int port_id, | 251 void PendingLazyBackgroundPagePostMessage(int port_id, |
| 261 const Message& message, | 252 const Message& message, |
| 262 extensions::ExtensionHost* host) { | 253 extensions::ExtensionHost* host) { |
| 263 if (host) | 254 if (host) |
| 264 PostMessage(port_id, message); | 255 PostMessage(port_id, message); |
| 265 } | 256 } |
| 266 | 257 |
| 267 // Immediate dispatches a disconnect to |source| for |port_id|. Sets source's | 258 // Immediate dispatches a disconnect to |source| for |port_id|. Sets source's |
| 268 // runtime.lastMessage to |error_message|, if any. | 259 // runtime.lastMessage to |error_message|, if any. |
| 269 void DispatchOnDisconnect(content::RenderProcessHost* source, | 260 void DispatchOnDisconnect(content::RenderFrameHost* source, |
| 270 int port_id, | 261 int port_id, |
| 271 const std::string& error_message); | 262 const std::string& error_message); |
| 272 | 263 |
| 273 void DispatchPendingMessages(const PendingMessagesQueue& queue, | 264 void DispatchPendingMessages(const PendingMessagesQueue& queue, |
| 274 int channel_id); | 265 int channel_id); |
| 275 | 266 |
| 276 // BrowserContextKeyedAPI implementation. | 267 // BrowserContextKeyedAPI implementation. |
| 277 static const char* service_name() { | 268 static const char* service_name() { |
| 278 return "MessageService"; | 269 return "MessageService"; |
| 279 } | 270 } |
| 280 static const bool kServiceRedirectedInIncognito = true; | 271 static const bool kServiceRedirectedInIncognito = true; |
| 281 static const bool kServiceIsCreatedWithBrowserContext = false; | 272 static const bool kServiceIsCreatedWithBrowserContext = false; |
| 282 static const bool kServiceIsNULLWhileTesting = true; | 273 static const bool kServiceIsNULLWhileTesting = true; |
| 283 | 274 |
| 284 content::NotificationRegistrar registrar_; | |
| 285 MessageChannelMap channels_; | 275 MessageChannelMap channels_; |
| 286 // A set of channel IDs waiting for TLS channel IDs to complete opening, and | 276 // A set of channel IDs waiting for TLS channel IDs to complete opening, and |
| 287 // any pending messages queued to be sent on those channels. This and the | 277 // any pending messages queued to be sent on those channels. This and the |
| 288 // following two maps form a pipeline where messages are queued before the | 278 // following two maps form a pipeline where messages are queued before the |
| 289 // channel they are addressed to is ready. | 279 // channel they are addressed to is ready. |
| 290 PendingChannelMap pending_tls_channel_id_channels_; | 280 PendingChannelMap pending_tls_channel_id_channels_; |
| 291 // A set of channel IDs waiting for user permission to cross the border | 281 // A set of channel IDs waiting for user permission to cross the border |
| 292 // between an incognito page and an app or extension, and any pending messages | 282 // between an incognito page and an app or extension, and any pending messages |
| 293 // queued to be sent on those channels. | 283 // queued to be sent on those channels. |
| 294 PendingChannelMap pending_incognito_channels_; | 284 PendingChannelMap pending_incognito_channels_; |
| 295 PendingLazyBackgroundPageChannelMap pending_lazy_background_page_channels_; | 285 PendingLazyBackgroundPageChannelMap pending_lazy_background_page_channels_; |
| 296 MessagePropertyProvider property_provider_; | 286 MessagePropertyProvider property_provider_; |
| 297 | 287 |
| 298 // Weak pointer. Guaranteed to outlive this class. | 288 // Weak pointer. Guaranteed to outlive this class. |
| 299 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 289 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
| 300 | 290 |
| 301 base::WeakPtrFactory<MessageService> weak_factory_; | 291 base::WeakPtrFactory<MessageService> weak_factory_; |
| 302 | 292 |
| 303 DISALLOW_COPY_AND_ASSIGN(MessageService); | 293 DISALLOW_COPY_AND_ASSIGN(MessageService); |
| 304 }; | 294 }; |
| 305 | 295 |
| 306 } // namespace extensions | 296 } // namespace extensions |
| 307 | 297 |
| 308 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 298 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
| OLD | NEW |