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
3 // found in the LICENSE file.
5 #include "content/child/child_thread_impl.h"
11 #include "base/allocator/allocator_extension.h"
12 #include "base/base_switches.h"
13 #include "base/basictypes.h"
14 #include "base/command_line.h"
15 #include "base/debug/leak_annotations.h"
16 #include "base/debug/profiler.h"
17 #include "base/lazy_instance.h"
18 #include "base/location.h"
19 #include "base/logging.h"
20 #include "base/message_loop/timer_slack.h"
21 #include "base/metrics/field_trial.h"
22 #include "base/process/process.h"
23 #include "base/process/process_handle.h"
24 #include "base/single_thread_task_runner.h"
25 #include "base/strings/string_number_conversions.h"
26 #include "base/strings/string_util.h"
27 #include "base/synchronization/condition_variable.h"
28 #include "base/synchronization/lock.h"
29 #include "base/thread_task_runner_handle.h"
30 #include "base/threading/thread_local.h"
31 #include "base/trace_event/memory_dump_manager.h"
32 #include "base/tracked_objects.h"
33 #include "components/tracing/child_trace_message_filter.h"
34 #include "content/child/child_discardable_shared_memory_manager.h"
35 #include "content/child/child_gpu_memory_buffer_manager.h"
36 #include "content/child/child_histogram_message_filter.h"
37 #include "content/child/child_process.h"
38 #include "content/child/child_resource_message_filter.h"
39 #include "content/child/child_shared_bitmap_manager.h"
40 #include "content/child/fileapi/file_system_dispatcher.h"
41 #include "content/child/fileapi/webfilesystem_impl.h"
42 #include "content/child/geofencing/geofencing_message_filter.h"
43 #include "content/child/mojo/mojo_application.h"
44 #include "content/child/notifications/notification_dispatcher.h"
45 #include "content/child/power_monitor_broadcast_source.h"
46 #include "content/child/push_messaging/push_dispatcher.h"
47 #include "content/child/quota_dispatcher.h"
48 #include "content/child/quota_message_filter.h"
49 #include "content/child/resource_dispatcher.h"
50 #include "content/child/service_worker/service_worker_message_filter.h"
51 #include "content/child/thread_safe_sender.h"
52 #include "content/child/websocket_dispatcher.h"
53 #include "content/common/child_process_messages.h"
54 #include "content/common/in_process_child_thread_params.h"
55 #include "content/public/common/content_switches.h"
56 #include "ipc/attachment_broker_unprivileged.h"
57 #include "ipc/ipc_logging.h"
58 #include "ipc/ipc_switches.h"
59 #include "ipc/ipc_sync_channel.h"
60 #include "ipc/ipc_sync_message_filter.h"
61 #include "ipc/mojo/ipc_channel_mojo.h"
63 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED)
64 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
67 #if defined(OS_MACOSX)
68 #include "content/child/child_io_surface_manager_mac.h"
72 #include "ipc/attachment_broker_unprivileged_win.h"
75 using tracked_objects::ThreadData
;
80 // How long to wait for a connection to the browser process before giving up.
81 const int kConnectionTimeoutS
= 15;
83 base::LazyInstance
<base::ThreadLocalPointer
<ChildThreadImpl
> > g_lazy_tls
=
84 LAZY_INSTANCE_INITIALIZER
;
86 // This isn't needed on Windows because there the sandbox's job object
87 // terminates child processes automatically. For unsandboxed processes (i.e.
88 // plugins), PluginThread has EnsureTerminateMessageFilter.
91 #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
92 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
93 defined(UNDEFINED_SANITIZER)
94 // A thread delegate that waits for |duration| and then exits the process with
96 class WaitAndExitDelegate
: public base::PlatformThread::Delegate
{
98 explicit WaitAndExitDelegate(base::TimeDelta duration
)
99 : duration_(duration
) {}
101 void ThreadMain() override
{
102 base::PlatformThread::Sleep(duration_
);
107 const base::TimeDelta duration_
;
108 DISALLOW_COPY_AND_ASSIGN(WaitAndExitDelegate
);
111 bool CreateWaitAndExitThread(base::TimeDelta duration
) {
112 scoped_ptr
<WaitAndExitDelegate
> delegate(new WaitAndExitDelegate(duration
));
114 const bool thread_created
=
115 base::PlatformThread::CreateNonJoinable(0, delegate
.get());
119 // A non joinable thread has been created. The thread will either terminate
120 // the process or will be terminated by the process. Therefore, keep the
121 // delegate object alive for the lifetime of the process.
122 WaitAndExitDelegate
* leaking_delegate
= delegate
.release();
123 ANNOTATE_LEAKING_OBJECT_PTR(leaking_delegate
);
124 ignore_result(leaking_delegate
);
129 class SuicideOnChannelErrorFilter
: public IPC::MessageFilter
{
131 // IPC::MessageFilter
132 void OnChannelError() override
{
133 // For renderer/worker processes:
134 // On POSIX, at least, one can install an unload handler which loops
135 // forever and leave behind a renderer process which eats 100% CPU forever.
137 // This is because the terminate signals (FrameMsg_BeforeUnload and the
138 // error from the IPC sender) are routed to the main message loop but never
139 // processed (because that message loop is stuck in V8).
141 // One could make the browser SIGKILL the renderers, but that leaves open a
142 // large window where a browser failure (or a user, manually terminating
143 // the browser because "it's stuck") will leave behind a process eating all
146 // So, we install a filter on the sender so that we can process this event
147 // here and kill the process.
148 base::debug::StopProfiling();
149 #if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
150 defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
151 defined(UNDEFINED_SANITIZER)
152 // Some sanitizer tools rely on exit handlers (e.g. to run leak detection,
153 // or dump code coverage data to disk). Instead of exiting the process
154 // immediately, we give it 60 seconds to run exit handlers.
155 CHECK(CreateWaitAndExitThread(base::TimeDelta::FromSeconds(60)));
156 #if defined(LEAK_SANITIZER)
157 // Invoke LeakSanitizer early to avoid detecting shutdown-only leaks. If
158 // leaks are found, the process will exit here.
159 __lsan_do_leak_check();
167 ~SuicideOnChannelErrorFilter() override
{}
172 #if defined(OS_MACOSX)
173 class IOSurfaceManagerFilter
: public IPC::MessageFilter
{
175 // Overridden from IPC::MessageFilter:
176 bool OnMessageReceived(const IPC::Message
& message
) override
{
178 IPC_BEGIN_MESSAGE_MAP(IOSurfaceManagerFilter
, message
)
179 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetIOSurfaceManagerToken
,
180 OnSetIOSurfaceManagerToken
)
181 IPC_MESSAGE_UNHANDLED(handled
= false)
182 IPC_END_MESSAGE_MAP()
187 ~IOSurfaceManagerFilter() override
{}
189 void OnSetIOSurfaceManagerToken(const IOSurfaceManagerToken
& token
) {
190 ChildIOSurfaceManager::GetInstance()->set_token(token
);
195 #if defined(OS_ANDROID)
196 // A class that allows for triggering a clean shutdown from another
197 // thread through draining the main thread's msg loop.
203 void BindToMainThread();
204 void PostQuitFromNonMainThread();
207 static void PostClosure(
208 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
209 base::Closure closure
);
212 base::ConditionVariable cond_var_
;
213 base::Closure closure_
;
216 QuitClosure::QuitClosure() : cond_var_(&lock_
) {
219 QuitClosure::~QuitClosure() {
222 void QuitClosure::PostClosure(
223 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
224 base::Closure closure
) {
225 task_runner
->PostTask(FROM_HERE
, closure
);
228 void QuitClosure::BindToMainThread() {
229 base::AutoLock
lock(lock_
);
230 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner(
231 base::ThreadTaskRunnerHandle::Get());
232 base::Closure quit_closure
=
233 base::MessageLoop::current()->QuitWhenIdleClosure();
234 closure_
= base::Bind(&QuitClosure::PostClosure
, task_runner
, quit_closure
);
238 void QuitClosure::PostQuitFromNonMainThread() {
239 base::AutoLock
lock(lock_
);
240 while (closure_
.is_null())
246 base::LazyInstance
<QuitClosure
> g_quit_closure
= LAZY_INSTANCE_INITIALIZER
;
251 ChildThread
* ChildThread::Get() {
252 return ChildThreadImpl::current();
255 ChildThreadImpl::Options::Options()
256 : channel_name(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
257 switches::kProcessChannelID
)),
258 use_mojo_channel(false) {
261 ChildThreadImpl::Options::~Options() {
264 ChildThreadImpl::Options::Builder::Builder() {
267 ChildThreadImpl::Options::Builder
&
268 ChildThreadImpl::Options::Builder::InBrowserProcess(
269 const InProcessChildThreadParams
& params
) {
270 options_
.browser_process_io_runner
= params
.io_runner();
271 options_
.channel_name
= params
.channel_name();
275 ChildThreadImpl::Options::Builder
&
276 ChildThreadImpl::Options::Builder::UseMojoChannel(bool use_mojo_channel
) {
277 options_
.use_mojo_channel
= use_mojo_channel
;
281 ChildThreadImpl::Options::Builder
&
282 ChildThreadImpl::Options::Builder::WithChannelName(
283 const std::string
& channel_name
) {
284 options_
.channel_name
= channel_name
;
288 ChildThreadImpl::Options::Builder
&
289 ChildThreadImpl::Options::Builder::AddStartupFilter(
290 IPC::MessageFilter
* filter
) {
291 options_
.startup_filters
.push_back(filter
);
295 ChildThreadImpl::Options
ChildThreadImpl::Options::Builder::Build() {
299 ChildThreadImpl::ChildThreadMessageRouter::ChildThreadMessageRouter(
303 bool ChildThreadImpl::ChildThreadMessageRouter::Send(IPC::Message
* msg
) {
304 return sender_
->Send(msg
);
307 ChildThreadImpl::ChildThreadImpl()
309 channel_connected_factory_(this) {
310 Init(Options::Builder().Build());
313 ChildThreadImpl::ChildThreadImpl(const Options
& options
)
315 browser_process_io_runner_(options
.browser_process_io_runner
),
316 channel_connected_factory_(this) {
320 scoped_refptr
<base::SequencedTaskRunner
> ChildThreadImpl::GetIOTaskRunner() {
321 if (IsInBrowserProcess())
322 return browser_process_io_runner_
;
323 return ChildProcess::current()->io_task_runner();
326 void ChildThreadImpl::ConnectChannel(bool use_mojo_channel
) {
327 bool create_pipe_now
= true;
328 if (use_mojo_channel
) {
329 VLOG(1) << "Mojo is enabled on child";
330 scoped_refptr
<base::SequencedTaskRunner
> io_task_runner
= GetIOTaskRunner();
331 DCHECK(io_task_runner
);
332 channel_
->Init(IPC::ChannelMojo::CreateClientFactory(
333 io_task_runner
, channel_name_
, attachment_broker_
.get()),
338 VLOG(1) << "Mojo is disabled on child";
339 channel_
->Init(channel_name_
, IPC::Channel::MODE_CLIENT
, create_pipe_now
,
340 attachment_broker_
.get());
343 void ChildThreadImpl::Init(const Options
& options
) {
344 channel_name_
= options
.channel_name
;
346 g_lazy_tls
.Pointer()->Set(this);
347 on_channel_error_called_
= false;
348 message_loop_
= base::MessageLoop::current();
349 #ifdef IPC_MESSAGE_LOG_ENABLED
350 // We must make sure to instantiate the IPC Logger *before* we create the
351 // channel, otherwise we can get a callback on the IO thread which creates
352 // the logger, and the logger does not like being created on the IO thread.
353 IPC::Logging::GetInstance();
356 IPC::SyncChannel::Create(this, ChildProcess::current()->io_task_runner(),
357 ChildProcess::current()->GetShutDownEvent());
358 #ifdef IPC_MESSAGE_LOG_ENABLED
359 if (!IsInBrowserProcess())
360 IPC::Logging::GetInstance()->SetIPCSender(this);
364 attachment_broker_
.reset(new IPC::AttachmentBrokerUnprivilegedWin());
367 mojo_application_
.reset(new MojoApplication(GetIOTaskRunner()));
369 sync_message_filter_
= channel_
->CreateSyncMessageFilter();
370 thread_safe_sender_
= new ThreadSafeSender(
371 message_loop_
->task_runner(), sync_message_filter_
.get());
373 resource_dispatcher_
.reset(new ResourceDispatcher(
374 this, message_loop()->task_runner()));
375 websocket_dispatcher_
.reset(new WebSocketDispatcher
);
376 file_system_dispatcher_
.reset(new FileSystemDispatcher());
378 histogram_message_filter_
= new ChildHistogramMessageFilter();
379 resource_message_filter_
=
380 new ChildResourceMessageFilter(resource_dispatcher());
382 service_worker_message_filter_
=
383 new ServiceWorkerMessageFilter(thread_safe_sender_
.get());
385 quota_message_filter_
=
386 new QuotaMessageFilter(thread_safe_sender_
.get());
387 quota_dispatcher_
.reset(new QuotaDispatcher(thread_safe_sender_
.get(),
388 quota_message_filter_
.get()));
389 geofencing_message_filter_
=
390 new GeofencingMessageFilter(thread_safe_sender_
.get());
391 notification_dispatcher_
=
392 new NotificationDispatcher(thread_safe_sender_
.get());
393 push_dispatcher_
= new PushDispatcher(thread_safe_sender_
.get());
395 channel_
->AddFilter(histogram_message_filter_
.get());
396 channel_
->AddFilter(resource_message_filter_
.get());
397 channel_
->AddFilter(quota_message_filter_
->GetFilter());
398 channel_
->AddFilter(notification_dispatcher_
->GetFilter());
399 channel_
->AddFilter(push_dispatcher_
->GetFilter());
400 channel_
->AddFilter(service_worker_message_filter_
->GetFilter());
401 channel_
->AddFilter(geofencing_message_filter_
->GetFilter());
403 if (!IsInBrowserProcess()) {
404 // In single process mode, browser-side tracing will cover the whole
405 // process including renderers.
406 channel_
->AddFilter(new tracing::ChildTraceMessageFilter(
407 ChildProcess::current()->io_task_runner()));
410 // In single process mode we may already have a power monitor
411 if (!base::PowerMonitor::Get()) {
412 scoped_ptr
<PowerMonitorBroadcastSource
> power_monitor_source(
413 new PowerMonitorBroadcastSource());
414 channel_
->AddFilter(power_monitor_source
->GetMessageFilter());
416 power_monitor_
.reset(new base::PowerMonitor(
417 power_monitor_source
.Pass()));
420 #if defined(OS_POSIX)
421 // Check that --process-type is specified so we don't do this in unit tests
422 // and single-process mode.
423 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kProcessType
))
424 channel_
->AddFilter(new SuicideOnChannelErrorFilter());
427 #if defined(OS_MACOSX)
428 channel_
->AddFilter(new IOSurfaceManagerFilter());
431 // Add filters passed here via options.
432 for (auto startup_filter
: options
.startup_filters
) {
433 channel_
->AddFilter(startup_filter
);
436 ConnectChannel(options
.use_mojo_channel
);
437 if (attachment_broker_
)
438 attachment_broker_
->DesignateBrokerCommunicationChannel(channel_
.get());
440 int connection_timeout
= kConnectionTimeoutS
;
441 std::string connection_override
=
442 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
443 switches::kIPCConnectionTimeout
);
444 if (!connection_override
.empty()) {
446 if (base::StringToInt(connection_override
, &temp
))
447 connection_timeout
= temp
;
450 message_loop_
->task_runner()->PostDelayedTask(
451 FROM_HERE
, base::Bind(&ChildThreadImpl::EnsureConnected
,
452 channel_connected_factory_
.GetWeakPtr()),
453 base::TimeDelta::FromSeconds(connection_timeout
));
455 #if defined(OS_ANDROID)
456 g_quit_closure
.Get().BindToMainThread();
459 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED)
460 trace_memory_controller_
.reset(new base::trace_event::TraceMemoryController(
461 message_loop_
->task_runner(), ::HeapProfilerWithPseudoStackStart
,
462 ::HeapProfilerStop
, ::GetHeapProfile
));
465 base::trace_event::MemoryDumpManager::GetInstance()->Initialize();
467 shared_bitmap_manager_
.reset(
468 new ChildSharedBitmapManager(thread_safe_sender()));
470 gpu_memory_buffer_manager_
.reset(
471 new ChildGpuMemoryBufferManager(thread_safe_sender()));
473 discardable_shared_memory_manager_
.reset(
474 new ChildDiscardableSharedMemoryManager(thread_safe_sender()));
477 ChildThreadImpl::~ChildThreadImpl() {
478 // ChildDiscardableSharedMemoryManager has to be destroyed while
479 // |thread_safe_sender_| is still valid.
480 discardable_shared_memory_manager_
.reset();
482 #ifdef IPC_MESSAGE_LOG_ENABLED
483 IPC::Logging::GetInstance()->SetIPCSender(NULL
);
486 channel_
->RemoveFilter(histogram_message_filter_
.get());
487 channel_
->RemoveFilter(sync_message_filter_
.get());
489 // The ChannelProxy object caches a pointer to the IPC thread, so need to
490 // reset it as it's not guaranteed to outlive this object.
491 // NOTE: this also has the side-effect of not closing the main IPC channel to
492 // the browser process. This is needed because this is the signal that the
493 // browser uses to know that this process has died, so we need it to be alive
494 // until this process is shut down, and the OS closes the handle
495 // automatically. We used to watch the object handle on Windows to do this,
496 // but it wasn't possible to do so on POSIX.
497 channel_
->ClearIPCTaskRunner();
498 g_lazy_tls
.Pointer()->Set(NULL
);
501 void ChildThreadImpl::Shutdown() {
502 // Delete objects that hold references to blink so derived classes can
503 // safely shutdown blink in their Shutdown implementation.
504 file_system_dispatcher_
.reset();
505 quota_dispatcher_
.reset();
506 WebFileSystemImpl::DeleteThreadSpecificInstance();
509 void ChildThreadImpl::OnChannelConnected(int32 peer_pid
) {
510 channel_connected_factory_
.InvalidateWeakPtrs();
513 void ChildThreadImpl::OnChannelError() {
514 set_on_channel_error_called(true);
515 base::MessageLoop::current()->Quit();
518 bool ChildThreadImpl::Send(IPC::Message
* msg
) {
519 DCHECK(base::MessageLoop::current() == message_loop());
525 return channel_
->Send(msg
);
529 void ChildThreadImpl::PreCacheFont(const LOGFONT
& log_font
) {
530 Send(new ChildProcessHostMsg_PreCacheFont(log_font
));
533 void ChildThreadImpl::ReleaseCachedFonts() {
534 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
538 IPC::AttachmentBroker
* ChildThreadImpl::GetAttachmentBroker() {
539 return attachment_broker_
.get();
542 MessageRouter
* ChildThreadImpl::GetRouter() {
543 DCHECK(base::MessageLoop::current() == message_loop());
547 scoped_ptr
<base::SharedMemory
> ChildThreadImpl::AllocateSharedMemory(
549 DCHECK(base::MessageLoop::current() == message_loop());
550 return AllocateSharedMemory(buf_size
, this);
554 scoped_ptr
<base::SharedMemory
> ChildThreadImpl::AllocateSharedMemory(
556 IPC::Sender
* sender
) {
557 scoped_ptr
<base::SharedMemory
> shared_buf
;
559 shared_buf
.reset(new base::SharedMemory
);
560 if (!shared_buf
->CreateAnonymous(buf_size
)) {
565 // On POSIX, we need to ask the browser to create the shared memory for us,
566 // since this is blocked by the sandbox.
567 base::SharedMemoryHandle shared_mem_handle
;
568 if (sender
->Send(new ChildProcessHostMsg_SyncAllocateSharedMemory(
569 buf_size
, &shared_mem_handle
))) {
570 if (base::SharedMemory::IsHandleValid(shared_mem_handle
)) {
571 shared_buf
.reset(new base::SharedMemory(shared_mem_handle
, false));
573 NOTREACHED() << "Browser failed to allocate shared memory";
577 NOTREACHED() << "Browser allocation request message failed";
584 bool ChildThreadImpl::OnMessageReceived(const IPC::Message
& msg
) {
585 if (mojo_application_
->OnMessageReceived(msg
))
588 // Resource responses are sent to the resource dispatcher.
589 if (resource_dispatcher_
->OnMessageReceived(msg
))
591 if (websocket_dispatcher_
->OnMessageReceived(msg
))
593 if (file_system_dispatcher_
->OnMessageReceived(msg
))
597 IPC_BEGIN_MESSAGE_MAP(ChildThreadImpl
, msg
)
598 IPC_MESSAGE_HANDLER(ChildProcessMsg_Shutdown
, OnShutdown
)
599 #if defined(IPC_MESSAGE_LOG_ENABLED)
600 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetIPCLoggingEnabled
,
601 OnSetIPCLoggingEnabled
)
603 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetProfilerStatus
,
605 IPC_MESSAGE_HANDLER(ChildProcessMsg_GetChildProfilerData
,
606 OnGetChildProfilerData
)
607 IPC_MESSAGE_HANDLER(ChildProcessMsg_ProfilingPhaseCompleted
,
608 OnProfilingPhaseCompleted
)
609 IPC_MESSAGE_HANDLER(ChildProcessMsg_SetProcessBackgrounded
,
610 OnProcessBackgrounded
)
611 #if defined(USE_TCMALLOC)
612 IPC_MESSAGE_HANDLER(ChildProcessMsg_GetTcmallocStats
, OnGetTcmallocStats
)
614 IPC_MESSAGE_UNHANDLED(handled
= false)
615 IPC_END_MESSAGE_MAP()
620 if (msg
.routing_id() == MSG_ROUTING_CONTROL
)
621 return OnControlMessageReceived(msg
);
623 return router_
.OnMessageReceived(msg
);
626 bool ChildThreadImpl::OnControlMessageReceived(const IPC::Message
& msg
) {
630 void ChildThreadImpl::OnShutdown() {
631 base::MessageLoop::current()->Quit();
634 #if defined(IPC_MESSAGE_LOG_ENABLED)
635 void ChildThreadImpl::OnSetIPCLoggingEnabled(bool enable
) {
637 IPC::Logging::GetInstance()->Enable();
639 IPC::Logging::GetInstance()->Disable();
641 #endif // IPC_MESSAGE_LOG_ENABLED
643 void ChildThreadImpl::OnSetProfilerStatus(ThreadData::Status status
) {
644 ThreadData::InitializeAndSetTrackingStatus(status
);
647 void ChildThreadImpl::OnGetChildProfilerData(int sequence_number
,
648 int current_profiling_phase
) {
649 tracked_objects::ProcessDataSnapshot process_data
;
650 ThreadData::Snapshot(current_profiling_phase
, &process_data
);
653 new ChildProcessHostMsg_ChildProfilerData(sequence_number
, process_data
));
656 void ChildThreadImpl::OnProfilingPhaseCompleted(int profiling_phase
) {
657 ThreadData::OnProfilingPhaseCompleted(profiling_phase
);
660 #if defined(USE_TCMALLOC)
661 void ChildThreadImpl::OnGetTcmallocStats() {
663 char buffer
[1024 * 32];
664 base::allocator::GetStats(buffer
, sizeof(buffer
));
665 result
.append(buffer
);
666 Send(new ChildProcessHostMsg_TcmallocStats(result
));
670 ChildThreadImpl
* ChildThreadImpl::current() {
671 return g_lazy_tls
.Pointer()->Get();
674 #if defined(OS_ANDROID)
675 // The method must NOT be called on the child thread itself.
676 // It may block the child thread if so.
677 void ChildThreadImpl::ShutdownThread() {
678 DCHECK(!ChildThreadImpl::current()) <<
679 "this method should NOT be called from child thread itself";
680 g_quit_closure
.Get().PostQuitFromNonMainThread();
684 void ChildThreadImpl::OnProcessFinalRelease() {
685 if (on_channel_error_called_
) {
686 base::MessageLoop::current()->Quit();
690 // The child process shutdown sequence is a request response based mechanism,
691 // where we send out an initial feeler request to the child process host
692 // instance in the browser to verify if it's ok to shutdown the child process.
693 // The browser then sends back a response if it's ok to shutdown. This avoids
694 // race conditions if the process refcount is 0 but there's an IPC message
695 // inflight that would addref it.
696 Send(new ChildProcessHostMsg_ShutdownRequest
);
699 void ChildThreadImpl::EnsureConnected() {
700 VLOG(0) << "ChildThreadImpl::EnsureConnected()";
701 base::Process::Current().Terminate(0, false);
704 bool ChildThreadImpl::IsInBrowserProcess() const {
705 return browser_process_io_runner_
;
708 void ChildThreadImpl::OnProcessBackgrounded(bool background
) {
709 // Set timer slack to maximum on main thread when in background.
710 base::TimerSlack timer_slack
= base::TIMER_SLACK_NONE
;
712 timer_slack
= base::TIMER_SLACK_MAXIMUM
;
713 base::MessageLoop::current()->SetTimerSlack(timer_slack
);
716 } // namespace content