| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) { | 311 void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) { |
| 312 if (listener_) | 312 if (listener_) |
| 313 listener_->OnBadMessageReceived(message); | 313 listener_->OnBadMessageReceived(message); |
| 314 } | 314 } |
| 315 | 315 |
| 316 void ChannelProxy::Context::ClearChannel() { | 316 void ChannelProxy::Context::ClearChannel() { |
| 317 base::AutoLock l(channel_lifetime_lock_); | 317 base::AutoLock l(channel_lifetime_lock_); |
| 318 channel_.reset(); | 318 channel_.reset(); |
| 319 } | 319 } |
| 320 | 320 |
| 321 void ChannelProxy::Context::SendFromThisThread(Message* message) { | 321 bool ChannelProxy::Context::SendFromThisThread(Message* message) { |
| 322 base::AutoLock l(channel_lifetime_lock_); | 322 base::AutoLock l(channel_lifetime_lock_); |
| 323 if (!channel_) | 323 if (!channel_) |
| 324 return; | 324 return false; |
| 325 DCHECK(channel_->IsSendThreadSafe()); | 325 DCHECK(channel_->IsSendThreadSafe()); |
| 326 channel_->Send(message); | 326 return channel_->Send(message); |
| 327 } | 327 } |
| 328 | 328 |
| 329 void ChannelProxy::Context::Send(Message* message) { | 329 bool ChannelProxy::Context::Send(Message* message, bool force_io_thread) { |
| 330 if (channel_send_thread_safe_) { | 330 if (channel_send_thread_safe_ && !force_io_thread) |
| 331 SendFromThisThread(message); | 331 return SendFromThisThread(message); |
| 332 return; | |
| 333 } | |
| 334 | 332 |
| 335 ipc_task_runner()->PostTask( | 333 ipc_task_runner()->PostTask( |
| 336 FROM_HERE, base::Bind(&ChannelProxy::Context::OnSendMessage, this, | 334 FROM_HERE, base::Bind(&ChannelProxy::Context::OnSendMessage, this, |
| 337 base::Passed(base::WrapUnique(message)))); | 335 base::Passed(base::WrapUnique(message)))); |
| 336 return true; |
| 338 } | 337 } |
| 339 | 338 |
| 340 bool ChannelProxy::Context::IsChannelSendThreadSafe() const { | 339 bool ChannelProxy::Context::IsChannelSendThreadSafe() const { |
| 341 return channel_send_thread_safe_; | 340 return channel_send_thread_safe_; |
| 342 } | 341 } |
| 343 | 342 |
| 344 //----------------------------------------------------------------------------- | 343 //----------------------------------------------------------------------------- |
| 345 | 344 |
| 346 // static | 345 // static |
| 347 std::unique_ptr<ChannelProxy> ChannelProxy::Create( | 346 std::unique_ptr<ChannelProxy> ChannelProxy::Create( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 370 : context_(context), | 369 : context_(context), |
| 371 did_init_(false) { | 370 did_init_(false) { |
| 372 #if defined(ENABLE_IPC_FUZZER) | 371 #if defined(ENABLE_IPC_FUZZER) |
| 373 outgoing_message_filter_ = NULL; | 372 outgoing_message_filter_ = NULL; |
| 374 #endif | 373 #endif |
| 375 } | 374 } |
| 376 | 375 |
| 377 ChannelProxy::ChannelProxy( | 376 ChannelProxy::ChannelProxy( |
| 378 Listener* listener, | 377 Listener* listener, |
| 379 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) | 378 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
| 380 : context_(new Context(listener, ipc_task_runner)), did_init_(false) { | 379 : ChannelProxy(new Context(listener, ipc_task_runner)) {} |
| 381 #if defined(ENABLE_IPC_FUZZER) | |
| 382 outgoing_message_filter_ = NULL; | |
| 383 #endif | |
| 384 } | |
| 385 | 380 |
| 386 ChannelProxy::~ChannelProxy() { | 381 ChannelProxy::~ChannelProxy() { |
| 387 DCHECK(CalledOnValidThread()); | 382 DCHECK(CalledOnValidThread()); |
| 388 | 383 |
| 389 Close(); | 384 Close(); |
| 390 } | 385 } |
| 391 | 386 |
| 392 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, | 387 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, |
| 393 Channel::Mode mode, | 388 Channel::Mode mode, |
| 394 bool create_pipe_now) { | 389 bool create_pipe_now) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 // possible that the channel could be closed while it is receiving messages! | 432 // possible that the channel could be closed while it is receiving messages! |
| 438 context_->Clear(); | 433 context_->Clear(); |
| 439 | 434 |
| 440 if (context_->ipc_task_runner()) { | 435 if (context_->ipc_task_runner()) { |
| 441 context_->ipc_task_runner()->PostTask( | 436 context_->ipc_task_runner()->PostTask( |
| 442 FROM_HERE, base::Bind(&Context::OnChannelClosed, context_.get())); | 437 FROM_HERE, base::Bind(&Context::OnChannelClosed, context_.get())); |
| 443 } | 438 } |
| 444 } | 439 } |
| 445 | 440 |
| 446 bool ChannelProxy::Send(Message* message) { | 441 bool ChannelProxy::Send(Message* message) { |
| 447 DCHECK(did_init_); | 442 return SendOnIPCThread(message); |
| 443 } |
| 448 | 444 |
| 449 // TODO(alexeypa): add DCHECK(CalledOnValidThread()) here. Currently there are | 445 bool ChannelProxy::SendNow(Message* message) { |
| 450 // tests that call Send() from a wrong thread. See http://crbug.com/163523. | 446 return SendImpl(message, false /* force_io_thread */); |
| 447 } |
| 451 | 448 |
| 452 #ifdef ENABLE_IPC_FUZZER | 449 bool ChannelProxy::SendOnIPCThread(Message* message) { |
| 453 // In IPC fuzzing builds, it is possible to define a filter to apply to | 450 return SendImpl(message, true /* force_io_thread */); |
| 454 // outgoing messages. It will either rewrite the message and return a new | |
| 455 // one, freeing the original, or return the message unchanged. | |
| 456 if (outgoing_message_filter()) | |
| 457 message = outgoing_message_filter()->Rewrite(message); | |
| 458 #endif | |
| 459 | |
| 460 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 461 Logging::GetInstance()->OnSendMessage(message, context_->channel_id()); | |
| 462 #endif | |
| 463 | |
| 464 context_->Send(message); | |
| 465 return true; | |
| 466 } | 451 } |
| 467 | 452 |
| 468 void ChannelProxy::AddFilter(MessageFilter* filter) { | 453 void ChannelProxy::AddFilter(MessageFilter* filter) { |
| 469 DCHECK(CalledOnValidThread()); | 454 DCHECK(CalledOnValidThread()); |
| 470 | 455 |
| 471 context_->AddFilter(filter); | 456 context_->AddFilter(filter); |
| 472 } | 457 } |
| 473 | 458 |
| 474 void ChannelProxy::RemoveFilter(MessageFilter* filter) { | 459 void ChannelProxy::RemoveFilter(MessageFilter* filter) { |
| 475 DCHECK(CalledOnValidThread()); | 460 DCHECK(CalledOnValidThread()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 Channel* channel = context_.get()->channel_.get(); | 497 Channel* channel = context_.get()->channel_.get(); |
| 513 // Channel must have been created first. | 498 // Channel must have been created first. |
| 514 DCHECK(channel) << context_.get()->channel_id_; | 499 DCHECK(channel) << context_.get()->channel_id_; |
| 515 return channel->TakeClientFileDescriptor(); | 500 return channel->TakeClientFileDescriptor(); |
| 516 } | 501 } |
| 517 #endif | 502 #endif |
| 518 | 503 |
| 519 void ChannelProxy::OnChannelInit() { | 504 void ChannelProxy::OnChannelInit() { |
| 520 } | 505 } |
| 521 | 506 |
| 507 bool ChannelProxy::SendImpl(Message* message, bool force_io_thread) { |
| 508 DCHECK(did_init_); |
| 509 |
| 510 // TODO(alexeypa): add DCHECK(CalledOnValidThread()) here. Currently there are |
| 511 // tests that call Send() from a wrong thread. See http://crbug.com/163523. |
| 512 |
| 513 #ifdef ENABLE_IPC_FUZZER |
| 514 // In IPC fuzzing builds, it is possible to define a filter to apply to |
| 515 // outgoing messages. It will either rewrite the message and return a new |
| 516 // one, freeing the original, or return the message unchanged. |
| 517 if (outgoing_message_filter()) |
| 518 message = outgoing_message_filter()->Rewrite(message); |
| 519 #endif |
| 520 |
| 521 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 522 Logging::GetInstance()->OnSendMessage(message, context_->channel_id()); |
| 523 #endif |
| 524 |
| 525 return context_->Send(message, force_io_thread); |
| 526 } |
| 527 |
| 522 //----------------------------------------------------------------------------- | 528 //----------------------------------------------------------------------------- |
| 523 | 529 |
| 524 } // namespace IPC | 530 } // namespace IPC |
| OLD | NEW |