1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/process/kill.h"
6
7 #include "base/task/thread_pool.h"
8
9 namespace base {
10
EnsureProcessTerminated(Process process)11 void EnsureProcessTerminated(Process process) {
12 DCHECK(!process.is_current());
13
14 constexpr int kWaitBeforeKillSeconds = 2;
15
16 #if TARGET_OS_SIMULATOR
17 // For iOS we support "content processes", processes that are launched using
18 // the BrowserEngineKit APIs (which have well defined roles and sandbox
19 // restricitons). For iOS simulator we additionally support processes that are
20 // forked so we can run tests (via gtest) in parallel.
21 if (!process.IsContentProcess()) {
22 WaitForChildToDie(process.Pid(), kWaitBeforeKillSeconds);
23 return;
24 }
25 #endif
26
27 if (process.WaitForExitWithTimeout(TimeDelta(), nullptr)) {
28 return;
29 }
30
31 ThreadPool::PostDelayedTask(
32 FROM_HERE,
33 {TaskPriority::BEST_EFFORT, TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
34 BindOnce(
35 [](Process process) {
36 if (process.WaitForExitWithTimeout(TimeDelta(), nullptr)) {
37 return;
38 }
39 process.Terminate(-1, false);
40 },
41 std::move(process)),
42 Seconds(kWaitBeforeKillSeconds));
43 }
44
45 } // namespace base
46