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_EXTENSION_MESSAGE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_MESSAGE_SERVICE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_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/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
14 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
15 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
16 | 16 |
17 class ExtensionHost; | 17 class ExtensionHost; |
18 class Profile; | 18 class Profile; |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 class RenderProcessHost; | 21 class RenderProcessHost; |
22 class WebContents; | 22 class WebContents; |
23 } | 23 } |
24 | 24 |
25 namespace extensions { | 25 namespace extensions { |
26 class LazyBackgroundTaskQueue; | 26 class LazyBackgroundTaskQueue; |
27 } | |
28 | 27 |
29 // This class manages message and event passing between renderer processes. | 28 // This class manages message and event passing between renderer processes. |
30 // It maintains a list of processes that are listening to events and a set of | 29 // It maintains a list of processes that are listening to events and a set of |
31 // open channels. | 30 // open channels. |
32 // | 31 // |
33 // Messaging works this way: | 32 // Messaging works this way: |
34 // - An extension-owned script context (like a background page or a content | 33 // - An extension-owned script context (like a background page or a content |
35 // script) adds an event listener to the "onConnect" event. | 34 // script) adds an event listener to the "onConnect" event. |
36 // - Another context calls "extension.connect()" to open a channel to the | 35 // - Another context calls "extension.connect()" to open a channel to the |
37 // extension process, or an extension context calls "tabs.connect(tabId)" to | 36 // extension process, or an extension context calls "tabs.connect(tabId)" to |
38 // open a channel to the content scripts for the given tab. The EMS notifies | 37 // open a channel to the content scripts for the given tab. The EMS notifies |
39 // the target process/tab, which then calls the onConnect event in every | 38 // the target process/tab, which then calls the onConnect event in every |
40 // context owned by the connecting extension in that process/tab. | 39 // context owned by the connecting extension in that process/tab. |
41 // - 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 |
42 // 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 |
43 // listeners. | 42 // listeners. |
44 // | 43 // |
45 // Terminology: | 44 // Terminology: |
46 // channel: connection between two ports | 45 // channel: connection between two ports |
47 // 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 |
48 // 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 |
49 // RenderProcessHost or a RenderViewHost. | 48 // RenderProcessHost or a RenderViewHost. |
50 class ExtensionMessageService : public content::NotificationObserver { | 49 class MessageService : public content::NotificationObserver { |
51 public: | 50 public: |
52 // 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 |
53 // 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 |
54 // example). | 53 // example). |
55 struct MessageChannel; | 54 struct MessageChannel; |
56 struct MessagePort; | 55 struct MessagePort; |
57 | 56 |
58 // Allocates a pair of port ids. | 57 // Allocates a pair of port ids. |
59 // NOTE: this can be called from any thread. | 58 // NOTE: this can be called from any thread. |
60 static void AllocatePortIdPair(int* port1, int* port2); | 59 static void AllocatePortIdPair(int* port1, int* port2); |
61 | 60 |
62 explicit ExtensionMessageService(extensions::LazyBackgroundTaskQueue* queue); | 61 explicit MessageService(LazyBackgroundTaskQueue* queue); |
63 virtual ~ExtensionMessageService(); | 62 virtual ~MessageService(); |
64 | 63 |
65 // 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" |
66 // and every listening context owned by that extension. |channel_name| is | 65 // and every listening context owned by that extension. |channel_name| is |
67 // an optional identifier for use by extension developers. | 66 // an optional identifier for use by extension developers. |
68 void OpenChannelToExtension( | 67 void OpenChannelToExtension( |
69 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, |
70 const std::string& source_extension_id, | 69 const std::string& source_extension_id, |
71 const std::string& target_extension_id, | 70 const std::string& target_extension_id, |
72 const std::string& channel_name); | 71 const std::string& channel_name); |
73 | 72 |
74 // 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 |
75 // 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, |
76 // only the targeted tab will receive messages. | 75 // only the targeted tab will receive messages. |
77 void OpenChannelToTab( | 76 void OpenChannelToTab( |
78 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, |
79 int tab_id, const std::string& extension_id, | 78 int tab_id, const std::string& extension_id, |
80 const std::string& channel_name); | 79 const std::string& channel_name); |
81 | 80 |
82 // Closes the message channel associated with the given port, and notifies | 81 // Closes the message channel associated with the given port, and notifies |
83 // the other side. | 82 // the other side. |
84 void CloseChannel(int port_id, bool connection_error); | 83 void CloseChannel(int port_id, bool connection_error); |
85 | 84 |
86 // Sends a message from a renderer to the given port. | 85 // Sends a message from a renderer to the given port. |
87 void PostMessageFromRenderer(int port_id, const std::string& message); | 86 void PostMessageFromRenderer(int port_id, const std::string& message); |
88 | 87 |
89 private: | 88 private: |
90 friend class MockExtensionMessageService; | 89 friend class MockMessageService; |
91 struct OpenChannelParams; | 90 struct OpenChannelParams; |
92 | 91 |
93 // A map of channel ID to its channel object. | 92 // A map of channel ID to its channel object. |
94 typedef std::map<int, MessageChannel*> MessageChannelMap; | 93 typedef std::map<int, MessageChannel*> MessageChannelMap; |
95 | 94 |
96 // 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 |
97 // for that channel to open. Used for lazy background pages. | 96 // for that channel to open. Used for lazy background pages. |
98 typedef std::string ExtensionID; | 97 typedef std::string ExtensionID; |
99 typedef std::pair<Profile*, ExtensionID> PendingChannel; | 98 typedef std::pair<Profile*, ExtensionID> PendingChannel; |
100 typedef std::map<int, PendingChannel> PendingChannelMap; | 99 typedef std::map<int, PendingChannel> PendingChannelMap; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 ExtensionHost* host) { | 135 ExtensionHost* host) { |
137 if (host) | 136 if (host) |
138 PostMessageFromRenderer(port_id, message); | 137 PostMessageFromRenderer(port_id, message); |
139 } | 138 } |
140 | 139 |
141 content::NotificationRegistrar registrar_; | 140 content::NotificationRegistrar registrar_; |
142 MessageChannelMap channels_; | 141 MessageChannelMap channels_; |
143 PendingChannelMap pending_channels_; | 142 PendingChannelMap pending_channels_; |
144 | 143 |
145 // Weak pointer. Guaranteed to outlive this class. | 144 // Weak pointer. Guaranteed to outlive this class. |
146 extensions::LazyBackgroundTaskQueue* lazy_background_task_queue_; | 145 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
147 | 146 |
148 DISALLOW_COPY_AND_ASSIGN(ExtensionMessageService); | 147 DISALLOW_COPY_AND_ASSIGN(MessageService); |
149 }; | 148 }; |
150 | 149 |
151 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MESSAGE_SERVICE_H_ | 150 } // namespace extensions |
| 151 |
| 152 #endif // CHROME_BROWSER_EXTENSIONS_MESSAGE_SERVICE_H_ |
OLD | NEW |