| 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 72 |
| 73 void ChannelProxy::Context::CreateChannel( | 73 void ChannelProxy::Context::CreateChannel( |
| 74 std::unique_ptr<ChannelFactory> factory) { | 74 std::unique_ptr<ChannelFactory> factory) { |
| 75 base::AutoLock l(channel_lifetime_lock_); | 75 base::AutoLock l(channel_lifetime_lock_); |
| 76 DCHECK(!channel_); | 76 DCHECK(!channel_); |
| 77 DCHECK_EQ(factory->GetIPCTaskRunner(), ipc_task_runner_); | 77 DCHECK_EQ(factory->GetIPCTaskRunner(), ipc_task_runner_); |
| 78 channel_id_ = factory->GetName(); | 78 channel_id_ = factory->GetName(); |
| 79 channel_ = factory->BuildChannel(this); | 79 channel_ = factory->BuildChannel(this); |
| 80 channel_send_thread_safe_ = channel_->IsSendThreadSafe(); | 80 channel_send_thread_safe_ = channel_->IsSendThreadSafe(); |
| 81 channel_->SetAttachmentBrokerEndpoint(attachment_broker_endpoint_); | 81 channel_->SetAttachmentBrokerEndpoint(attachment_broker_endpoint_); |
| 82 | |
| 83 Channel::AssociatedInterfaceSupport* support = | |
| 84 channel_->GetAssociatedInterfaceSupport(); | |
| 85 if (support) { | |
| 86 associated_group_ = *support->GetAssociatedGroup(); | |
| 87 | |
| 88 base::AutoLock l(pending_filters_lock_); | |
| 89 for (auto& entry : pending_interfaces_) | |
| 90 support->AddGenericAssociatedInterface(entry.first, entry.second); | |
| 91 pending_interfaces_.clear(); | |
| 92 } | |
| 93 } | 82 } |
| 94 | 83 |
| 95 bool ChannelProxy::Context::TryFilters(const Message& message) { | 84 bool ChannelProxy::Context::TryFilters(const Message& message) { |
| 96 DCHECK(message_filter_router_); | 85 DCHECK(message_filter_router_); |
| 97 #ifdef IPC_MESSAGE_LOG_ENABLED | 86 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 98 Logging* logger = Logging::GetInstance(); | 87 Logging* logger = Logging::GetInstance(); |
| 99 if (logger->Enabled()) | 88 if (logger->Enabled()) |
| 100 logger->OnPreDispatchMessage(message); | 89 logger->OnPreDispatchMessage(message); |
| 101 #endif | 90 #endif |
| 102 | 91 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // will be released when we are closed. | 155 // will be released when we are closed. |
| 167 AddRef(); | 156 AddRef(); |
| 168 | 157 |
| 169 if (!channel_->Connect()) { | 158 if (!channel_->Connect()) { |
| 170 OnChannelError(); | 159 OnChannelError(); |
| 171 return; | 160 return; |
| 172 } | 161 } |
| 173 | 162 |
| 174 for (size_t i = 0; i < filters_.size(); ++i) | 163 for (size_t i = 0; i < filters_.size(); ++i) |
| 175 filters_[i]->OnFilterAdded(channel_.get()); | 164 filters_[i]->OnFilterAdded(channel_.get()); |
| 165 |
| 166 Channel::AssociatedInterfaceSupport* support = |
| 167 channel_->GetAssociatedInterfaceSupport(); |
| 168 if (support) { |
| 169 support->SetProxyTaskRunner(listener_task_runner_); |
| 170 for (auto& entry : io_thread_interfaces_) |
| 171 support->AddGenericAssociatedInterface(entry.first, entry.second); |
| 172 for (auto& entry : proxy_thread_interfaces_) { |
| 173 support->AddGenericAssociatedInterface( |
| 174 entry.first, base::Bind(&BindAssociatedInterfaceOnTaskRunner, |
| 175 listener_task_runner_, entry.second)); |
| 176 } |
| 177 } else { |
| 178 // Sanity check to ensure nobody's expecting to use associated interfaces on |
| 179 // a Channel that doesn't support them. |
| 180 DCHECK(io_thread_interfaces_.empty() && proxy_thread_interfaces_.empty()); |
| 181 } |
| 176 } | 182 } |
| 177 | 183 |
| 178 // Called on the IPC::Channel thread | 184 // Called on the IPC::Channel thread |
| 179 void ChannelProxy::Context::OnChannelClosed() { | 185 void ChannelProxy::Context::OnChannelClosed() { |
| 180 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. | 186 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. |
| 181 tracked_objects::ScopedTracker tracking_profile( | 187 tracked_objects::ScopedTracker tracking_profile( |
| 182 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 188 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 183 "477117 ChannelProxy::Context::OnChannelClosed")); | 189 "477117 ChannelProxy::Context::OnChannelClosed")); |
| 184 // It's okay for IPC::ChannelProxy::Close to be called more than once, which | 190 // It's okay for IPC::ChannelProxy::Close to be called more than once, which |
| 185 // would result in this branch being taken. | 191 // would result in this branch being taken. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 if (logger->Enabled()) | 325 if (logger->Enabled()) |
| 320 logger->OnPostDispatchMessage(message, channel_id_); | 326 logger->OnPostDispatchMessage(message, channel_id_); |
| 321 #endif | 327 #endif |
| 322 } | 328 } |
| 323 | 329 |
| 324 // Called on the listener's thread | 330 // Called on the listener's thread |
| 325 void ChannelProxy::Context::OnDispatchConnected() { | 331 void ChannelProxy::Context::OnDispatchConnected() { |
| 326 if (channel_connected_called_) | 332 if (channel_connected_called_) |
| 327 return; | 333 return; |
| 328 | 334 |
| 335 { |
| 336 base::AutoLock l(channel_lifetime_lock_); |
| 337 if (channel_) { |
| 338 Channel::AssociatedInterfaceSupport* associated_interface_support = |
| 339 channel_->GetAssociatedInterfaceSupport(); |
| 340 if (associated_interface_support) { |
| 341 channel_associated_group_.reset(new mojo::AssociatedGroup( |
| 342 *associated_interface_support->GetAssociatedGroup())); |
| 343 } |
| 344 } |
| 345 } |
| 346 |
| 329 base::ProcessId peer_pid; | 347 base::ProcessId peer_pid; |
| 330 { | 348 { |
| 331 base::AutoLock l(peer_pid_lock_); | 349 base::AutoLock l(peer_pid_lock_); |
| 332 peer_pid = peer_pid_; | 350 peer_pid = peer_pid_; |
| 333 } | 351 } |
| 334 channel_connected_called_ = true; | 352 channel_connected_called_ = true; |
| 335 if (listener_) | 353 if (listener_) |
| 336 listener_->OnChannelConnected(peer_pid); | 354 listener_->OnChannelConnected(peer_pid); |
| 337 } | 355 } |
| 338 | 356 |
| 339 // Called on the listener's thread | 357 // Called on the listener's thread |
| 340 void ChannelProxy::Context::OnDispatchError() { | 358 void ChannelProxy::Context::OnDispatchError() { |
| 341 if (listener_) | 359 if (listener_) |
| 342 listener_->OnChannelError(); | 360 listener_->OnChannelError(); |
| 343 } | 361 } |
| 344 | 362 |
| 345 // Called on the listener's thread | 363 // Called on the listener's thread |
| 346 void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) { | 364 void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) { |
| 347 if (listener_) | 365 if (listener_) |
| 348 listener_->OnBadMessageReceived(message); | 366 listener_->OnBadMessageReceived(message); |
| 349 } | 367 } |
| 350 | 368 |
| 351 void ChannelProxy::Context::ClearChannel() { | 369 void ChannelProxy::Context::ClearChannel() { |
| 352 base::AutoLock l(channel_lifetime_lock_); | 370 base::AutoLock l(channel_lifetime_lock_); |
| 353 channel_.reset(); | 371 channel_.reset(); |
| 354 associated_group_ = mojo::AssociatedGroup(); | |
| 355 } | |
| 356 | |
| 357 void ChannelProxy::Context::AddGenericAssociatedInterface( | |
| 358 const std::string& name, | |
| 359 const GenericAssociatedInterfaceFactory& factory) { | |
| 360 AddGenericAssociatedInterfaceForIOThread( | |
| 361 name, base::Bind(&BindAssociatedInterfaceOnTaskRunner, | |
| 362 listener_task_runner_, factory)); | |
| 363 } | |
| 364 | |
| 365 void ChannelProxy::Context::AddGenericAssociatedInterfaceForIOThread( | |
| 366 const std::string& name, | |
| 367 const GenericAssociatedInterfaceFactory& factory) { | |
| 368 base::AutoLock l(channel_lifetime_lock_); | |
| 369 if (!channel_) { | |
| 370 base::AutoLock l(pending_filters_lock_); | |
| 371 pending_interfaces_.emplace_back(name, factory); | |
| 372 return; | |
| 373 } | |
| 374 Channel::AssociatedInterfaceSupport* support = | |
| 375 channel_->GetAssociatedInterfaceSupport(); | |
| 376 DCHECK(support); | |
| 377 support->AddGenericAssociatedInterface(name, factory); | |
| 378 } | 372 } |
| 379 | 373 |
| 380 void ChannelProxy::Context::SendFromThisThread(Message* message) { | 374 void ChannelProxy::Context::SendFromThisThread(Message* message) { |
| 381 base::AutoLock l(channel_lifetime_lock_); | 375 base::AutoLock l(channel_lifetime_lock_); |
| 382 if (!channel_) | 376 if (!channel_) |
| 383 return; | 377 return; |
| 384 DCHECK(channel_->IsSendThreadSafe()); | 378 DCHECK(channel_->IsSendThreadSafe()); |
| 385 channel_->Send(message); | 379 channel_->Send(message); |
| 386 } | 380 } |
| 387 | 381 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 } | 539 } |
| 546 | 540 |
| 547 void ChannelProxy::RemoveFilter(MessageFilter* filter) { | 541 void ChannelProxy::RemoveFilter(MessageFilter* filter) { |
| 548 DCHECK(CalledOnValidThread()); | 542 DCHECK(CalledOnValidThread()); |
| 549 | 543 |
| 550 context_->ipc_task_runner()->PostTask( | 544 context_->ipc_task_runner()->PostTask( |
| 551 FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(), | 545 FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(), |
| 552 base::RetainedRef(filter))); | 546 base::RetainedRef(filter))); |
| 553 } | 547 } |
| 554 | 548 |
| 549 void ChannelProxy::AddGenericAssociatedInterfaceForIOThread( |
| 550 const std::string& name, |
| 551 const GenericAssociatedInterfaceFactory& factory) { |
| 552 DCHECK(CalledOnValidThread()); |
| 553 DCHECK(!did_init_); |
| 554 context_->io_thread_interfaces_.insert({ name, factory }); |
| 555 } |
| 556 |
| 555 void ChannelProxy::AddGenericAssociatedInterface( | 557 void ChannelProxy::AddGenericAssociatedInterface( |
| 556 const std::string& name, | 558 const std::string& name, |
| 557 const GenericAssociatedInterfaceFactory& factory) { | 559 const GenericAssociatedInterfaceFactory& factory) { |
| 558 context()->AddGenericAssociatedInterface(name, factory); | 560 DCHECK(CalledOnValidThread()); |
| 559 } | 561 DCHECK(!did_init_); |
| 560 | 562 context_->proxy_thread_interfaces_.insert({ name, factory }); |
| 561 void ChannelProxy::AddGenericAssociatedInterfaceForIOThread( | |
| 562 const std::string& name, | |
| 563 const GenericAssociatedInterfaceFactory& factory) { | |
| 564 context()->AddGenericAssociatedInterfaceForIOThread(name, factory); | |
| 565 } | 563 } |
| 566 | 564 |
| 567 mojo::AssociatedGroup* ChannelProxy::GetAssociatedGroup() { | 565 mojo::AssociatedGroup* ChannelProxy::GetAssociatedGroup() { |
| 568 return context()->associated_group(); | 566 return context_->channel_associated_group_.get(); |
| 569 } | 567 } |
| 570 | 568 |
| 571 void ChannelProxy::GetGenericRemoteAssociatedInterface( | 569 void ChannelProxy::GetGenericRemoteAssociatedInterface( |
| 572 const std::string& name, | 570 const std::string& name, |
| 573 mojo::ScopedInterfaceEndpointHandle handle) { | 571 mojo::ScopedInterfaceEndpointHandle handle) { |
| 574 context_->ipc_task_runner()->PostTask( | 572 context_->ipc_task_runner()->PostTask( |
| 575 FROM_HERE, base::Bind(&Context::GetRemoteAssociatedInterface, | 573 FROM_HERE, base::Bind(&Context::GetRemoteAssociatedInterface, |
| 576 context_.get(), name, base::Passed(&handle))); | 574 context_.get(), name, base::Passed(&handle))); |
| 577 } | 575 } |
| 578 | 576 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 return channel->TakeClientFileDescriptor(); | 611 return channel->TakeClientFileDescriptor(); |
| 614 } | 612 } |
| 615 #endif | 613 #endif |
| 616 | 614 |
| 617 void ChannelProxy::OnChannelInit() { | 615 void ChannelProxy::OnChannelInit() { |
| 618 } | 616 } |
| 619 | 617 |
| 620 //----------------------------------------------------------------------------- | 618 //----------------------------------------------------------------------------- |
| 621 | 619 |
| 622 } // namespace IPC | 620 } // namespace IPC |
| OLD | NEW |