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

Side by Side Diff: ipc/ipc_channel_proxy.cc

Issue 9968111: Make IPC channels track their peer process ID (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 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
OLDNEW
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/bind.h" 5 #include "base/bind.h"
6 #include "base/compiler_specific.h" 6 #include "base/compiler_specific.h"
7 #include "base/debug/trace_event.h" 7 #include "base/debug/trace_event.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 delete this; 59 delete this;
60 } 60 }
61 61
62 //------------------------------------------------------------------------------ 62 //------------------------------------------------------------------------------
63 63
64 ChannelProxy::Context::Context(Channel::Listener* listener, 64 ChannelProxy::Context::Context(Channel::Listener* listener,
65 base::MessageLoopProxy* ipc_message_loop) 65 base::MessageLoopProxy* ipc_message_loop)
66 : listener_message_loop_(base::MessageLoopProxy::current()), 66 : listener_message_loop_(base::MessageLoopProxy::current()),
67 listener_(listener), 67 listener_(listener),
68 ipc_message_loop_(ipc_message_loop), 68 ipc_message_loop_(ipc_message_loop),
69 peer_pid_(0),
70 channel_connected_called_(false) { 69 channel_connected_called_(false) {
71 } 70 }
72 71
73 ChannelProxy::Context::~Context() { 72 ChannelProxy::Context::~Context() {
74 } 73 }
75 74
76 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle, 75 void ChannelProxy::Context::CreateChannel(const IPC::ChannelHandle& handle,
77 const Channel::Mode& mode) { 76 const Channel::Mode& mode) {
78 DCHECK(channel_.get() == NULL); 77 DCHECK(channel_.get() == NULL);
79 channel_id_ = handle.name; 78 channel_id_ = handle.name;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 } 118 }
120 119
121 // Called on the IPC::Channel thread 120 // Called on the IPC::Channel thread
122 void ChannelProxy::Context::OnChannelConnected(int32 peer_pid) { 121 void ChannelProxy::Context::OnChannelConnected(int32 peer_pid) {
123 // Add any pending filters. This avoids a race condition where someone 122 // Add any pending filters. This avoids a race condition where someone
124 // creates a ChannelProxy, calls AddFilter, and then right after starts the 123 // creates a ChannelProxy, calls AddFilter, and then right after starts the
125 // peer process. The IO thread could receive a message before the task to add 124 // peer process. The IO thread could receive a message before the task to add
126 // the filter is run on the IO thread. 125 // the filter is run on the IO thread.
127 OnAddFilter(); 126 OnAddFilter();
128 127
129 peer_pid_ = peer_pid;
130 for (size_t i = 0; i < filters_.size(); ++i) 128 for (size_t i = 0; i < filters_.size(); ++i)
131 filters_[i]->OnChannelConnected(peer_pid); 129 filters_[i]->OnChannelConnected(peer_pid);
132 130
133 // See above comment about using listener_message_loop_ here. 131 // See above comment about using listener_message_loop_ here.
134 listener_message_loop_->PostTask( 132 listener_message_loop_->PostTask(
135 FROM_HERE, base::Bind(&Context::OnDispatchConnected, this)); 133 FROM_HERE, base::Bind(&Context::OnDispatchConnected, this));
136 } 134 }
137 135
138 // Called on the IPC::Channel thread 136 // Called on the IPC::Channel thread
139 void ChannelProxy::Context::OnChannelError() { 137 void ChannelProxy::Context::OnChannelError() {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 { 199 {
202 base::AutoLock auto_lock(pending_filters_lock_); 200 base::AutoLock auto_lock(pending_filters_lock_);
203 new_filters.swap(pending_filters_); 201 new_filters.swap(pending_filters_);
204 } 202 }
205 203
206 for (size_t i = 0; i < new_filters.size(); ++i) { 204 for (size_t i = 0; i < new_filters.size(); ++i) {
207 filters_.push_back(new_filters[i]); 205 filters_.push_back(new_filters[i]);
208 206
209 // If the channel has already been created, then we need to send this 207 // If the channel has already been created, then we need to send this
210 // message so that the filter gets access to the Channel. 208 // message so that the filter gets access to the Channel.
211 if (channel_.get()) 209 if (channel_.get()) {
212 new_filters[i]->OnFilterAdded(channel_.get()); 210 new_filters[i]->OnFilterAdded(channel_.get());
213 // Ditto for if the channel has been connected. 211 // Ditto for if the channel has been connected.
214 if (peer_pid_) 212 base::ProcessId peer_pid = channel_->peer_pid();
215 new_filters[i]->OnChannelConnected(peer_pid_); 213 if (peer_pid)
214 new_filters[i]->OnChannelConnected(peer_pid);
215 }
216 } 216 }
217 } 217 }
218 218
219 // Called on the IPC::Channel thread 219 // Called on the IPC::Channel thread
220 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { 220 void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) {
221 for (size_t i = 0; i < filters_.size(); ++i) { 221 for (size_t i = 0; i < filters_.size(); ++i) {
222 if (filters_[i].get() == filter) { 222 if (filters_[i].get() == filter) {
223 filter->OnFilterRemoved(); 223 filter->OnFilterRemoved();
224 filters_.erase(filters_.begin() + i); 224 filters_.erase(filters_.begin() + i);
225 return; 225 return;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 #endif 273 #endif
274 } 274 }
275 275
276 // Called on the listener's thread 276 // Called on the listener's thread
277 void ChannelProxy::Context::OnDispatchConnected() { 277 void ChannelProxy::Context::OnDispatchConnected() {
278 if (channel_connected_called_) 278 if (channel_connected_called_)
279 return; 279 return;
280 280
281 channel_connected_called_ = true; 281 channel_connected_called_ = true;
282 if (listener_) 282 if (listener_)
283 listener_->OnChannelConnected(peer_pid_); 283 listener_->OnChannelConnected(channel_->peer_pid());
284 } 284 }
285 285
286 // Called on the listener's thread 286 // Called on the listener's thread
287 void ChannelProxy::Context::OnDispatchError() { 287 void ChannelProxy::Context::OnDispatchError() {
288 if (listener_) 288 if (listener_)
289 listener_->OnChannelError(); 289 listener_->OnChannelError();
290 } 290 }
291 291
292 //----------------------------------------------------------------------------- 292 //-----------------------------------------------------------------------------
293 293
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 void ChannelProxy::RemoveFilter(MessageFilter* filter) { 379 void ChannelProxy::RemoveFilter(MessageFilter* filter) {
380 context_->ipc_message_loop()->PostTask( 380 context_->ipc_message_loop()->PostTask(
381 FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(), 381 FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(),
382 make_scoped_refptr(filter))); 382 make_scoped_refptr(filter)));
383 } 383 }
384 384
385 void ChannelProxy::ClearIPCMessageLoop() { 385 void ChannelProxy::ClearIPCMessageLoop() {
386 context()->ClearIPCMessageLoop(); 386 context()->ClearIPCMessageLoop();
387 } 387 }
388 388
389 base::ProcessId ChannelProxy::peer_pid() const {
jam 2012/04/04 19:05:00 ChannelProxy gets called on the listener thread, b
jschuh 2012/04/04 19:18:08 That is a subtlety I definitely did not appreciate
390 if (context_.get() && context_->channel_.get())
391 return context_->channel_->peer_pid();
392 return 0;
Tom Sepez 2012/04/04 19:07:07 return base::kNullProcessId constant? Any reason
jschuh 2012/04/04 21:05:13 Done.
393 }
394
389 #if defined(OS_POSIX) && !defined(OS_NACL) 395 #if defined(OS_POSIX) && !defined(OS_NACL)
390 // See the TODO regarding lazy initialization of the channel in 396 // See the TODO regarding lazy initialization of the channel in
391 // ChannelProxy::Init(). 397 // ChannelProxy::Init().
392 int ChannelProxy::GetClientFileDescriptor() { 398 int ChannelProxy::GetClientFileDescriptor() {
393 Channel* channel = context_.get()->channel_.get(); 399 Channel* channel = context_.get()->channel_.get();
394 // Channel must have been created first. 400 // Channel must have been created first.
395 DCHECK(channel) << context_.get()->channel_id_; 401 DCHECK(channel) << context_.get()->channel_id_;
396 return channel->GetClientFileDescriptor(); 402 return channel->GetClientFileDescriptor();
397 } 403 }
398 404
399 int ChannelProxy::TakeClientFileDescriptor() { 405 int ChannelProxy::TakeClientFileDescriptor() {
400 Channel* channel = context_.get()->channel_.get(); 406 Channel* channel = context_.get()->channel_.get();
401 // Channel must have been created first. 407 // Channel must have been created first.
402 DCHECK(channel) << context_.get()->channel_id_; 408 DCHECK(channel) << context_.get()->channel_id_;
403 return channel->TakeClientFileDescriptor(); 409 return channel->TakeClientFileDescriptor();
404 } 410 }
405 411
406 bool ChannelProxy::GetClientEuid(uid_t* client_euid) const { 412 bool ChannelProxy::GetClientEuid(uid_t* client_euid) const {
407 Channel* channel = context_.get()->channel_.get(); 413 Channel* channel = context_.get()->channel_.get();
408 // Channel must have been created first. 414 // Channel must have been created first.
409 DCHECK(channel) << context_.get()->channel_id_; 415 DCHECK(channel) << context_.get()->channel_id_;
410 return channel->GetClientEuid(client_euid); 416 return channel->GetClientEuid(client_euid);
411 } 417 }
412 #endif 418 #endif
413 419
414 //----------------------------------------------------------------------------- 420 //-----------------------------------------------------------------------------
415 421
416 } // namespace IPC 422 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698