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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
content / browser / compute_pressure / pressure_service_for_worker_unittest.cc [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 <vector>
#include "base/barrier_closure.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ref.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "content/browser/compute_pressure/pressure_client_impl.h"
#include "content/browser/compute_pressure/web_contents_pressure_manager_proxy.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/worker_host/dedicated_worker_host.h"
#include "content/browser/worker_host/dedicated_worker_service_impl.h"
#include "content/browser/worker_host/shared_worker_host.h"
#include "content/browser/worker_host/shared_worker_service_impl.h"
#include "content/public/browser/shared_worker_instance.h"
#include "content/public/test/navigation_simulator.h"
#include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "services/device/public/cpp/test/scoped_pressure_manager_overrider.h"
#include "services/device/public/mojom/pressure_manager.mojom.h"
#include "services/device/public/mojom/pressure_update.mojom.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/permissions_policy/permissions_policy.h"
#include "third_party/blink/public/common/permissions_policy/permissions_policy_declaration.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/compute_pressure/web_pressure_manager.mojom.h"
#include "url/gurl.h"
namespace content {
using device::mojom::PressureManagerAddClientError;
using device::mojom::PressureSource;
using device::mojom::PressureState;
using device::mojom::PressureUpdate;
namespace {
// Test double for PressureClient that records all updates.
class FakePressureClient : public device::mojom::PressureClient {
public:
FakePressureClient() : receiver_(this) {}
~FakePressureClient() override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
FakePressureClient(const FakePressureClient&) = delete;
FakePressureClient& operator=(const FakePressureClient&) = delete;
// device::mojom::PressureClient implementation.
void OnPressureUpdated(device::mojom::PressureUpdatePtr state) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
updates_.push_back(*state);
if (update_callback_) {
std::move(update_callback_).Run();
update_callback_.Reset();
}
}
std::vector<PressureUpdate>& updates() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return updates_;
}
void SetNextUpdateCallback(base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(!update_callback_) << " already called before update received";
update_callback_ = std::move(callback);
}
void WaitForUpdate() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::RunLoop run_loop;
SetNextUpdateCallback(run_loop.QuitClosure());
run_loop.Run();
}
static void WaitForUpdates(
std::initializer_list<FakePressureClient*> clients) {
base::RunLoop run_loop;
base::RepeatingClosure update_barrier =
base::BarrierClosure(clients.size(), run_loop.QuitClosure());
for (FakePressureClient* client : clients) {
client->SetNextUpdateCallback(update_barrier);
}
run_loop.Run();
}
void Bind(
mojo::PendingReceiver<device::mojom::PressureClient> pending_receiver) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
receiver_.Bind(std::move(pending_receiver));
}
private:
SEQUENCE_CHECKER(sequence_checker_);
std::vector<PressureUpdate> updates_ GUARDED_BY_CONTEXT(sequence_checker_);
// Used to implement WaitForUpdate().
base::OnceClosure update_callback_ GUARDED_BY_CONTEXT(sequence_checker_);
mojo::Receiver<device::mojom::PressureClient> receiver_
GUARDED_BY_CONTEXT(sequence_checker_);
};
} // namespace
class PressureServiceForDedicatedWorkerTest
: public RenderViewHostImplTestHarness {
public:
PressureServiceForDedicatedWorkerTest() = default;
~PressureServiceForDedicatedWorkerTest() override = default;
PressureServiceForDedicatedWorkerTest(
const PressureServiceForDedicatedWorkerTest&) = delete;
PressureServiceForDedicatedWorkerTest& operator=(
const PressureServiceForDedicatedWorkerTest&) = delete;
void TearDown() override {
pressure_manager_overrider_.reset();
worker_host_.reset();
task_environment()->RunUntilIdle();
RenderViewHostImplTestHarness::TearDown();
}
void SetPressureServiceForDedicatedWorker() {
pressure_manager_overrider_ =
std::make_unique<device::ScopedPressureManagerOverrider>();
pressure_manager_.reset();
auto* rfh = contents()->GetPrimaryMainFrame();
CHECK_EQ(rfh->GetLastCommittedOrigin(), rfh->GetStorageKey().origin());
worker_host_ = std::make_unique<DedicatedWorkerHost>(
&worker_service_, blink::DedicatedWorkerToken(), rfh->GetProcess(),
rfh->GetGlobalId(), rfh->GetGlobalId(), rfh->GetStorageKey(),
rfh->GetStorageKey().origin(), rfh->GetIsolationInfoForSubresources(),
rfh->BuildClientSecurityState(), nullptr, nullptr,
mojo::PendingReceiver<blink::mojom::DedicatedWorkerHost>());
mojo::Receiver<blink::mojom::BrowserInterfaceBroker>& bib =
worker_host_->browser_interface_broker_receiver_for_testing();
blink::mojom::BrowserInterfaceBroker* broker = bib.internal_state()->impl();
broker->GetInterface(pressure_manager_.BindNewPipeAndPassReceiver());
// Focus on the page and frame to make HasImplicitFocus() return true
// by default.
rfh->GetRenderWidgetHost()->Focus();
FocusWebContentsOnMainFrame();
task_environment()->RunUntilIdle();
}
protected:
const GURL kTestUrl{"https://example.com/compute_pressure.html"};
mojo::Remote<blink::mojom::WebPressureManager> pressure_manager_;
std::unique_ptr<device::ScopedPressureManagerOverrider>
pressure_manager_overrider_;
DedicatedWorkerServiceImpl worker_service_;
std::unique_ptr<DedicatedWorkerHost> worker_host_;
};
TEST_F(PressureServiceForDedicatedWorkerTest, AddClient) {
NavigateAndCommit(kTestUrl);
SetPressureServiceForDedicatedWorker();
FakePressureClient client;
base::test::TestFuture<device::mojom::PressureManagerAddClientResultPtr>
future;
pressure_manager_->AddClient(PressureSource::kCpu, future.GetCallback());
ASSERT_TRUE(future.Get()->is_pressure_client());
auto result = future.Take();
client.Bind(std::move(result->get_pressure_client()));
const base::TimeTicks time = base::TimeTicks::Now();
PressureUpdate update(PressureSource::kCpu, PressureState::kNominal, time);
pressure_manager_overrider_->UpdateClients(update);
client.WaitForUpdate();
ASSERT_EQ(client.updates().size(), 1u);
EXPECT_EQ(client.updates()[0], update);
}
TEST_F(PressureServiceForDedicatedWorkerTest,
WebContentPressureManagerProxyTest) {
NavigateAndCommit(kTestUrl);
SetPressureServiceForDedicatedWorker();
ASSERT_NE(worker_host_->pressure_service(), nullptr);
auto* web_contents =
WebContents::FromRenderFrameHost(RenderFrameHostImpl::FromID(
worker_host_->GetAncestorRenderFrameHostId()));
EXPECT_EQ(WebContentsPressureManagerProxy::FromWebContents(web_contents),
nullptr);
auto* pressure_manager_proxy =
WebContentsPressureManagerProxy::GetOrCreate(web_contents);
EXPECT_NE(pressure_manager_proxy, nullptr);
EXPECT_EQ(pressure_manager_proxy,
WebContentsPressureManagerProxy::FromWebContents(web_contents));
EXPECT_EQ(worker_host_->pressure_service()->GetTokenFor(PressureSource::kCpu),
std::nullopt);
{
auto pressure_source =
pressure_manager_proxy->CreateVirtualPressureSourceForDevTools(
PressureSource::kCpu,
device::mojom::VirtualPressureSourceMetadata::New());
EXPECT_NE(
worker_host_->pressure_service()->GetTokenFor(PressureSource::kCpu),
std::nullopt);
}
EXPECT_EQ(worker_host_->pressure_service()->GetTokenFor(PressureSource::kCpu),
std::nullopt);
}
TEST_F(PressureServiceForDedicatedWorkerTest, PermissionsPolicyBlock) {
// Make compute pressure blocked by permissions policy and it can only be
// made once on page load, so we refresh the page to simulate that.
blink::ParsedPermissionsPolicy permissions_policy(1);
permissions_policy[0].feature =
blink::mojom::PermissionsPolicyFeature::kComputePressure;
auto navigation_simulator =
NavigationSimulator::CreateRendererInitiated(kTestUrl, main_rfh());
navigation_simulator->SetPermissionsPolicyHeader(permissions_policy);
navigation_simulator->Commit();
SetPressureServiceForDedicatedWorker();
EXPECT_EQ(worker_host_->pressure_service(), nullptr);
}
class PressureServiceForSharedWorkerTest
: public RenderViewHostImplTestHarness {
public:
PressureServiceForSharedWorkerTest() = default;
~PressureServiceForSharedWorkerTest() override = default;
PressureServiceForSharedWorkerTest(
const PressureServiceForSharedWorkerTest&) = delete;
PressureServiceForSharedWorkerTest& operator=(
const PressureServiceForSharedWorkerTest&) = delete;
void TearDown() override {
pressure_manager_overrider_.reset();
worker_host_.reset();
worker_service_.reset();
task_environment()->RunUntilIdle();
RenderViewHostImplTestHarness::TearDown();
}
void SetPressureServiceForSharedWorker() {
pressure_manager_overrider_ =
std::make_unique<device::ScopedPressureManagerOverrider>();
pressure_manager_.reset();
auto* rfh = contents()->GetPrimaryMainFrame();
SharedWorkerInstance instance(
kWorkerUrl, blink::mojom::ScriptType::kClassic,
network::mojom::CredentialsMode::kSameOrigin, "name",
rfh->GetStorageKey(),
blink::mojom::SharedWorkerCreationContextType::kSecure,
rfh->GetStorageKey().IsFirstPartyContext()
? blink::mojom::SharedWorkerSameSiteCookies::kAll
: blink::mojom::SharedWorkerSameSiteCookies::kNone);
worker_service_ = std::make_unique<SharedWorkerServiceImpl>(
rfh->GetStoragePartition(), nullptr /* service_worker_context */);
worker_host_ = std::make_unique<SharedWorkerHost>(
worker_service_.get(), instance, rfh->GetSiteInstance(),
std::vector<network::mojom::ContentSecurityPolicyPtr>(),
base::MakeRefCounted<PolicyContainerHost>());
AddClient(receiver_.InitWithNewPipeAndPassRemote(), rfh->GetGlobalId(),
blink::MessagePortChannel(port_pair_.TakePort0()),
kClientUkmSourceId);
mojo::Receiver<blink::mojom::BrowserInterfaceBroker>& bib =
worker_host_->browser_interface_broker_receiver_for_testing();
blink::mojom::BrowserInterfaceBroker* broker = bib.internal_state()->impl();
broker->GetInterface(pressure_manager_.BindNewPipeAndPassReceiver());
// Focus on the page and frame to make HasImplicitFocus() return true
// by default.
rfh->GetRenderWidgetHost()->Focus();
FocusWebContentsOnMainFrame();
task_environment()->RunUntilIdle();
}
void AddClient(mojo::PendingRemote<blink::mojom::SharedWorkerClient> client,
GlobalRenderFrameHostId client_render_frame_host_id,
const blink::MessagePortChannel& port,
ukm::SourceId client_ukm_source_id) {
worker_host_->AddClient(std::move(client), client_render_frame_host_id,
port, client_ukm_source_id);
}
protected:
const GURL kTestUrl{"https://example.com/compute_pressure.html"};
const GURL kWorkerUrl{"https://example.com/w.js"};
const ukm::SourceId kClientUkmSourceId = 12345;
mojo::Remote<blink::mojom::WebPressureManager> pressure_manager_;
mojo::PendingReceiver<blink::mojom::SharedWorkerClient> receiver_;
std::unique_ptr<device::ScopedPressureManagerOverrider>
pressure_manager_overrider_;
blink::MessagePortDescriptorPair port_pair_;
std::unique_ptr<SharedWorkerServiceImpl> worker_service_;
std::unique_ptr<SharedWorkerHost> worker_host_;
};
TEST_F(PressureServiceForSharedWorkerTest, AddClient) {
NavigateAndCommit(kTestUrl);
SetPressureServiceForSharedWorker();
FakePressureClient client;
base::test::TestFuture<device::mojom::PressureManagerAddClientResultPtr>
future;
pressure_manager_->AddClient(PressureSource::kCpu, future.GetCallback());
ASSERT_TRUE(future.Get()->is_pressure_client());
auto result = future.Take();
client.Bind(std::move(result->get_pressure_client()));
const base::TimeTicks time = base::TimeTicks::Now();
PressureUpdate update(PressureSource::kCpu, PressureState::kNominal, time);
pressure_manager_overrider_->UpdateClients(update);
client.WaitForUpdate();
ASSERT_EQ(client.updates().size(), 1u);
EXPECT_EQ(client.updates()[0], update);
}
TEST_F(PressureServiceForSharedWorkerTest, WebContentPressureManagerProxyTest) {
NavigateAndCommit(kTestUrl);
SetPressureServiceForSharedWorker();
ASSERT_NE(worker_host_->pressure_service(), nullptr);
// Not much to test for shared workers: there is no way to retrieve a
// WebContentsPressureManagerProxy instance since there isn't one single
// WebContents we could use.
EXPECT_EQ(worker_host_->pressure_service()->GetTokenFor(PressureSource::kCpu),
std::nullopt);
}
TEST_F(PressureServiceForSharedWorkerTest, PermissionsPolicyBlock) {
// Make compute pressure blocked by permissions policy and it can only be
// made once on page load, so we refresh the page to simulate that.
blink::ParsedPermissionsPolicy permissions_policy(1);
permissions_policy[0].feature =
blink::mojom::PermissionsPolicyFeature::kComputePressure;
auto navigation_simulator =
NavigationSimulator::CreateRendererInitiated(kTestUrl, main_rfh());
navigation_simulator->SetPermissionsPolicyHeader(permissions_policy);
navigation_simulator->Commit();
SetPressureServiceForSharedWorker();
EXPECT_EQ(worker_host_->pressure_service(), nullptr);
}
TEST_F(PressureServiceForSharedWorkerTest,
ResetWhenClientWithoutPermissionsPolicy) {
NavigateAndCommit(kTestUrl);
SetPressureServiceForSharedWorker();
EXPECT_NE(worker_host_->pressure_service(), nullptr);
auto web_contents = TestWebContents::Create(browser_context(), nullptr);
auto* rfh = web_contents->GetPrimaryMainFrame();
blink::ParsedPermissionsPolicy permissions_policy(1);
permissions_policy[0].feature =
blink::mojom::PermissionsPolicyFeature::kComputePressure;
auto navigation_simulator =
NavigationSimulator::CreateRendererInitiated(kTestUrl, rfh);
navigation_simulator->SetPermissionsPolicyHeader(permissions_policy);
navigation_simulator->Commit();
mojo::PendingReceiver<blink::mojom::SharedWorkerClient> receiver;
blink::MessagePortDescriptorPair port_pair;
AddClient(receiver.InitWithNewPipeAndPassRemote(), rfh->GetGlobalId(),
blink::MessagePortChannel(port_pair.TakePort0()),
kClientUkmSourceId);
EXPECT_EQ(worker_host_->pressure_service(), nullptr);
}
} // namespace content