| 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 "content/child/child_thread_impl.h" | 5 #include "content/child/child_thread_impl.h" |
| 6 | 6 |
| 7 #include <signal.h> | 7 #include <signal.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 #include "ipc/ipc_channel_mojo.h" | 61 #include "ipc/ipc_channel_mojo.h" |
| 62 #include "ipc/ipc_logging.h" | 62 #include "ipc/ipc_logging.h" |
| 63 #include "ipc/ipc_platform_file.h" | 63 #include "ipc/ipc_platform_file.h" |
| 64 #include "ipc/ipc_switches.h" | 64 #include "ipc/ipc_switches.h" |
| 65 #include "ipc/ipc_sync_channel.h" | 65 #include "ipc/ipc_sync_channel.h" |
| 66 #include "ipc/ipc_sync_message_filter.h" | 66 #include "ipc/ipc_sync_message_filter.h" |
| 67 #include "mojo/edk/embedder/embedder.h" | 67 #include "mojo/edk/embedder/embedder.h" |
| 68 #include "mojo/edk/embedder/named_platform_channel_pair.h" | 68 #include "mojo/edk/embedder/named_platform_channel_pair.h" |
| 69 #include "mojo/edk/embedder/platform_channel_pair.h" | 69 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 70 #include "mojo/edk/embedder/scoped_ipc_support.h" | 70 #include "mojo/edk/embedder/scoped_ipc_support.h" |
| 71 #include "services/shell/public/cpp/interface_provider.h" | |
| 72 #include "services/shell/public/cpp/interface_registry.h" | |
| 73 #include "services/shell/runner/common/client_util.h" | 71 #include "services/shell/runner/common/client_util.h" |
| 74 | 72 |
| 75 #if defined(OS_POSIX) | 73 #if defined(OS_POSIX) |
| 76 #include "base/posix/global_descriptors.h" | 74 #include "base/posix/global_descriptors.h" |
| 77 #include "content/public/common/content_descriptors.h" | 75 #include "content/public/common/content_descriptors.h" |
| 78 #endif | 76 #endif |
| 79 | 77 |
| 80 using tracked_objects::ThreadData; | 78 using tracked_objects::ThreadData; |
| 81 | 79 |
| 82 namespace content { | 80 namespace content { |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&msg); | 324 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&msg); |
| 327 reply->set_reply_error(); | 325 reply->set_reply_error(); |
| 328 Send(reply); | 326 Send(reply); |
| 329 } | 327 } |
| 330 #endif | 328 #endif |
| 331 return handled; | 329 return handled; |
| 332 } | 330 } |
| 333 | 331 |
| 334 ChildThreadImpl::ChildThreadImpl() | 332 ChildThreadImpl::ChildThreadImpl() |
| 335 : router_(this), | 333 : router_(this), |
| 336 channel_connected_factory_( | 334 channel_connected_factory_(this) { |
| 337 new base::WeakPtrFactory<ChildThreadImpl>(this)), | |
| 338 weak_factory_(this) { | |
| 339 Init(Options::Builder().Build()); | 335 Init(Options::Builder().Build()); |
| 340 } | 336 } |
| 341 | 337 |
| 342 ChildThreadImpl::ChildThreadImpl(const Options& options) | 338 ChildThreadImpl::ChildThreadImpl(const Options& options) |
| 343 : router_(this), | 339 : router_(this), |
| 344 browser_process_io_runner_(options.browser_process_io_runner), | 340 browser_process_io_runner_(options.browser_process_io_runner), |
| 345 channel_connected_factory_( | 341 channel_connected_factory_(this) { |
| 346 new base::WeakPtrFactory<ChildThreadImpl>(this)), | |
| 347 weak_factory_(this) { | |
| 348 Init(options); | 342 Init(options); |
| 349 } | 343 } |
| 350 | 344 |
| 351 scoped_refptr<base::SequencedTaskRunner> ChildThreadImpl::GetIOTaskRunner() { | 345 scoped_refptr<base::SequencedTaskRunner> ChildThreadImpl::GetIOTaskRunner() { |
| 352 if (IsInBrowserProcess()) | 346 if (IsInBrowserProcess()) |
| 353 return browser_process_io_runner_; | 347 return browser_process_io_runner_; |
| 354 return ChildProcess::current()->io_task_runner(); | 348 return ChildProcess::current()->io_task_runner(); |
| 355 } | 349 } |
| 356 | 350 |
| 357 void ChildThreadImpl::ConnectChannel(bool use_mojo_channel, | 351 void ChildThreadImpl::ConnectChannel(bool use_mojo_channel, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 406 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 413 switches::kMojoApplicationChannelToken); | 407 switches::kMojoApplicationChannelToken); |
| 414 } else { | 408 } else { |
| 415 mojo_application_token = options.in_process_application_token; | 409 mojo_application_token = options.in_process_application_token; |
| 416 } | 410 } |
| 417 if (!mojo_application_token.empty()) { | 411 if (!mojo_application_token.empty()) { |
| 418 mojo::ScopedMessagePipeHandle handle = | 412 mojo::ScopedMessagePipeHandle handle = |
| 419 mojo::edk::CreateChildMessagePipe(mojo_application_token); | 413 mojo::edk::CreateChildMessagePipe(mojo_application_token); |
| 420 DCHECK(handle.is_valid()); | 414 DCHECK(handle.is_valid()); |
| 421 mojo_shell_connection_ = MojoShellConnection::Create( | 415 mojo_shell_connection_ = MojoShellConnection::Create( |
| 422 mojo::MakeRequest<shell::mojom::Service>(std::move(handle)), | 416 mojo::MakeRequest<shell::mojom::Service>(std::move(handle))); |
| 423 GetIOTaskRunner()); | 417 mojo_shell_connection_->MergeService(this); |
| 424 | |
| 425 // TODO(rockot): Remove this once all child-to-browser interface connections | |
| 426 // are made via a Connector rather than directly through an | |
| 427 // InterfaceProvider, and all exposed interfaces are exposed via a | |
| 428 // ConnectionFilter. | |
| 429 mojo_shell_connection_->SetupInterfaceRequestProxies( | |
| 430 GetInterfaceRegistry(), GetRemoteInterfaces()); | |
| 431 | |
| 432 AddConnectionFilters(mojo_shell_connection_.get()); | |
| 433 mojo_shell_connection_->Start(); | |
| 434 } | 418 } |
| 435 | 419 |
| 436 sync_message_filter_ = channel_->CreateSyncMessageFilter(); | 420 sync_message_filter_ = channel_->CreateSyncMessageFilter(); |
| 437 thread_safe_sender_ = new ThreadSafeSender( | 421 thread_safe_sender_ = new ThreadSafeSender( |
| 438 message_loop_->task_runner(), sync_message_filter_.get()); | 422 message_loop_->task_runner(), sync_message_filter_.get()); |
| 439 | 423 |
| 440 resource_dispatcher_.reset(new ResourceDispatcher( | 424 resource_dispatcher_.reset(new ResourceDispatcher( |
| 441 this, message_loop()->task_runner())); | 425 this, message_loop()->task_runner())); |
| 442 websocket_dispatcher_.reset(new WebSocketDispatcher); | 426 websocket_dispatcher_.reset(new WebSocketDispatcher); |
| 443 file_system_dispatcher_.reset(new FileSystemDispatcher()); | 427 file_system_dispatcher_.reset(new FileSystemDispatcher()); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 492 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 509 switches::kIPCConnectionTimeout); | 493 switches::kIPCConnectionTimeout); |
| 510 if (!connection_override.empty()) { | 494 if (!connection_override.empty()) { |
| 511 int temp; | 495 int temp; |
| 512 if (base::StringToInt(connection_override, &temp)) | 496 if (base::StringToInt(connection_override, &temp)) |
| 513 connection_timeout = temp; | 497 connection_timeout = temp; |
| 514 } | 498 } |
| 515 | 499 |
| 516 message_loop_->task_runner()->PostDelayedTask( | 500 message_loop_->task_runner()->PostDelayedTask( |
| 517 FROM_HERE, base::Bind(&ChildThreadImpl::EnsureConnected, | 501 FROM_HERE, base::Bind(&ChildThreadImpl::EnsureConnected, |
| 518 channel_connected_factory_->GetWeakPtr()), | 502 channel_connected_factory_.GetWeakPtr()), |
| 519 base::TimeDelta::FromSeconds(connection_timeout)); | 503 base::TimeDelta::FromSeconds(connection_timeout)); |
| 520 | 504 |
| 521 #if defined(OS_ANDROID) | 505 #if defined(OS_ANDROID) |
| 522 g_quit_closure.Get().BindToMainThread(); | 506 g_quit_closure.Get().BindToMainThread(); |
| 523 #endif | 507 #endif |
| 524 | 508 |
| 525 shared_bitmap_manager_.reset( | 509 shared_bitmap_manager_.reset( |
| 526 new ChildSharedBitmapManager(thread_safe_sender())); | 510 new ChildSharedBitmapManager(thread_safe_sender())); |
| 527 | 511 |
| 528 gpu_memory_buffer_manager_.reset( | 512 gpu_memory_buffer_manager_.reset( |
| 529 new ChildGpuMemoryBufferManager(thread_safe_sender())); | 513 new ChildGpuMemoryBufferManager(thread_safe_sender())); |
| 530 | 514 |
| 531 discardable_shared_memory_manager_.reset( | 515 discardable_shared_memory_manager_.reset( |
| 532 new ChildDiscardableSharedMemoryManager(thread_safe_sender())); | 516 new ChildDiscardableSharedMemoryManager(thread_safe_sender())); |
| 533 } | 517 } |
| 534 | 518 |
| 535 ChildThreadImpl::~ChildThreadImpl() { | 519 ChildThreadImpl::~ChildThreadImpl() { |
| 520 if (MojoShellConnection::GetForProcess()) |
| 521 MojoShellConnection::DestroyForProcess(); |
| 522 |
| 536 #ifdef IPC_MESSAGE_LOG_ENABLED | 523 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 537 IPC::Logging::GetInstance()->SetIPCSender(NULL); | 524 IPC::Logging::GetInstance()->SetIPCSender(NULL); |
| 538 #endif | 525 #endif |
| 539 | 526 |
| 540 IPC::AttachmentBroker* broker = IPC::AttachmentBroker::GetGlobal(); | 527 IPC::AttachmentBroker* broker = IPC::AttachmentBroker::GetGlobal(); |
| 541 if (broker && !broker->IsPrivilegedBroker()) | 528 if (broker && !broker->IsPrivilegedBroker()) |
| 542 broker->DeregisterBrokerCommunicationChannel(channel_.get()); | 529 broker->DeregisterBrokerCommunicationChannel(channel_.get()); |
| 543 | 530 |
| 544 channel_->RemoveFilter(histogram_message_filter_.get()); | 531 channel_->RemoveFilter(histogram_message_filter_.get()); |
| 545 channel_->RemoveFilter(sync_message_filter_.get()); | 532 channel_->RemoveFilter(sync_message_filter_.get()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 562 file_system_dispatcher_.reset(); | 549 file_system_dispatcher_.reset(); |
| 563 quota_dispatcher_.reset(); | 550 quota_dispatcher_.reset(); |
| 564 WebFileSystemImpl::DeleteThreadSpecificInstance(); | 551 WebFileSystemImpl::DeleteThreadSpecificInstance(); |
| 565 } | 552 } |
| 566 | 553 |
| 567 void ChildThreadImpl::ShutdownDiscardableSharedMemoryManager() { | 554 void ChildThreadImpl::ShutdownDiscardableSharedMemoryManager() { |
| 568 discardable_shared_memory_manager_.reset(); | 555 discardable_shared_memory_manager_.reset(); |
| 569 } | 556 } |
| 570 | 557 |
| 571 void ChildThreadImpl::OnChannelConnected(int32_t peer_pid) { | 558 void ChildThreadImpl::OnChannelConnected(int32_t peer_pid) { |
| 572 channel_connected_factory_.reset(); | 559 channel_connected_factory_.InvalidateWeakPtrs(); |
| 573 } | 560 } |
| 574 | 561 |
| 575 void ChildThreadImpl::OnChannelError() { | 562 void ChildThreadImpl::OnChannelError() { |
| 576 on_channel_error_called_ = true; | 563 on_channel_error_called_ = true; |
| 577 base::MessageLoop::current()->QuitWhenIdle(); | 564 base::MessageLoop::current()->QuitWhenIdle(); |
| 578 } | 565 } |
| 579 | 566 |
| 580 bool ChildThreadImpl::Send(IPC::Message* msg) { | 567 bool ChildThreadImpl::Send(IPC::Message* msg) { |
| 581 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | 568 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 582 if (!channel_) { | 569 if (!channel_) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 interface_registry_.reset(new shell::InterfaceRegistry(nullptr)); | 601 interface_registry_.reset(new shell::InterfaceRegistry(nullptr)); |
| 615 return interface_registry_.get(); | 602 return interface_registry_.get(); |
| 616 } | 603 } |
| 617 | 604 |
| 618 shell::InterfaceProvider* ChildThreadImpl::GetRemoteInterfaces() { | 605 shell::InterfaceProvider* ChildThreadImpl::GetRemoteInterfaces() { |
| 619 if (!remote_interfaces_.get()) | 606 if (!remote_interfaces_.get()) |
| 620 remote_interfaces_.reset(new shell::InterfaceProvider); | 607 remote_interfaces_.reset(new shell::InterfaceProvider); |
| 621 return remote_interfaces_.get(); | 608 return remote_interfaces_.get(); |
| 622 } | 609 } |
| 623 | 610 |
| 611 shell::InterfaceRegistry* ChildThreadImpl::GetInterfaceRegistryForConnection() { |
| 612 return GetInterfaceRegistry(); |
| 613 } |
| 614 |
| 615 shell::InterfaceProvider* ChildThreadImpl::GetInterfaceProviderForConnection() { |
| 616 return GetRemoteInterfaces(); |
| 617 } |
| 618 |
| 624 IPC::MessageRouter* ChildThreadImpl::GetRouter() { | 619 IPC::MessageRouter* ChildThreadImpl::GetRouter() { |
| 625 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | 620 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 626 return &router_; | 621 return &router_; |
| 627 } | 622 } |
| 628 | 623 |
| 629 std::unique_ptr<base::SharedMemory> ChildThreadImpl::AllocateSharedMemory( | 624 std::unique_ptr<base::SharedMemory> ChildThreadImpl::AllocateSharedMemory( |
| 630 size_t buf_size) { | 625 size_t buf_size) { |
| 631 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | 626 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 632 return AllocateSharedMemory(buf_size, this, nullptr); | 627 return AllocateSharedMemory(buf_size, this, nullptr); |
| 633 } | 628 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 | 684 |
| 690 if (handled) | 685 if (handled) |
| 691 return true; | 686 return true; |
| 692 | 687 |
| 693 if (msg.routing_id() == MSG_ROUTING_CONTROL) | 688 if (msg.routing_id() == MSG_ROUTING_CONTROL) |
| 694 return OnControlMessageReceived(msg); | 689 return OnControlMessageReceived(msg); |
| 695 | 690 |
| 696 return router_.OnMessageReceived(msg); | 691 return router_.OnMessageReceived(msg); |
| 697 } | 692 } |
| 698 | 693 |
| 699 void ChildThreadImpl::AddConnectionFilters(MojoShellConnection* connection) {} | |
| 700 | |
| 701 bool ChildThreadImpl::OnControlMessageReceived(const IPC::Message& msg) { | 694 bool ChildThreadImpl::OnControlMessageReceived(const IPC::Message& msg) { |
| 702 return false; | 695 return false; |
| 703 } | 696 } |
| 704 | 697 |
| 705 void ChildThreadImpl::OnProcessBackgrounded(bool backgrounded) { | 698 void ChildThreadImpl::OnProcessBackgrounded(bool backgrounded) { |
| 706 // Set timer slack to maximum on main thread when in background. | 699 // Set timer slack to maximum on main thread when in background. |
| 707 base::TimerSlack timer_slack = base::TIMER_SLACK_NONE; | 700 base::TimerSlack timer_slack = base::TIMER_SLACK_NONE; |
| 708 if (backgrounded) | 701 if (backgrounded) |
| 709 timer_slack = base::TIMER_SLACK_MAXIMUM; | 702 timer_slack = base::TIMER_SLACK_MAXIMUM; |
| 710 base::MessageLoop::current()->SetTimerSlack(timer_slack); | 703 base::MessageLoop::current()->SetTimerSlack(timer_slack); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 773 void ChildThreadImpl::EnsureConnected() { | 766 void ChildThreadImpl::EnsureConnected() { |
| 774 VLOG(0) << "ChildThreadImpl::EnsureConnected()"; | 767 VLOG(0) << "ChildThreadImpl::EnsureConnected()"; |
| 775 base::Process::Current().Terminate(0, false); | 768 base::Process::Current().Terminate(0, false); |
| 776 } | 769 } |
| 777 | 770 |
| 778 bool ChildThreadImpl::IsInBrowserProcess() const { | 771 bool ChildThreadImpl::IsInBrowserProcess() const { |
| 779 return static_cast<bool>(browser_process_io_runner_); | 772 return static_cast<bool>(browser_process_io_runner_); |
| 780 } | 773 } |
| 781 | 774 |
| 782 } // namespace content | 775 } // namespace content |
| OLD | NEW |