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/bind.h" |
6 #include "base/location.h" | 6 #include "base/location.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "ipc/ipc_channel_proxy.h" | 9 #include "ipc/ipc_channel_proxy.h" |
10 #include "ipc/ipc_logging.h" | 10 #include "ipc/ipc_logging.h" |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 listener_->OnChannelError(); | 278 listener_->OnChannelError(); |
279 } | 279 } |
280 | 280 |
281 //----------------------------------------------------------------------------- | 281 //----------------------------------------------------------------------------- |
282 | 282 |
283 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, | 283 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, |
284 Channel::Mode mode, | 284 Channel::Mode mode, |
285 Channel::Listener* listener, | 285 Channel::Listener* listener, |
286 base::MessageLoopProxy* ipc_thread) | 286 base::MessageLoopProxy* ipc_thread) |
287 : context_(new Context(listener, ipc_thread)), | 287 : context_(new Context(listener, ipc_thread)), |
288 outgoing_message_filter_(NULL) { | 288 outgoing_message_filter_(NULL), |
289 Init(channel_handle, mode, ipc_thread, true); | 289 did_init_(false) { |
| 290 Init(channel_handle, mode, true); |
290 } | 291 } |
291 | 292 |
292 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, | 293 ChannelProxy::ChannelProxy(Context* context) |
293 Channel::Mode mode, | |
294 base::MessageLoopProxy* ipc_thread, | |
295 Context* context, | |
296 bool create_pipe_now) | |
297 : context_(context), | 294 : context_(context), |
298 outgoing_message_filter_(NULL) { | 295 outgoing_message_filter_(NULL), |
299 Init(channel_handle, mode, ipc_thread, create_pipe_now); | 296 did_init_(false) { |
300 } | 297 } |
301 | 298 |
302 ChannelProxy::~ChannelProxy() { | 299 ChannelProxy::~ChannelProxy() { |
303 Close(); | 300 Close(); |
304 } | 301 } |
305 | 302 |
306 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, | 303 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, |
307 Channel::Mode mode, | 304 Channel::Mode mode, |
308 base::MessageLoopProxy* ipc_thread_loop, | |
309 bool create_pipe_now) { | 305 bool create_pipe_now) { |
| 306 DCHECK(!did_init_); |
310 #if defined(OS_POSIX) | 307 #if defined(OS_POSIX) |
311 // When we are creating a server on POSIX, we need its file descriptor | 308 // When we are creating a server on POSIX, we need its file descriptor |
312 // to be created immediately so that it can be accessed and passed | 309 // to be created immediately so that it can be accessed and passed |
313 // to other processes. Forcing it to be created immediately avoids | 310 // to other processes. Forcing it to be created immediately avoids |
314 // race conditions that may otherwise arise. | 311 // race conditions that may otherwise arise. |
315 if (mode & Channel::MODE_SERVER_FLAG) { | 312 if (mode & Channel::MODE_SERVER_FLAG) { |
316 create_pipe_now = true; | 313 create_pipe_now = true; |
317 } | 314 } |
318 #endif // defined(OS_POSIX) | 315 #endif // defined(OS_POSIX) |
319 | 316 |
320 if (create_pipe_now) { | 317 if (create_pipe_now) { |
321 // Create the channel immediately. This effectively sets up the | 318 // Create the channel immediately. This effectively sets up the |
322 // low-level pipe so that the client can connect. Without creating | 319 // low-level pipe so that the client can connect. Without creating |
323 // the pipe immediately, it is possible for a listener to attempt | 320 // 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. | 321 // to connect and get an error since the pipe doesn't exist yet. |
325 context_->CreateChannel(channel_handle, mode); | 322 context_->CreateChannel(channel_handle, mode); |
326 } else { | 323 } else { |
327 context_->ipc_message_loop()->PostTask( | 324 context_->ipc_message_loop()->PostTask( |
328 FROM_HERE, base::Bind(&Context::CreateChannel, context_.get(), | 325 FROM_HERE, base::Bind(&Context::CreateChannel, context_.get(), |
329 channel_handle, mode)); | 326 channel_handle, mode)); |
330 } | 327 } |
331 | 328 |
332 // complete initialization on the background thread | 329 // complete initialization on the background thread |
333 context_->ipc_message_loop()->PostTask( | 330 context_->ipc_message_loop()->PostTask( |
334 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); | 331 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); |
| 332 |
| 333 did_init_ = true; |
335 } | 334 } |
336 | 335 |
337 void ChannelProxy::Close() { | 336 void ChannelProxy::Close() { |
338 // Clear the backpointer to the listener so that any pending calls to | 337 // Clear the backpointer to the listener so that any pending calls to |
339 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is | 338 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is |
340 // possible that the channel could be closed while it is receiving messages! | 339 // possible that the channel could be closed while it is receiving messages! |
341 context_->Clear(); | 340 context_->Clear(); |
342 | 341 |
343 if (context_->ipc_message_loop()) { | 342 if (context_->ipc_message_loop()) { |
344 context_->ipc_message_loop()->PostTask( | 343 context_->ipc_message_loop()->PostTask( |
345 FROM_HERE, base::Bind(&Context::OnChannelClosed, context_.get())); | 344 FROM_HERE, base::Bind(&Context::OnChannelClosed, context_.get())); |
346 } | 345 } |
347 } | 346 } |
348 | 347 |
349 bool ChannelProxy::Send(Message* message) { | 348 bool ChannelProxy::Send(Message* message) { |
| 349 DCHECK(did_init_); |
350 if (outgoing_message_filter()) | 350 if (outgoing_message_filter()) |
351 message = outgoing_message_filter()->Rewrite(message); | 351 message = outgoing_message_filter()->Rewrite(message); |
352 | 352 |
353 #ifdef IPC_MESSAGE_LOG_ENABLED | 353 #ifdef IPC_MESSAGE_LOG_ENABLED |
354 Logging::GetInstance()->OnSendMessage(message, context_->channel_id()); | 354 Logging::GetInstance()->OnSendMessage(message, context_->channel_id()); |
355 #endif | 355 #endif |
356 | 356 |
357 context_->ipc_message_loop()->PostTask(FROM_HERE, | 357 context_->ipc_message_loop()->PostTask(FROM_HERE, |
358 new SendTask(context_.get(), message)); | 358 new SendTask(context_.get(), message)); |
359 return true; | 359 return true; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 Channel* channel = context_.get()->channel_.get(); | 394 Channel* channel = context_.get()->channel_.get(); |
395 // Channel must have been created first. | 395 // Channel must have been created first. |
396 DCHECK(channel) << context_.get()->channel_id_; | 396 DCHECK(channel) << context_.get()->channel_id_; |
397 return channel->GetClientEuid(client_euid); | 397 return channel->GetClientEuid(client_euid); |
398 } | 398 } |
399 #endif | 399 #endif |
400 | 400 |
401 //----------------------------------------------------------------------------- | 401 //----------------------------------------------------------------------------- |
402 | 402 |
403 } // namespace IPC | 403 } // namespace IPC |
OLD | NEW |