| 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/bind.h" |
| 5 #include "base/location.h" | 6 #include "base/location.h" |
| 6 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 7 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 8 #include "ipc/ipc_channel_proxy.h" | 9 #include "ipc/ipc_channel_proxy.h" |
| 9 #include "ipc/ipc_logging.h" | 10 #include "ipc/ipc_logging.h" |
| 10 #include "ipc/ipc_message_utils.h" | 11 #include "ipc/ipc_message_utils.h" |
| 11 | 12 |
| 12 namespace IPC { | 13 namespace IPC { |
| 13 | 14 |
| 14 //------------------------------------------------------------------------------ | 15 //------------------------------------------------------------------------------ |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 OnMessageReceivedNoFilter(message); | 105 OnMessageReceivedNoFilter(message); |
| 105 return true; | 106 return true; |
| 106 } | 107 } |
| 107 | 108 |
| 108 // Called on the IPC::Channel thread | 109 // Called on the IPC::Channel thread |
| 109 bool ChannelProxy::Context::OnMessageReceivedNoFilter(const Message& message) { | 110 bool ChannelProxy::Context::OnMessageReceivedNoFilter(const Message& message) { |
| 110 // NOTE: This code relies on the listener's message loop not going away while | 111 // NOTE: This code relies on the listener's message loop not going away while |
| 111 // this thread is active. That should be a reasonable assumption, but it | 112 // this thread is active. That should be a reasonable assumption, but it |
| 112 // feels risky. We may want to invent some more indirect way of referring to | 113 // feels risky. We may want to invent some more indirect way of referring to |
| 113 // a MessageLoop if this becomes a problem. | 114 // a MessageLoop if this becomes a problem. |
| 114 listener_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 115 listener_message_loop_->PostTask( |
| 115 this, &Context::OnDispatchMessage, message)); | 116 FROM_HERE, base::Bind(&Context::OnDispatchMessage, this, message)); |
| 116 return true; | 117 return true; |
| 117 } | 118 } |
| 118 | 119 |
| 119 // Called on the IPC::Channel thread | 120 // Called on the IPC::Channel thread |
| 120 void ChannelProxy::Context::OnChannelConnected(int32 peer_pid) { | 121 void ChannelProxy::Context::OnChannelConnected(int32 peer_pid) { |
| 121 // Add any pending filters. This avoids a race condition where someone | 122 // Add any pending filters. This avoids a race condition where someone |
| 122 // creates a ChannelProxy, calls AddFilter, and then right after starts the | 123 // creates a ChannelProxy, calls AddFilter, and then right after starts the |
| 123 // 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 |
| 124 // the filter is run on the IO thread. | 125 // the filter is run on the IO thread. |
| 125 OnAddFilter(); | 126 OnAddFilter(); |
| 126 | 127 |
| 127 peer_pid_ = peer_pid; | 128 peer_pid_ = peer_pid; |
| 128 for (size_t i = 0; i < filters_.size(); ++i) | 129 for (size_t i = 0; i < filters_.size(); ++i) |
| 129 filters_[i]->OnChannelConnected(peer_pid); | 130 filters_[i]->OnChannelConnected(peer_pid); |
| 130 | 131 |
| 131 // See above comment about using listener_message_loop_ here. | 132 // See above comment about using listener_message_loop_ here. |
| 132 listener_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 133 listener_message_loop_->PostTask( |
| 133 this, &Context::OnDispatchConnected)); | 134 FROM_HERE, base::Bind(&Context::OnDispatchConnected, this)); |
| 134 } | 135 } |
| 135 | 136 |
| 136 // Called on the IPC::Channel thread | 137 // Called on the IPC::Channel thread |
| 137 void ChannelProxy::Context::OnChannelError() { | 138 void ChannelProxy::Context::OnChannelError() { |
| 138 for (size_t i = 0; i < filters_.size(); ++i) | 139 for (size_t i = 0; i < filters_.size(); ++i) |
| 139 filters_[i]->OnChannelError(); | 140 filters_[i]->OnChannelError(); |
| 140 | 141 |
| 141 // See above comment about using listener_message_loop_ here. | 142 // See above comment about using listener_message_loop_ here. |
| 142 listener_message_loop_->PostTask(FROM_HERE, NewRunnableMethod( | 143 listener_message_loop_->PostTask( |
| 143 this, &Context::OnDispatchError)); | 144 FROM_HERE, base::Bind(&Context::OnDispatchError, this)); |
| 144 } | 145 } |
| 145 | 146 |
| 146 // Called on the IPC::Channel thread | 147 // Called on the IPC::Channel thread |
| 147 void ChannelProxy::Context::OnChannelOpened() { | 148 void ChannelProxy::Context::OnChannelOpened() { |
| 148 DCHECK(channel_ != NULL); | 149 DCHECK(channel_ != NULL); |
| 149 | 150 |
| 150 // Assume a reference to ourselves on behalf of this thread. This reference | 151 // Assume a reference to ourselves on behalf of this thread. This reference |
| 151 // will be released when we are closed. | 152 // will be released when we are closed. |
| 152 AddRef(); | 153 AddRef(); |
| 153 | 154 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 } | 226 } |
| 226 | 227 |
| 227 NOTREACHED() << "filter to be removed not found"; | 228 NOTREACHED() << "filter to be removed not found"; |
| 228 } | 229 } |
| 229 | 230 |
| 230 // Called on the listener's thread | 231 // Called on the listener's thread |
| 231 void ChannelProxy::Context::AddFilter(MessageFilter* filter) { | 232 void ChannelProxy::Context::AddFilter(MessageFilter* filter) { |
| 232 base::AutoLock auto_lock(pending_filters_lock_); | 233 base::AutoLock auto_lock(pending_filters_lock_); |
| 233 pending_filters_.push_back(make_scoped_refptr(filter)); | 234 pending_filters_.push_back(make_scoped_refptr(filter)); |
| 234 ipc_message_loop_->PostTask( | 235 ipc_message_loop_->PostTask( |
| 235 FROM_HERE, | 236 FROM_HERE, base::Bind(&Context::OnAddFilter, this)); |
| 236 NewRunnableMethod(this, &Context::OnAddFilter)); | |
| 237 } | 237 } |
| 238 | 238 |
| 239 // Called on the listener's thread | 239 // Called on the listener's thread |
| 240 void ChannelProxy::Context::OnDispatchMessage(const Message& message) { | 240 void ChannelProxy::Context::OnDispatchMessage(const Message& message) { |
| 241 if (!listener_) | 241 if (!listener_) |
| 242 return; | 242 return; |
| 243 | 243 |
| 244 OnDispatchConnected(); | 244 OnDispatchConnected(); |
| 245 | 245 |
| 246 #ifdef IPC_MESSAGE_LOG_ENABLED | 246 #ifdef IPC_MESSAGE_LOG_ENABLED |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 } | 317 } |
| 318 #endif // defined(OS_POSIX) | 318 #endif // defined(OS_POSIX) |
| 319 | 319 |
| 320 if (create_pipe_now) { | 320 if (create_pipe_now) { |
| 321 // Create the channel immediately. This effectively sets up the | 321 // Create the channel immediately. This effectively sets up the |
| 322 // low-level pipe so that the client can connect. Without creating | 322 // low-level pipe so that the client can connect. Without creating |
| 323 // the pipe immediately, it is possible for a listener to attempt | 323 // the pipe immediately, it is possible for a listener to attempt |
| 324 // to connect and get an error since the pipe doesn't exist yet. | 324 // to connect and get an error since the pipe doesn't exist yet. |
| 325 context_->CreateChannel(channel_handle, mode); | 325 context_->CreateChannel(channel_handle, mode); |
| 326 } else { | 326 } else { |
| 327 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 327 context_->ipc_message_loop()->PostTask( |
| 328 context_.get(), &Context::CreateChannel, channel_handle, mode)); | 328 FROM_HERE, base::Bind(&Context::CreateChannel, context_.get(), |
| 329 channel_handle, mode)); |
| 329 } | 330 } |
| 330 | 331 |
| 331 // complete initialization on the background thread | 332 // complete initialization on the background thread |
| 332 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 333 context_->ipc_message_loop()->PostTask( |
| 333 context_.get(), &Context::OnChannelOpened)); | 334 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); |
| 334 } | 335 } |
| 335 | 336 |
| 336 void ChannelProxy::Close() { | 337 void ChannelProxy::Close() { |
| 337 // Clear the backpointer to the listener so that any pending calls to | 338 // Clear the backpointer to the listener so that any pending calls to |
| 338 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is | 339 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is |
| 339 // possible that the channel could be closed while it is receiving messages! | 340 // possible that the channel could be closed while it is receiving messages! |
| 340 context_->Clear(); | 341 context_->Clear(); |
| 341 | 342 |
| 342 if (context_->ipc_message_loop()) { | 343 if (context_->ipc_message_loop()) { |
| 343 context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | 344 context_->ipc_message_loop()->PostTask( |
| 344 context_.get(), &Context::OnChannelClosed)); | 345 FROM_HERE, base::Bind(&Context::OnChannelClosed, context_.get())); |
| 345 } | 346 } |
| 346 } | 347 } |
| 347 | 348 |
| 348 bool ChannelProxy::Send(Message* message) { | 349 bool ChannelProxy::Send(Message* message) { |
| 349 if (outgoing_message_filter()) | 350 if (outgoing_message_filter()) |
| 350 message = outgoing_message_filter()->Rewrite(message); | 351 message = outgoing_message_filter()->Rewrite(message); |
| 351 | 352 |
| 352 #ifdef IPC_MESSAGE_LOG_ENABLED | 353 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 353 Logging::GetInstance()->OnSendMessage(message, context_->channel_id()); | 354 Logging::GetInstance()->OnSendMessage(message, context_->channel_id()); |
| 354 #endif | 355 #endif |
| 355 | 356 |
| 356 context_->ipc_message_loop()->PostTask(FROM_HERE, | 357 context_->ipc_message_loop()->PostTask(FROM_HERE, |
| 357 new SendTask(context_.get(), message)); | 358 new SendTask(context_.get(), message)); |
| 358 return true; | 359 return true; |
| 359 } | 360 } |
| 360 | 361 |
| 361 void ChannelProxy::AddFilter(MessageFilter* filter) { | 362 void ChannelProxy::AddFilter(MessageFilter* filter) { |
| 362 context_->AddFilter(filter); | 363 context_->AddFilter(filter); |
| 363 } | 364 } |
| 364 | 365 |
| 365 void ChannelProxy::RemoveFilter(MessageFilter* filter) { | 366 void ChannelProxy::RemoveFilter(MessageFilter* filter) { |
| 366 context_->ipc_message_loop()->PostTask( | 367 context_->ipc_message_loop()->PostTask( |
| 367 FROM_HERE, NewRunnableMethod( | 368 FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(), |
| 368 context_.get(), | 369 make_scoped_refptr(filter))); |
| 369 &Context::OnRemoveFilter, | |
| 370 make_scoped_refptr(filter))); | |
| 371 } | 370 } |
| 372 | 371 |
| 373 void ChannelProxy::ClearIPCMessageLoop() { | 372 void ChannelProxy::ClearIPCMessageLoop() { |
| 374 context()->ClearIPCMessageLoop(); | 373 context()->ClearIPCMessageLoop(); |
| 375 } | 374 } |
| 376 | 375 |
| 377 #if defined(OS_POSIX) && !defined(OS_NACL) | 376 #if defined(OS_POSIX) && !defined(OS_NACL) |
| 378 // See the TODO regarding lazy initialization of the channel in | 377 // See the TODO regarding lazy initialization of the channel in |
| 379 // ChannelProxy::Init(). | 378 // ChannelProxy::Init(). |
| 380 int ChannelProxy::GetClientFileDescriptor() { | 379 int ChannelProxy::GetClientFileDescriptor() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 395 Channel* channel = context_.get()->channel_.get(); | 394 Channel* channel = context_.get()->channel_.get(); |
| 396 // Channel must have been created first. | 395 // Channel must have been created first. |
| 397 DCHECK(channel) << context_.get()->channel_id_; | 396 DCHECK(channel) << context_.get()->channel_id_; |
| 398 return channel->GetClientEuid(client_euid); | 397 return channel->GetClientEuid(client_euid); |
| 399 } | 398 } |
| 400 #endif | 399 #endif |
| 401 | 400 |
| 402 //----------------------------------------------------------------------------- | 401 //----------------------------------------------------------------------------- |
| 403 | 402 |
| 404 } // namespace IPC | 403 } // namespace IPC |
| OLD | NEW |