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/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
16 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
18 | 18 |
| 19 class GURL; |
19 class Profile; | 20 class Profile; |
20 | 21 |
| 22 namespace base { |
| 23 class DictionaryValue; |
| 24 } |
| 25 |
21 namespace content { | 26 namespace content { |
22 class RenderProcessHost; | 27 class RenderProcessHost; |
23 class WebContents; | 28 class WebContents; |
24 } | 29 } |
25 | 30 |
26 namespace extensions { | 31 namespace extensions { |
| 32 class Extension; |
27 class ExtensionHost; | 33 class ExtensionHost; |
28 class LazyBackgroundTaskQueue; | 34 class LazyBackgroundTaskQueue; |
29 | 35 |
30 // This class manages message and event passing between renderer processes. | 36 // This class manages message and event passing between renderer processes. |
31 // It maintains a list of processes that are listening to events and a set of | 37 // It maintains a list of processes that are listening to events and a set of |
32 // open channels. | 38 // open channels. |
33 // | 39 // |
34 // Messaging works this way: | 40 // Messaging works this way: |
35 // - An extension-owned script context (like a background page or a content | 41 // - An extension-owned script context (like a background page or a content |
36 // script) adds an event listener to the "onConnect" event. | 42 // script) adds an event listener to the "onConnect" event. |
(...skipping 19 matching lines...) Expand all Loading... |
56 // example). | 62 // example). |
57 struct MessageChannel; | 63 struct MessageChannel; |
58 | 64 |
59 // One side of the communication handled by extensions::MessageService. | 65 // One side of the communication handled by extensions::MessageService. |
60 class MessagePort { | 66 class MessagePort { |
61 public: | 67 public: |
62 virtual ~MessagePort() {} | 68 virtual ~MessagePort() {} |
63 // Notify the port that the channel has been opened. | 69 // Notify the port that the channel has been opened. |
64 virtual void DispatchOnConnect(int dest_port_id, | 70 virtual void DispatchOnConnect(int dest_port_id, |
65 const std::string& channel_name, | 71 const std::string& channel_name, |
66 const std::string& tab_json, | 72 const base::DictionaryValue& source_tab, |
67 const std::string& source_extension_id, | 73 const std::string& source_extension_id, |
68 const std::string& target_extension_id) {} | 74 const std::string& target_extension_id, |
| 75 const GURL& source_url) {} |
69 | 76 |
70 // Notify the port that the channel has been closed. If |error_message| is | 77 // Notify the port that the channel has been closed. If |error_message| is |
71 // non-empty, it indicates an error occurred while opening the connection. | 78 // non-empty, it indicates an error occurred while opening the connection. |
72 virtual void DispatchOnDisconnect(int source_port_id, | 79 virtual void DispatchOnDisconnect(int source_port_id, |
73 const std::string& error_message) {} | 80 const std::string& error_message) {} |
74 | 81 |
75 // Dispatch a message to this end of the communication. | 82 // Dispatch a message to this end of the communication. |
76 virtual void DispatchOnMessage(const std::string& message, | 83 virtual void DispatchOnMessage(const std::string& message, |
77 int target_port_id) = 0; | 84 int target_port_id) = 0; |
78 | 85 |
(...skipping 19 matching lines...) Expand all Loading... |
98 explicit MessageService(LazyBackgroundTaskQueue* queue); | 105 explicit MessageService(LazyBackgroundTaskQueue* queue); |
99 virtual ~MessageService(); | 106 virtual ~MessageService(); |
100 | 107 |
101 // Given an extension's ID, opens a channel between the given renderer "port" | 108 // Given an extension's ID, opens a channel between the given renderer "port" |
102 // and every listening context owned by that extension. |channel_name| is | 109 // and every listening context owned by that extension. |channel_name| is |
103 // an optional identifier for use by extension developers. | 110 // an optional identifier for use by extension developers. |
104 void OpenChannelToExtension( | 111 void OpenChannelToExtension( |
105 int source_process_id, int source_routing_id, int receiver_port_id, | 112 int source_process_id, int source_routing_id, int receiver_port_id, |
106 const std::string& source_extension_id, | 113 const std::string& source_extension_id, |
107 const std::string& target_extension_id, | 114 const std::string& target_extension_id, |
| 115 const GURL& source_url, |
108 const std::string& channel_name); | 116 const std::string& channel_name); |
109 | 117 |
110 // Same as above, but opens a channel to the tab with the given ID. Messages | 118 // Same as above, but opens a channel to the tab with the given ID. Messages |
111 // are restricted to that tab, so if there are multiple tabs in that process, | 119 // are restricted to that tab, so if there are multiple tabs in that process, |
112 // only the targeted tab will receive messages. | 120 // only the targeted tab will receive messages. |
113 void OpenChannelToTab( | 121 void OpenChannelToTab( |
114 int source_process_id, int source_routing_id, int receiver_port_id, | 122 int source_process_id, int source_routing_id, int receiver_port_id, |
115 int tab_id, const std::string& extension_id, | 123 int tab_id, const std::string& extension_id, |
116 const std::string& channel_name); | 124 const std::string& channel_name); |
117 | 125 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 const content::NotificationSource& source, | 173 const content::NotificationSource& source, |
166 const content::NotificationDetails& details) OVERRIDE; | 174 const content::NotificationDetails& details) OVERRIDE; |
167 | 175 |
168 // A process that might be in our list of channels has closed. | 176 // A process that might be in our list of channels has closed. |
169 void OnProcessClosed(content::RenderProcessHost* process); | 177 void OnProcessClosed(content::RenderProcessHost* process); |
170 | 178 |
171 // Potentially registers a pending task with the LazyBackgroundTaskQueue | 179 // Potentially registers a pending task with the LazyBackgroundTaskQueue |
172 // to open a channel. Returns true if a task was queued. | 180 // to open a channel. Returns true if a task was queued. |
173 // Takes ownership of |params| if true is returned. | 181 // Takes ownership of |params| if true is returned. |
174 bool MaybeAddPendingOpenChannelTask(Profile* profile, | 182 bool MaybeAddPendingOpenChannelTask(Profile* profile, |
| 183 const Extension* extension, |
175 OpenChannelParams* params); | 184 OpenChannelParams* params); |
176 | 185 |
177 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an | 186 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an |
178 // ExtensionHost to its task callbacks, though some of our callbacks don't | 187 // ExtensionHost to its task callbacks, though some of our callbacks don't |
179 // use that argument. | 188 // use that argument. |
180 void PendingOpenChannel(scoped_ptr<OpenChannelParams> params, | 189 void PendingOpenChannel(scoped_ptr<OpenChannelParams> params, |
181 int source_process_id, | 190 int source_process_id, |
182 extensions::ExtensionHost* host); | 191 extensions::ExtensionHost* host); |
183 void PendingCloseChannel(int port_id, | 192 void PendingCloseChannel(int port_id, |
184 const std::string& error_message, | 193 const std::string& error_message, |
(...skipping 16 matching lines...) Expand all Loading... |
201 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 210 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
202 | 211 |
203 base::WeakPtrFactory<MessageService> weak_factory_; | 212 base::WeakPtrFactory<MessageService> weak_factory_; |
204 | 213 |
205 DISALLOW_COPY_AND_ASSIGN(MessageService); | 214 DISALLOW_COPY_AND_ASSIGN(MessageService); |
206 }; | 215 }; |
207 | 216 |
208 } // namespace extensions | 217 } // namespace extensions |
209 | 218 |
210 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 219 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
OLD | NEW |