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 IPC_IPC_CHANNEL_PROXY_H_ | 5 #ifndef IPC_IPC_CHANNEL_PROXY_H_ |
6 #define IPC_IPC_CHANNEL_PROXY_H_ | 6 #define IPC_IPC_CHANNEL_PROXY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop_proxy.h" | 13 #include "base/message_loop_proxy.h" |
14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
15 #include "ipc/ipc_channel.h" | 15 #include "ipc/ipc_channel.h" |
16 #include "ipc/ipc_channel_handle.h" | 16 #include "ipc/ipc_channel_handle.h" |
| 17 #include "ipc/ipc_listener.h" |
| 18 #include "ipc/ipc_sender.h" |
17 | 19 |
18 namespace IPC { | 20 namespace IPC { |
19 | 21 |
20 class SendCallbackHelper; | 22 class SendCallbackHelper; |
21 | 23 |
22 //----------------------------------------------------------------------------- | 24 //----------------------------------------------------------------------------- |
23 // IPC::ChannelProxy | 25 // IPC::ChannelProxy |
24 // | 26 // |
25 // This class is a helper class that is useful when you wish to run an IPC | 27 // This class is a helper class that is useful when you wish to run an IPC |
26 // channel on a background thread. It provides you with the option of either | 28 // channel on a background thread. It provides you with the option of either |
(...skipping 13 matching lines...) Expand all Loading... |
40 // An IPC::ChannelProxy can have a MessageFilter associated with it, which will | 42 // An IPC::ChannelProxy can have a MessageFilter associated with it, which will |
41 // be notified of incoming messages on the IPC::Channel's thread. This gives | 43 // be notified of incoming messages on the IPC::Channel's thread. This gives |
42 // the consumer of IPC::ChannelProxy the ability to respond to incoming | 44 // the consumer of IPC::ChannelProxy the ability to respond to incoming |
43 // messages on this background thread instead of on their own thread, which may | 45 // messages on this background thread instead of on their own thread, which may |
44 // be bogged down with other processing. The result can be greatly improved | 46 // be bogged down with other processing. The result can be greatly improved |
45 // latency for messages that can be handled on a background thread. | 47 // latency for messages that can be handled on a background thread. |
46 // | 48 // |
47 // The consumer of IPC::ChannelProxy is responsible for allocating the Thread | 49 // The consumer of IPC::ChannelProxy is responsible for allocating the Thread |
48 // instance where the IPC::Channel will be created and operated. | 50 // instance where the IPC::Channel will be created and operated. |
49 // | 51 // |
50 class IPC_EXPORT ChannelProxy : public Message::Sender { | 52 class IPC_EXPORT ChannelProxy : public Sender { |
51 public: | 53 public: |
52 | |
53 struct MessageFilterTraits; | 54 struct MessageFilterTraits; |
54 | 55 |
55 // A class that receives messages on the thread where the IPC channel is | 56 // A class that receives messages on the thread where the IPC channel is |
56 // running. It can choose to prevent the default action for an IPC message. | 57 // running. It can choose to prevent the default action for an IPC message. |
57 class IPC_EXPORT MessageFilter | 58 class IPC_EXPORT MessageFilter |
58 : public base::RefCountedThreadSafe<MessageFilter, MessageFilterTraits> { | 59 : public base::RefCountedThreadSafe<MessageFilter, MessageFilterTraits> { |
59 public: | 60 public: |
60 MessageFilter(); | 61 MessageFilter(); |
61 | 62 |
62 // Called on the background thread to provide the filter with access to the | 63 // Called on the background thread to provide the filter with access to the |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // Initializes a channel proxy. The channel_handle and mode parameters are | 118 // Initializes a channel proxy. The channel_handle and mode parameters are |
118 // passed directly to the underlying IPC::Channel. The listener is called on | 119 // passed directly to the underlying IPC::Channel. The listener is called on |
119 // the thread that creates the ChannelProxy. The filter's OnMessageReceived | 120 // the thread that creates the ChannelProxy. The filter's OnMessageReceived |
120 // method is called on the thread where the IPC::Channel is running. The | 121 // method is called on the thread where the IPC::Channel is running. The |
121 // filter may be null if the consumer is not interested in handling messages | 122 // filter may be null if the consumer is not interested in handling messages |
122 // on the background thread. Any message not handled by the filter will be | 123 // on the background thread. Any message not handled by the filter will be |
123 // dispatched to the listener. The given message loop indicates where the | 124 // dispatched to the listener. The given message loop indicates where the |
124 // IPC::Channel should be created. | 125 // IPC::Channel should be created. |
125 ChannelProxy(const IPC::ChannelHandle& channel_handle, | 126 ChannelProxy(const IPC::ChannelHandle& channel_handle, |
126 Channel::Mode mode, | 127 Channel::Mode mode, |
127 Channel::Listener* listener, | 128 Listener* listener, |
128 base::MessageLoopProxy* ipc_thread_loop); | 129 base::MessageLoopProxy* ipc_thread_loop); |
129 | 130 |
130 // Creates an uninitialized channel proxy. Init must be called to receive | 131 // Creates an uninitialized channel proxy. Init must be called to receive |
131 // or send any messages. This two-step setup allows message filters to be | 132 // or send any messages. This two-step setup allows message filters to be |
132 // added before any messages are sent or received. | 133 // added before any messages are sent or received. |
133 ChannelProxy(Channel::Listener* listener, | 134 ChannelProxy(Listener* listener, |
134 base::MessageLoopProxy* ipc_thread_loop); | 135 base::MessageLoopProxy* ipc_thread_loop); |
135 | 136 |
136 virtual ~ChannelProxy(); | 137 virtual ~ChannelProxy(); |
137 | 138 |
138 // Initializes the channel proxy. Only call this once to initialize a channel | 139 // Initializes the channel proxy. Only call this once to initialize a channel |
139 // proxy that was not initialized in its constructor. If create_pipe_now is | 140 // proxy that was not initialized in its constructor. If create_pipe_now is |
140 // true, the pipe is created synchronously. Otherwise it's created on the IO | 141 // true, the pipe is created synchronously. Otherwise it's created on the IO |
141 // thread. | 142 // thread. |
142 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, | 143 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, |
143 bool create_pipe_now); | 144 bool create_pipe_now); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 #endif // defined(OS_POSIX) | 188 #endif // defined(OS_POSIX) |
188 | 189 |
189 protected: | 190 protected: |
190 class Context; | 191 class Context; |
191 // A subclass uses this constructor if it needs to add more information | 192 // A subclass uses this constructor if it needs to add more information |
192 // to the internal state. | 193 // to the internal state. |
193 ChannelProxy(Context* context); | 194 ChannelProxy(Context* context); |
194 | 195 |
195 // Used internally to hold state that is referenced on the IPC thread. | 196 // Used internally to hold state that is referenced on the IPC thread. |
196 class Context : public base::RefCountedThreadSafe<Context>, | 197 class Context : public base::RefCountedThreadSafe<Context>, |
197 public Channel::Listener { | 198 public Listener { |
198 public: | 199 public: |
199 Context(Channel::Listener* listener, base::MessageLoopProxy* ipc_thread); | 200 Context(Listener* listener, base::MessageLoopProxy* ipc_thread); |
200 void ClearIPCMessageLoop() { ipc_message_loop_ = NULL; } | 201 void ClearIPCMessageLoop() { ipc_message_loop_ = NULL; } |
201 base::MessageLoopProxy* ipc_message_loop() const { | 202 base::MessageLoopProxy* ipc_message_loop() const { |
202 return ipc_message_loop_.get(); | 203 return ipc_message_loop_.get(); |
203 } | 204 } |
204 const std::string& channel_id() const { return channel_id_; } | 205 const std::string& channel_id() const { return channel_id_; } |
205 | 206 |
206 // Dispatches a message on the listener thread. | 207 // Dispatches a message on the listener thread. |
207 void OnDispatchMessage(const Message& message); | 208 void OnDispatchMessage(const Message& message); |
208 | 209 |
209 protected: | 210 protected: |
210 friend class base::RefCountedThreadSafe<Context>; | 211 friend class base::RefCountedThreadSafe<Context>; |
211 virtual ~Context(); | 212 virtual ~Context(); |
212 | 213 |
213 // IPC::Channel::Listener methods: | 214 // IPC::Listener methods: |
214 virtual bool OnMessageReceived(const Message& message) OVERRIDE; | 215 virtual bool OnMessageReceived(const Message& message) OVERRIDE; |
215 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | 216 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; |
216 virtual void OnChannelError() OVERRIDE; | 217 virtual void OnChannelError() OVERRIDE; |
217 | 218 |
218 // Like OnMessageReceived but doesn't try the filters. | 219 // Like OnMessageReceived but doesn't try the filters. |
219 bool OnMessageReceivedNoFilter(const Message& message); | 220 bool OnMessageReceivedNoFilter(const Message& message); |
220 | 221 |
221 // Gives the filters a chance at processing |message|. | 222 // Gives the filters a chance at processing |message|. |
222 // Returns true if the message was processed, false otherwise. | 223 // Returns true if the message was processed, false otherwise. |
223 bool TryFilters(const Message& message); | 224 bool TryFilters(const Message& message); |
(...skipping 19 matching lines...) Expand all Loading... |
243 void OnSendMessage(scoped_ptr<Message> message_ptr); | 244 void OnSendMessage(scoped_ptr<Message> message_ptr); |
244 void OnAddFilter(); | 245 void OnAddFilter(); |
245 void OnRemoveFilter(MessageFilter* filter); | 246 void OnRemoveFilter(MessageFilter* filter); |
246 | 247 |
247 // Methods called on the listener thread. | 248 // Methods called on the listener thread. |
248 void AddFilter(MessageFilter* filter); | 249 void AddFilter(MessageFilter* filter); |
249 void OnDispatchConnected(); | 250 void OnDispatchConnected(); |
250 void OnDispatchError(); | 251 void OnDispatchError(); |
251 | 252 |
252 scoped_refptr<base::MessageLoopProxy> listener_message_loop_; | 253 scoped_refptr<base::MessageLoopProxy> listener_message_loop_; |
253 Channel::Listener* listener_; | 254 Listener* listener_; |
254 | 255 |
255 // List of filters. This is only accessed on the IPC thread. | 256 // List of filters. This is only accessed on the IPC thread. |
256 std::vector<scoped_refptr<MessageFilter> > filters_; | 257 std::vector<scoped_refptr<MessageFilter> > filters_; |
257 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; | 258 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; |
258 scoped_ptr<Channel> channel_; | 259 scoped_ptr<Channel> channel_; |
259 std::string channel_id_; | 260 std::string channel_id_; |
260 bool channel_connected_called_; | 261 bool channel_connected_called_; |
261 | 262 |
262 // Holds filters between the AddFilter call on the listerner thread and the | 263 // Holds filters between the AddFilter call on the listerner thread and the |
263 // IPC thread when they're added to filters_. | 264 // IPC thread when they're added to filters_. |
(...skipping 22 matching lines...) Expand all Loading... |
286 | 287 |
287 OutgoingMessageFilter* outgoing_message_filter_; | 288 OutgoingMessageFilter* outgoing_message_filter_; |
288 | 289 |
289 // Whether the channel has been initialized. | 290 // Whether the channel has been initialized. |
290 bool did_init_; | 291 bool did_init_; |
291 }; | 292 }; |
292 | 293 |
293 } // namespace IPC | 294 } // namespace IPC |
294 | 295 |
295 #endif // IPC_IPC_CHANNEL_PROXY_H_ | 296 #endif // IPC_IPC_CHANNEL_PROXY_H_ |
OLD | NEW |