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 // Notify the port that the channel has been opened. |
| 64 virtual void DispatchOnConnect(int dest_port_id, |
| 65 const std::string& channel_name, |
| 66 const std::string& tab_json, |
| 67 const std::string& source_extension_id, |
| 68 const std::string& target_extension_id) {} |
| 69 |
| 70 // Notify the port that the channel has been closed. |
| 71 virtual void DispatchOnDisconnect(int source_port_id, |
| 72 bool connection_error) {} |
| 73 |
| 74 // Dispatch a message to this end of the communication. |
| 75 virtual void DispatchOnMessage(const std::string& message, |
| 76 int target_port_id) = 0; |
| 77 |
| 78 // MessagPorts that target extensions will need to adjust their keepalive |
| 79 // counts for their lazy background page. |
| 80 virtual void IncrementLazyKeepaliveCount() {} |
| 81 virtual void DecrementLazyKeepaliveCount() {} |
| 82 |
| 83 // Get the RenderProcessHost (if any) associated with the port. |
| 84 virtual content::RenderProcessHost* GetRenderProcessHost() { return NULL; } |
| 85 |
| 86 protected: |
| 87 MessagePort() {} |
| 88 |
| 89 private: |
| 90 DISALLOW_COPY_AND_ASSIGN(MessagePort); |
| 91 }; |
56 | 92 |
57 // Allocates a pair of port ids. | 93 // Allocates a pair of port ids. |
58 // NOTE: this can be called from any thread. | 94 // NOTE: this can be called from any thread. |
59 static void AllocatePortIdPair(int* port1, int* port2); | 95 static void AllocatePortIdPair(int* port1, int* port2); |
60 | 96 |
61 explicit MessageService(LazyBackgroundTaskQueue* queue); | 97 explicit MessageService(LazyBackgroundTaskQueue* queue); |
62 virtual ~MessageService(); | 98 virtual ~MessageService(); |
63 | 99 |
64 // Given an extension's ID, opens a channel between the given renderer "port" | 100 // 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 | 101 // and every listening context owned by that extension. |channel_name| is |
66 // an optional identifier for use by extension developers. | 102 // an optional identifier for use by extension developers. |
67 void OpenChannelToExtension( | 103 void OpenChannelToExtension( |
68 int source_process_id, int source_routing_id, int receiver_port_id, | 104 int source_process_id, int source_routing_id, int receiver_port_id, |
69 const std::string& source_extension_id, | 105 const std::string& source_extension_id, |
70 const std::string& target_extension_id, | 106 const std::string& target_extension_id, |
71 const std::string& channel_name); | 107 const std::string& channel_name); |
72 | 108 |
73 // Same as above, but opens a channel to the tab with the given ID. Messages | 109 // 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, | 110 // are restricted to that tab, so if there are multiple tabs in that process, |
75 // only the targeted tab will receive messages. | 111 // only the targeted tab will receive messages. |
76 void OpenChannelToTab( | 112 void OpenChannelToTab( |
77 int source_process_id, int source_routing_id, int receiver_port_id, | 113 int source_process_id, int source_routing_id, int receiver_port_id, |
78 int tab_id, const std::string& extension_id, | 114 int tab_id, const std::string& extension_id, |
79 const std::string& channel_name); | 115 const std::string& channel_name); |
80 | 116 |
| 117 void OpenChannelToNativeApp( |
| 118 int source_process_id, |
| 119 int source_routing_id, |
| 120 int receiver_port_id, |
| 121 const std::string& source_extension_id, |
| 122 const std::string& native_app_name, |
| 123 const std::string& channel_name, |
| 124 const std::string& connect_message); |
| 125 |
| 126 // Should be called on the UI thread. |
| 127 void FinalizeOpenChannelToNativeApp( |
| 128 int receiver_port_id, |
| 129 const std::string& channel_name, |
| 130 scoped_ptr<MessageChannel> channel, |
| 131 const std::string& tab_json, |
| 132 NativeMessageProcessHost::ScopedNativeMessageProcessHost native_process); |
| 133 |
81 // Closes the message channel associated with the given port, and notifies | 134 // Closes the message channel associated with the given port, and notifies |
82 // the other side. | 135 // the other side. |
83 void CloseChannel(int port_id, bool connection_error); | 136 void CloseChannel(int port_id, bool connection_error) OVERRIDE; |
84 | 137 |
85 // Sends a message from a renderer to the given port. | 138 // Sends a message to the given port. |
86 void PostMessageFromRenderer(int port_id, const std::string& message); | 139 void PostMessage(int port_id, const std::string& message); |
| 140 |
| 141 // NativeMessageProcessHost::Client |
| 142 void PostMessageFromNativeProcess(int port_id, const std::string& message) |
| 143 OVERRIDE { |
| 144 PostMessage(port_id, message); |
| 145 } |
87 | 146 |
88 private: | 147 private: |
89 friend class MockMessageService; | 148 friend class MockMessageService; |
90 struct OpenChannelParams; | 149 struct OpenChannelParams; |
91 | 150 |
92 // A map of channel ID to its channel object. | 151 // A map of channel ID to its channel object. |
93 typedef std::map<int, MessageChannel*> MessageChannelMap; | 152 typedef std::map<int, MessageChannel*> MessageChannelMap; |
94 | 153 |
95 // A map of channel ID to information about the extension that is waiting | 154 // A map of channel ID to information about the extension that is waiting |
96 // for that channel to open. Used for lazy background pages. | 155 // for that channel to open. Used for lazy background pages. |
97 typedef std::string ExtensionID; | 156 typedef std::string ExtensionID; |
98 typedef std::pair<Profile*, ExtensionID> PendingChannel; | 157 typedef std::pair<Profile*, ExtensionID> PendingChannel; |
99 typedef std::map<int, PendingChannel> PendingChannelMap; | 158 typedef std::map<int, PendingChannel> PendingChannelMap; |
100 | 159 |
101 // Common among OpenChannel* variants. | 160 // Common among OpenChannel* variants. |
102 bool OpenChannelImpl(const OpenChannelParams& params); | 161 bool OpenChannelImpl(scoped_ptr<OpenChannelParams> params); |
103 | 162 |
104 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, | 163 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, |
105 int port_id, bool connection_error, | 164 int port_id, bool connection_error, |
106 bool notify_other_port); | 165 bool notify_other_port); |
107 | 166 |
| 167 // Have MessageService take ownership of |channel|, and remove any pending |
| 168 // channels with the same id. |
| 169 void AddChannel(MessageChannel* channel, int receiver_port_id); |
| 170 |
108 // content::NotificationObserver interface. | 171 // content::NotificationObserver interface. |
109 virtual void Observe(int type, | 172 virtual void Observe(int type, |
110 const content::NotificationSource& source, | 173 const content::NotificationSource& source, |
111 const content::NotificationDetails& details) OVERRIDE; | 174 const content::NotificationDetails& details) OVERRIDE; |
112 | 175 |
113 // 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. |
114 void OnProcessClosed(content::RenderProcessHost* process); | 177 void OnProcessClosed(content::RenderProcessHost* process); |
115 | 178 |
116 // Potentially registers a pending task with the LazyBackgroundTaskQueue | 179 // Potentially registers a pending task with the LazyBackgroundTaskQueue |
117 // to open a channel. Returns true if a task was queued. | 180 // to open a channel. Returns true if a task was queued. |
| 181 // Takes ownership of |params| if true is returned. |
118 bool MaybeAddPendingOpenChannelTask(Profile* profile, | 182 bool MaybeAddPendingOpenChannelTask(Profile* profile, |
119 const OpenChannelParams& params); | 183 OpenChannelParams* params); |
120 | 184 |
121 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an | 185 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an |
122 // ExtensionHost to its task callbacks, though some of our callbacks don't | 186 // ExtensionHost to its task callbacks, though some of our callbacks don't |
123 // use that argument. | 187 // use that argument. |
124 void PendingOpenChannel(const OpenChannelParams& params, | 188 void PendingOpenChannel(scoped_ptr<OpenChannelParams> params, |
125 int source_process_id, | 189 int source_process_id, |
126 extensions::ExtensionHost* host); | 190 extensions::ExtensionHost* host); |
127 void PendingCloseChannel(int port_id, | 191 void PendingCloseChannel(int port_id, |
128 bool connection_error, | 192 bool connection_error, |
129 extensions::ExtensionHost* host) { | 193 extensions::ExtensionHost* host) { |
130 if (host) | 194 if (host) |
131 CloseChannel(port_id, connection_error); | 195 CloseChannel(port_id, connection_error); |
132 } | 196 } |
133 void PendingPostMessage(int port_id, | 197 void PendingPostMessage(int port_id, |
134 const std::string& message, | 198 const std::string& message, |
135 extensions::ExtensionHost* host) { | 199 extensions::ExtensionHost* host) { |
136 if (host) | 200 if (host) |
137 PostMessageFromRenderer(port_id, message); | 201 PostMessage(port_id, message); |
138 } | 202 } |
139 | 203 |
140 content::NotificationRegistrar registrar_; | 204 content::NotificationRegistrar registrar_; |
141 MessageChannelMap channels_; | 205 MessageChannelMap channels_; |
142 PendingChannelMap pending_channels_; | 206 PendingChannelMap pending_channels_; |
143 | 207 |
144 // Weak pointer. Guaranteed to outlive this class. | 208 // Weak pointer. Guaranteed to outlive this class. |
145 LazyBackgroundTaskQueue* lazy_background_task_queue_; | 209 LazyBackgroundTaskQueue* lazy_background_task_queue_; |
146 | 210 |
| 211 base::WeakPtrFactory<MessageService> weak_factory_; |
| 212 |
147 DISALLOW_COPY_AND_ASSIGN(MessageService); | 213 DISALLOW_COPY_AND_ASSIGN(MessageService); |
148 }; | 214 }; |
149 | 215 |
150 } // namespace extensions | 216 } // namespace extensions |
151 | 217 |
152 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ | 218 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ |
OLD | NEW |