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 receiver 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() {} |
| 64 |
| 65 // Called right before a port is connected to a channel. If false, the port |
| 66 // is not used and the channel is closed. |
| 67 virtual bool IsValidPort() = 0; |
| 68 |
71 // Notify the port that the channel has been opened. | 69 // Notify the port that the channel has been opened. |
72 virtual void DispatchOnConnect(int dest_port_id, | 70 virtual void DispatchOnConnect(const std::string& channel_name, |
73 const std::string& channel_name, | |
74 scoped_ptr<base::DictionaryValue> source_tab, | 71 scoped_ptr<base::DictionaryValue> source_tab, |
75 int source_frame_id, | 72 int source_frame_id, |
76 int target_tab_id, | |
77 int target_frame_id, | |
78 int guest_process_id, | 73 int guest_process_id, |
79 int guest_render_frame_routing_id, | 74 int guest_render_frame_routing_id, |
80 const std::string& source_extension_id, | 75 const std::string& source_extension_id, |
81 const std::string& target_extension_id, | 76 const std::string& target_extension_id, |
82 const GURL& source_url, | 77 const GURL& source_url, |
83 const std::string& tls_channel_id) {} | 78 const std::string& tls_channel_id) {} |
84 | 79 |
85 // Notify the port that the channel has been closed. If |error_message| is | 80 // 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. | 81 // non-empty, it indicates an error occurred while opening the connection. |
87 virtual void DispatchOnDisconnect(int source_port_id, | 82 virtual void DispatchOnDisconnect(const std::string& error_message) {} |
88 const std::string& error_message) {} | |
89 | 83 |
90 // Dispatch a message to this end of the communication. | 84 // Dispatch a message to this end of the communication. |
91 virtual void DispatchOnMessage(const Message& message, | 85 virtual void DispatchOnMessage(const Message& message) = 0; |
92 int target_port_id) = 0; | |
93 | 86 |
94 // MessagPorts that target extensions will need to adjust their keepalive | 87 // Mark the port as opened by the specific frame. |
| 88 virtual void OpenPort(int process_id, int routing_id) {} |
| 89 |
| 90 // Close the port for the given frame. |
| 91 virtual void ClosePort(int process_id, int routing_id) {} |
| 92 |
| 93 // MessagePorts that target extensions will need to adjust their keepalive |
95 // counts for their lazy background page. | 94 // counts for their lazy background page. |
96 virtual void IncrementLazyKeepaliveCount() {} | 95 virtual void IncrementLazyKeepaliveCount() {} |
97 virtual void DecrementLazyKeepaliveCount() {} | 96 virtual void DecrementLazyKeepaliveCount() {} |
98 | 97 |
99 // Get the RenderProcessHost (if any) associated with the port. | |
100 virtual content::RenderProcessHost* GetRenderProcessHost(); | |
101 | |
102 protected: | 98 protected: |
103 MessagePort() {} | 99 MessagePort() {} |
104 | 100 |
105 private: | 101 private: |
106 DISALLOW_COPY_AND_ASSIGN(MessagePort); | 102 DISALLOW_COPY_AND_ASSIGN(MessagePort); |
107 }; | 103 }; |
108 | 104 |
109 enum PolicyPermission { | 105 enum PolicyPermission { |
110 DISALLOW, // The host is not allowed. | 106 DISALLOW, // The host is not allowed. |
111 ALLOW_SYSTEM_ONLY, // Allowed only when installed on system level. | 107 ALLOW_SYSTEM_ONLY, // Allowed only when installed on system level. |
(...skipping 25 matching lines...) Expand all Loading... |
137 const std::string& source_extension_id, | 133 const std::string& source_extension_id, |
138 const std::string& target_extension_id, | 134 const std::string& target_extension_id, |
139 const GURL& source_url, | 135 const GURL& source_url, |
140 const std::string& channel_name, | 136 const std::string& channel_name, |
141 bool include_tls_channel_id); | 137 bool include_tls_channel_id); |
142 | 138 |
143 // Same as above, but opens a channel to the tab with the given ID. Messages | 139 // 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, | 140 // are restricted to that tab, so if there are multiple tabs in that process, |
145 // only the targeted tab will receive messages. | 141 // only the targeted tab will receive messages. |
146 void OpenChannelToTab(int source_process_id, | 142 void OpenChannelToTab(int source_process_id, |
| 143 int source_routing_id, |
147 int receiver_port_id, | 144 int receiver_port_id, |
148 int tab_id, | 145 int tab_id, |
149 int frame_id, | 146 int frame_id, |
150 const std::string& extension_id, | 147 const std::string& extension_id, |
151 const std::string& channel_name); | 148 const std::string& channel_name); |
152 | 149 |
153 void OpenChannelToNativeApp( | 150 void OpenChannelToNativeApp( |
154 int source_process_id, | 151 int source_process_id, |
155 int source_routing_id, | 152 int source_routing_id, |
156 int receiver_port_id, | 153 int receiver_port_id, |
157 const std::string& source_extension_id, | 154 const std::string& source_extension_id, |
158 const std::string& native_app_name); | 155 const std::string& native_app_name); |
159 | 156 |
| 157 // Mark the given port as opened by the frame identified by |
| 158 // (process_id, routing_id). |
| 159 void OpenPort(int port_id, int process_id, int routing_id); |
| 160 |
| 161 // Closes the given port in the given frame. If this was the last frame or if |
| 162 // |force_close| is true, then the other side is closed as well. |
| 163 void ClosePort(int port_id, int process_id, int routing_id, bool force_close); |
| 164 |
160 // Closes the message channel associated with the given port, and notifies | 165 // Closes the message channel associated with the given port, and notifies |
161 // the other side. | 166 // the other side. |
162 void CloseChannel(int port_id, const std::string& error_message); | 167 void CloseChannel(int port_id, const std::string& error_message); |
163 | 168 |
164 // Enqueues a message on a pending channel, or sends a message to the given | 169 // Enqueues a message on a pending channel, or sends a message to the given |
165 // port if the channel isn't pending. | 170 // port if the channel isn't pending. |
166 void PostMessage(int port_id, const Message& message); | 171 void PostMessage(int port_id, const Message& message); |
167 | 172 |
168 private: | 173 private: |
169 friend class MockMessageService; | 174 friend class MockMessageService; |
(...skipping 22 matching lines...) Expand all Loading... |
192 // |target_extension| will be non-null if |params->target_extension_id| is | 197 // |target_extension| will be non-null if |params->target_extension_id| is |
193 // non-empty, that is, if the target is an extension, it must exist. | 198 // non-empty, that is, if the target is an extension, it must exist. |
194 // | 199 // |
195 // |did_enqueue| will be true if the channel opening was delayed while | 200 // |did_enqueue| will be true if the channel opening was delayed while |
196 // waiting for an event page to start, false otherwise. | 201 // waiting for an event page to start, false otherwise. |
197 void OpenChannelImpl(content::BrowserContext* browser_context, | 202 void OpenChannelImpl(content::BrowserContext* browser_context, |
198 scoped_ptr<OpenChannelParams> params, | 203 scoped_ptr<OpenChannelParams> params, |
199 const Extension* target_extension, | 204 const Extension* target_extension, |
200 bool did_enqueue); | 205 bool did_enqueue); |
201 | 206 |
| 207 void ClosePortImpl(int port_id, |
| 208 int process_id, |
| 209 int routing_id, |
| 210 bool force_close, |
| 211 const std::string& error_message); |
| 212 |
202 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, | 213 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, |
203 int port_id, | 214 int port_id, |
204 const std::string& error_message, | 215 const std::string& error_message, |
205 bool notify_other_port); | 216 bool notify_other_port); |
206 | 217 |
207 // Have MessageService take ownership of |channel|, and remove any pending | 218 // Have MessageService take ownership of |channel|, and remove any pending |
208 // channels with the same id. | 219 // channels with the same id. |
209 void AddChannel(MessageChannel* channel, int receiver_port_id); | 220 void AddChannel(MessageChannel* channel, int receiver_port_id); |
210 | 221 |
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 | 222 // If the channel is being opened from an incognito tab the user must allow |
220 // the connection. | 223 // the connection. |
221 void OnOpenChannelAllowed(scoped_ptr<OpenChannelParams> params, bool allowed); | 224 void OnOpenChannelAllowed(scoped_ptr<OpenChannelParams> params, bool allowed); |
222 void GotChannelID(scoped_ptr<OpenChannelParams> params, | 225 void GotChannelID(scoped_ptr<OpenChannelParams> params, |
223 const std::string& tls_channel_id); | 226 const std::string& tls_channel_id); |
224 | 227 |
225 // Enqueues a message on a pending channel. | 228 // Enqueues a message on a pending channel. |
226 void EnqueuePendingMessage(int port_id, int channel_id, | 229 void EnqueuePendingMessage(int port_id, int channel_id, |
227 const Message& message); | 230 const Message& message); |
228 | 231 |
(...skipping 15 matching lines...) Expand all Loading... |
244 scoped_ptr<OpenChannelParams>* params, | 247 scoped_ptr<OpenChannelParams>* params, |
245 const PendingMessagesQueue& pending_messages); | 248 const PendingMessagesQueue& pending_messages); |
246 | 249 |
247 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an | 250 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an |
248 // ExtensionHost to its task callbacks, though some of our callbacks don't | 251 // ExtensionHost to its task callbacks, though some of our callbacks don't |
249 // use that argument. | 252 // use that argument. |
250 void PendingLazyBackgroundPageOpenChannel( | 253 void PendingLazyBackgroundPageOpenChannel( |
251 scoped_ptr<OpenChannelParams> params, | 254 scoped_ptr<OpenChannelParams> params, |
252 int source_process_id, | 255 int source_process_id, |
253 extensions::ExtensionHost* host); | 256 extensions::ExtensionHost* host); |
254 void PendingLazyBackgroundPageCloseChannel(int port_id, | 257 void PendingLazyBackgroundPageClosePort(int port_id, |
255 const std::string& error_message, | 258 int process_id, |
256 extensions::ExtensionHost* host) { | 259 int routing_id, |
| 260 bool force_close, |
| 261 const std::string& error_message, |
| 262 extensions::ExtensionHost* host) { |
257 if (host) | 263 if (host) |
258 CloseChannel(port_id, error_message); | 264 ClosePortImpl(port_id, process_id, routing_id, force_close, |
| 265 error_message); |
259 } | 266 } |
260 void PendingLazyBackgroundPagePostMessage(int port_id, | 267 void PendingLazyBackgroundPagePostMessage(int port_id, |
261 const Message& message, | 268 const Message& message, |
262 extensions::ExtensionHost* host) { | 269 extensions::ExtensionHost* host) { |
263 if (host) | 270 if (host) |
264 PostMessage(port_id, message); | 271 PostMessage(port_id, message); |
265 } | 272 } |
266 | 273 |
267 // Immediate dispatches a disconnect to |source| for |port_id|. Sets source's | 274 // Immediate dispatches a disconnect to |source| for |port_id|. Sets source's |
268 // runtime.lastMessage to |error_message|, if any. | 275 // runtime.lastMessage to |error_message|, if any. |
269 void DispatchOnDisconnect(content::RenderProcessHost* source, | 276 void DispatchOnDisconnect(content::RenderFrameHost* source, |
270 int port_id, | 277 int port_id, |
271 const std::string& error_message); | 278 const std::string& error_message); |
272 | 279 |
273 void DispatchPendingMessages(const PendingMessagesQueue& queue, | 280 void DispatchPendingMessages(const PendingMessagesQueue& queue, |
274 int channel_id); | 281 int channel_id); |
275 | 282 |
276 // BrowserContextKeyedAPI implementation. | 283 // BrowserContextKeyedAPI implementation. |
277 static const char* service_name() { | 284 static const char* service_name() { |
278 return "MessageService"; | 285 return "MessageService"; |
279 } | 286 } |
280 static const bool kServiceRedirectedInIncognito = true; | 287 static const bool kServiceRedirectedInIncognito = true; |
281 static const bool kServiceIsCreatedWithBrowserContext = false; | 288 static const bool kServiceIsCreatedWithBrowserContext = false; |
282 static const bool kServiceIsNULLWhileTesting = true; | 289 static const bool kServiceIsNULLWhileTesting = true; |
283 | 290 |
284 content::NotificationRegistrar registrar_; | |
285 MessageChannelMap channels_; | 291 MessageChannelMap channels_; |
286 // A set of channel IDs waiting for TLS channel IDs to complete opening, and | 292 // 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 | 293 // 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 | 294 // following two maps form a pipeline where messages are queued before the |
289 // channel they are addressed to is ready. | 295 // channel they are addressed to is ready. |
290 PendingChannelMap pending_tls_channel_id_channels_; | 296 PendingChannelMap pending_tls_channel_id_channels_; |
291 // A set of channel IDs waiting for user permission to cross the border | 297 // 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 | 298 // between an incognito page and an app or extension, and any pending messages |
293 // queued to be sent on those channels. | 299 // queued to be sent on those channels. |
294 PendingChannelMap pending_incognito_channels_; | 300 PendingChannelMap pending_incognito_channels_; |
295 PendingLazyBackgroundPageChannelMap pending_lazy_background_page_channels_; | 301 PendingLazyBackgroundPageChannelMap pending_lazy_background_page_channels_; |
296 MessagePropertyProvider property_provider_; | 302 MessagePropertyProvider property_provider_; |
297 | 303 |
298 // Weak pointer. Guaranteed to outlive this class. | 304 // Weak pointer. Guaranteed to outlive this class. |
299 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 305 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
300 | 306 |
301 base::WeakPtrFactory<MessageService> weak_factory_; | 307 base::WeakPtrFactory<MessageService> weak_factory_; |
302 | 308 |
303 DISALLOW_COPY_AND_ASSIGN(MessageService); | 309 DISALLOW_COPY_AND_ASSIGN(MessageService); |
304 }; | 310 }; |
305 | 311 |
306 } // namespace extensions | 312 } // namespace extensions |
307 | 313 |
308 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 314 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
OLD | NEW |