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_channel_listener.h" |
| 18 #include "ipc/ipc_message_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 MessageSender { |
51 public: | 53 public: |
52 | 54 |
53 struct MessageFilterTraits; | 55 struct MessageFilterTraits; |
54 | 56 |
55 // A class that receives messages on the thread where the IPC channel is | 57 // 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. | 58 // running. It can choose to prevent the default action for an IPC message. |
57 class IPC_EXPORT MessageFilter | 59 class IPC_EXPORT MessageFilter |
58 : public base::RefCountedThreadSafe<MessageFilter, MessageFilterTraits> { | 60 : public base::RefCountedThreadSafe<MessageFilter, MessageFilterTraits> { |
59 public: | 61 public: |
60 MessageFilter(); | 62 MessageFilter(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // Initializes a channel proxy. The channel_handle and mode parameters are | 119 // Initializes a channel proxy. The channel_handle and mode parameters are |
118 // passed directly to the underlying IPC::Channel. The listener is called on | 120 // passed directly to the underlying IPC::Channel. The listener is called on |
119 // the thread that creates the ChannelProxy. The filter's OnMessageReceived | 121 // the thread that creates the ChannelProxy. The filter's OnMessageReceived |
120 // method is called on the thread where the IPC::Channel is running. The | 122 // 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 | 123 // 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 | 124 // 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 | 125 // dispatched to the listener. The given message loop indicates where the |
124 // IPC::Channel should be created. | 126 // IPC::Channel should be created. |
125 ChannelProxy(const IPC::ChannelHandle& channel_handle, | 127 ChannelProxy(const IPC::ChannelHandle& channel_handle, |
126 Channel::Mode mode, | 128 Channel::Mode mode, |
127 Channel::Listener* listener, | 129 ChannelListener* listener, |
128 base::MessageLoopProxy* ipc_thread_loop); | 130 base::MessageLoopProxy* ipc_thread_loop); |
129 | 131 |
130 // Creates an uninitialized channel proxy. Init must be called to receive | 132 // 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 | 133 // or send any messages. This two-step setup allows message filters to be |
132 // added before any messages are sent or received. | 134 // added before any messages are sent or received. |
133 ChannelProxy(Channel::Listener* listener, | 135 ChannelProxy(ChannelListener* listener, |
134 base::MessageLoopProxy* ipc_thread_loop); | 136 base::MessageLoopProxy* ipc_thread_loop); |
135 | 137 |
136 virtual ~ChannelProxy(); | 138 virtual ~ChannelProxy(); |
137 | 139 |
138 // Initializes the channel proxy. Only call this once to initialize a channel | 140 // 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 | 141 // 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 | 142 // true, the pipe is created synchronously. Otherwise it's created on the IO |
141 // thread. | 143 // thread. |
142 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, | 144 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, |
143 bool create_pipe_now); | 145 bool create_pipe_now); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 #endif // defined(OS_POSIX) | 189 #endif // defined(OS_POSIX) |
188 | 190 |
189 protected: | 191 protected: |
190 class Context; | 192 class Context; |
191 // A subclass uses this constructor if it needs to add more information | 193 // A subclass uses this constructor if it needs to add more information |
192 // to the internal state. | 194 // to the internal state. |
193 ChannelProxy(Context* context); | 195 ChannelProxy(Context* context); |
194 | 196 |
195 // Used internally to hold state that is referenced on the IPC thread. | 197 // Used internally to hold state that is referenced on the IPC thread. |
196 class Context : public base::RefCountedThreadSafe<Context>, | 198 class Context : public base::RefCountedThreadSafe<Context>, |
197 public Channel::Listener { | 199 public ChannelListener { |
198 public: | 200 public: |
199 Context(Channel::Listener* listener, base::MessageLoopProxy* ipc_thread); | 201 Context(ChannelListener* listener, base::MessageLoopProxy* ipc_thread); |
200 void ClearIPCMessageLoop() { ipc_message_loop_ = NULL; } | 202 void ClearIPCMessageLoop() { ipc_message_loop_ = NULL; } |
201 base::MessageLoopProxy* ipc_message_loop() const { | 203 base::MessageLoopProxy* ipc_message_loop() const { |
202 return ipc_message_loop_.get(); | 204 return ipc_message_loop_.get(); |
203 } | 205 } |
204 const std::string& channel_id() const { return channel_id_; } | 206 const std::string& channel_id() const { return channel_id_; } |
205 | 207 |
206 // Dispatches a message on the listener thread. | 208 // Dispatches a message on the listener thread. |
207 void OnDispatchMessage(const Message& message); | 209 void OnDispatchMessage(const Message& message); |
208 | 210 |
209 protected: | 211 protected: |
210 friend class base::RefCountedThreadSafe<Context>; | 212 friend class base::RefCountedThreadSafe<Context>; |
211 virtual ~Context(); | 213 virtual ~Context(); |
212 | 214 |
213 // IPC::Channel::Listener methods: | 215 // IPC::ChannelListener methods: |
214 virtual bool OnMessageReceived(const Message& message) OVERRIDE; | 216 virtual bool OnMessageReceived(const Message& message) OVERRIDE; |
215 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | 217 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; |
216 virtual void OnChannelError() OVERRIDE; | 218 virtual void OnChannelError() OVERRIDE; |
217 | 219 |
218 // Like OnMessageReceived but doesn't try the filters. | 220 // Like OnMessageReceived but doesn't try the filters. |
219 bool OnMessageReceivedNoFilter(const Message& message); | 221 bool OnMessageReceivedNoFilter(const Message& message); |
220 | 222 |
221 // Gives the filters a chance at processing |message|. | 223 // Gives the filters a chance at processing |message|. |
222 // Returns true if the message was processed, false otherwise. | 224 // Returns true if the message was processed, false otherwise. |
223 bool TryFilters(const Message& message); | 225 bool TryFilters(const Message& message); |
(...skipping 19 matching lines...) Expand all Loading... |
243 void OnSendMessage(scoped_ptr<Message> message_ptr); | 245 void OnSendMessage(scoped_ptr<Message> message_ptr); |
244 void OnAddFilter(); | 246 void OnAddFilter(); |
245 void OnRemoveFilter(MessageFilter* filter); | 247 void OnRemoveFilter(MessageFilter* filter); |
246 | 248 |
247 // Methods called on the listener thread. | 249 // Methods called on the listener thread. |
248 void AddFilter(MessageFilter* filter); | 250 void AddFilter(MessageFilter* filter); |
249 void OnDispatchConnected(); | 251 void OnDispatchConnected(); |
250 void OnDispatchError(); | 252 void OnDispatchError(); |
251 | 253 |
252 scoped_refptr<base::MessageLoopProxy> listener_message_loop_; | 254 scoped_refptr<base::MessageLoopProxy> listener_message_loop_; |
253 Channel::Listener* listener_; | 255 ChannelListener* listener_; |
254 | 256 |
255 // List of filters. This is only accessed on the IPC thread. | 257 // List of filters. This is only accessed on the IPC thread. |
256 std::vector<scoped_refptr<MessageFilter> > filters_; | 258 std::vector<scoped_refptr<MessageFilter> > filters_; |
257 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; | 259 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; |
258 scoped_ptr<Channel> channel_; | 260 scoped_ptr<Channel> channel_; |
259 std::string channel_id_; | 261 std::string channel_id_; |
260 bool channel_connected_called_; | 262 bool channel_connected_called_; |
261 | 263 |
262 // Holds filters between the AddFilter call on the listerner thread and the | 264 // Holds filters between the AddFilter call on the listerner thread and the |
263 // IPC thread when they're added to filters_. | 265 // IPC thread when they're added to filters_. |
(...skipping 22 matching lines...) Expand all Loading... |
286 | 288 |
287 OutgoingMessageFilter* outgoing_message_filter_; | 289 OutgoingMessageFilter* outgoing_message_filter_; |
288 | 290 |
289 // Whether the channel has been initialized. | 291 // Whether the channel has been initialized. |
290 bool did_init_; | 292 bool did_init_; |
291 }; | 293 }; |
292 | 294 |
293 } // namespace IPC | 295 } // namespace IPC |
294 | 296 |
295 #endif // IPC_IPC_CHANNEL_PROXY_H_ | 297 #endif // IPC_IPC_CHANNEL_PROXY_H_ |
OLD | NEW |