Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: chrome/browser/extensions/api/messaging/message_service.h

Issue 10818013: Native Messaging! (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: nit Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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();
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::ScopedHost 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 virtual 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 virtual void PostMessageFromNativeProcess(
143 int port_id, const std::string& message) OVERRIDE;
87 144
88 private: 145 private:
89 friend class MockMessageService; 146 friend class MockMessageService;
90 struct OpenChannelParams; 147 struct OpenChannelParams;
91 148
92 // A map of channel ID to its channel object. 149 // A map of channel ID to its channel object.
93 typedef std::map<int, MessageChannel*> MessageChannelMap; 150 typedef std::map<int, MessageChannel*> MessageChannelMap;
94 151
95 // A map of channel ID to information about the extension that is waiting 152 // A map of channel ID to information about the extension that is waiting
96 // for that channel to open. Used for lazy background pages. 153 // for that channel to open. Used for lazy background pages.
97 typedef std::string ExtensionID; 154 typedef std::string ExtensionID;
98 typedef std::pair<Profile*, ExtensionID> PendingChannel; 155 typedef std::pair<Profile*, ExtensionID> PendingChannel;
99 typedef std::map<int, PendingChannel> PendingChannelMap; 156 typedef std::map<int, PendingChannel> PendingChannelMap;
100 157
101 // Common among OpenChannel* variants. 158 // Common among OpenChannel* variants.
102 bool OpenChannelImpl(const OpenChannelParams& params); 159 bool OpenChannelImpl(scoped_ptr<OpenChannelParams> params);
103 160
104 void CloseChannelImpl(MessageChannelMap::iterator channel_iter, 161 void CloseChannelImpl(MessageChannelMap::iterator channel_iter,
105 int port_id, bool connection_error, 162 int port_id, bool connection_error,
106 bool notify_other_port); 163 bool notify_other_port);
107 164
165 // Have MessageService take ownership of |channel|, and remove any pending
166 // channels with the same id.
167 void AddChannel(MessageChannel* channel, int receiver_port_id);
168
108 // content::NotificationObserver interface. 169 // content::NotificationObserver interface.
109 virtual void Observe(int type, 170 virtual void Observe(int type,
110 const content::NotificationSource& source, 171 const content::NotificationSource& source,
111 const content::NotificationDetails& details) OVERRIDE; 172 const content::NotificationDetails& details) OVERRIDE;
112 173
113 // A process that might be in our list of channels has closed. 174 // A process that might be in our list of channels has closed.
114 void OnProcessClosed(content::RenderProcessHost* process); 175 void OnProcessClosed(content::RenderProcessHost* process);
115 176
116 // Potentially registers a pending task with the LazyBackgroundTaskQueue 177 // Potentially registers a pending task with the LazyBackgroundTaskQueue
117 // to open a channel. Returns true if a task was queued. 178 // to open a channel. Returns true if a task was queued.
179 // Takes ownership of |params| if true is returned.
118 bool MaybeAddPendingOpenChannelTask(Profile* profile, 180 bool MaybeAddPendingOpenChannelTask(Profile* profile,
119 const OpenChannelParams& params); 181 OpenChannelParams* params);
120 182
121 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an 183 // Callbacks for LazyBackgroundTaskQueue tasks. The queue passes in an
122 // ExtensionHost to its task callbacks, though some of our callbacks don't 184 // ExtensionHost to its task callbacks, though some of our callbacks don't
123 // use that argument. 185 // use that argument.
124 void PendingOpenChannel(const OpenChannelParams& params, 186 void PendingOpenChannel(scoped_ptr<OpenChannelParams> params,
125 int source_process_id, 187 int source_process_id,
126 extensions::ExtensionHost* host); 188 extensions::ExtensionHost* host);
127 void PendingCloseChannel(int port_id, 189 void PendingCloseChannel(int port_id,
128 bool connection_error, 190 bool connection_error,
129 extensions::ExtensionHost* host) { 191 extensions::ExtensionHost* host) {
130 if (host) 192 if (host)
131 CloseChannel(port_id, connection_error); 193 CloseChannel(port_id, connection_error);
132 } 194 }
133 void PendingPostMessage(int port_id, 195 void PendingPostMessage(int port_id,
134 const std::string& message, 196 const std::string& message,
135 extensions::ExtensionHost* host) { 197 extensions::ExtensionHost* host) {
136 if (host) 198 if (host)
137 PostMessageFromRenderer(port_id, message); 199 PostMessage(port_id, message);
138 } 200 }
139 201
140 content::NotificationRegistrar registrar_; 202 content::NotificationRegistrar registrar_;
141 MessageChannelMap channels_; 203 MessageChannelMap channels_;
142 PendingChannelMap pending_channels_; 204 PendingChannelMap pending_channels_;
143 205
144 // Weak pointer. Guaranteed to outlive this class. 206 // Weak pointer. Guaranteed to outlive this class.
145 LazyBackgroundTaskQueue* lazy_background_task_queue_; 207 LazyBackgroundTaskQueue* lazy_background_task_queue_;
146 208
209 base::WeakPtrFactory<MessageService> weak_factory_;
210
147 DISALLOW_COPY_AND_ASSIGN(MessageService); 211 DISALLOW_COPY_AND_ASSIGN(MessageService);
148 }; 212 };
149 213
150 } // namespace extensions 214 } // namespace extensions
151 215
152 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_ 216 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_MESSAGE_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698