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

Side by Side Diff: ipc/ipc_channel_proxy.h

Issue 245443005: Move IPC::MessageFilter and router to a separate file (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix gn build Created 6 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « ipc/ipc.gypi ('k') | ipc/ipc_channel_proxy.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/synchronization/lock.h" 12 #include "base/synchronization/lock.h"
13 #include "base/threading/non_thread_safe.h" 13 #include "base/threading/non_thread_safe.h"
14 #include "ipc/ipc_channel.h" 14 #include "ipc/ipc_channel.h"
15 #include "ipc/ipc_channel_handle.h" 15 #include "ipc/ipc_channel_handle.h"
16 #include "ipc/ipc_listener.h" 16 #include "ipc/ipc_listener.h"
17 #include "ipc/ipc_sender.h" 17 #include "ipc/ipc_sender.h"
18 18
19 namespace base { 19 namespace base {
20 class SingleThreadTaskRunner; 20 class SingleThreadTaskRunner;
21 } 21 }
22 22
23 namespace IPC { 23 namespace IPC {
24 24
25 class MessageFilter;
26 class MessageFilterRouter;
25 class SendCallbackHelper; 27 class SendCallbackHelper;
26 28
27 //----------------------------------------------------------------------------- 29 //-----------------------------------------------------------------------------
28 // IPC::ChannelProxy 30 // IPC::ChannelProxy
29 // 31 //
30 // This class is a helper class that is useful when you wish to run an IPC 32 // This class is a helper class that is useful when you wish to run an IPC
31 // channel on a background thread. It provides you with the option of either 33 // channel on a background thread. It provides you with the option of either
32 // handling IPC messages on that background thread or having them dispatched to 34 // handling IPC messages on that background thread or having them dispatched to
33 // your main thread (the thread on which the IPC::ChannelProxy is created). 35 // your main thread (the thread on which the IPC::ChannelProxy is created).
34 // 36 //
(...skipping 12 matching lines...) Expand all
47 // the consumer of IPC::ChannelProxy the ability to respond to incoming 49 // the consumer of IPC::ChannelProxy the ability to respond to incoming
48 // messages on this background thread instead of on their own thread, which may 50 // messages on this background thread instead of on their own thread, which may
49 // be bogged down with other processing. The result can be greatly improved 51 // be bogged down with other processing. The result can be greatly improved
50 // latency for messages that can be handled on a background thread. 52 // latency for messages that can be handled on a background thread.
51 // 53 //
52 // The consumer of IPC::ChannelProxy is responsible for allocating the Thread 54 // The consumer of IPC::ChannelProxy is responsible for allocating the Thread
53 // instance where the IPC::Channel will be created and operated. 55 // instance where the IPC::Channel will be created and operated.
54 // 56 //
55 class IPC_EXPORT ChannelProxy : public Sender, public base::NonThreadSafe { 57 class IPC_EXPORT ChannelProxy : public Sender, public base::NonThreadSafe {
56 public: 58 public:
57
58 // A class that receives messages on the thread where the IPC channel is
59 // running. It can choose to prevent the default action for an IPC message.
60 class IPC_EXPORT MessageFilter
61 : public base::RefCountedThreadSafe<MessageFilter> {
62 public:
63 MessageFilter();
64
65 // Called on the background thread to provide the filter with access to the
66 // channel. Called when the IPC channel is initialized or when AddFilter
67 // is called if the channel is already initialized.
68 virtual void OnFilterAdded(Channel* channel);
69
70 // Called on the background thread when the filter has been removed from
71 // the ChannelProxy and when the Channel is closing. After a filter is
72 // removed, it will not be called again.
73 virtual void OnFilterRemoved();
74
75 // Called to inform the filter that the IPC channel is connected and we
76 // have received the internal Hello message from the peer.
77 virtual void OnChannelConnected(int32 peer_pid);
78
79 // Called when there is an error on the channel, typically that the channel
80 // has been closed.
81 virtual void OnChannelError();
82
83 // Called to inform the filter that the IPC channel will be destroyed.
84 // OnFilterRemoved is called immediately after this.
85 virtual void OnChannelClosing();
86
87 // Return true to indicate that the message was handled, or false to let
88 // the message be handled in the default way.
89 virtual bool OnMessageReceived(const Message& message);
90
91 // Called to query the Message classes supported by the filter. Return
92 // false to indicate that all message types should reach the filter, or true
93 // if the resulting contents of |supported_message_classes| may be used to
94 // selectively offer messages of a particular class to the filter.
95 virtual bool GetSupportedMessageClasses(
96 std::vector<uint32>* supported_message_classes) const;
97
98 protected:
99 virtual ~MessageFilter();
100
101 private:
102 friend class base::RefCountedThreadSafe<MessageFilter>;
103 };
104
105 // Initializes a channel proxy. The channel_handle and mode parameters are 59 // Initializes a channel proxy. The channel_handle and mode parameters are
106 // passed directly to the underlying IPC::Channel. The listener is called on 60 // passed directly to the underlying IPC::Channel. The listener is called on
107 // the thread that creates the ChannelProxy. The filter's OnMessageReceived 61 // the thread that creates the ChannelProxy. The filter's OnMessageReceived
108 // method is called on the thread where the IPC::Channel is running. The 62 // method is called on the thread where the IPC::Channel is running. The
109 // filter may be null if the consumer is not interested in handling messages 63 // filter may be null if the consumer is not interested in handling messages
110 // on the background thread. Any message not handled by the filter will be 64 // on the background thread. Any message not handled by the filter will be
111 // dispatched to the listener. The given task runner correspond to a thread 65 // dispatched to the listener. The given task runner correspond to a thread
112 // on which IPC::Channel is created and used (e.g. IO thread). 66 // on which IPC::Channel is created and used (e.g. IO thread).
113 ChannelProxy(const IPC::ChannelHandle& channel_handle, 67 ChannelProxy(const IPC::ChannelHandle& channel_handle,
114 Channel::Mode mode, 68 Channel::Mode mode,
115 Listener* listener, 69 Listener* listener,
116 base::SingleThreadTaskRunner* ipc_task_runner); 70 base::SingleThreadTaskRunner* ipc_task_runner);
117 71
118 virtual ~ChannelProxy(); 72 virtual ~ChannelProxy();
119 73
120 // Initializes the channel proxy. Only call this once to initialize a channel 74 // Initializes the channel proxy. Only call this once to initialize a channel
121 // proxy that was not initialized in its constructor. If create_pipe_now is 75 // proxy that was not initialized in its constructor. If create_pipe_now is
122 // true, the pipe is created synchronously. Otherwise it's created on the IO 76 // true, the pipe is created synchronously. Otherwise it's created on the IO
123 // thread. 77 // thread.
124 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, 78 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode,
125 bool create_pipe_now); 79 bool create_pipe_now);
126 80
127 // Close the IPC::Channel. This operation completes asynchronously, once the 81 // Close the IPC::Channel. This operation completes asynchronously, once the
128 // background thread processes the command to close the channel. It is ok to 82 // background thread processes the command to close the channel. It is ok to
129 // call this method multiple times. Redundant calls are ignored. 83 // call this method multiple times. Redundant calls are ignored.
130 // 84 //
131 // WARNING: The MessageFilter object held by the ChannelProxy is also 85 // WARNING: MessageFilter objects held by the ChannelProxy is also
132 // released asynchronously, and it may in fact have its final reference 86 // released asynchronously, and it may in fact have its final reference
133 // released on the background thread. The caller should be careful to deal 87 // released on the background thread. The caller should be careful to deal
134 // with / allow for this possibility. 88 // with / allow for this possibility.
135 void Close(); 89 void Close();
136 90
137 // Send a message asynchronously. The message is routed to the background 91 // Send a message asynchronously. The message is routed to the background
138 // thread where it is passed to the IPC::Channel's Send method. 92 // thread where it is passed to the IPC::Channel's Send method.
139 virtual bool Send(Message* message) OVERRIDE; 93 virtual bool Send(Message* message) OVERRIDE;
140 94
141 // Used to intercept messages as they are received on the background thread. 95 // Used to intercept messages as they are received on the background thread.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 190
237 // Note, channel_ may be set on the Listener thread or the IPC thread. 191 // Note, channel_ may be set on the Listener thread or the IPC thread.
238 // But once it has been set, it must only be read or cleared on the IPC 192 // But once it has been set, it must only be read or cleared on the IPC
239 // thread. 193 // thread.
240 scoped_ptr<Channel> channel_; 194 scoped_ptr<Channel> channel_;
241 std::string channel_id_; 195 std::string channel_id_;
242 bool channel_connected_called_; 196 bool channel_connected_called_;
243 197
244 // Routes a given message to a proper subset of |filters_|, depending 198 // Routes a given message to a proper subset of |filters_|, depending
245 // on which message classes a filter might support. 199 // on which message classes a filter might support.
246 class MessageFilterRouter;
247 scoped_ptr<MessageFilterRouter> message_filter_router_; 200 scoped_ptr<MessageFilterRouter> message_filter_router_;
248 201
249 // Holds filters between the AddFilter call on the listerner thread and the 202 // Holds filters between the AddFilter call on the listerner thread and the
250 // IPC thread when they're added to filters_. 203 // IPC thread when they're added to filters_.
251 std::vector<scoped_refptr<MessageFilter> > pending_filters_; 204 std::vector<scoped_refptr<MessageFilter> > pending_filters_;
252 // Lock for pending_filters_. 205 // Lock for pending_filters_.
253 base::Lock pending_filters_lock_; 206 base::Lock pending_filters_lock_;
254 207
255 // Cached copy of the peer process ID. Set on IPC but read on both IPC and 208 // Cached copy of the peer process ID. Set on IPC but read on both IPC and
256 // listener threads. 209 // listener threads.
(...skipping 10 matching lines...) Expand all
267 // that involves this data. 220 // that involves this data.
268 scoped_refptr<Context> context_; 221 scoped_refptr<Context> context_;
269 222
270 // Whether the channel has been initialized. 223 // Whether the channel has been initialized.
271 bool did_init_; 224 bool did_init_;
272 }; 225 };
273 226
274 } // namespace IPC 227 } // namespace IPC
275 228
276 #endif // IPC_IPC_CHANNEL_PROXY_H_ 229 #endif // IPC_IPC_CHANNEL_PROXY_H_
OLDNEW
« no previous file with comments | « ipc/ipc.gypi ('k') | ipc/ipc_channel_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698