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