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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
content / browser / service_worker / embedded_worker_instance_unittest.cc [blame]
// Copyright 2013 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/service_worker/embedded_worker_instance.h"
#include <stdint.h>
#include <utility>
#include <vector>
#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "content/browser/service_worker/embedded_worker_test_helper.h"
#include "content/browser/service_worker/fake_embedded_worker_instance_client.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/public/browser/child_process_host.h"
#include "content/public/test/embedded_worker_instance_test_harness.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
#include "third_party/blink/public/mojom/service_worker/embedded_worker.mojom.h"
namespace content {
namespace {
using RegistrationAndVersionPair =
EmbeddedWorkerTestHelper::RegistrationAndVersionPair;
} // namespace
class EmbeddedWorkerInstanceTest : public EmbeddedWorkerInstanceTestHarness,
public EmbeddedWorkerInstance::Listener {
public:
EmbeddedWorkerInstanceTest()
: EmbeddedWorkerInstanceTestHarness(BrowserTaskEnvironment::IO_MAINLOOP) {
}
protected:
enum EventType {
PROCESS_ALLOCATED,
START_WORKER_MESSAGE_SENT,
STARTED,
STOPPED,
DETACHED,
};
struct EventLog {
EventType type;
std::optional<blink::EmbeddedWorkerStatus> status;
std::optional<blink::mojom::ServiceWorkerStartStatus> start_status;
};
void RecordEvent(
EventType type,
std::optional<blink::EmbeddedWorkerStatus> status = std::nullopt,
std::optional<blink::mojom::ServiceWorkerStartStatus> start_status =
std::nullopt) {
EventLog log = {type, status, start_status};
events_.push_back(log);
}
void OnProcessAllocated() override { RecordEvent(PROCESS_ALLOCATED); }
void OnStartWorkerMessageSent() override {
RecordEvent(START_WORKER_MESSAGE_SENT);
}
void OnStarted(blink::mojom::ServiceWorkerStartStatus status,
blink::mojom::ServiceWorkerFetchHandlerType fetch_handler_type,
bool has_hid_event_handlers,
bool has_usb_event_handlers) override {
fetch_handler_type_ = fetch_handler_type;
RecordEvent(STARTED, std::nullopt, status);
}
void OnStopped(blink::EmbeddedWorkerStatus old_status) override {
RecordEvent(STOPPED, old_status);
}
void OnDetached(blink::EmbeddedWorkerStatus old_status) override {
RecordEvent(DETACHED, old_status);
}
ServiceWorkerContextCore* context() { return helper_->context(); }
std::vector<EventLog> events_;
blink::mojom::ServiceWorkerFetchHandlerType fetch_handler_type_;
};
TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
worker->AddObserver(this);
// Start should succeed.
helper_->StartWorker(worker.get(), helper_->CreateStartParams(pair.second));
// The 'WorkerStarted' message should have been sent by
// EmbeddedWorkerTestHelper.
EXPECT_EQ(blink::EmbeddedWorkerStatus::kRunning, worker->status());
EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id());
// Stop the worker.
worker->Stop();
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopping, worker->status());
base::RunLoop().RunUntilIdle();
// The 'WorkerStopped' message should have been sent by
// EmbeddedWorkerTestHelper.
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
// Check if the IPCs are fired in expected order.
ASSERT_EQ(4u, events_.size());
EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
EXPECT_EQ(STARTED, events_[2].type);
EXPECT_EQ(blink::mojom::ServiceWorkerStartStatus::kNormalCompletion,
events_[2].start_status.value());
EXPECT_EQ(STOPPED, events_[3].type);
}
// Test that a worker that failed twice will use a new render process
// on the next attempt.
TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
const int64_t service_worker_version_id = pair.second->version_id();
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
{
// Start once normally.
helper_->StartWorker(worker.get(), helper_->CreateStartParams(pair.second));
// The worker should be using the default render process.
EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id());
worker->Stop();
base::RunLoop().RunUntilIdle();
}
// Fail twice.
context()->UpdateVersionFailureCount(
service_worker_version_id, blink::ServiceWorkerStatusCode::kErrorFailed);
context()->UpdateVersionFailureCount(
service_worker_version_id, blink::ServiceWorkerStatusCode::kErrorFailed);
{
// Start again.
base::RunLoop run_loop;
helper_->StartWorker(worker.get(), helper_->CreateStartParams(pair.second));
// The worker should be using the new render process.
EXPECT_EQ(helper_->new_render_process_id(), worker->process_id());
worker->Stop();
base::RunLoop().RunUntilIdle();
}
}
TEST_F(EmbeddedWorkerInstanceTest, StopWhenDevToolsAttached) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
// Start the worker and then call StopIfNotAttachedToDevTools().
helper_->StartWorker(worker.get(), helper_->CreateStartParams(pair.second));
EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id());
worker->StopIfNotAttachedToDevTools();
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopping, worker->status());
base::RunLoop().RunUntilIdle();
// The worker must be stopped now.
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
// Set devtools_attached to true, and do the same.
worker->SetDevToolsAttached(true);
helper_->StartWorker(worker.get(), helper_->CreateStartParams(pair.second));
EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id());
worker->StopIfNotAttachedToDevTools();
base::RunLoop().RunUntilIdle();
// The worker must not be stopped this time.
EXPECT_EQ(blink::EmbeddedWorkerStatus::kRunning, worker->status());
// Calling Stop() actually stops the worker regardless of whether devtools
// is attached or not.
worker->Stop();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
}
TEST_F(EmbeddedWorkerInstanceTest, DetachAfterSendingStartWorkerMessage) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
worker->AddObserver(this);
auto* client = helper_->AddNewPendingInstanceClient<
DelayedFakeEmbeddedWorkerInstanceClient>(helper_.get());
client->UnblockStopWorker();
helper_->StartWorkerUntilStartSent(worker.get(),
helper_->CreateStartParams(pair.second));
ASSERT_EQ(2u, events_.size());
EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
events_.clear();
worker->Detach();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id());
// "STARTED" event should not be recorded.
ASSERT_EQ(1u, events_.size());
EXPECT_EQ(DETACHED, events_[0].type);
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStarting, events_[0].status.value());
}
TEST_F(EmbeddedWorkerInstanceTest, StopAfterSendingStartWorkerMessage) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
worker->AddObserver(this);
auto* client = helper_->AddNewPendingInstanceClient<
DelayedFakeEmbeddedWorkerInstanceClient>(helper_.get());
client->UnblockStopWorker();
helper_->StartWorkerUntilStartSent(worker.get(),
helper_->CreateStartParams(pair.second));
ASSERT_EQ(2u, events_.size());
EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
events_.clear();
worker->Stop();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
EXPECT_EQ(ChildProcessHost::kInvalidUniqueID, worker->process_id());
// "STARTED" event should not be recorded.
ASSERT_EQ(1u, events_.size());
EXPECT_EQ(STOPPED, events_[0].type);
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopping, events_[0].status.value());
events_.clear();
// Restart the worker.
helper_->StartWorker(worker.get(), helper_->CreateStartParams(pair.second));
// The worker should be started.
ASSERT_EQ(3u, events_.size());
EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
EXPECT_EQ(STARTED, events_[2].type);
// Tear down the worker.
worker->Stop();
}
TEST_F(EmbeddedWorkerInstanceTest, Detach) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
// Start the worker.
helper_->StartWorker(worker.get(), helper_->CreateStartParams(pair.second));
// Detach.
worker->Detach();
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
}
// Test for when sending the start IPC failed.
TEST_F(EmbeddedWorkerInstanceTest, FailToSendStartIPC) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
// Let StartWorker fail; mojo IPC fails to connect to a remote interface.
helper_->AddPendingInstanceClient(nullptr);
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
worker->AddObserver(this);
// Attempt to start the worker. From the browser process's point of view, the
// start IPC was sent.
base::test::TestFuture<blink::ServiceWorkerStatusCode> future;
worker->Start(helper_->CreateStartParams(pair.second), future.GetCallback());
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk, future.Get());
// But the renderer should not receive the message and the binding is broken.
// Worker should handle the failure of binding on the remote side as detach.
base::RunLoop().RunUntilIdle();
ASSERT_EQ(3u, events_.size());
EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
EXPECT_EQ(DETACHED, events_[2].type);
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStarting, events_[2].status.value());
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
}
TEST_F(EmbeddedWorkerInstanceTest, RemoveRemoteInterface) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
worker->AddObserver(this);
// Attempt to start the worker.
auto* client = helper_->AddNewPendingInstanceClient<
DelayedFakeEmbeddedWorkerInstanceClient>(helper_.get());
base::test::TestFuture<blink::ServiceWorkerStatusCode> future;
worker->Start(helper_->CreateStartParams(pair.second), future.GetCallback());
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk, future.Get());
// Disconnect the Mojo connection. Worker should handle the sudden shutdown as
// detach.
client->RunUntilBound();
client->Disconnect();
base::RunLoop().RunUntilIdle();
ASSERT_EQ(3u, events_.size());
EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
EXPECT_EQ(DETACHED, events_[2].type);
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStarting, events_[2].status.value());
}
class RecordCacheStorageInstanceClient
: public FakeEmbeddedWorkerInstanceClient {
public:
explicit RecordCacheStorageInstanceClient(EmbeddedWorkerTestHelper* helper)
: FakeEmbeddedWorkerInstanceClient(helper) {}
~RecordCacheStorageInstanceClient() override = default;
void StartWorker(
blink::mojom::EmbeddedWorkerStartParamsPtr start_params) override {
had_cache_storage_ = start_params->provider_info->cache_storage &&
start_params->provider_info->cache_storage.is_valid();
FakeEmbeddedWorkerInstanceClient::StartWorker(std::move(start_params));
}
bool had_cache_storage() const { return had_cache_storage_; }
private:
bool had_cache_storage_ = false;
};
// Test that the worker is given a CacheStoragePtr during startup.
TEST_F(EmbeddedWorkerInstanceTest, CacheStorageOptimization) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
// We should set COEP, or cache storage pipe won't be made.
pair.second->set_policy_container_host(
base::MakeRefCounted<PolicyContainerHost>());
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
// Start the worker.
auto* client =
helper_->AddNewPendingInstanceClient<RecordCacheStorageInstanceClient>(
helper_.get());
helper_->StartWorker(worker.get(), helper_->CreateStartParams(pair.second));
// Cache storage should have been sent.
EXPECT_TRUE(client->had_cache_storage());
// Stop the worker.
worker->Stop();
base::RunLoop().RunUntilIdle();
}
// Starts the worker with kAbruptCompletion status.
class AbruptCompletionInstanceClient : public FakeEmbeddedWorkerInstanceClient {
public:
explicit AbruptCompletionInstanceClient(EmbeddedWorkerTestHelper* helper)
: FakeEmbeddedWorkerInstanceClient(helper) {}
~AbruptCompletionInstanceClient() override = default;
protected:
void EvaluateScript() override {
host()->OnScriptEvaluationStart();
host()->OnStarted(blink::mojom::ServiceWorkerStartStatus::kAbruptCompletion,
blink::mojom::ServiceWorkerFetchHandlerType::kNoHandler,
/*has_hid_event_handlers=*/false,
/*has_usb_event_handlers=*/false,
helper()->GetNextThreadId(),
blink::mojom::EmbeddedWorkerStartTiming::New());
}
};
// Tests that kAbruptCompletion is the OnStarted() status when the
// renderer reports abrupt completion.
TEST_F(EmbeddedWorkerInstanceTest, AbruptCompletion) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker = std::make_unique<EmbeddedWorkerInstance>(pair.second.get());
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker->status());
worker->AddObserver(this);
helper_->AddPendingInstanceClient(
std::make_unique<AbruptCompletionInstanceClient>(helper_.get()));
helper_->StartWorker(worker.get(), helper_->CreateStartParams(pair.second));
ASSERT_EQ(3u, events_.size());
EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type);
EXPECT_EQ(START_WORKER_MESSAGE_SENT, events_[1].type);
EXPECT_EQ(STARTED, events_[2].type);
EXPECT_EQ(blink::mojom::ServiceWorkerStartStatus::kAbruptCompletion,
events_[2].start_status.value());
worker->Stop();
}
// A fake instance client for toggling whether a fetch event handler exists.
class FetchHandlerInstanceClient : public FakeEmbeddedWorkerInstanceClient {
public:
explicit FetchHandlerInstanceClient(EmbeddedWorkerTestHelper* helper)
: FakeEmbeddedWorkerInstanceClient(helper) {}
~FetchHandlerInstanceClient() override = default;
void set_fetch_handler_type(
blink::mojom::ServiceWorkerFetchHandlerType fetch_handler_type) {
fetch_handler_type_ = fetch_handler_type;
}
protected:
void EvaluateScript() override {
host()->OnScriptEvaluationStart();
host()->OnStarted(blink::mojom::ServiceWorkerStartStatus::kNormalCompletion,
fetch_handler_type_, /*has_hid_event_handlers=*/false,
/*has_usb_event_handlers=*/false,
helper()->GetNextThreadId(),
blink::mojom::EmbeddedWorkerStartTiming::New());
}
private:
blink::mojom::ServiceWorkerFetchHandlerType fetch_handler_type_ =
blink::mojom::ServiceWorkerFetchHandlerType::kNoHandler;
};
// Tests that whether a fetch event handler exists.
TEST_F(EmbeddedWorkerInstanceTest, HasFetchHandler) {
const GURL scope("http://example.com/");
const GURL url("http://example.com/worker.js");
RegistrationAndVersionPair pair1 =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker1 = std::make_unique<EmbeddedWorkerInstance>(pair1.second.get());
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker1->status());
worker1->AddObserver(this);
auto* fetch_handler_worker =
helper_->AddNewPendingInstanceClient<FetchHandlerInstanceClient>(
helper_.get());
fetch_handler_worker->set_fetch_handler_type(
blink::mojom::ServiceWorkerFetchHandlerType::kNotSkippable);
helper_->StartWorker(worker1.get(), helper_->CreateStartParams(pair1.second));
EXPECT_NE(blink::mojom::ServiceWorkerFetchHandlerType::kNoHandler,
fetch_handler_type_);
worker1->Stop();
RegistrationAndVersionPair pair2 =
helper_->PrepareRegistrationAndVersion(scope, url);
auto worker2 = std::make_unique<EmbeddedWorkerInstance>(pair2.second.get());
EXPECT_EQ(blink::EmbeddedWorkerStatus::kStopped, worker2->status());
worker2->AddObserver(this);
auto* no_fetch_handler_worker =
helper_->AddNewPendingInstanceClient<FetchHandlerInstanceClient>(
helper_.get());
no_fetch_handler_worker->set_fetch_handler_type(
blink::mojom::ServiceWorkerFetchHandlerType::kNoHandler);
helper_->StartWorker(worker2.get(), helper_->CreateStartParams(pair2.second));
EXPECT_EQ(blink::mojom::ServiceWorkerFetchHandlerType::kNoHandler,
fetch_handler_type_);
worker2->Stop();
}
} // namespace content