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