1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
content / browser / gpu / browser_child_process_backgrounded_bridge_browsertest.mm [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/gpu/browser_child_process_backgrounded_bridge.h"
#include "base/mac/mac_util.h"
#include "base/process/process.h"
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/gpu/gpu_process_host.h"
#include "content/public/browser/browser_child_process_host.h"
#include "content/public/browser/child_process_launcher_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "gpu/config/gpu_finch_features.h"
namespace content {
base::Process::Priority GetProcessPriority(base::ProcessId pid) {
base::Process process = base::Process::Open(pid);
if (process.is_current()) {
base::SelfPortProvider self_port_provider;
return process.GetPriority(&self_port_provider);
}
return process.GetPriority(
content::BrowserChildProcessHost::GetPortProvider());
}
void SetProcessPriority(base::ProcessId pid, base::Process::Priority priority) {
base::Process process = base::Process::Open(pid);
if (process.is_current()) {
base::SelfPortProvider self_port_provider;
process.SetPriority(&self_port_provider, priority);
return;
}
process.SetPriority(content::BrowserChildProcessHost::GetPortProvider(),
priority);
}
class BrowserChildProcessBackgroundedBridgeTest
: public content::ContentBrowserTest,
public base::PortProvider::Observer,
public testing::WithParamInterface<bool> {
public:
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(
features::kAdjustGpuProcessPriority);
content::BrowserChildProcessBackgroundedBridge::
SetOSNotificationsEnabledForTesting(false);
content::ContentBrowserTest::SetUp();
}
void TearDown() override {
content::ContentBrowserTest::TearDown();
content::BrowserChildProcessBackgroundedBridge::
SetOSNotificationsEnabledForTesting(true);
}
// Waits until the port for the GPU process is available.
void WaitForPort() {
auto* port_provider = content::BrowserChildProcessHost::GetPortProvider();
// Note: On macOS, a process id and a process handle are the same thing.
DCHECK(port_provider->TaskForHandle(
content::GpuProcessHost::Get()->process_id()) == MACH_PORT_NULL);
port_provider->AddObserver(this);
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
run_loop.Run();
}
void EnsureBackgroundedStateChange() {
// Do a round-trip to the process launcher task to ensure any queued task is
// run.
base::RunLoop run_loop;
GetProcessLauncherTaskRunner()->PostTaskAndReply(
FROM_HERE, base::DoNothing(), run_loop.QuitClosure());
run_loop.Run();
}
private:
void OnReceivedTaskPort(base::ProcessHandle process_handle) override {
if (process_handle != content::GpuProcessHost::Get()->process_id()) {
return;
}
content::BrowserChildProcessHost::GetPortProvider()->RemoveObserver(this);
std::move(quit_closure_).Run();
}
base::test::ScopedFeatureList scoped_feature_list_;
base::OnceClosure quit_closure_;
};
IN_PROC_BROWSER_TEST_F(BrowserChildProcessBackgroundedBridgeTest,
InitiallyForegrounded) {
if (base::mac::MacOSMajorVersion() >= 13) {
GTEST_SKIP() << "Flaking on macOS 13: https://crbug.com/1444130";
}
// Set the browser process as foregrounded.
SetProcessPriority(base::Process::Current().Pid(),
base::Process::Priority::kUserBlocking);
// Wait until we receive the port for the GPU process.
WaitForPort();
// Ensure that the initial backgrounded state changed.
EnsureBackgroundedStateChange();
auto* gpu_process_host = content::GpuProcessHost::Get();
EXPECT_TRUE(gpu_process_host);
EXPECT_EQ(GetProcessPriority(gpu_process_host->process_id()),
base::Process::Priority::kUserBlocking);
}
// TODO(crbug.com/40899195): Disabled because this test is flaky.
IN_PROC_BROWSER_TEST_F(BrowserChildProcessBackgroundedBridgeTest,
DISABLED_InitiallyBackgrounded) {
// Set the browser process as backgrounded.
SetProcessPriority(base::Process::Current().Pid(),
base::Process::Priority::kBestEffort);
// Wait until we receive the port for the GPU process.
WaitForPort();
// Ensure that the initial backgrounded state changed.
EnsureBackgroundedStateChange();
auto* gpu_process_host = content::GpuProcessHost::Get();
EXPECT_TRUE(gpu_process_host);
EXPECT_EQ(GetProcessPriority(gpu_process_host->process_id()),
base::Process::Priority::kUserVisible);
}
// Flaky: https://crbug.com/1443367
IN_PROC_BROWSER_TEST_F(BrowserChildProcessBackgroundedBridgeTest,
DISABLED_OnBackgroundedStateChanged) {
// Wait until we receive the port for the GPU process.
WaitForPort();
auto* gpu_process_host = content::GpuProcessHost::Get();
EXPECT_TRUE(gpu_process_host);
auto* bridge =
gpu_process_host->browser_child_process_backgrounded_bridge_for_testing();
ASSERT_TRUE(bridge);
bridge->SimulateBrowserProcessForegroundedForTesting();
EnsureBackgroundedStateChange();
EXPECT_EQ(GetProcessPriority(gpu_process_host->process_id()),
base::Process::Priority::kUserBlocking);
bridge->SimulateBrowserProcessBackgroundedForTesting();
EnsureBackgroundedStateChange();
EXPECT_EQ(GetProcessPriority(gpu_process_host->process_id()),
base::Process::Priority::kUserVisible);
bridge->SimulateBrowserProcessForegroundedForTesting();
EnsureBackgroundedStateChange();
EXPECT_EQ(GetProcessPriority(gpu_process_host->process_id()),
base::Process::Priority::kUserBlocking);
}
} // namespace content