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

Side by Side Diff: ipc/ipc_channel_proxy.h

Issue 10694014: Cleanup IPC::ChannelProxy to use SingleThreadTaskRunner (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 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
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 #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"
14 #include "base/synchronization/lock.h" 13 #include "base/synchronization/lock.h"
15 #include "ipc/ipc_channel.h" 14 #include "ipc/ipc_channel.h"
16 #include "ipc/ipc_channel_handle.h" 15 #include "ipc/ipc_channel_handle.h"
17 #include "ipc/ipc_listener.h" 16 #include "ipc/ipc_listener.h"
18 #include "ipc/ipc_sender.h" 17 #include "ipc/ipc_sender.h"
19 18
19 namespace base {
20 class SingleThreadTaskRunner;
21 } // namespace base
jam 2012/07/01 22:19:41 ditto
Sergey Ulanov 2012/07/02 18:44:34 Done.
22
20 namespace IPC { 23 namespace IPC {
21 24
22 class SendCallbackHelper; 25 class SendCallbackHelper;
23 26
24 //----------------------------------------------------------------------------- 27 //-----------------------------------------------------------------------------
25 // IPC::ChannelProxy 28 // IPC::ChannelProxy
26 // 29 //
27 // This class is a helper class that is useful when you wish to run an IPC 30 // This class is a helper class that is useful when you wish to run an IPC
28 // channel on a background thread. It provides you with the option of either 31 // channel on a background thread. It provides you with the option of either
29 // handling IPC messages on that background thread or having them dispatched to 32 // handling IPC messages on that background thread or having them dispatched to
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 // original unchanged if no rewrite indicated. 117 // original unchanged if no rewrite indicated.
115 virtual Message *Rewrite(Message *message) = 0; 118 virtual Message *Rewrite(Message *message) = 0;
116 }; 119 };
117 120
118 // Initializes a channel proxy. The channel_handle and mode parameters are 121 // Initializes a channel proxy. The channel_handle and mode parameters are
119 // passed directly to the underlying IPC::Channel. The listener is called on 122 // passed directly to the underlying IPC::Channel. The listener is called on
120 // the thread that creates the ChannelProxy. The filter's OnMessageReceived 123 // the thread that creates the ChannelProxy. The filter's OnMessageReceived
121 // method is called on the thread where the IPC::Channel is running. The 124 // method is called on the thread where the IPC::Channel is running. The
122 // filter may be null if the consumer is not interested in handling messages 125 // filter may be null if the consumer is not interested in handling messages
123 // on the background thread. Any message not handled by the filter will be 126 // on the background thread. Any message not handled by the filter will be
124 // dispatched to the listener. The given message loop indicates where the 127 // dispatched to the listener. The given task runner correspond to a thread
125 // IPC::Channel should be created. 128 // on which IPC::Channel is created and used (e.g. IO thread).
126 ChannelProxy(const IPC::ChannelHandle& channel_handle, 129 ChannelProxy(const IPC::ChannelHandle& channel_handle,
127 Channel::Mode mode, 130 Channel::Mode mode,
128 Listener* listener, 131 Listener* listener,
129 base::MessageLoopProxy* ipc_thread_loop); 132 base::SingleThreadTaskRunner* ipc_task_runner);
130 133
131 virtual ~ChannelProxy(); 134 virtual ~ChannelProxy();
132 135
133 // Initializes the channel proxy. Only call this once to initialize a channel 136 // Initializes the channel proxy. Only call this once to initialize a channel
134 // proxy that was not initialized in its constructor. If create_pipe_now is 137 // proxy that was not initialized in its constructor. If create_pipe_now is
135 // true, the pipe is created synchronously. Otherwise it's created on the IO 138 // true, the pipe is created synchronously. Otherwise it's created on the IO
136 // thread. 139 // thread.
137 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode, 140 void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode,
138 bool create_pipe_now); 141 bool create_pipe_now);
139 142
(...skipping 20 matching lines...) Expand all
160 // guaranteed to not miss any messages. But if you call this anytime after, 163 // guaranteed to not miss any messages. But if you call this anytime after,
161 // then some messages might be missed since the filter is added internally on 164 // then some messages might be missed since the filter is added internally on
162 // the IO thread. 165 // the IO thread.
163 void AddFilter(MessageFilter* filter); 166 void AddFilter(MessageFilter* filter);
164 void RemoveFilter(MessageFilter* filter); 167 void RemoveFilter(MessageFilter* filter);
165 168
166 void set_outgoing_message_filter(OutgoingMessageFilter* filter) { 169 void set_outgoing_message_filter(OutgoingMessageFilter* filter) {
167 outgoing_message_filter_ = filter; 170 outgoing_message_filter_ = filter;
168 } 171 }
169 172
170 // Called to clear the pointer to the IPC message loop when it's going away. 173 // Called to clear the pointer to the IPC task runner when it's going away.
171 void ClearIPCMessageLoop(); 174 void ClearIPCTaskRunner();
172 175
173 // Get the process ID for the connected peer. 176 // Get the process ID for the connected peer.
174 // Returns base::kNullProcessId if the peer is not connected yet. 177 // Returns base::kNullProcessId if the peer is not connected yet.
175 base::ProcessId peer_pid() const { return context_->peer_pid_; } 178 base::ProcessId peer_pid() const { return context_->peer_pid_; }
176 179
177 #if defined(OS_POSIX) && !defined(OS_NACL) 180 #if defined(OS_POSIX) && !defined(OS_NACL)
178 // Calls through to the underlying channel's methods. 181 // Calls through to the underlying channel's methods.
179 int GetClientFileDescriptor(); 182 int GetClientFileDescriptor();
180 int TakeClientFileDescriptor(); 183 int TakeClientFileDescriptor();
181 bool GetClientEuid(uid_t* client_euid) const; 184 bool GetClientEuid(uid_t* client_euid) const;
182 #endif // defined(OS_POSIX) 185 #endif // defined(OS_POSIX)
183 186
184 protected: 187 protected:
185 class Context; 188 class Context;
186 // A subclass uses this constructor if it needs to add more information 189 // A subclass uses this constructor if it needs to add more information
187 // to the internal state. 190 // to the internal state.
188 ChannelProxy(Context* context); 191 ChannelProxy(Context* context);
189 192
190 // Used internally to hold state that is referenced on the IPC thread. 193 // Used internally to hold state that is referenced on the IPC thread.
191 class Context : public base::RefCountedThreadSafe<Context>, 194 class Context : public base::RefCountedThreadSafe<Context>,
192 public Listener { 195 public Listener {
193 public: 196 public:
194 Context(Listener* listener, base::MessageLoopProxy* ipc_thread); 197 Context(Listener* listener, base::SingleThreadTaskRunner* ipc_thread);
195 void ClearIPCMessageLoop() { ipc_message_loop_ = NULL; } 198 void ClearIPCTaskRunner();
196 base::MessageLoopProxy* ipc_message_loop() const { 199 base::SingleThreadTaskRunner* ipc_task_runner() const {
197 return ipc_message_loop_.get(); 200 return ipc_task_runner_;
198 } 201 }
199 const std::string& channel_id() const { return channel_id_; } 202 const std::string& channel_id() const { return channel_id_; }
200 203
201 // Dispatches a message on the listener thread. 204 // Dispatches a message on the listener thread.
202 void OnDispatchMessage(const Message& message); 205 void OnDispatchMessage(const Message& message);
203 206
204 protected: 207 protected:
205 friend class base::RefCountedThreadSafe<Context>; 208 friend class base::RefCountedThreadSafe<Context>;
206 virtual ~Context(); 209 virtual ~Context();
207 210
(...skipping 29 matching lines...) Expand all
237 // Methods called on the IO thread. 240 // Methods called on the IO thread.
238 void OnSendMessage(scoped_ptr<Message> message_ptr); 241 void OnSendMessage(scoped_ptr<Message> message_ptr);
239 void OnAddFilter(); 242 void OnAddFilter();
240 void OnRemoveFilter(MessageFilter* filter); 243 void OnRemoveFilter(MessageFilter* filter);
241 244
242 // Methods called on the listener thread. 245 // Methods called on the listener thread.
243 void AddFilter(MessageFilter* filter); 246 void AddFilter(MessageFilter* filter);
244 void OnDispatchConnected(); 247 void OnDispatchConnected();
245 void OnDispatchError(); 248 void OnDispatchError();
246 249
247 scoped_refptr<base::MessageLoopProxy> listener_message_loop_; 250 scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_;
248 Listener* listener_; 251 Listener* listener_;
249 252
250 // List of filters. This is only accessed on the IPC thread. 253 // List of filters. This is only accessed on the IPC thread.
251 std::vector<scoped_refptr<MessageFilter> > filters_; 254 std::vector<scoped_refptr<MessageFilter> > filters_;
252 scoped_refptr<base::MessageLoopProxy> ipc_message_loop_; 255 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
253 scoped_ptr<Channel> channel_; 256 scoped_ptr<Channel> channel_;
254 std::string channel_id_; 257 std::string channel_id_;
255 bool channel_connected_called_; 258 bool channel_connected_called_;
256 259
257 // Holds filters between the AddFilter call on the listerner thread and the 260 // Holds filters between the AddFilter call on the listerner thread and the
258 // IPC thread when they're added to filters_. 261 // IPC thread when they're added to filters_.
259 std::vector<scoped_refptr<MessageFilter> > pending_filters_; 262 std::vector<scoped_refptr<MessageFilter> > pending_filters_;
260 // Lock for pending_filters_. 263 // Lock for pending_filters_.
261 base::Lock pending_filters_lock_; 264 base::Lock pending_filters_lock_;
262 265
(...skipping 18 matching lines...) Expand all
281 284
282 OutgoingMessageFilter* outgoing_message_filter_; 285 OutgoingMessageFilter* outgoing_message_filter_;
283 286
284 // Whether the channel has been initialized. 287 // Whether the channel has been initialized.
285 bool did_init_; 288 bool did_init_;
286 }; 289 };
287 290
288 } // namespace IPC 291 } // namespace IPC
289 292
290 #endif // IPC_IPC_CHANNEL_PROXY_H_ 293 #endif // IPC_IPC_CHANNEL_PROXY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698