OLD | NEW |
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 #include "ipc/ipc_channel_proxy.h" | 5 #include "ipc/ipc_channel_proxy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 } | 297 } |
298 | 298 |
299 // Called on the listener's thread | 299 // Called on the listener's thread |
300 void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) { | 300 void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) { |
301 if (listener_) | 301 if (listener_) |
302 listener_->OnBadMessageReceived(message); | 302 listener_->OnBadMessageReceived(message); |
303 } | 303 } |
304 | 304 |
305 //----------------------------------------------------------------------------- | 305 //----------------------------------------------------------------------------- |
306 | 306 |
307 // static | 307 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, |
308 scoped_ptr<ChannelProxy> ChannelProxy::Create( | 308 Channel::Mode mode, |
309 Listener* listener, | 309 Listener* listener, |
310 base::SingleThreadTaskRunner* ipc_task_runner) { | 310 base::SingleThreadTaskRunner* ipc_task_runner) |
311 return make_scoped_ptr(new ChannelProxy( | 311 : context_(new Context(listener, ipc_task_runner)), |
312 listener, ipc_task_runner)); | 312 did_init_(false) { |
313 } | 313 Init(channel_handle, mode, true); |
314 | |
315 // static | |
316 scoped_ptr<ChannelProxy> ChannelProxy::CreateClient( | |
317 const IPC::ChannelHandle& channel_handle, | |
318 Listener* listener, | |
319 base::SingleThreadTaskRunner* ipc_task_runner) { | |
320 scoped_ptr<ChannelProxy> channel = Create( | |
321 listener, ipc_task_runner); | |
322 channel->InitClient(channel_handle, true); | |
323 return channel.Pass(); | |
324 } | |
325 | |
326 // static | |
327 scoped_ptr<ChannelProxy> ChannelProxy::CreateServer( | |
328 const IPC::ChannelHandle& channel_handle, | |
329 Listener* listener, | |
330 base::SingleThreadTaskRunner* ipc_task_runner) { | |
331 scoped_ptr<ChannelProxy> channel = Create( | |
332 listener, ipc_task_runner); | |
333 channel->InitServer(channel_handle, true); | |
334 return channel.Pass(); | |
335 } | |
336 | |
337 // static | |
338 scoped_ptr<ChannelProxy> ChannelProxy::CreateNamedClient( | |
339 const IPC::ChannelHandle& channel_handle, | |
340 Listener* listener, | |
341 base::SingleThreadTaskRunner* ipc_task_runner) { | |
342 scoped_ptr<ChannelProxy> channel = Create( | |
343 listener, ipc_task_runner); | |
344 channel->InitNamedClient(channel_handle, true); | |
345 return channel.Pass(); | |
346 } | |
347 | |
348 // static | |
349 scoped_ptr<ChannelProxy> ChannelProxy::CreateNamedServer( | |
350 const IPC::ChannelHandle& channel_handle, | |
351 Listener* listener, | |
352 base::SingleThreadTaskRunner* ipc_task_runner) { | |
353 scoped_ptr<ChannelProxy> channel = Create( | |
354 listener, ipc_task_runner); | |
355 channel->InitNamedServer(channel_handle, true); | |
356 return channel.Pass(); | |
357 } | 314 } |
358 | 315 |
359 ChannelProxy::ChannelProxy(Context* context) | 316 ChannelProxy::ChannelProxy(Context* context) |
360 : context_(context), | 317 : context_(context), |
361 did_init_(false) { | 318 did_init_(false) { |
362 } | 319 } |
363 | 320 |
364 ChannelProxy::ChannelProxy(Listener* listener, | |
365 base::SingleThreadTaskRunner* ipc_task_runner) | |
366 : context_(new Context(listener, ipc_task_runner)), | |
367 did_init_(false) { | |
368 } | |
369 | |
370 ChannelProxy::~ChannelProxy() { | 321 ChannelProxy::~ChannelProxy() { |
371 DCHECK(CalledOnValidThread()); | 322 DCHECK(CalledOnValidThread()); |
372 | 323 |
373 Close(); | 324 Close(); |
374 } | 325 } |
375 | 326 |
376 void ChannelProxy::InitByMode( | 327 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, |
377 const IPC::ChannelHandle& channel_handle, | 328 Channel::Mode mode, |
378 Channel::Mode mode, | 329 bool create_pipe_now) { |
379 bool create_pipe_now) { | |
380 DCHECK(CalledOnValidThread()); | 330 DCHECK(CalledOnValidThread()); |
381 DCHECK(!did_init_); | 331 DCHECK(!did_init_); |
382 #if defined(OS_POSIX) | 332 #if defined(OS_POSIX) |
383 // When we are creating a server on POSIX, we need its file descriptor | 333 // When we are creating a server on POSIX, we need its file descriptor |
384 // to be created immediately so that it can be accessed and passed | 334 // to be created immediately so that it can be accessed and passed |
385 // to other processes. Forcing it to be created immediately avoids | 335 // to other processes. Forcing it to be created immediately avoids |
386 // race conditions that may otherwise arise. | 336 // race conditions that may otherwise arise. |
387 if (mode & Channel::MODE_SERVER_FLAG) { | 337 if (mode & Channel::MODE_SERVER_FLAG) { |
388 create_pipe_now = true; | 338 create_pipe_now = true; |
389 } | 339 } |
(...skipping 11 matching lines...) Expand all Loading... |
401 channel_handle, mode)); | 351 channel_handle, mode)); |
402 } | 352 } |
403 | 353 |
404 // complete initialization on the background thread | 354 // complete initialization on the background thread |
405 context_->ipc_task_runner()->PostTask( | 355 context_->ipc_task_runner()->PostTask( |
406 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); | 356 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); |
407 | 357 |
408 did_init_ = true; | 358 did_init_ = true; |
409 } | 359 } |
410 | 360 |
411 void ChannelProxy::InitClient(const IPC::ChannelHandle& channel_handle, | |
412 bool create_pipe_now) { | |
413 InitByMode(channel_handle, Channel::MODE_CLIENT, create_pipe_now); | |
414 } | |
415 | |
416 void ChannelProxy::InitServer(const IPC::ChannelHandle& channel_handle, | |
417 bool create_pipe_now) { | |
418 InitByMode(channel_handle, Channel::MODE_SERVER, create_pipe_now); | |
419 } | |
420 | |
421 void ChannelProxy::InitNamedClient(const IPC::ChannelHandle& channel_handle, | |
422 bool create_pipe_now) { | |
423 InitByMode(channel_handle, Channel::MODE_NAMED_CLIENT, create_pipe_now); | |
424 } | |
425 | |
426 void ChannelProxy::InitNamedServer(const IPC::ChannelHandle& channel_handle, | |
427 bool create_pipe_now) { | |
428 InitByMode(channel_handle, Channel::MODE_NAMED_SERVER, create_pipe_now); | |
429 } | |
430 | |
431 void ChannelProxy::Close() { | 361 void ChannelProxy::Close() { |
432 DCHECK(CalledOnValidThread()); | 362 DCHECK(CalledOnValidThread()); |
433 | 363 |
434 // Clear the backpointer to the listener so that any pending calls to | 364 // Clear the backpointer to the listener so that any pending calls to |
435 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is | 365 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is |
436 // possible that the channel could be closed while it is receiving messages! | 366 // possible that the channel could be closed while it is receiving messages! |
437 context_->Clear(); | 367 context_->Clear(); |
438 | 368 |
439 if (context_->ipc_task_runner()) { | 369 if (context_->ipc_task_runner()) { |
440 context_->ipc_task_runner()->PostTask( | 370 context_->ipc_task_runner()->PostTask( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 Channel* channel = context_.get()->channel_.get(); | 436 Channel* channel = context_.get()->channel_.get(); |
507 // Channel must have been created first. | 437 // Channel must have been created first. |
508 DCHECK(channel) << context_.get()->channel_id_; | 438 DCHECK(channel) << context_.get()->channel_id_; |
509 return channel->GetPeerEuid(peer_euid); | 439 return channel->GetPeerEuid(peer_euid); |
510 } | 440 } |
511 #endif | 441 #endif |
512 | 442 |
513 //----------------------------------------------------------------------------- | 443 //----------------------------------------------------------------------------- |
514 | 444 |
515 } // namespace IPC | 445 } // namespace IPC |
OLD | NEW |