| 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" |
| 71 #include "services/shell/runner/common/client_util.h" | 73 #include "services/shell/runner/common/client_util.h" |
| 72 | 74 |
| 73 #if defined(OS_POSIX) | 75 #if defined(OS_POSIX) |
| 74 #include "base/posix/global_descriptors.h" | 76 #include "base/posix/global_descriptors.h" |
| 75 #include "content/public/common/content_descriptors.h" | 77 #include "content/public/common/content_descriptors.h" |
| 76 #endif | 78 #endif |
| 77 | 79 |
| 78 using tracked_objects::ThreadData; | 80 using tracked_objects::ThreadData; |
| 79 | 81 |
| 80 namespace content { | 82 namespace content { |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 } // namespace | 258 } // namespace |
| 257 | 259 |
| 258 ChildThread* ChildThread::Get() { | 260 ChildThread* ChildThread::Get() { |
| 259 return ChildThreadImpl::current(); | 261 return ChildThreadImpl::current(); |
| 260 } | 262 } |
| 261 | 263 |
| 262 ChildThreadImpl::Options::Options() | 264 ChildThreadImpl::Options::Options() |
| 263 : channel_name(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 265 : channel_name(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 264 switches::kProcessChannelID)), | 266 switches::kProcessChannelID)), |
| 265 use_mojo_channel(base::CommandLine::ForCurrentProcess()->HasSwitch( | 267 use_mojo_channel(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 266 switches::kMojoChannelToken)) { | 268 switches::kMojoChannelToken)), |
| 269 auto_start_mojo_shell_connection(true) { |
| 267 } | 270 } |
| 268 | 271 |
| 269 ChildThreadImpl::Options::Options(const Options& other) = default; | 272 ChildThreadImpl::Options::Options(const Options& other) = default; |
| 270 | 273 |
| 271 ChildThreadImpl::Options::~Options() { | 274 ChildThreadImpl::Options::~Options() { |
| 272 } | 275 } |
| 273 | 276 |
| 274 ChildThreadImpl::Options::Builder::Builder() { | 277 ChildThreadImpl::Options::Builder::Builder() { |
| 275 } | 278 } |
| 276 | 279 |
| 277 ChildThreadImpl::Options::Builder& | 280 ChildThreadImpl::Options::Builder& |
| 278 ChildThreadImpl::Options::Builder::InBrowserProcess( | 281 ChildThreadImpl::Options::Builder::InBrowserProcess( |
| 279 const InProcessChildThreadParams& params) { | 282 const InProcessChildThreadParams& params) { |
| 280 options_.browser_process_io_runner = params.io_runner(); | 283 options_.browser_process_io_runner = params.io_runner(); |
| 281 options_.channel_name = params.channel_name(); | 284 options_.channel_name = params.channel_name(); |
| 282 options_.in_process_ipc_token = params.ipc_token(); | 285 options_.in_process_ipc_token = params.ipc_token(); |
| 283 options_.in_process_application_token = params.application_token(); | 286 options_.in_process_application_token = params.application_token(); |
| 284 return *this; | 287 return *this; |
| 285 } | 288 } |
| 286 | 289 |
| 287 ChildThreadImpl::Options::Builder& | 290 ChildThreadImpl::Options::Builder& |
| 288 ChildThreadImpl::Options::Builder::UseMojoChannel(bool use_mojo_channel) { | 291 ChildThreadImpl::Options::Builder::UseMojoChannel(bool use_mojo_channel) { |
| 289 options_.use_mojo_channel = use_mojo_channel; | 292 options_.use_mojo_channel = use_mojo_channel; |
| 290 return *this; | 293 return *this; |
| 291 } | 294 } |
| 292 | 295 |
| 293 ChildThreadImpl::Options::Builder& | 296 ChildThreadImpl::Options::Builder& |
| 297 ChildThreadImpl::Options::Builder::AutoStartMojoShellConnection( |
| 298 bool auto_start) { |
| 299 options_.auto_start_mojo_shell_connection = auto_start; |
| 300 return *this; |
| 301 } |
| 302 |
| 303 ChildThreadImpl::Options::Builder& |
| 294 ChildThreadImpl::Options::Builder::WithChannelName( | 304 ChildThreadImpl::Options::Builder::WithChannelName( |
| 295 const std::string& channel_name) { | 305 const std::string& channel_name) { |
| 296 options_.channel_name = channel_name; | 306 options_.channel_name = channel_name; |
| 297 return *this; | 307 return *this; |
| 298 } | 308 } |
| 299 | 309 |
| 300 ChildThreadImpl::Options::Builder& | 310 ChildThreadImpl::Options::Builder& |
| 301 ChildThreadImpl::Options::Builder::AddStartupFilter( | 311 ChildThreadImpl::Options::Builder::AddStartupFilter( |
| 302 IPC::MessageFilter* filter) { | 312 IPC::MessageFilter* filter) { |
| 303 options_.startup_filters.push_back(filter); | 313 options_.startup_filters.push_back(filter); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 324 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&msg); | 334 IPC::Message* reply = IPC::SyncMessage::GenerateReply(&msg); |
| 325 reply->set_reply_error(); | 335 reply->set_reply_error(); |
| 326 Send(reply); | 336 Send(reply); |
| 327 } | 337 } |
| 328 #endif | 338 #endif |
| 329 return handled; | 339 return handled; |
| 330 } | 340 } |
| 331 | 341 |
| 332 ChildThreadImpl::ChildThreadImpl() | 342 ChildThreadImpl::ChildThreadImpl() |
| 333 : router_(this), | 343 : router_(this), |
| 334 channel_connected_factory_(this) { | 344 channel_connected_factory_( |
| 345 new base::WeakPtrFactory<ChildThreadImpl>(this)), |
| 346 weak_factory_(this) { |
| 335 Init(Options::Builder().Build()); | 347 Init(Options::Builder().Build()); |
| 336 } | 348 } |
| 337 | 349 |
| 338 ChildThreadImpl::ChildThreadImpl(const Options& options) | 350 ChildThreadImpl::ChildThreadImpl(const Options& options) |
| 339 : router_(this), | 351 : router_(this), |
| 340 browser_process_io_runner_(options.browser_process_io_runner), | 352 browser_process_io_runner_(options.browser_process_io_runner), |
| 341 channel_connected_factory_(this) { | 353 channel_connected_factory_( |
| 354 new base::WeakPtrFactory<ChildThreadImpl>(this)), |
| 355 weak_factory_(this) { |
| 342 Init(options); | 356 Init(options); |
| 343 } | 357 } |
| 344 | 358 |
| 345 scoped_refptr<base::SequencedTaskRunner> ChildThreadImpl::GetIOTaskRunner() { | 359 scoped_refptr<base::SequencedTaskRunner> ChildThreadImpl::GetIOTaskRunner() { |
| 346 if (IsInBrowserProcess()) | 360 if (IsInBrowserProcess()) |
| 347 return browser_process_io_runner_; | 361 return browser_process_io_runner_; |
| 348 return ChildProcess::current()->io_task_runner(); | 362 return ChildProcess::current()->io_task_runner(); |
| 349 } | 363 } |
| 350 | 364 |
| 351 void ChildThreadImpl::ConnectChannel(bool use_mojo_channel, | 365 void ChildThreadImpl::ConnectChannel(bool use_mojo_channel, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 420 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 407 switches::kMojoApplicationChannelToken); | 421 switches::kMojoApplicationChannelToken); |
| 408 } else { | 422 } else { |
| 409 mojo_application_token = options.in_process_application_token; | 423 mojo_application_token = options.in_process_application_token; |
| 410 } | 424 } |
| 411 if (!mojo_application_token.empty()) { | 425 if (!mojo_application_token.empty()) { |
| 412 mojo::ScopedMessagePipeHandle handle = | 426 mojo::ScopedMessagePipeHandle handle = |
| 413 mojo::edk::CreateChildMessagePipe(mojo_application_token); | 427 mojo::edk::CreateChildMessagePipe(mojo_application_token); |
| 414 DCHECK(handle.is_valid()); | 428 DCHECK(handle.is_valid()); |
| 415 mojo_shell_connection_ = MojoShellConnection::Create( | 429 mojo_shell_connection_ = MojoShellConnection::Create( |
| 416 mojo::MakeRequest<shell::mojom::Service>(std::move(handle))); | 430 mojo::MakeRequest<shell::mojom::Service>(std::move(handle)), |
| 417 mojo_shell_connection_->MergeService(this); | 431 GetIOTaskRunner()); |
| 432 |
| 433 // TODO(rockot): Remove this once all child-to-browser interface connections |
| 434 // are made via a Connector rather than directly through an |
| 435 // InterfaceProvider, and all exposed interfaces are exposed via a |
| 436 // ConnectionFilter. |
| 437 mojo_shell_connection_->SetupInterfaceRequestProxies( |
| 438 GetInterfaceRegistry(), GetRemoteInterfaces()); |
| 439 |
| 440 if (options.auto_start_mojo_shell_connection) |
| 441 StartMojoShellConnection(); |
| 418 } | 442 } |
| 419 | 443 |
| 420 sync_message_filter_ = channel_->CreateSyncMessageFilter(); | 444 sync_message_filter_ = channel_->CreateSyncMessageFilter(); |
| 421 thread_safe_sender_ = new ThreadSafeSender( | 445 thread_safe_sender_ = new ThreadSafeSender( |
| 422 message_loop_->task_runner(), sync_message_filter_.get()); | 446 message_loop_->task_runner(), sync_message_filter_.get()); |
| 423 | 447 |
| 424 resource_dispatcher_.reset(new ResourceDispatcher( | 448 resource_dispatcher_.reset(new ResourceDispatcher( |
| 425 this, message_loop()->task_runner())); | 449 this, message_loop()->task_runner())); |
| 426 websocket_dispatcher_.reset(new WebSocketDispatcher); | 450 websocket_dispatcher_.reset(new WebSocketDispatcher); |
| 427 file_system_dispatcher_.reset(new FileSystemDispatcher()); | 451 file_system_dispatcher_.reset(new FileSystemDispatcher()); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 516 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 493 switches::kIPCConnectionTimeout); | 517 switches::kIPCConnectionTimeout); |
| 494 if (!connection_override.empty()) { | 518 if (!connection_override.empty()) { |
| 495 int temp; | 519 int temp; |
| 496 if (base::StringToInt(connection_override, &temp)) | 520 if (base::StringToInt(connection_override, &temp)) |
| 497 connection_timeout = temp; | 521 connection_timeout = temp; |
| 498 } | 522 } |
| 499 | 523 |
| 500 message_loop_->task_runner()->PostDelayedTask( | 524 message_loop_->task_runner()->PostDelayedTask( |
| 501 FROM_HERE, base::Bind(&ChildThreadImpl::EnsureConnected, | 525 FROM_HERE, base::Bind(&ChildThreadImpl::EnsureConnected, |
| 502 channel_connected_factory_.GetWeakPtr()), | 526 channel_connected_factory_->GetWeakPtr()), |
| 503 base::TimeDelta::FromSeconds(connection_timeout)); | 527 base::TimeDelta::FromSeconds(connection_timeout)); |
| 504 | 528 |
| 505 #if defined(OS_ANDROID) | 529 #if defined(OS_ANDROID) |
| 506 g_quit_closure.Get().BindToMainThread(); | 530 g_quit_closure.Get().BindToMainThread(); |
| 507 #endif | 531 #endif |
| 508 | 532 |
| 509 shared_bitmap_manager_.reset( | 533 shared_bitmap_manager_.reset( |
| 510 new ChildSharedBitmapManager(thread_safe_sender())); | 534 new ChildSharedBitmapManager(thread_safe_sender())); |
| 511 | 535 |
| 512 gpu_memory_buffer_manager_.reset( | 536 gpu_memory_buffer_manager_.reset( |
| 513 new ChildGpuMemoryBufferManager(thread_safe_sender())); | 537 new ChildGpuMemoryBufferManager(thread_safe_sender())); |
| 514 | 538 |
| 515 discardable_shared_memory_manager_.reset( | 539 discardable_shared_memory_manager_.reset( |
| 516 new ChildDiscardableSharedMemoryManager(thread_safe_sender())); | 540 new ChildDiscardableSharedMemoryManager(thread_safe_sender())); |
| 517 } | 541 } |
| 518 | 542 |
| 519 ChildThreadImpl::~ChildThreadImpl() { | 543 ChildThreadImpl::~ChildThreadImpl() { |
| 520 if (MojoShellConnection::GetForProcess()) | |
| 521 MojoShellConnection::DestroyForProcess(); | |
| 522 | |
| 523 #ifdef IPC_MESSAGE_LOG_ENABLED | 544 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 524 IPC::Logging::GetInstance()->SetIPCSender(NULL); | 545 IPC::Logging::GetInstance()->SetIPCSender(NULL); |
| 525 #endif | 546 #endif |
| 526 | 547 |
| 527 IPC::AttachmentBroker* broker = IPC::AttachmentBroker::GetGlobal(); | 548 IPC::AttachmentBroker* broker = IPC::AttachmentBroker::GetGlobal(); |
| 528 if (broker && !broker->IsPrivilegedBroker()) | 549 if (broker && !broker->IsPrivilegedBroker()) |
| 529 broker->DeregisterBrokerCommunicationChannel(channel_.get()); | 550 broker->DeregisterBrokerCommunicationChannel(channel_.get()); |
| 530 | 551 |
| 531 channel_->RemoveFilter(histogram_message_filter_.get()); | 552 channel_->RemoveFilter(histogram_message_filter_.get()); |
| 532 channel_->RemoveFilter(sync_message_filter_.get()); | 553 channel_->RemoveFilter(sync_message_filter_.get()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 549 file_system_dispatcher_.reset(); | 570 file_system_dispatcher_.reset(); |
| 550 quota_dispatcher_.reset(); | 571 quota_dispatcher_.reset(); |
| 551 WebFileSystemImpl::DeleteThreadSpecificInstance(); | 572 WebFileSystemImpl::DeleteThreadSpecificInstance(); |
| 552 } | 573 } |
| 553 | 574 |
| 554 void ChildThreadImpl::ShutdownDiscardableSharedMemoryManager() { | 575 void ChildThreadImpl::ShutdownDiscardableSharedMemoryManager() { |
| 555 discardable_shared_memory_manager_.reset(); | 576 discardable_shared_memory_manager_.reset(); |
| 556 } | 577 } |
| 557 | 578 |
| 558 void ChildThreadImpl::OnChannelConnected(int32_t peer_pid) { | 579 void ChildThreadImpl::OnChannelConnected(int32_t peer_pid) { |
| 559 channel_connected_factory_.InvalidateWeakPtrs(); | 580 channel_connected_factory_.reset(); |
| 560 } | 581 } |
| 561 | 582 |
| 562 void ChildThreadImpl::OnChannelError() { | 583 void ChildThreadImpl::OnChannelError() { |
| 563 on_channel_error_called_ = true; | 584 on_channel_error_called_ = true; |
| 564 base::MessageLoop::current()->QuitWhenIdle(); | 585 base::MessageLoop::current()->QuitWhenIdle(); |
| 565 } | 586 } |
| 566 | 587 |
| 567 bool ChildThreadImpl::Send(IPC::Message* msg) { | 588 bool ChildThreadImpl::Send(IPC::Message* msg) { |
| 568 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | 589 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 569 if (!channel_) { | 590 if (!channel_) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 interface_registry_.reset(new shell::InterfaceRegistry(nullptr)); | 622 interface_registry_.reset(new shell::InterfaceRegistry(nullptr)); |
| 602 return interface_registry_.get(); | 623 return interface_registry_.get(); |
| 603 } | 624 } |
| 604 | 625 |
| 605 shell::InterfaceProvider* ChildThreadImpl::GetRemoteInterfaces() { | 626 shell::InterfaceProvider* ChildThreadImpl::GetRemoteInterfaces() { |
| 606 if (!remote_interfaces_.get()) | 627 if (!remote_interfaces_.get()) |
| 607 remote_interfaces_.reset(new shell::InterfaceProvider); | 628 remote_interfaces_.reset(new shell::InterfaceProvider); |
| 608 return remote_interfaces_.get(); | 629 return remote_interfaces_.get(); |
| 609 } | 630 } |
| 610 | 631 |
| 611 shell::InterfaceRegistry* ChildThreadImpl::GetInterfaceRegistryForConnection() { | |
| 612 return GetInterfaceRegistry(); | |
| 613 } | |
| 614 | |
| 615 shell::InterfaceProvider* ChildThreadImpl::GetInterfaceProviderForConnection() { | |
| 616 return GetRemoteInterfaces(); | |
| 617 } | |
| 618 | |
| 619 IPC::MessageRouter* ChildThreadImpl::GetRouter() { | 632 IPC::MessageRouter* ChildThreadImpl::GetRouter() { |
| 620 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | 633 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 621 return &router_; | 634 return &router_; |
| 622 } | 635 } |
| 623 | 636 |
| 624 std::unique_ptr<base::SharedMemory> ChildThreadImpl::AllocateSharedMemory( | 637 std::unique_ptr<base::SharedMemory> ChildThreadImpl::AllocateSharedMemory( |
| 625 size_t buf_size) { | 638 size_t buf_size) { |
| 626 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); | 639 DCHECK(message_loop_->task_runner()->BelongsToCurrentThread()); |
| 627 return AllocateSharedMemory(buf_size, this, nullptr); | 640 return AllocateSharedMemory(buf_size, this, nullptr); |
| 628 } | 641 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 | 697 |
| 685 if (handled) | 698 if (handled) |
| 686 return true; | 699 return true; |
| 687 | 700 |
| 688 if (msg.routing_id() == MSG_ROUTING_CONTROL) | 701 if (msg.routing_id() == MSG_ROUTING_CONTROL) |
| 689 return OnControlMessageReceived(msg); | 702 return OnControlMessageReceived(msg); |
| 690 | 703 |
| 691 return router_.OnMessageReceived(msg); | 704 return router_.OnMessageReceived(msg); |
| 692 } | 705 } |
| 693 | 706 |
| 707 void ChildThreadImpl::StartMojoShellConnection() { |
| 708 DCHECK(mojo_shell_connection_); |
| 709 mojo_shell_connection_->Start(); |
| 710 } |
| 711 |
| 694 bool ChildThreadImpl::OnControlMessageReceived(const IPC::Message& msg) { | 712 bool ChildThreadImpl::OnControlMessageReceived(const IPC::Message& msg) { |
| 695 return false; | 713 return false; |
| 696 } | 714 } |
| 697 | 715 |
| 698 void ChildThreadImpl::OnProcessBackgrounded(bool backgrounded) { | 716 void ChildThreadImpl::OnProcessBackgrounded(bool backgrounded) { |
| 699 // Set timer slack to maximum on main thread when in background. | 717 // Set timer slack to maximum on main thread when in background. |
| 700 base::TimerSlack timer_slack = base::TIMER_SLACK_NONE; | 718 base::TimerSlack timer_slack = base::TIMER_SLACK_NONE; |
| 701 if (backgrounded) | 719 if (backgrounded) |
| 702 timer_slack = base::TIMER_SLACK_MAXIMUM; | 720 timer_slack = base::TIMER_SLACK_MAXIMUM; |
| 703 base::MessageLoop::current()->SetTimerSlack(timer_slack); | 721 base::MessageLoop::current()->SetTimerSlack(timer_slack); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 766 void ChildThreadImpl::EnsureConnected() { | 784 void ChildThreadImpl::EnsureConnected() { |
| 767 VLOG(0) << "ChildThreadImpl::EnsureConnected()"; | 785 VLOG(0) << "ChildThreadImpl::EnsureConnected()"; |
| 768 base::Process::Current().Terminate(0, false); | 786 base::Process::Current().Terminate(0, false); |
| 769 } | 787 } |
| 770 | 788 |
| 771 bool ChildThreadImpl::IsInBrowserProcess() const { | 789 bool ChildThreadImpl::IsInBrowserProcess() const { |
| 772 return static_cast<bool>(browser_process_io_runner_); | 790 return static_cast<bool>(browser_process_io_runner_); |
| 773 } | 791 } |
| 774 | 792 |
| 775 } // namespace content | 793 } // namespace content |
| OLD | NEW |