OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "ipc/ipc_channel_proxy.h" | 7 #include "ipc/ipc_channel_proxy.h" |
8 #include "ipc/ipc_logging.h" | 8 #include "ipc/ipc_logging.h" |
9 #include "ipc/ipc_message_utils.h" | 9 #include "ipc/ipc_message_utils.h" |
10 | 10 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 listener_(listener), | 64 listener_(listener), |
65 ipc_message_loop_(ipc_message_loop), | 65 ipc_message_loop_(ipc_message_loop), |
66 peer_pid_(0), | 66 peer_pid_(0), |
67 channel_connected_called_(false) { | 67 channel_connected_called_(false) { |
68 } | 68 } |
69 | 69 |
70 ChannelProxy::Context::~Context() { | 70 ChannelProxy::Context::~Context() { |
71 } | 71 } |
72 | 72 |
73 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle, | 73 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle, |
74 const Channel::Mode& mode, | 74 const Channel::Mode& mode) { |
75 bool needs_override_peer_pid) { | |
76 DCHECK(channel_.get() == NULL); | 75 DCHECK(channel_.get() == NULL); |
77 channel_id_ = handle.name; | 76 channel_id_ = handle.name; |
78 channel_.reset(new Channel(handle, mode, this)); | 77 channel_.reset(new Channel(handle, mode, this)); |
79 #if defined(OS_LINUX) | |
80 if (needs_override_peer_pid) | |
81 channel_->SetNeedsOverridePeerPid(); | |
82 #endif | |
83 } | 78 } |
84 | 79 |
85 bool ChannelProxy::Context::TryFilters(const Message& message) { | 80 bool ChannelProxy::Context::TryFilters(const Message& message) { |
86 #ifdef IPC_MESSAGE_LOG_ENABLED | 81 #ifdef IPC_MESSAGE_LOG_ENABLED |
87 Logging* logger = Logging::GetInstance(); | 82 Logging* logger = Logging::GetInstance(); |
88 if (logger->Enabled()) | 83 if (logger->Enabled()) |
89 logger->OnPreDispatchMessage(message); | 84 logger->OnPreDispatchMessage(message); |
90 #endif | 85 #endif |
91 | 86 |
92 for (size_t i = 0; i < filters_.size(); ++i) { | 87 for (size_t i = 0; i < filters_.size(); ++i) { |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 if (filters_[i].get() == filter) { | 219 if (filters_[i].get() == filter) { |
225 filter->OnFilterRemoved(); | 220 filter->OnFilterRemoved(); |
226 filters_.erase(filters_.begin() + i); | 221 filters_.erase(filters_.begin() + i); |
227 return; | 222 return; |
228 } | 223 } |
229 } | 224 } |
230 | 225 |
231 NOTREACHED() << "filter to be removed not found"; | 226 NOTREACHED() << "filter to be removed not found"; |
232 } | 227 } |
233 | 228 |
234 #if defined(OS_LINUX) | |
235 // Called on the IPC::Channel thread | |
236 void ChannelProxy::Context::OnOverridePeerPid(int32 peer_pid) { | |
237 if (channel_.get()) | |
238 channel_->OverridePeerPid(peer_pid); | |
239 } | |
240 #endif // defined(OS_LINUX) | |
241 | |
242 // Called on the listener's thread | 229 // Called on the listener's thread |
243 void ChannelProxy::Context::AddFilter(MessageFilter* filter) { | 230 void ChannelProxy::Context::AddFilter(MessageFilter* filter) { |
244 base::AutoLock auto_lock(pending_filters_lock_); | 231 base::AutoLock auto_lock(pending_filters_lock_); |
245 pending_filters_.push_back(make_scoped_refptr(filter)); | 232 pending_filters_.push_back(make_scoped_refptr(filter)); |
246 ipc_message_loop_->PostTask( | 233 ipc_message_loop_->PostTask( |
247 FROM_HERE, | 234 FROM_HERE, |
248 NewRunnableMethod(this, &Context::OnAddFilter)); | 235 NewRunnableMethod(this, &Context::OnAddFilter)); |
249 } | 236 } |
250 | 237 |
251 // Called on the listener's thread | 238 // Called on the listener's thread |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 void ChannelProxy::Context::OnDispatchError() { | 275 void ChannelProxy::Context::OnDispatchError() { |
289 if (listener_) | 276 if (listener_) |
290 listener_->OnChannelError(); | 277 listener_->OnChannelError(); |
291 } | 278 } |
292 | 279 |
293 //----------------------------------------------------------------------------- | 280 //----------------------------------------------------------------------------- |
294 | 281 |
295 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, | 282 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, |
296 Channel::Mode mode, | 283 Channel::Mode mode, |
297 Channel::Listener* listener, | 284 Channel::Listener* listener, |
298 base::MessageLoopProxy* ipc_thread, | 285 base::MessageLoopProxy* ipc_thread) |
299 bool needs_override_peer_pid) | |
300 : context_(new Context(listener, ipc_thread)), | 286 : context_(new Context(listener, ipc_thread)), |
301 outgoing_message_filter_(NULL) { | 287 outgoing_message_filter_(NULL) { |
302 Init(channel_handle, mode, ipc_thread, true, needs_override_peer_pid); | 288 Init(channel_handle, mode, ipc_thread, true); |
303 } | 289 } |
304 | 290 |
305 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, | 291 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, |
306 Channel::Mode mode, | 292 Channel::Mode mode, |
307 base::MessageLoopProxy* ipc_thread, | 293 base::MessageLoopProxy* ipc_thread, |
308 bool needs_override_peer_pid, | |
309 Context* context, | 294 Context* context, |
310 bool create_pipe_now) | 295 bool create_pipe_now) |
311 : context_(context), | 296 : context_(context), |
312 outgoing_message_filter_(NULL) { | 297 outgoing_message_filter_(NULL) { |
313 Init(channel_handle, mode, ipc_thread, create_pipe_now, | 298 Init(channel_handle, mode, ipc_thread, create_pipe_now); |
314 needs_override_peer_pid); | |
315 } | 299 } |
316 | 300 |
317 ChannelProxy::~ChannelProxy() { | 301 ChannelProxy::~ChannelProxy() { |
318 Close(); | 302 Close(); |
319 } | 303 } |
320 | 304 |
321 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, | 305 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, |
322 Channel::Mode mode, | 306 Channel::Mode mode, |
323 base::MessageLoopProxy* ipc_thread_loop, | 307 base::MessageLoopProxy* ipc_thread_loop, |
324 bool create_pipe_now, | 308 bool create_pipe_now) { |
325 bool needs_override_peer_pid) { | |
326 #if defined(OS_POSIX) | 309 #if defined(OS_POSIX) |
327 // When we are creating a server on POSIX, we need its file descriptor | 310 // When we are creating a server on POSIX, we need its file descriptor |
328 // to be created immediately so that it can be accessed and passed | 311 // to be created immediately so that it can be accessed and passed |
329 // to other processes. Forcing it to be created immediately avoids | 312 // to other processes. Forcing it to be created immediately avoids |
330 // race conditions that may otherwise arise. | 313 // race conditions that may otherwise arise. |
331 if (mode & Channel::MODE_SERVER_FLAG) { | 314 if (mode & Channel::MODE_SERVER_FLAG) { |
332 create_pipe_now = true; | 315 create_pipe_now = true; |
333 } | 316 } |
334 #endif // defined(OS_POSIX) | 317 #endif // defined(OS_POSIX) |
335 | 318 |
336 if (create_pipe_now) { | 319 if (create_pipe_now) { |
337 // Create the channel immediately. This effectively sets up the | 320 // Create the channel immediately. This effectively sets up the |
338 // low-level pipe so that the client can connect. Without creating | 321 // low-level pipe so that the client can connect. Without creating |
339 // the pipe immediately, it is possible for a listener to attempt | 322 // the pipe immediately, it is possible for a listener to attempt |
340 // to connect and get an error since the pipe doesn't exist yet. | 323 // to connect and get an error since the pipe doesn't exist yet. |
341 context_->CreateChannel(channel_handle, mode, needs_override_peer_pid); | 324 context_->CreateChannel(channel_handle, mode); |
342 } else { | 325 } else { |
343 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 326 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
344 context_.get(), &Context::CreateChannel, channel_handle, mode, | 327 context_.get(), &Context::CreateChannel, channel_handle, mode)); |
345 needs_override_peer_pid)); | |
346 } | 328 } |
347 | 329 |
348 // complete initialization on the background thread | 330 // complete initialization on the background thread |
349 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 331 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( |
350 context_.get(), &Context::OnChannelOpened)); | 332 context_.get(), &Context::OnChannelOpened)); |
351 } | 333 } |
352 | 334 |
353 void ChannelProxy::Close() { | 335 void ChannelProxy::Close() { |
354 // Clear the backpointer to the listener so that any pending calls to | 336 // Clear the backpointer to the listener so that any pending calls to |
355 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is | 337 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 } | 385 } |
404 | 386 |
405 bool ChannelProxy::GetClientEuid(uid_t* client_euid) const { | 387 bool ChannelProxy::GetClientEuid(uid_t* client_euid) const { |
406 Channel *channel = context_.get()->channel_.get(); | 388 Channel *channel = context_.get()->channel_.get(); |
407 // Channel must have been created first. | 389 // Channel must have been created first. |
408 DCHECK(channel) << context_.get()->channel_id_; | 390 DCHECK(channel) << context_.get()->channel_id_; |
409 return channel->GetClientEuid(client_euid); | 391 return channel->GetClientEuid(client_euid); |
410 } | 392 } |
411 #endif | 393 #endif |
412 | 394 |
413 #if defined(OS_LINUX) | |
414 void ChannelProxy::OverridePeerPid(int32 peer_pid) { | |
415 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | |
416 context_.get(), &Context::OnOverridePeerPid, peer_pid)); | |
417 } | |
418 #endif // defined(OS_LINUX) | |
419 | |
420 //----------------------------------------------------------------------------- | 395 //----------------------------------------------------------------------------- |
421 | 396 |
422 } // namespace IPC | 397 } // namespace IPC |
OLD | NEW |