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 <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/browser/extensions/api/messaging/message_property_provider.h" | 18 #include "chrome/browser/extensions/api/messaging/message_property_provider.h" |
19 #include "extensions/browser/api/messaging/native_message_host.h" | 19 #include "extensions/browser/api/messaging/native_message_host.h" |
20 #include "extensions/browser/browser_context_keyed_api_factory.h" | 20 #include "extensions/browser/browser_context_keyed_api_factory.h" |
21 #include "extensions/common/api/messaging/message.h" | 21 #include "extensions/common/api/messaging/message.h" |
| 22 #include "extensions/common/api/messaging/port_id.h" |
22 #include "extensions/common/extension_id.h" | 23 #include "extensions/common/extension_id.h" |
23 | 24 |
24 class GURL; | 25 class GURL; |
25 | 26 |
26 namespace content { | 27 namespace content { |
27 class BrowserContext; | 28 class BrowserContext; |
28 } | 29 } |
29 | 30 |
30 namespace extensions { | 31 namespace extensions { |
31 class Extension; | 32 class Extension; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 enum PolicyPermission { | 116 enum PolicyPermission { |
116 DISALLOW, // The host is not allowed. | 117 DISALLOW, // The host is not allowed. |
117 ALLOW_SYSTEM_ONLY, // Allowed only when installed on system level. | 118 ALLOW_SYSTEM_ONLY, // Allowed only when installed on system level. |
118 ALLOW_ALL, // Allowed when installed on system or user level. | 119 ALLOW_ALL, // Allowed when installed on system or user level. |
119 }; | 120 }; |
120 | 121 |
121 static PolicyPermission IsNativeMessagingHostAllowed( | 122 static PolicyPermission IsNativeMessagingHostAllowed( |
122 const PrefService* pref_service, | 123 const PrefService* pref_service, |
123 const std::string& native_host_name); | 124 const std::string& native_host_name); |
124 | 125 |
125 // Allocates a pair of port ids. | |
126 // NOTE: this can be called from any thread. | |
127 static void AllocatePortIdPair(int* port1, int* port2); | |
128 | |
129 explicit MessageService(content::BrowserContext* context); | 126 explicit MessageService(content::BrowserContext* context); |
130 ~MessageService() override; | 127 ~MessageService() override; |
131 | 128 |
132 // BrowserContextKeyedAPI implementation. | 129 // BrowserContextKeyedAPI implementation. |
133 static BrowserContextKeyedAPIFactory<MessageService>* GetFactoryInstance(); | 130 static BrowserContextKeyedAPIFactory<MessageService>* GetFactoryInstance(); |
134 | 131 |
135 // Convenience method to get the MessageService for a browser context. | 132 // Convenience method to get the MessageService for a browser context. |
136 static MessageService* Get(content::BrowserContext* context); | 133 static MessageService* Get(content::BrowserContext* context); |
137 | 134 |
138 // Given an extension's ID, opens a channel between the given renderer "port" | 135 // Given an extension's ID, opens a channel between the given renderer "port" |
139 // and every listening context owned by that extension. |channel_name| is | 136 // and every listening context owned by that extension. |channel_name| is |
140 // an optional identifier for use by extension developers. | 137 // an optional identifier for use by extension developers. |
141 void OpenChannelToExtension( | 138 void OpenChannelToExtension(int source_process_id, |
142 int source_process_id, int source_routing_id, int receiver_port_id, | 139 int source_routing_id, |
143 const std::string& source_extension_id, | 140 const PortId& source_port_id, |
144 const std::string& target_extension_id, | 141 const std::string& source_extension_id, |
145 const GURL& source_url, | 142 const std::string& target_extension_id, |
146 const std::string& channel_name, | 143 const GURL& source_url, |
147 bool include_tls_channel_id); | 144 const std::string& channel_name, |
| 145 bool include_tls_channel_id); |
148 | 146 |
149 // Same as above, but opens a channel to the tab with the given ID. Messages | 147 // Same as above, but opens a channel to the tab with the given ID. Messages |
150 // are restricted to that tab, so if there are multiple tabs in that process, | 148 // are restricted to that tab, so if there are multiple tabs in that process, |
151 // only the targeted tab will receive messages. | 149 // only the targeted tab will receive messages. |
152 void OpenChannelToTab(int source_process_id, | 150 void OpenChannelToTab(int source_process_id, |
153 int source_routing_id, | 151 int source_routing_id, |
154 int receiver_port_id, | 152 const PortId& source_port_id, |
155 int tab_id, | 153 int tab_id, |
156 int frame_id, | 154 int frame_id, |
157 const std::string& extension_id, | 155 const std::string& extension_id, |
158 const std::string& channel_name); | 156 const std::string& channel_name); |
159 | 157 |
160 void OpenChannelToNativeApp( | 158 void OpenChannelToNativeApp(int source_process_id, |
161 int source_process_id, | 159 int source_routing_id, |
162 int source_routing_id, | 160 const PortId& source_port_id, |
163 int receiver_port_id, | 161 const std::string& native_app_name); |
164 const std::string& native_app_name); | |
165 | 162 |
166 // Mark the given port as opened by the frame identified by | 163 // Mark the given port as opened by the frame identified by |
167 // (process_id, routing_id). | 164 // (process_id, routing_id). |
168 void OpenPort(int port_id, int process_id, int routing_id); | 165 void OpenPort(const PortId& port_id, int process_id, int routing_id); |
169 | 166 |
170 // Closes the given port in the given frame. If this was the last frame or if | 167 // Closes the given port in the given frame. If this was the last frame or if |
171 // |force_close| is true, then the other side is closed as well. | 168 // |force_close| is true, then the other side is closed as well. |
172 void ClosePort(int port_id, int process_id, int routing_id, bool force_close); | 169 void ClosePort(const PortId& port_id, |
| 170 int process_id, |
| 171 int routing_id, |
| 172 bool force_close); |
173 | 173 |
174 // Closes the message channel associated with the given port, and notifies | 174 // Closes the message channel associated with the given port, and notifies |
175 // the other side. | 175 // the other side. |
176 void CloseChannel(int port_id, const std::string& error_message); | 176 void CloseChannel(const PortId& port_id, const std::string& error_message); |
177 | 177 |
178 // Enqueues a message on a pending channel, or sends a message to the given | 178 // Enqueues a message on a pending channel, or sends a message to the given |
179 // port if the channel isn't pending. | 179 // port if the channel isn't pending. |
180 void PostMessage(int port_id, const Message& message); | 180 void PostMessage(const PortId& port_id, const Message& message); |
181 | 181 |
182 private: | 182 private: |
183 friend class MockMessageService; | 183 friend class MockMessageService; |
184 friend class BrowserContextKeyedAPIFactory<MessageService>; | 184 friend class BrowserContextKeyedAPIFactory<MessageService>; |
185 struct OpenChannelParams; | 185 struct OpenChannelParams; |
186 | 186 |
187 // A map of channel ID to its channel object. | 187 // A map of channel ID to its channel object. |
188 using MessageChannelMap = std::map<int, std::unique_ptr<MessageChannel>>; | 188 using MessageChannelMap = |
| 189 std::map<ChannelId, std::unique_ptr<MessageChannel>>; |
189 | 190 |
190 using PendingMessage = std::pair<int, Message>; | 191 using PendingMessage = std::pair<PortId, Message>; |
191 using PendingMessagesQueue = std::vector<PendingMessage>; | 192 using PendingMessagesQueue = std::vector<PendingMessage>; |
192 // A set of channel IDs waiting to complete opening, and any pending messages | 193 // A set of channel IDs waiting to complete opening, and any pending messages |
193 // queued to be sent on those channels. | 194 // queued to be sent on those channels. |
194 using PendingChannelMap = std::map<int, PendingMessagesQueue>; | 195 using PendingChannelMap = std::map<ChannelId, PendingMessagesQueue>; |
195 | 196 |
196 // A map of channel ID to information about the extension that is waiting | 197 // A map of channel ID to information about the extension that is waiting |
197 // for that channel to open. Used for lazy background pages. | 198 // for that channel to open. Used for lazy background pages. |
198 using PendingLazyBackgroundPageChannel = | 199 using PendingLazyBackgroundPageChannel = |
199 std::pair<content::BrowserContext*, ExtensionId>; | 200 std::pair<content::BrowserContext*, ExtensionId>; |
200 using PendingLazyBackgroundPageChannelMap = | 201 using PendingLazyBackgroundPageChannelMap = |
201 std::map<int, PendingLazyBackgroundPageChannel>; | 202 std::map<ChannelId, PendingLazyBackgroundPageChannel>; |
202 | 203 |
203 // Common implementation for opening a channel configured by |params|. | 204 // Common implementation for opening a channel configured by |params|. |
204 // | 205 // |
205 // |target_extension| will be non-null if |params->target_extension_id| is | 206 // |target_extension| will be non-null if |params->target_extension_id| is |
206 // non-empty, that is, if the target is an extension, it must exist. | 207 // non-empty, that is, if the target is an extension, it must exist. |
207 // | 208 // |
208 // |did_enqueue| will be true if the channel opening was delayed while | 209 // |did_enqueue| will be true if the channel opening was delayed while |
209 // waiting for an event page to start, false otherwise. | 210 // waiting for an event page to start, false otherwise. |
210 void OpenChannelImpl(content::BrowserContext* browser_context, | 211 void OpenChannelImpl(content::BrowserContext* browser_context, |
211 std::unique_ptr<OpenChannelParams> params, | 212 std::unique_ptr<OpenChannelParams> params, |
212 const Extension* target_extension, | 213 const Extension* target_extension, |
213 bool did_enqueue); | 214 bool did_enqueue); |
214 | 215 |
215 void ClosePortImpl(int port_id, | 216 void ClosePortImpl(const PortId& port_id, |
216 int process_id, | 217 int process_id, |
217 int routing_id, | 218 int routing_id, |
218 bool force_close, | 219 bool force_close, |
219 const std::string& error_message); | 220 const std::string& error_message); |
220 | 221 |
221 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, | 222 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, |
222 int port_id, | 223 const PortId& port_id, |
223 const std::string& error_message, | 224 const std::string& error_message, |
224 bool notify_other_port); | 225 bool notify_other_port); |
225 | 226 |
226 // Have MessageService take ownership of |channel|, and remove any pending | 227 // Have MessageService take ownership of |channel|, and remove any pending |
227 // channels with the same id. | 228 // channels with the same id. |
228 void AddChannel(std::unique_ptr<MessageChannel> channel, | 229 void AddChannel(std::unique_ptr<MessageChannel> channel, |
229 int receiver_port_id); | 230 const PortId& receiver_port_id); |
230 | 231 |
231 // If the channel is being opened from an incognito tab the user must allow | 232 // If the channel is being opened from an incognito tab the user must allow |
232 // the connection. | 233 // the connection. |
233 void OnOpenChannelAllowed(std::unique_ptr<OpenChannelParams> params, | 234 void OnOpenChannelAllowed(std::unique_ptr<OpenChannelParams> params, |
234 bool allowed); | 235 bool allowed); |
235 void GotChannelID(std::unique_ptr<OpenChannelParams> params, | 236 void GotChannelID(std::unique_ptr<OpenChannelParams> params, |
236 const std::string& tls_channel_id); | 237 const std::string& tls_channel_id); |
237 | 238 |
238 // Enqueues a message on a pending channel. | 239 // Enqueues a message on a pending channel. |
239 void EnqueuePendingMessage(int port_id, int channel_id, | 240 void EnqueuePendingMessage(const PortId& port_id, |
| 241 const ChannelId& channel_id, |
240 const Message& message); | 242 const Message& message); |
241 | 243 |
242 // Enqueues a message on a channel pending on a lazy background page load. | 244 // Enqueues a message on a channel pending on a lazy background page load. |
243 void EnqueuePendingMessageForLazyBackgroundLoad(int port_id, | 245 void EnqueuePendingMessageForLazyBackgroundLoad(const PortId& port_id, |
244 int channel_id, | 246 const ChannelId& channel_id, |
245 const Message& message); | 247 const Message& message); |
246 | 248 |
247 // Immediately sends a message to the given port. | 249 // Immediately sends a message to the given port. |
248 void DispatchMessage(int port_id, MessageChannel* channel, | 250 void DispatchMessage(const PortId& port_id, |
| 251 MessageChannel* channel, |
249 const Message& message); | 252 const Message& message); |
250 | 253 |
251 // Potentially registers a pending task with the LazyBackgroundTaskQueue | 254 // Potentially registers a pending task with the LazyBackgroundTaskQueue |
252 // to open a channel. Returns true if a task was queued. | 255 // to open a channel. Returns true if a task was queued. |
253 // Takes ownership of |params| if true is returned. | 256 // Takes ownership of |params| if true is returned. |
254 bool MaybeAddPendingLazyBackgroundPageOpenChannelTask( | 257 bool MaybeAddPendingLazyBackgroundPageOpenChannelTask( |
255 content::BrowserContext* context, | 258 content::BrowserContext* context, |
256 const Extension* extension, | 259 const Extension* extension, |
257 std::unique_ptr<OpenChannelParams>* params, | 260 std::unique_ptr<OpenChannelParams>* params, |
258 const PendingMessagesQueue& pending_messages); | 261 const PendingMessagesQueue& pending_messages); |
259 | 262 |
260 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an | 263 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an |
261 // ExtensionHost to its task callbacks, though some of our callbacks don't | 264 // ExtensionHost to its task callbacks, though some of our callbacks don't |
262 // use that argument. | 265 // use that argument. |
263 void PendingLazyBackgroundPageOpenChannel( | 266 void PendingLazyBackgroundPageOpenChannel( |
264 std::unique_ptr<OpenChannelParams> params, | 267 std::unique_ptr<OpenChannelParams> params, |
265 int source_process_id, | 268 int source_process_id, |
266 extensions::ExtensionHost* host); | 269 extensions::ExtensionHost* host); |
267 void PendingLazyBackgroundPageClosePort(int port_id, | 270 void PendingLazyBackgroundPageClosePort(const PortId& port_id, |
268 int process_id, | 271 int process_id, |
269 int routing_id, | 272 int routing_id, |
270 bool force_close, | 273 bool force_close, |
271 const std::string& error_message, | 274 const std::string& error_message, |
272 extensions::ExtensionHost* host) { | 275 extensions::ExtensionHost* host) { |
273 if (host) | 276 if (host) |
274 ClosePortImpl(port_id, process_id, routing_id, force_close, | 277 ClosePortImpl(port_id, process_id, routing_id, force_close, |
275 error_message); | 278 error_message); |
276 } | 279 } |
277 void PendingLazyBackgroundPagePostMessage(int port_id, | 280 void PendingLazyBackgroundPagePostMessage(const PortId& port_id, |
278 const Message& message, | 281 const Message& message, |
279 extensions::ExtensionHost* host) { | 282 extensions::ExtensionHost* host) { |
280 if (host) | 283 if (host) |
281 PostMessage(port_id, message); | 284 PostMessage(port_id, message); |
282 } | 285 } |
283 | 286 |
284 // Immediate dispatches a disconnect to |source| for |port_id|. Sets source's | 287 // Immediate dispatches a disconnect to |source| for |port_id|. Sets source's |
285 // runtime.lastMessage to |error_message|, if any. | 288 // runtime.lastMessage to |error_message|, if any. |
286 void DispatchOnDisconnect(content::RenderFrameHost* source, | 289 void DispatchOnDisconnect(content::RenderFrameHost* source, |
287 int port_id, | 290 const PortId& port_id, |
288 const std::string& error_message); | 291 const std::string& error_message); |
289 | 292 |
290 void DispatchPendingMessages(const PendingMessagesQueue& queue, | 293 void DispatchPendingMessages(const PendingMessagesQueue& queue, |
291 int channel_id); | 294 const ChannelId& channel_id); |
292 | 295 |
293 // BrowserContextKeyedAPI implementation. | 296 // BrowserContextKeyedAPI implementation. |
294 static const char* service_name() { | 297 static const char* service_name() { |
295 return "MessageService"; | 298 return "MessageService"; |
296 } | 299 } |
297 static const bool kServiceRedirectedInIncognito = true; | 300 static const bool kServiceRedirectedInIncognito = true; |
298 static const bool kServiceIsCreatedWithBrowserContext = false; | 301 static const bool kServiceIsCreatedWithBrowserContext = false; |
299 static const bool kServiceIsNULLWhileTesting = true; | 302 static const bool kServiceIsNULLWhileTesting = true; |
300 | 303 |
301 MessageChannelMap channels_; | 304 MessageChannelMap channels_; |
(...skipping 13 matching lines...) Expand all Loading... |
315 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 318 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
316 | 319 |
317 base::WeakPtrFactory<MessageService> weak_factory_; | 320 base::WeakPtrFactory<MessageService> weak_factory_; |
318 | 321 |
319 DISALLOW_COPY_AND_ASSIGN(MessageService); | 322 DISALLOW_COPY_AND_ASSIGN(MessageService); |
320 }; | 323 }; |
321 | 324 |
322 } // namespace extensions | 325 } // namespace extensions |
323 | 326 |
324 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 327 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
OLD | NEW |