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/linked_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | |
15 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | |
14 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
15 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
16 | 18 |
17 class Profile; | 19 class Profile; |
18 | 20 |
19 namespace content { | 21 namespace content { |
20 class RenderProcessHost; | 22 class RenderProcessHost; |
21 class WebContents; | 23 class WebContents; |
22 } | 24 } |
23 | 25 |
(...skipping 15 matching lines...) Expand all Loading... | |
39 // context owned by the connecting extension in that process/tab. | 41 // context owned by the connecting extension in that process/tab. |
40 // - Once the channel is established, either side can call postMessage to send | 42 // - Once the channel is established, either side can call postMessage to send |
41 // a message to the opposite side of the channel, which may have multiple | 43 // a message to the opposite side of the channel, which may have multiple |
42 // listeners. | 44 // listeners. |
43 // | 45 // |
44 // Terminology: | 46 // Terminology: |
45 // channel: connection between two ports | 47 // channel: connection between two ports |
46 // port: an IPC::Message::Process interface and an optional routing_id (in the | 48 // port: an IPC::Message::Process interface and an optional routing_id (in the |
47 // case that the port is a tab). The Process is usually either a | 49 // case that the port is a tab). The Process is usually either a |
48 // RenderProcessHost or a RenderViewHost. | 50 // RenderProcessHost or a RenderViewHost. |
49 class MessageService : public content::NotificationObserver { | 51 class MessageService : public content::NotificationObserver, |
52 public NativeMessageProcessHost::Client { | |
50 public: | 53 public: |
51 // A messaging channel. Note that the opening port can be the same as the | 54 // A messaging channel. Note that the opening port can be the same as the |
52 // receiver, if an extension background page wants to talk to its tab (for | 55 // receiver, if an extension background page wants to talk to its tab (for |
53 // example). | 56 // example). |
54 struct MessageChannel; | 57 struct MessageChannel; |
55 struct MessagePort; | 58 |
59 // One side of the communication handled by extensions::MessageService. | |
60 class MessagePort { | |
61 public: | |
62 virtual ~MessagePort() {} | |
63 virtual void DispatchOnConnect(int dest_port_id, | |
Aaron Boodman
2012/08/22 21:47:21
You should docment all these methods. You won't ne
| |
64 const std::string& channel_name, | |
65 const std::string& tab_json, | |
66 const std::string& source_extension_id, | |
67 const std::string& target_extension_id) {} | |
68 virtual void DispatchOnDisconnect(int source_port_id, | |
69 bool connection_error) {} | |
70 virtual void DispatchOnMessage(const std::string& message, | |
71 int target_port_id) = 0; | |
72 virtual void IncrementLazyKeepaliveCount() {} | |
73 virtual void DecrementLazyKeepaliveCount() {} | |
74 virtual content::RenderProcessHost* process() { return NULL; } | |
Matt Perry
2012/08/22 00:02:42
It's not really ideal to expose this method here,
Aaron Boodman
2012/08/22 21:47:21
I heard Darin say once that virtual methods should
eaugusti
2012/08/31 23:47:13
Done.
eaugusti
2012/08/31 23:47:13
Done.
| |
75 | |
76 protected: | |
77 MessagePort() {} | |
78 | |
79 private: | |
80 DISALLOW_COPY_AND_ASSIGN(MessagePort); | |
81 }; | |
56 | 82 |
57 // Allocates a pair of port ids. | 83 // Allocates a pair of port ids. |
58 // NOTE: this can be called from any thread. | 84 // NOTE: this can be called from any thread. |
59 static void AllocatePortIdPair(int* port1, int* port2); | 85 static void AllocatePortIdPair(int* port1, int* port2); |
60 | 86 |
61 explicit MessageService(LazyBackgroundTaskQueue* queue); | 87 explicit MessageService(LazyBackgroundTaskQueue* queue); |
62 virtual ~MessageService(); | 88 virtual ~MessageService(); |
63 | 89 |
64 // Given an extension's ID, opens a channel between the given renderer "port" | 90 // Given an extension's ID, opens a channel between the given renderer "port" |
65 // and every listening context owned by that extension. |channel_name| is | 91 // and every listening context owned by that extension. |channel_name| is |
66 // an optional identifier for use by extension developers. | 92 // an optional identifier for use by extension developers. |
67 void OpenChannelToExtension( | 93 void OpenChannelToExtension( |
68 int source_process_id, int source_routing_id, int receiver_port_id, | 94 int source_process_id, int source_routing_id, int receiver_port_id, |
69 const std::string& source_extension_id, | 95 const std::string& source_extension_id, |
70 const std::string& target_extension_id, | 96 const std::string& target_extension_id, |
71 const std::string& channel_name); | 97 const std::string& channel_name); |
72 | 98 |
73 // Same as above, but opens a channel to the tab with the given ID. Messages | 99 // Same as above, but opens a channel to the tab with the given ID. Messages |
74 // are restricted to that tab, so if there are multiple tabs in that process, | 100 // are restricted to that tab, so if there are multiple tabs in that process, |
75 // only the targeted tab will receive messages. | 101 // only the targeted tab will receive messages. |
76 void OpenChannelToTab( | 102 void OpenChannelToTab( |
77 int source_process_id, int source_routing_id, int receiver_port_id, | 103 int source_process_id, int source_routing_id, int receiver_port_id, |
78 int tab_id, const std::string& extension_id, | 104 int tab_id, const std::string& extension_id, |
79 const std::string& channel_name); | 105 const std::string& channel_name); |
80 | 106 |
107 void OpenChannelToNativeApp( | |
108 int source_process_id, | |
109 int source_routing_id, | |
110 int receiver_port_id, | |
111 const std::string& source_extension_id, | |
112 const std::string& native_app_name, | |
113 const std::string& channel_name, | |
114 const std::string& connect_message); | |
115 | |
116 // Should be called on the UI thread. | |
117 void FinalizeOpenChannelToNativeApp( | |
118 int receiver_port_id, | |
119 const std::string& channel_name, | |
120 scoped_ptr<MessageChannel> channel, | |
121 const std::string& tab_json, | |
122 NativeMessageProcessHost* native_process); | |
123 | |
81 // Closes the message channel associated with the given port, and notifies | 124 // Closes the message channel associated with the given port, and notifies |
82 // the other side. | 125 // the other side. |
83 void CloseChannel(int port_id, bool connection_error); | 126 void CloseChannel(int port_id, bool connection_error); |
84 | 127 |
85 // Sends a message from a renderer to the given port. | 128 // Sends a message to the given port. |
86 void PostMessageFromRenderer(int port_id, const std::string& message); | 129 void PostMessage(int port_id, const std::string& message); |
130 | |
131 // NativeMessageProcessHost::Client | |
132 void PostMessageFromNativeProcess(int port_id, const std::string& message) | |
133 OVERRIDE { | |
134 PostMessage(port_id, message); | |
135 } | |
87 | 136 |
88 private: | 137 private: |
89 friend class MockMessageService; | 138 friend class MockMessageService; |
90 struct OpenChannelParams; | 139 struct OpenChannelParams; |
91 | 140 |
92 // A map of channel ID to its channel object. | 141 // A map of channel ID to its channel object. |
93 typedef std::map<int, MessageChannel*> MessageChannelMap; | 142 typedef std::map<int, MessageChannel*> MessageChannelMap; |
94 | 143 |
95 // A map of channel ID to information about the extension that is waiting | 144 // A map of channel ID to information about the extension that is waiting |
96 // for that channel to open. Used for lazy background pages. | 145 // for that channel to open. Used for lazy background pages. |
97 typedef std::string ExtensionID; | 146 typedef std::string ExtensionID; |
98 typedef std::pair<Profile*, ExtensionID> PendingChannel; | 147 typedef std::pair<Profile*, ExtensionID> PendingChannel; |
99 typedef std::map<int, PendingChannel> PendingChannelMap; | 148 typedef std::map<int, PendingChannel> PendingChannelMap; |
100 | 149 |
101 // Common among OpenChannel* variants. | 150 // Common among OpenChannel* variants. |
102 bool OpenChannelImpl(const OpenChannelParams& params); | 151 bool OpenChannelImpl(scoped_ptr<OpenChannelParams> params); |
103 | 152 |
104 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, | 153 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, |
105 int port_id, bool connection_error, | 154 int port_id, bool connection_error, |
106 bool notify_other_port); | 155 bool notify_other_port); |
107 | 156 |
157 // Have MessageService take ownership of |channel|, and remove any pending | |
158 // channels with the same id. | |
159 void AddChannel(MessageChannel* channel, int receiver_port_id); | |
160 | |
108 // content::NotificationObserver interface. | 161 // content::NotificationObserver interface. |
109 virtual void Observe(int type, | 162 virtual void Observe(int type, |
110 const content::NotificationSource& source, | 163 const content::NotificationSource& source, |
111 const content::NotificationDetails& details) OVERRIDE; | 164 const content::NotificationDetails& details) OVERRIDE; |
112 | 165 |
113 // A process that might be in our list of channels has closed. | 166 // A process that might be in our list of channels has closed. |
114 void OnProcessClosed(content::RenderProcessHost* process); | 167 void OnProcessClosed(content::RenderProcessHost* process); |
115 | 168 |
116 // Potentially registers a pending task with the LazyBackgroundTaskQueue | 169 // Potentially registers a pending task with the LazyBackgroundTaskQueue |
117 // to open a channel. Returns true if a task was queued. | 170 // to open a channel. Returns true if a task was queued. |
171 // Takes ownership of |params| if true is returned. | |
118 bool MaybeAddPendingOpenChannelTask(Profile* profile, | 172 bool MaybeAddPendingOpenChannelTask(Profile* profile, |
119 const OpenChannelParams& params); | 173 OpenChannelParams* params); |
120 | 174 |
121 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an | 175 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an |
122 // ExtensionHost to its task callbacks, though some of our callbacks don't | 176 // ExtensionHost to its task callbacks, though some of our callbacks don't |
123 // use that argument. | 177 // use that argument. |
124 void PendingOpenChannel(const OpenChannelParams& params, | 178 void PendingOpenChannel(scoped_ptr<OpenChannelParams> params, |
125 int source_process_id, | 179 int source_process_id, |
126 extensions::ExtensionHost* host); | 180 extensions::ExtensionHost* host); |
127 void PendingCloseChannel(int port_id, | 181 void PendingCloseChannel(int port_id, |
128 bool connection_error, | 182 bool connection_error, |
129 extensions::ExtensionHost* host) { | 183 extensions::ExtensionHost* host) { |
130 if (host) | 184 if (host) |
131 CloseChannel(port_id, connection_error); | 185 CloseChannel(port_id, connection_error); |
132 } | 186 } |
133 void PendingPostMessage(int port_id, | 187 void PendingPostMessage(int port_id, |
134 const std::string& message, | 188 const std::string& message, |
135 extensions::ExtensionHost* host) { | 189 extensions::ExtensionHost* host) { |
136 if (host) | 190 if (host) |
137 PostMessageFromRenderer(port_id, message); | 191 PostMessage(port_id, message); |
138 } | 192 } |
139 | 193 |
140 content::NotificationRegistrar registrar_; | 194 content::NotificationRegistrar registrar_; |
141 MessageChannelMap channels_; | 195 MessageChannelMap channels_; |
142 PendingChannelMap pending_channels_; | 196 PendingChannelMap pending_channels_; |
143 | 197 |
144 // Weak pointer. Guaranteed to outlive this class. | 198 // Weak pointer. Guaranteed to outlive this class. |
145 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 199 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
146 | 200 |
201 base::WeakPtrFactory<MessageService> weak_factory_; | |
202 | |
147 DISALLOW_COPY_AND_ASSIGN(MessageService); | 203 DISALLOW_COPY_AND_ASSIGN(MessageService); |
148 }; | 204 }; |
149 | 205 |
150 } // namespace extensions | 206 } // namespace extensions |
151 | 207 |
152 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 208 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
OLD | NEW |