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 ChannelProxy::ChannelProxy(const IPC::ChannelHandle& channel_handle, | 307 // static |
308 Channel::Mode mode, | 308 scoped_ptr<ChannelProxy> ChannelProxy::Create( |
309 Listener* listener, | 309 Listener* listener, |
310 base::SingleThreadTaskRunner* ipc_task_runner) | 310 base::SingleThreadTaskRunner* ipc_task_runner) { |
311 : context_(new Context(listener, ipc_task_runner)), | 311 return make_scoped_ptr(new ChannelProxy( |
312 did_init_(false) { | 312 listener, ipc_task_runner)); |
313 Init(channel_handle, mode, true); | 313 } |
| 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(); |
314 } | 357 } |
315 | 358 |
316 ChannelProxy::ChannelProxy(Context* context) | 359 ChannelProxy::ChannelProxy(Context* context) |
317 : context_(context), | 360 : context_(context), |
318 did_init_(false) { | 361 did_init_(false) { |
319 } | 362 } |
320 | 363 |
| 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 |
321 ChannelProxy::~ChannelProxy() { | 370 ChannelProxy::~ChannelProxy() { |
322 DCHECK(CalledOnValidThread()); | 371 DCHECK(CalledOnValidThread()); |
323 | 372 |
324 Close(); | 373 Close(); |
325 } | 374 } |
326 | 375 |
327 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, | 376 void ChannelProxy::InitByMode( |
328 Channel::Mode mode, | 377 const IPC::ChannelHandle& channel_handle, |
329 bool create_pipe_now) { | 378 Channel::Mode mode, |
| 379 bool create_pipe_now) { |
330 DCHECK(CalledOnValidThread()); | 380 DCHECK(CalledOnValidThread()); |
331 DCHECK(!did_init_); | 381 DCHECK(!did_init_); |
332 #if defined(OS_POSIX) | 382 #if defined(OS_POSIX) |
333 // When we are creating a server on POSIX, we need its file descriptor | 383 // When we are creating a server on POSIX, we need its file descriptor |
334 // to be created immediately so that it can be accessed and passed | 384 // to be created immediately so that it can be accessed and passed |
335 // to other processes. Forcing it to be created immediately avoids | 385 // to other processes. Forcing it to be created immediately avoids |
336 // race conditions that may otherwise arise. | 386 // race conditions that may otherwise arise. |
337 if (mode & Channel::MODE_SERVER_FLAG) { | 387 if (mode & Channel::MODE_SERVER_FLAG) { |
338 create_pipe_now = true; | 388 create_pipe_now = true; |
339 } | 389 } |
(...skipping 11 matching lines...) Expand all Loading... |
351 channel_handle, mode)); | 401 channel_handle, mode)); |
352 } | 402 } |
353 | 403 |
354 // complete initialization on the background thread | 404 // complete initialization on the background thread |
355 context_->ipc_task_runner()->PostTask( | 405 context_->ipc_task_runner()->PostTask( |
356 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); | 406 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); |
357 | 407 |
358 did_init_ = true; | 408 did_init_ = true; |
359 } | 409 } |
360 | 410 |
| 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 |
361 void ChannelProxy::Close() { | 431 void ChannelProxy::Close() { |
362 DCHECK(CalledOnValidThread()); | 432 DCHECK(CalledOnValidThread()); |
363 | 433 |
364 // Clear the backpointer to the listener so that any pending calls to | 434 // Clear the backpointer to the listener so that any pending calls to |
365 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is | 435 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is |
366 // possible that the channel could be closed while it is receiving messages! | 436 // possible that the channel could be closed while it is receiving messages! |
367 context_->Clear(); | 437 context_->Clear(); |
368 | 438 |
369 if (context_->ipc_task_runner()) { | 439 if (context_->ipc_task_runner()) { |
370 context_->ipc_task_runner()->PostTask( | 440 context_->ipc_task_runner()->PostTask( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 Channel* channel = context_.get()->channel_.get(); | 506 Channel* channel = context_.get()->channel_.get(); |
437 // Channel must have been created first. | 507 // Channel must have been created first. |
438 DCHECK(channel) << context_.get()->channel_id_; | 508 DCHECK(channel) << context_.get()->channel_id_; |
439 return channel->GetPeerEuid(peer_euid); | 509 return channel->GetPeerEuid(peer_euid); |
440 } | 510 } |
441 #endif | 511 #endif |
442 | 512 |
443 //----------------------------------------------------------------------------- | 513 //----------------------------------------------------------------------------- |
444 | 514 |
445 } // namespace IPC | 515 } // namespace IPC |
OLD | NEW |