Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(438)

Side by Side Diff: content/child/child_thread_impl.cc

Issue 1685183004: Bootstrap Mojo IPC independent of Chrome IPC (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: and fix posix Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include "base/base_switches.h" 11 #include "base/base_switches.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/debug/leak_annotations.h" 13 #include "base/debug/leak_annotations.h"
14 #include "base/debug/profiler.h" 14 #include "base/debug/profiler.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/location.h" 16 #include "base/location.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/message_loop/timer_slack.h" 19 #include "base/message_loop/timer_slack.h"
20 #include "base/metrics/field_trial.h" 20 #include "base/metrics/field_trial.h"
21 #include "base/process/process.h" 21 #include "base/process/process.h"
22 #include "base/process/process_handle.h" 22 #include "base/process/process_handle.h"
23 #include "base/single_thread_task_runner.h" 23 #include "base/single_thread_task_runner.h"
24 #include "base/strings/string_number_conversions.h" 24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_util.h" 25 #include "base/strings/string_util.h"
26 #include "base/strings/utf_string_conversions.h"
26 #include "base/synchronization/condition_variable.h" 27 #include "base/synchronization/condition_variable.h"
27 #include "base/synchronization/lock.h" 28 #include "base/synchronization/lock.h"
28 #include "base/thread_task_runner_handle.h" 29 #include "base/thread_task_runner_handle.h"
29 #include "base/threading/thread_local.h" 30 #include "base/threading/thread_local.h"
30 #include "base/tracked_objects.h" 31 #include "base/tracked_objects.h"
31 #include "build/build_config.h" 32 #include "build/build_config.h"
32 #include "components/tracing/child_trace_message_filter.h" 33 #include "components/tracing/child_trace_message_filter.h"
33 #include "content/child/child_discardable_shared_memory_manager.h" 34 #include "content/child/child_discardable_shared_memory_manager.h"
34 #include "content/child/child_gpu_memory_buffer_manager.h" 35 #include "content/child/child_gpu_memory_buffer_manager.h"
35 #include "content/child/child_histogram_message_filter.h" 36 #include "content/child/child_histogram_message_filter.h"
36 #include "content/child/child_process.h" 37 #include "content/child/child_process.h"
37 #include "content/child/child_resource_message_filter.h" 38 #include "content/child/child_resource_message_filter.h"
38 #include "content/child/child_shared_bitmap_manager.h" 39 #include "content/child/child_shared_bitmap_manager.h"
39 #include "content/child/fileapi/file_system_dispatcher.h" 40 #include "content/child/fileapi/file_system_dispatcher.h"
40 #include "content/child/fileapi/webfilesystem_impl.h" 41 #include "content/child/fileapi/webfilesystem_impl.h"
41 #include "content/child/geofencing/geofencing_message_filter.h" 42 #include "content/child/geofencing/geofencing_message_filter.h"
42 #include "content/child/memory/child_memory_message_filter.h" 43 #include "content/child/memory/child_memory_message_filter.h"
43 #include "content/child/mojo/mojo_application.h" 44 #include "content/child/mojo/mojo_application.h"
44 #include "content/child/notifications/notification_dispatcher.h" 45 #include "content/child/notifications/notification_dispatcher.h"
45 #include "content/child/power_monitor_broadcast_source.h" 46 #include "content/child/power_monitor_broadcast_source.h"
46 #include "content/child/push_messaging/push_dispatcher.h" 47 #include "content/child/push_messaging/push_dispatcher.h"
47 #include "content/child/quota_dispatcher.h" 48 #include "content/child/quota_dispatcher.h"
48 #include "content/child/quota_message_filter.h" 49 #include "content/child/quota_message_filter.h"
49 #include "content/child/resource_dispatcher.h" 50 #include "content/child/resource_dispatcher.h"
50 #include "content/child/service_worker/service_worker_message_filter.h" 51 #include "content/child/service_worker/service_worker_message_filter.h"
51 #include "content/child/thread_safe_sender.h" 52 #include "content/child/thread_safe_sender.h"
52 #include "content/child/websocket_dispatcher.h" 53 #include "content/child/websocket_dispatcher.h"
53 #include "content/common/child_process_messages.h" 54 #include "content/common/child_process_messages.h"
54 #include "content/common/in_process_child_thread_params.h" 55 #include "content/common/in_process_child_thread_params.h"
55 #include "content/common/mojo/mojo_messages.h"
56 #include "content/public/common/content_switches.h" 56 #include "content/public/common/content_switches.h"
57 #include "ipc/attachment_broker.h" 57 #include "ipc/attachment_broker.h"
58 #include "ipc/attachment_broker_unprivileged.h" 58 #include "ipc/attachment_broker_unprivileged.h"
59 #include "ipc/ipc_descriptors.h"
59 #include "ipc/ipc_logging.h" 60 #include "ipc/ipc_logging.h"
60 #include "ipc/ipc_platform_file.h" 61 #include "ipc/ipc_platform_file.h"
61 #include "ipc/ipc_switches.h" 62 #include "ipc/ipc_switches.h"
62 #include "ipc/ipc_sync_channel.h" 63 #include "ipc/ipc_sync_channel.h"
63 #include "ipc/ipc_sync_message_filter.h" 64 #include "ipc/ipc_sync_message_filter.h"
64 #include "ipc/mojo/ipc_channel_mojo.h" 65 #include "ipc/mojo/ipc_channel_mojo.h"
65 #include "mojo/edk/embedder/embedder.h" 66 #include "mojo/edk/embedder/embedder.h"
66 67
67 #if defined(USE_OZONE) 68 #if defined(USE_OZONE)
68 #include "ui/ozone/public/client_native_pixmap_factory.h" 69 #include "ui/ozone/public/client_native_pixmap_factory.h"
69 #endif 70 #endif
70 71
72 #if defined(OS_POSIX)
73 #include "base/posix/global_descriptors.h"
74 #endif
75
71 #if defined(MOJO_SHELL_CLIENT) 76 #if defined(MOJO_SHELL_CLIENT)
72 #include "content/common/mojo/mojo_shell_connection_impl.h" 77 #include "content/common/mojo/mojo_shell_connection_impl.h"
73 #endif 78 #endif
74 79
75 using tracked_objects::ThreadData; 80 using tracked_objects::ThreadData;
76 81
77 namespace content { 82 namespace content {
78 namespace { 83 namespace {
79 84
80 // How long to wait for a connection to the browser process before giving up. 85 // How long to wait for a connection to the browser process before giving up.
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 base::AutoLock lock(lock_); 246 base::AutoLock lock(lock_);
242 while (closure_.is_null()) 247 while (closure_.is_null())
243 cond_var_.Wait(); 248 cond_var_.Wait();
244 249
245 closure_.Run(); 250 closure_.Run();
246 } 251 }
247 252
248 base::LazyInstance<QuitClosure> g_quit_closure = LAZY_INSTANCE_INITIALIZER; 253 base::LazyInstance<QuitClosure> g_quit_closure = LAZY_INSTANCE_INITIALIZER;
249 #endif 254 #endif
250 255
256 void InitializeMojoIPCChannel() {
257 mojo::edk::ScopedPlatformHandle platform_channel;
258 #if defined(OS_WIN)
259 std::wstring pipe_name = base::UTF8ToWide(
260 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
261 switches::kMojoChannelName));
262 std::string pipe_secret =
263 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
264 switches::kMojoChannelSecret);
265 const DWORD kFlags =
266 SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS | FILE_FLAG_OVERLAPPED;
267 platform_channel.reset(mojo::edk::PlatformHandle(
268 CreateFileW(pipe_name.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
269 nullptr, OPEN_EXISTING, kFlags, nullptr)));
270 PCHECK(platform_channel.is_valid());
271 mojo::edk::SetParentPipeHandle(std::move(platform_channel), pipe_secret);
272 #elif defined(OS_POSIX)
273 platform_channel.reset(mojo::edk::PlatformHandle(
274 base::GlobalDescriptors::GetInstance()->Get(kMojoIPCChannel)));
275 CHECK(platform_channel.is_valid());
276 mojo::edk::SetParentPipeHandle(std::move(platform_channel));
277 #endif
278 }
279
251 } // namespace 280 } // namespace
252 281
253 ChildThread* ChildThread::Get() { 282 ChildThread* ChildThread::Get() {
254 return ChildThreadImpl::current(); 283 return ChildThreadImpl::current();
255 } 284 }
256 285
257 ChildThreadImpl::Options::Options() 286 ChildThreadImpl::Options::Options()
258 : channel_name(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 287 : channel_name(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
259 switches::kProcessChannelID)), 288 switches::kProcessChannelID)),
260 use_mojo_channel(false) { 289 use_mojo_channel(false) {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 IPC::SyncChannel::Create(this, ChildProcess::current()->io_task_runner(), 409 IPC::SyncChannel::Create(this, ChildProcess::current()->io_task_runner(),
381 ChildProcess::current()->GetShutDownEvent()); 410 ChildProcess::current()->GetShutDownEvent());
382 #ifdef IPC_MESSAGE_LOG_ENABLED 411 #ifdef IPC_MESSAGE_LOG_ENABLED
383 if (!IsInBrowserProcess()) 412 if (!IsInBrowserProcess())
384 IPC::Logging::GetInstance()->SetIPCSender(this); 413 IPC::Logging::GetInstance()->SetIPCSender(this);
385 #endif 414 #endif
386 415
387 if (!IsInBrowserProcess()) { 416 if (!IsInBrowserProcess()) {
388 // Don't double-initialize IPC support in single-process mode. 417 // Don't double-initialize IPC support in single-process mode.
389 mojo_ipc_support_.reset(new IPC::ScopedIPCSupport(GetIOTaskRunner())); 418 mojo_ipc_support_.reset(new IPC::ScopedIPCSupport(GetIOTaskRunner()));
419
420 InitializeMojoIPCChannel();
390 } 421 }
391 422
423 #if defined(MOJO_SHELL_CLIENT)
424 // If this process was launched with a primordial pipe token, we exchange it
425 // for a pipe to connect to the shell.
426 std::string pipe_token =
427 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
428 switches::kMojoPrimordialPipeToken);
429 if (!pipe_token.empty() && MojoShellConnectionImpl::Get()) {
430 mojo::ScopedMessagePipeHandle pipe =
431 mojo::edk::CreateChildMessagePipe(pipe_token);
432 MojoShellConnectionImpl::Get()->BindToMessagePipe(std::move(pipe));
433 }
434 #endif
435
392 mojo_application_.reset(new MojoApplication(GetIOTaskRunner())); 436 mojo_application_.reset(new MojoApplication(GetIOTaskRunner()));
393 437
394 sync_message_filter_ = channel_->CreateSyncMessageFilter(); 438 sync_message_filter_ = channel_->CreateSyncMessageFilter();
395 thread_safe_sender_ = new ThreadSafeSender( 439 thread_safe_sender_ = new ThreadSafeSender(
396 message_loop_->task_runner(), sync_message_filter_.get()); 440 message_loop_->task_runner(), sync_message_filter_.get());
397 441
398 resource_dispatcher_.reset(new ResourceDispatcher( 442 resource_dispatcher_.reset(new ResourceDispatcher(
399 this, message_loop()->task_runner())); 443 this, message_loop()->task_runner()));
400 websocket_dispatcher_.reset(new WebSocketDispatcher); 444 websocket_dispatcher_.reset(new WebSocketDispatcher);
401 file_system_dispatcher_.reset(new FileSystemDispatcher()); 445 file_system_dispatcher_.reset(new FileSystemDispatcher());
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 OnSetIPCLoggingEnabled) 659 OnSetIPCLoggingEnabled)
616 #endif 660 #endif
617 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetProfilerStatus, 661 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetProfilerStatus,
618 OnSetProfilerStatus) 662 OnSetProfilerStatus)
619 IPC_MESSAGE_HANDLER(ChildProcessMsg_GetChildProfilerData, 663 IPC_MESSAGE_HANDLER(ChildProcessMsg_GetChildProfilerData,
620 OnGetChildProfilerData) 664 OnGetChildProfilerData)
621 IPC_MESSAGE_HANDLER(ChildProcessMsg_ProfilingPhaseCompleted, 665 IPC_MESSAGE_HANDLER(ChildProcessMsg_ProfilingPhaseCompleted,
622 OnProfilingPhaseCompleted) 666 OnProfilingPhaseCompleted)
623 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetProcessBackgrounded, 667 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetProcessBackgrounded,
624 OnProcessBackgrounded) 668 OnProcessBackgrounded)
625 IPC_MESSAGE_HANDLER(MojoMsg_BindExternalMojoShellHandle,
626 OnBindExternalMojoShellHandle)
627 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetMojoParentPipeHandle,
628 OnSetMojoParentPipeHandle)
629 IPC_MESSAGE_UNHANDLED(handled = false) 669 IPC_MESSAGE_UNHANDLED(handled = false)
630 IPC_END_MESSAGE_MAP() 670 IPC_END_MESSAGE_MAP()
631 671
632 if (handled) 672 if (handled)
633 return true; 673 return true;
634 674
635 if (msg.routing_id() == MSG_ROUTING_CONTROL) 675 if (msg.routing_id() == MSG_ROUTING_CONTROL)
636 return OnControlMessageReceived(msg); 676 return OnControlMessageReceived(msg);
637 677
638 return router_.OnMessageReceived(msg); 678 return router_.OnMessageReceived(msg);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 ThreadData::Snapshot(current_profiling_phase, &process_data); 713 ThreadData::Snapshot(current_profiling_phase, &process_data);
674 714
675 Send( 715 Send(
676 new ChildProcessHostMsg_ChildProfilerData(sequence_number, process_data)); 716 new ChildProcessHostMsg_ChildProfilerData(sequence_number, process_data));
677 } 717 }
678 718
679 void ChildThreadImpl::OnProfilingPhaseCompleted(int profiling_phase) { 719 void ChildThreadImpl::OnProfilingPhaseCompleted(int profiling_phase) {
680 ThreadData::OnProfilingPhaseCompleted(profiling_phase); 720 ThreadData::OnProfilingPhaseCompleted(profiling_phase);
681 } 721 }
682 722
683 void ChildThreadImpl::OnBindExternalMojoShellHandle(
684 const IPC::PlatformFileForTransit& file) {
685 #if defined(MOJO_SHELL_CLIENT)
686 #if defined(OS_POSIX)
687 base::PlatformFile handle = file.fd;
688 #elif defined(OS_WIN)
689 base::PlatformFile handle = file;
690 #endif
691 mojo::ScopedMessagePipeHandle pipe =
692 mojo_shell_channel_init_.Init(handle, GetIOTaskRunner());
693 MojoShellConnectionImpl::Get()->BindToMessagePipe(std::move(pipe));
694 #endif // defined(MOJO_SHELL_CLIENT)
695 }
696
697 void ChildThreadImpl::OnSetMojoParentPipeHandle(
698 const IPC::PlatformFileForTransit& file) {
699 mojo::edk::SetParentPipeHandle(
700 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(
701 IPC::PlatformFileForTransitToPlatformFile(file))));
702 }
703
704 ChildThreadImpl* ChildThreadImpl::current() { 723 ChildThreadImpl* ChildThreadImpl::current() {
705 return g_lazy_tls.Pointer()->Get(); 724 return g_lazy_tls.Pointer()->Get();
706 } 725 }
707 726
708 #if defined(OS_ANDROID) 727 #if defined(OS_ANDROID)
709 // The method must NOT be called on the child thread itself. 728 // The method must NOT be called on the child thread itself.
710 // It may block the child thread if so. 729 // It may block the child thread if so.
711 void ChildThreadImpl::ShutdownThread() { 730 void ChildThreadImpl::ShutdownThread() {
712 DCHECK(!ChildThreadImpl::current()) << 731 DCHECK(!ChildThreadImpl::current()) <<
713 "this method should NOT be called from child thread itself"; 732 "this method should NOT be called from child thread itself";
(...skipping 17 matching lines...) Expand all
731 void ChildThreadImpl::EnsureConnected() { 750 void ChildThreadImpl::EnsureConnected() {
732 VLOG(0) << "ChildThreadImpl::EnsureConnected()"; 751 VLOG(0) << "ChildThreadImpl::EnsureConnected()";
733 base::Process::Current().Terminate(0, false); 752 base::Process::Current().Terminate(0, false);
734 } 753 }
735 754
736 bool ChildThreadImpl::IsInBrowserProcess() const { 755 bool ChildThreadImpl::IsInBrowserProcess() const {
737 return browser_process_io_runner_; 756 return browser_process_io_runner_;
738 } 757 }
739 758
740 } // namespace content 759 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698