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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
content / browser / service_worker / service_worker_context_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/public/browser/service_worker_context.h"
#include <stdint.h>
#include <memory>
#include "base/containers/contains.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/time/time.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_client.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_context_core_observer.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_host.h"
#include "content/browser/service_worker/service_worker_metrics.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_test_utils.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/public/browser/service_worker_context_observer.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/service_worker/service_worker_types.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
#include "url/origin.h"
namespace content {
namespace {
void SaveResponseCallback(bool* called,
int64_t* store_registration_id,
blink::ServiceWorkerStatusCode status,
const std::string& status_message,
int64_t registration_id) {
EXPECT_EQ(blink::ServiceWorkerStatusCode::kOk, status)
<< blink::ServiceWorkerStatusToString(status);
*called = true;
*store_registration_id = registration_id;
}
ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback(
bool* called,
int64_t* store_registration_id) {
return base::BindOnce(&SaveResponseCallback, called, store_registration_id);
}
void RegisteredCallback(base::OnceClosure quit_closure,
blink::ServiceWorkerStatusCode status,
const std::string& status_message,
int64_t registration_id) {
std::move(quit_closure).Run();
}
void CallCompletedCallback(bool* called, blink::ServiceWorkerStatusCode) {
*called = true;
}
ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback(
bool* called) {
return base::BindOnce(&CallCompletedCallback, called);
}
void ExpectRegisteredWorkers(
blink::ServiceWorkerStatusCode expect_status,
bool expect_waiting,
bool expect_active,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) {
ASSERT_EQ(expect_status, status);
if (status != blink::ServiceWorkerStatusCode::kOk) {
EXPECT_FALSE(registration.get());
return;
}
if (expect_waiting) {
EXPECT_TRUE(registration->waiting_version());
} else {
EXPECT_FALSE(registration->waiting_version());
}
if (expect_active) {
EXPECT_TRUE(registration->active_version());
} else {
EXPECT_FALSE(registration->active_version());
}
EXPECT_EQ(blink::mojom::ServiceWorkerUpdateViaCache::kImports,
registration->update_via_cache());
}
class InstallActivateWorker : public FakeServiceWorker {
public:
explicit InstallActivateWorker(EmbeddedWorkerTestHelper* helper)
: FakeServiceWorker(helper) {}
~InstallActivateWorker() override = default;
const std::vector<ServiceWorkerMetrics::EventType>& events() const {
return events_;
}
void SetToRejectInstall() { reject_install_ = true; }
void SetToRejectActivate() { reject_activate_ = true; }
void DispatchInstallEvent(
blink::mojom::ServiceWorker::DispatchInstallEventCallback callback)
override {
events_.emplace_back(ServiceWorkerMetrics::EventType::INSTALL);
std::move(callback).Run(
reject_install_ ? blink::mojom::ServiceWorkerEventStatus::REJECTED
: blink::mojom::ServiceWorkerEventStatus::COMPLETED,
/*fetch_count=*/0);
}
void DispatchActivateEvent(
blink::mojom::ServiceWorker::DispatchActivateEventCallback callback)
override {
events_.emplace_back(ServiceWorkerMetrics::EventType::ACTIVATE);
std::move(callback).Run(
reject_activate_ ? blink::mojom::ServiceWorkerEventStatus::REJECTED
: blink::mojom::ServiceWorkerEventStatus::COMPLETED);
}
void OnConnectionError() override {
// Do nothing. This allows the object to stay until the test is over, so
// |events_| can be accessed even after the worker is stopped in the case of
// rejected install.
}
private:
std::vector<ServiceWorkerMetrics::EventType> events_;
bool reject_install_ = false;
bool reject_activate_ = false;
};
enum NotificationType {
REGISTRATION_COMPLETED,
REGISTRATION_STORED,
REGISTRATION_DELETED,
STORAGE_RECOVERED,
};
struct NotificationLog {
NotificationType type;
GURL scope;
int64_t registration_id;
};
} // namespace
class ServiceWorkerContextTest : public ServiceWorkerContextCoreObserver,
public testing::Test {
public:
ServiceWorkerContextTest()
: task_environment_(BrowserTaskEnvironment::IO_MAINLOOP) {}
void SetUp() override {
helper_ = std::make_unique<EmbeddedWorkerTestHelper>(base::FilePath());
helper_->context_wrapper()->AddObserver(this);
}
void TearDown() override {
helper_.reset();
// The helper may post tasks to release resources in |temp_dir_|. Allow
// them to run now so that the directory may be deleted.
task_environment_.RunUntilIdle();
EXPECT_TRUE(!temp_dir_.IsValid() || temp_dir_.Delete())
<< temp_dir_.GetPath();
}
// ServiceWorkerContextCoreObserver overrides.
void OnRegistrationCompleted(int64_t registration_id,
const GURL& scope,
const blink::StorageKey& key) override {
NotificationLog log;
log.type = REGISTRATION_COMPLETED;
log.scope = scope;
log.registration_id = registration_id;
notifications_.push_back(log);
}
void OnRegistrationStored(int64_t registration_id,
const GURL& scope,
const blink::StorageKey& key) override {
NotificationLog log;
log.type = REGISTRATION_STORED;
log.scope = scope;
log.registration_id = registration_id;
notifications_.push_back(log);
}
void OnRegistrationDeleted(int64_t registration_id,
const GURL& scope,
const blink::StorageKey& key) override {
NotificationLog log;
log.type = REGISTRATION_DELETED;
log.scope = scope;
log.registration_id = registration_id;
notifications_.push_back(log);
}
void OnStorageWiped() override {
NotificationLog log;
log.type = STORAGE_RECOVERED;
notifications_.push_back(log);
}
ServiceWorkerContextCore* context() { return helper_->context(); }
ServiceWorkerContextWrapper* context_wrapper() {
return helper_->context_wrapper();
}
void GetTemporaryDirectory(base::FilePath* temp_dir) {
ASSERT_FALSE(temp_dir_.IsValid());
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
*temp_dir = temp_dir_.GetPath();
}
protected:
base::ScopedTempDir temp_dir_;
BrowserTaskEnvironment task_environment_;
std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
std::vector<NotificationLog> notifications_;
};
class RecordableEmbeddedWorkerInstanceClient
: public FakeEmbeddedWorkerInstanceClient {
public:
enum class Message { StartWorker, StopWorker };
explicit RecordableEmbeddedWorkerInstanceClient(
EmbeddedWorkerTestHelper* helper)
: FakeEmbeddedWorkerInstanceClient(helper) {}
RecordableEmbeddedWorkerInstanceClient(
const RecordableEmbeddedWorkerInstanceClient&) = delete;
RecordableEmbeddedWorkerInstanceClient& operator=(
const RecordableEmbeddedWorkerInstanceClient&) = delete;
void OnConnectionError() override {
// Do nothing. This allows the object to stay until the test is over, so
// |events_| can be accessed even after the worker is stopped in the case of
// rejected install.
}
const std::vector<Message>& events() const { return events_; }
protected:
void StartWorker(blink::mojom::EmbeddedWorkerStartParamsPtr params) override {
events_.push_back(Message::StartWorker);
FakeEmbeddedWorkerInstanceClient::StartWorker(std::move(params));
}
void StopWorker() override {
events_.push_back(Message::StopWorker);
// Let stop complete, but don't call the base class's StopWorker(), which
// would destroy |this| before the test can retrieve events().
host()->OnStopped();
}
private:
std::vector<Message> events_;
};
class TestServiceWorkerContextObserver : public ServiceWorkerContextObserver {
public:
enum class EventType {
RegistrationCompleted,
RegistrationStored,
VersionActivated,
VersionRedundant,
ControlleeAdded,
ControlleeRemoved,
NoControllees,
ControlleeNavigationCommitted,
VersionStartedRunning,
VersionStoppedRunning,
Destruct
};
struct EventLog {
EventType type;
std::optional<GURL> url;
std::optional<int64_t> version_id;
std::optional<int64_t> registration_id;
std::optional<bool> is_running;
};
explicit TestServiceWorkerContextObserver(ServiceWorkerContext* context) {
scoped_observation_.Observe(context);
}
TestServiceWorkerContextObserver(const TestServiceWorkerContextObserver&) =
delete;
TestServiceWorkerContextObserver& operator=(
const TestServiceWorkerContextObserver&) = delete;
~TestServiceWorkerContextObserver() override = default;
void OnRegistrationCompleted(const GURL& scope) override {
EventLog log;
log.type = EventType::RegistrationCompleted;
log.url = scope;
events_.push_back(log);
}
void OnRegistrationStored(int64_t registration_id,
const GURL& scope) override {
EventLog log;
log.type = EventType::RegistrationStored;
log.registration_id = registration_id;
log.url = scope;
events_.push_back(log);
}
void OnVersionActivated(int64_t version_id, const GURL& scope) override {
EventLog log;
log.type = EventType::VersionActivated;
log.version_id = version_id;
log.url = scope;
events_.push_back(log);
}
void OnVersionRedundant(int64_t version_id, const GURL& scope) override {
EventLog log;
log.type = EventType::VersionRedundant;
log.version_id = version_id;
log.url = scope;
events_.push_back(log);
}
void OnControlleeAdded(int64_t version_id,
const std::string& client_uuid,
const ServiceWorkerClientInfo& client_info) override {
EventLog log;
log.type = EventType::ControlleeAdded;
log.version_id = version_id;
events_.push_back(log);
}
void OnControlleeRemoved(int64_t version_id,
const std::string& client_uuid) override {
EventLog log;
log.type = EventType::ControlleeRemoved;
log.version_id = version_id;
events_.push_back(log);
}
void OnNoControllees(int64_t version_id, const GURL& scope) override {
EventLog log;
log.type = EventType::NoControllees;
log.version_id = version_id;
log.url = scope;
events_.push_back(log);
}
void OnControlleeNavigationCommitted(
int64_t version_id,
const std::string& client_uuid,
GlobalRenderFrameHostId render_frame_host_id) override {
EventLog log;
log.type = EventType::ControlleeNavigationCommitted;
log.version_id = version_id;
events_.push_back(log);
}
void OnVersionStartedRunning(
int64_t version_id,
const ServiceWorkerRunningInfo& running_info) override {
EventLog log;
log.type = EventType::VersionStartedRunning;
log.version_id = version_id;
events_.push_back(log);
}
void OnVersionStoppedRunning(int64_t version_id) override {
EventLog log;
log.type = EventType::VersionStoppedRunning;
log.version_id = version_id;
events_.push_back(log);
}
void OnDestruct(content::ServiceWorkerContext* context) override {
DCHECK(scoped_observation_.IsObserving());
scoped_observation_.Reset();
EventLog log;
log.type = EventType::Destruct;
events_.push_back(log);
}
const std::vector<EventLog>& events() { return events_; }
private:
base::ScopedObservation<ServiceWorkerContext, ServiceWorkerContextObserver>
scoped_observation_{this};
std::vector<EventLog> events_;
};
// Make sure OnRegistrationCompleted is called on observer.
TEST_F(ServiceWorkerContextTest, RegistrationCompletedObserver) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
TestServiceWorkerContextObserver observer(context_wrapper());
int64_t registration_id = blink::mojom::kInvalidServiceWorkerRegistrationId;
bool called = false;
context()->RegisterServiceWorker(
script_url, key, options, blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId, registration_id);
std::vector<TestServiceWorkerContextObserver::EventLog> events;
// Filter the events to be verified.
for (auto event : observer.events()) {
if (event.type == TestServiceWorkerContextObserver::EventType::
RegistrationCompleted ||
event.type ==
TestServiceWorkerContextObserver::EventType::RegistrationStored ||
event.type ==
TestServiceWorkerContextObserver::EventType::VersionActivated)
events.push_back(event);
}
ASSERT_EQ(3u, events.size());
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::RegistrationCompleted,
events[0].type);
EXPECT_EQ(scope, events[0].url);
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::RegistrationStored,
events[1].type);
EXPECT_EQ(scope, events[1].url);
EXPECT_EQ(registration_id, events[1].registration_id);
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::VersionActivated,
events[2].type);
EXPECT_EQ(scope, events[2].url);
}
// Make sure OnControlleeAdded, OnControlleeRemoved and OnNoControllees are
// called on observer.
TEST_F(ServiceWorkerContextTest, Observer_ControlleeEvents) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
auto registration = ServiceWorkerRegistration::Create(
options, key, 1l /* dummy registration id */, context()->AsWeakPtr(),
blink::mojom::AncestorFrameType::kNormalFrame);
auto version = base::MakeRefCounted<ServiceWorkerVersion>(
registration.get(), script_url, blink::mojom::ScriptType::kClassic,
2l /* dummy version id */,
mojo::PendingRemote<storage::mojom::ServiceWorkerLiveVersionRef>(),
context()->AsWeakPtr());
version->set_fetch_handler_type(
ServiceWorkerVersion::FetchHandlerType::kNotSkippable);
version->SetStatus(ServiceWorkerVersion::ACTIVATED);
// Flush tasks related to `SetStatus(ACTIVATED)` above before creating
// `observer`.
base::RunLoop().RunUntilIdle();
ScopedServiceWorkerClient service_worker_client =
CreateServiceWorkerClient(context());
TestServiceWorkerContextObserver observer(context_wrapper());
version->AddControllee(service_worker_client.get());
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1u, observer.events().size());
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::ControlleeAdded,
observer.events()[0].type);
CommittedServiceWorkerClient committed_service_worker_client(
std::move(service_worker_client),
GlobalRenderFrameHostId(helper_->mock_render_process_id(),
/*mock frame_routing_id=*/1));
version->OnControlleeNavigationCommitted(
committed_service_worker_client->client_uuid(),
committed_service_worker_client->GetRenderFrameHostId());
base::RunLoop().RunUntilIdle();
ASSERT_EQ(2u, observer.events().size());
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::
ControlleeNavigationCommitted,
observer.events()[1].type);
version->RemoveControllee(committed_service_worker_client->client_uuid());
base::RunLoop().RunUntilIdle();
ASSERT_EQ(4u, observer.events().size());
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::ControlleeRemoved,
observer.events()[2].type);
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::NoControllees,
observer.events()[3].type);
}
// Make sure OnVersionActivated is called on observer.
TEST_F(ServiceWorkerContextTest, VersionActivatedObserver) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
auto registration = ServiceWorkerRegistration::Create(
options, key, 1l /* dummy registration id */, context()->AsWeakPtr(),
blink::mojom::AncestorFrameType::kNormalFrame);
auto version = base::MakeRefCounted<ServiceWorkerVersion>(
registration.get(), script_url, blink::mojom::ScriptType::kClassic,
2l /* dummy version id */,
mojo::PendingRemote<storage::mojom::ServiceWorkerLiveVersionRef>(),
context()->AsWeakPtr());
TestServiceWorkerContextObserver observer(context_wrapper());
version->set_fetch_handler_type(
ServiceWorkerVersion::FetchHandlerType::kNoHandler);
version->SetStatus(ServiceWorkerVersion::Status::ACTIVATED);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1u, observer.events().size());
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::VersionActivated,
observer.events()[0].type);
EXPECT_EQ(2l, observer.events()[0].version_id);
}
// Make sure OnVersionRedundant is called on observer.
TEST_F(ServiceWorkerContextTest, VersionRedundantObserver) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
auto registration = ServiceWorkerRegistration::Create(
options, key, 1l /* dummy registration id */, context()->AsWeakPtr(),
blink::mojom::AncestorFrameType::kNormalFrame);
auto version = base::MakeRefCounted<ServiceWorkerVersion>(
registration.get(), script_url, blink::mojom::ScriptType::kClassic,
2l /* dummy version id */,
mojo::PendingRemote<storage::mojom::ServiceWorkerLiveVersionRef>(),
context()->AsWeakPtr());
TestServiceWorkerContextObserver observer(context_wrapper());
version->set_fetch_handler_type(
ServiceWorkerVersion::FetchHandlerType::kNoHandler);
version->SetStatus(ServiceWorkerVersion::Status::REDUNDANT);
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1u, observer.events().size());
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::VersionRedundant,
observer.events()[0].type);
EXPECT_EQ(2l, observer.events()[0].version_id);
}
// Make sure OnVersionStartedRunning and OnVersionStoppedRunning are called on
// observer.
TEST_F(ServiceWorkerContextTest, OnVersionRunningStatusChangedObserver) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
TestServiceWorkerContextObserver observer(context_wrapper());
base::RunLoop run_loop;
context()->RegisterServiceWorker(
script_url, key, options, blink::mojom::FetchClientSettingsObject::New(),
base::BindOnce(&RegisteredCallback, run_loop.QuitClosure()),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
run_loop.Run();
context_wrapper()->StopAllServiceWorkersForStorageKey(
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope)));
base::RunLoop().RunUntilIdle();
std::vector<TestServiceWorkerContextObserver::EventLog> events;
// Filter the events to be verified.
for (auto event : observer.events()) {
if (event.type == TestServiceWorkerContextObserver::EventType::
VersionStartedRunning ||
event.type == TestServiceWorkerContextObserver::EventType::
VersionStoppedRunning) {
events.push_back(event);
}
}
ASSERT_EQ(2u, events.size());
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::VersionStartedRunning,
events[0].type);
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::VersionStoppedRunning,
events[1].type);
EXPECT_EQ(events[0].version_id, events[1].version_id);
}
// Make sure OnDestruct is called on observer.
TEST_F(ServiceWorkerContextTest, OnDestructObserver) {
ServiceWorkerContextWrapper* context = context_wrapper();
TestServiceWorkerContextObserver observer(context);
helper_->ShutdownContext();
base::RunLoop().RunUntilIdle();
ASSERT_EQ(1u, observer.events().size());
EXPECT_EQ(TestServiceWorkerContextObserver::EventType::Destruct,
observer.events()[0].type);
}
// Make sure basic registration is working.
TEST_F(ServiceWorkerContextTest, Register) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
auto* client =
helper_
->AddNewPendingInstanceClient<RecordableEmbeddedWorkerInstanceClient>(
helper_.get());
auto* worker =
helper_->AddNewPendingServiceWorker<InstallActivateWorker>(helper_.get());
int64_t registration_id = blink::mojom::kInvalidServiceWorkerRegistrationId;
bool called = false;
context()->RegisterServiceWorker(
script_url, key, options, blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
ASSERT_EQ(2u, worker->events().size());
ASSERT_EQ(1u, client->events().size());
EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker,
client->events()[0]);
EXPECT_EQ(ServiceWorkerMetrics::EventType::INSTALL, worker->events()[0]);
EXPECT_EQ(ServiceWorkerMetrics::EventType::ACTIVATE, worker->events()[1]);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId, registration_id);
ASSERT_EQ(2u, notifications_.size());
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[0].type);
EXPECT_EQ(scope, notifications_[0].scope);
EXPECT_EQ(registration_id, notifications_[0].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
EXPECT_EQ(scope, notifications_[1].scope);
EXPECT_EQ(registration_id, notifications_[1].registration_id);
context()->registry()->FindRegistrationForId(
registration_id,
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope)),
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kOk,
false /* expect_waiting */, true /* expect_active */));
base::RunLoop().RunUntilIdle();
}
// Test registration when the service worker rejects the install event. The
// registration callback should indicate success, but there should be no waiting
// or active worker in the registration.
TEST_F(ServiceWorkerContextTest, Register_RejectInstall) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
auto* client =
helper_
->AddNewPendingInstanceClient<RecordableEmbeddedWorkerInstanceClient>(
helper_.get());
auto* worker =
helper_->AddNewPendingServiceWorker<InstallActivateWorker>(helper_.get());
worker->SetToRejectInstall();
int64_t registration_id = blink::mojom::kInvalidServiceWorkerRegistrationId;
bool called = false;
context()->RegisterServiceWorker(
script_url, key, options, blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
ASSERT_EQ(1u, worker->events().size());
ASSERT_EQ(2u, client->events().size());
EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker,
client->events()[0]);
EXPECT_EQ(ServiceWorkerMetrics::EventType::INSTALL, worker->events()[0]);
EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StopWorker,
client->events()[1]);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId, registration_id);
ASSERT_EQ(1u, notifications_.size());
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[0].type);
EXPECT_EQ(scope, notifications_[0].scope);
EXPECT_EQ(registration_id, notifications_[0].registration_id);
context()->registry()->FindRegistrationForId(
registration_id,
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope)),
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kErrorNotFound,
false /* expect_waiting */, false /* expect_active */));
base::RunLoop().RunUntilIdle();
}
// Test registration when the service worker rejects the activate event. The
// worker should be activated anyway.
TEST_F(ServiceWorkerContextTest, Register_RejectActivate) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
auto* client =
helper_
->AddNewPendingInstanceClient<RecordableEmbeddedWorkerInstanceClient>(
helper_.get());
auto* worker =
helper_->AddNewPendingServiceWorker<InstallActivateWorker>(helper_.get());
worker->SetToRejectActivate();
int64_t registration_id = blink::mojom::kInvalidServiceWorkerRegistrationId;
bool called = false;
context()->RegisterServiceWorker(
script_url, key, options, blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called);
ASSERT_EQ(2u, worker->events().size());
ASSERT_EQ(1u, client->events().size());
EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker,
client->events()[0]);
EXPECT_EQ(ServiceWorkerMetrics::EventType::INSTALL, worker->events()[0]);
EXPECT_EQ(ServiceWorkerMetrics::EventType::ACTIVATE, worker->events()[1]);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId, registration_id);
ASSERT_EQ(2u, notifications_.size());
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[0].type);
EXPECT_EQ(scope, notifications_[0].scope);
EXPECT_EQ(registration_id, notifications_[0].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
EXPECT_EQ(scope, notifications_[1].scope);
EXPECT_EQ(registration_id, notifications_[1].registration_id);
context()->registry()->FindRegistrationForId(
registration_id,
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope)),
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kOk,
false /* expect_waiting */, true /* expect_active */));
base::RunLoop().RunUntilIdle();
}
// Make sure registrations are cleaned up when they are unregistered.
TEST_F(ServiceWorkerContextTest, Unregister) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
bool called = false;
int64_t registration_id = blink::mojom::kInvalidServiceWorkerRegistrationId;
context()->RegisterServiceWorker(
GURL("https://www.example.com/service_worker.js"), key, options,
blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId, registration_id);
called = false;
context()->UnregisterServiceWorker(scope, key, /*is_immediate=*/false,
MakeUnregisteredCallback(&called));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
context()->registry()->FindRegistrationForId(
registration_id,
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope)),
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kErrorNotFound,
false /* expect_waiting */, false /* expect_active */));
base::RunLoop().RunUntilIdle();
ASSERT_EQ(3u, notifications_.size());
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[0].type);
EXPECT_EQ(scope, notifications_[0].scope);
EXPECT_EQ(registration_id, notifications_[0].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
EXPECT_EQ(scope, notifications_[1].scope);
EXPECT_EQ(registration_id, notifications_[1].registration_id);
EXPECT_EQ(REGISTRATION_DELETED, notifications_[2].type);
EXPECT_EQ(scope, notifications_[2].scope);
EXPECT_EQ(registration_id, notifications_[2].registration_id);
}
// Make sure registrations are cleaned up when they are unregistered in bulk.
TEST_F(ServiceWorkerContextTest, UnregisterMultiple) {
GURL origin1_s1("https://www.example.com/test");
GURL origin1_s2("https://www.example.com/hello");
GURL origin2_s1("https://www.example.com:8080/again");
GURL origin3_s1("https://www.other.com/");
const blink::StorageKey key1 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(origin1_s1));
const blink::StorageKey key2 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(origin2_s1));
const blink::StorageKey key3 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(origin3_s1));
EXPECT_EQ(key1, blink::StorageKey::CreateFirstParty(
url::Origin::Create(origin1_s2)));
int64_t registration_id1 = blink::mojom::kInvalidServiceWorkerRegistrationId;
int64_t registration_id2 = blink::mojom::kInvalidServiceWorkerRegistrationId;
int64_t registration_id3 = blink::mojom::kInvalidServiceWorkerRegistrationId;
int64_t registration_id4 = blink::mojom::kInvalidServiceWorkerRegistrationId;
{
bool called = false;
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = origin1_s1;
context()->RegisterServiceWorker(
GURL("https://www.example.com/service_worker.js"), key1, options,
blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id1),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
}
{
bool called = false;
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = origin1_s2;
context()->RegisterServiceWorker(
GURL("https://www.example.com/service_worker2.js"), key1, options,
blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id2),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
}
{
bool called = false;
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = origin2_s1;
context()->RegisterServiceWorker(
GURL("https://www.example.com:8080/service_worker3.js"), key2, options,
blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id3),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
}
{
bool called = false;
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = origin3_s1;
context()->RegisterServiceWorker(
GURL("https://www.other.com/service_worker4.js"), key3, options,
blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id4),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
}
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId,
registration_id1);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId,
registration_id2);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId,
registration_id3);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId,
registration_id4);
bool called = false;
context()->DeleteForStorageKey(key1, MakeUnregisteredCallback(&called));
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
context()->registry()->FindRegistrationForId(
registration_id1, key1,
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kErrorNotFound,
false /* expect_waiting */, false /* expect_active */));
context()->registry()->FindRegistrationForId(
registration_id2, key1,
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kErrorNotFound,
false /* expect_waiting */, false /* expect_active */));
context()->registry()->FindRegistrationForId(
registration_id3, key2,
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kOk,
false /* expect_waiting */, true /* expect_active */));
context()->registry()->FindRegistrationForId(
registration_id4, key3,
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kOk,
false /* expect_waiting */, true /* expect_active */));
base::RunLoop().RunUntilIdle();
ASSERT_EQ(10u, notifications_.size());
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[0].type);
EXPECT_EQ(registration_id1, notifications_[0].registration_id);
EXPECT_EQ(origin1_s1, notifications_[0].scope);
EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
EXPECT_EQ(registration_id1, notifications_[1].registration_id);
EXPECT_EQ(origin1_s1, notifications_[1].scope);
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[2].type);
EXPECT_EQ(origin1_s2, notifications_[2].scope);
EXPECT_EQ(registration_id2, notifications_[2].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[3].type);
EXPECT_EQ(origin1_s2, notifications_[3].scope);
EXPECT_EQ(registration_id2, notifications_[3].registration_id);
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[4].type);
EXPECT_EQ(origin2_s1, notifications_[4].scope);
EXPECT_EQ(registration_id3, notifications_[4].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[5].type);
EXPECT_EQ(origin2_s1, notifications_[5].scope);
EXPECT_EQ(registration_id3, notifications_[5].registration_id);
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[6].type);
EXPECT_EQ(origin3_s1, notifications_[6].scope);
EXPECT_EQ(registration_id4, notifications_[6].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[7].type);
EXPECT_EQ(origin3_s1, notifications_[7].scope);
EXPECT_EQ(registration_id4, notifications_[7].registration_id);
EXPECT_EQ(REGISTRATION_DELETED, notifications_[8].type);
EXPECT_EQ(origin1_s1, notifications_[8].scope);
EXPECT_EQ(registration_id1, notifications_[8].registration_id);
EXPECT_EQ(REGISTRATION_DELETED, notifications_[9].type);
EXPECT_EQ(origin1_s2, notifications_[9].scope);
EXPECT_EQ(registration_id2, notifications_[9].registration_id);
}
// Make sure registering a new script shares an existing registration.
TEST_F(ServiceWorkerContextTest, RegisterNewScript) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
bool called = false;
int64_t old_registration_id =
blink::mojom::kInvalidServiceWorkerRegistrationId;
context()->RegisterServiceWorker(
GURL("https://www.example.com/service_worker.js"), key, options,
blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, &old_registration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId,
old_registration_id);
called = false;
int64_t new_registration_id =
blink::mojom::kInvalidServiceWorkerRegistrationId;
context()->RegisterServiceWorker(
GURL("https://www.example.com/service_worker_new.js"), key, options,
blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, &new_registration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId,
new_registration_id);
EXPECT_EQ(old_registration_id, new_registration_id);
ASSERT_EQ(4u, notifications_.size());
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[0].type);
EXPECT_EQ(scope, notifications_[0].scope);
EXPECT_EQ(old_registration_id, notifications_[0].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
EXPECT_EQ(scope, notifications_[1].scope);
EXPECT_EQ(old_registration_id, notifications_[1].registration_id);
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[2].type);
EXPECT_EQ(scope, notifications_[2].scope);
EXPECT_EQ(new_registration_id, notifications_[2].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[3].type);
EXPECT_EQ(scope, notifications_[3].scope);
EXPECT_EQ(new_registration_id, notifications_[3].registration_id);
}
// Make sure that when registering a duplicate scope+script_url
// combination, that the same registration is used.
TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
bool called = false;
int64_t old_registration_id =
blink::mojom::kInvalidServiceWorkerRegistrationId;
context()->RegisterServiceWorker(
script_url, key, options, blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, &old_registration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
EXPECT_NE(blink::mojom::kInvalidServiceWorkerRegistrationId,
old_registration_id);
called = false;
int64_t new_registration_id =
blink::mojom::kInvalidServiceWorkerRegistrationId;
context()->RegisterServiceWorker(
script_url, key, options, blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, &new_registration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(called);
EXPECT_EQ(old_registration_id, new_registration_id);
ASSERT_EQ(3u, notifications_.size());
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[0].type);
EXPECT_EQ(scope, notifications_[0].scope);
EXPECT_EQ(old_registration_id, notifications_[0].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
EXPECT_EQ(scope, notifications_[1].scope);
EXPECT_EQ(old_registration_id, notifications_[1].registration_id);
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[2].type);
EXPECT_EQ(scope, notifications_[2].scope);
EXPECT_EQ(old_registration_id, notifications_[2].registration_id);
}
TEST_F(ServiceWorkerContextTest, ContainerHostIterator) {
const int kRenderProcessId2 = 2;
const GURL kOrigin1 = GURL("https://www.example.com/");
const GURL kOrigin2 = GURL("https://another-origin.example.net/");
const blink::StorageKey kKey1 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(kOrigin1));
const blink::StorageKey kKey2 =
blink::StorageKey::CreateFirstParty(url::Origin::Create(kOrigin2));
// Clients with kOrigin1, kOrigin2, and kOrigin1.
ScopedServiceWorkerClient service_worker_client1 =
CreateServiceWorkerClient(context(), kOrigin1);
ScopedServiceWorkerClient service_worker_client2 =
CreateServiceWorkerClient(context(), kOrigin2);
ScopedServiceWorkerClient service_worker_client3 =
CreateServiceWorkerClient(context(), kOrigin1);
// Host4 : process_id=2, origin2, for ServiceWorker.
blink::mojom::ServiceWorkerRegistrationOptions registration_opt;
registration_opt.scope = GURL("https://another-origin.example.net/test/");
const blink::StorageKey key_other = blink::StorageKey::CreateFirstParty(
url::Origin::Create(registration_opt.scope));
scoped_refptr<ServiceWorkerRegistration> registration =
ServiceWorkerRegistration::Create(
registration_opt, key_other, 1L /* registration_id */,
helper_->context()->AsWeakPtr(),
blink::mojom::AncestorFrameType::kNormalFrame);
scoped_refptr<ServiceWorkerVersion> version =
base::MakeRefCounted<ServiceWorkerVersion>(
registration.get(),
GURL("https://another-origin.example.net/test/script_url"),
blink::mojom::ScriptType::kClassic, 1L /* version_id */,
mojo::PendingRemote<storage::mojom::ServiceWorkerLiveVersionRef>(),
helper_->context()->AsWeakPtr());
// ServiceWorkerHost creates ServiceWorkerClient for a service worker
// execution context.
std::unique_ptr<ServiceWorkerHost> worker_host4 = CreateServiceWorkerHost(
kRenderProcessId2, true /* is_parent_frame_secure */, *version,
context()->AsWeakPtr());
ASSERT_TRUE(service_worker_client1.get());
ASSERT_TRUE(service_worker_client2.get());
ASSERT_TRUE(service_worker_client3.get());
ASSERT_TRUE(worker_host4->container_host());
// Iterate over the client container hosts that belong to kOrigin1.
std::set<ServiceWorkerClient*> results;
for (auto it =
context()->service_worker_client_owner().GetServiceWorkerClients(
kKey1, true /* include_reserved_clients */,
false /* include_back_forward_cached_clients */);
!it.IsAtEnd(); ++it) {
results.insert(&*it);
}
EXPECT_EQ(2u, results.size());
EXPECT_TRUE(base::Contains(results, service_worker_client1.get()));
EXPECT_TRUE(base::Contains(results, service_worker_client3.get()));
// Iterate over the container hosts that belong to kOrigin2. This should not
// include worker_host4->service_worker_client() as it's not for controllee.
results.clear();
for (auto it =
context()->service_worker_client_owner().GetServiceWorkerClients(
kKey2, true /* include_reserved_clients */,
false /* include_back_forward_cached_clients */);
!it.IsAtEnd(); ++it) {
results.insert(&*it);
}
EXPECT_EQ(1u, results.size());
EXPECT_TRUE(base::Contains(results, service_worker_client2.get()));
}
class ServiceWorkerContextRecoveryTest
: public ServiceWorkerContextTest,
public testing::WithParamInterface<bool /* is_storage_on_disk */> {
public:
ServiceWorkerContextRecoveryTest() {}
virtual ~ServiceWorkerContextRecoveryTest() {}
protected:
bool is_storage_on_disk() const { return GetParam(); }
};
TEST_P(ServiceWorkerContextRecoveryTest, DeleteAndStartOver) {
GURL scope("https://www.example.com/");
const blink::StorageKey key =
blink::StorageKey::CreateFirstParty(url::Origin::Create(scope));
GURL script_url("https://www.example.com/service_worker.js");
blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope;
if (is_storage_on_disk()) {
// Reinitialize the helper to test on-disk storage.
base::FilePath user_data_directory;
ASSERT_NO_FATAL_FAILURE(GetTemporaryDirectory(&user_data_directory));
helper_ = std::make_unique<EmbeddedWorkerTestHelper>(user_data_directory);
helper_->context_wrapper()->AddObserver(this);
}
int64_t registration_id = blink::mojom::kInvalidServiceWorkerRegistrationId;
bool called = false;
context()->RegisterServiceWorker(
script_url, key, options, blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
content::RunAllTasksUntilIdle();
EXPECT_TRUE(called);
context()->registry()->FindRegistrationForId(
registration_id, key,
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kOk,
false /* expect_waiting */, true /* expect_active */));
content::RunAllTasksUntilIdle();
// Emulate a service worker client is created before
// `ScheduleDeleteAndStartOver()` and redirected, committed and destroyed
// after `ScheduleDeleteAndStartOver()`.
ScopedServiceWorkerClient service_worker_client =
CreateServiceWorkerClient(context(), scope);
EXPECT_EQ(service_worker_client->context().get(), context());
context()->ScheduleDeleteAndStartOver();
// The storage is disabled while the recovery process is running, so the
// operation should be aborted.
context()->registry()->FindRegistrationForId(
registration_id, key,
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kErrorAbort,
false /* expect_waiting */, true /* expect_active */));
content::RunAllTasksUntilIdle();
// The context started over and the storage was re-initialized, so the
// registration should not be found.
context()->registry()->FindRegistrationForId(
registration_id, key,
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kErrorNotFound,
false /* expect_waiting */, true /* expect_active */));
content::RunAllTasksUntilIdle();
{
// Perform a cross-origin redirect for `service_worker_client`. This updates
// the client UUID of `service_worker_client`, and should update the UUID
// maintained by `ServiceWorkerClientOwner`, not to cause the client UUID
// inconsistency.
GURL cross_site_url("https://www.example.org/");
EXPECT_FALSE(service_worker_client->context());
service_worker_client->UpdateUrls(cross_site_url,
url::Origin::Create(cross_site_url),
blink::StorageKey::CreateFirstParty(
url::Origin::Create(cross_site_url)));
auto committed_service_worker_client = CommittedServiceWorkerClient(
std::move(service_worker_client),
GlobalRenderFrameHostId(/*child_id=*/1,
/*frame_routing_id=*/1));
}
// Destruct the service worker client via
// `OnContainerHostReceiverDisconnected()` by destructing
// `committed_service_worker_client`.
// This doesn't crash if the client UUID was updated consistently above.
content::RunAllTasksUntilIdle();
called = false;
context()->RegisterServiceWorker(
script_url, key, options, blink::mojom::FetchClientSettingsObject::New(),
MakeRegisteredCallback(&called, ®istration_id),
/*requesting_frame_id=*/GlobalRenderFrameHostId(),
PolicyContainerPolicies());
ASSERT_FALSE(called);
content::RunAllTasksUntilIdle();
EXPECT_TRUE(called);
context()->registry()->FindRegistrationForId(
registration_id, key,
base::BindOnce(&ExpectRegisteredWorkers,
blink::ServiceWorkerStatusCode::kOk,
false /* expect_waiting */, true /* expect_active */));
content::RunAllTasksUntilIdle();
ASSERT_EQ(5u, notifications_.size());
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[0].type);
EXPECT_EQ(scope, notifications_[0].scope);
EXPECT_EQ(registration_id, notifications_[0].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
EXPECT_EQ(scope, notifications_[1].scope);
EXPECT_EQ(registration_id, notifications_[1].registration_id);
EXPECT_EQ(STORAGE_RECOVERED, notifications_[2].type);
EXPECT_EQ(REGISTRATION_COMPLETED, notifications_[3].type);
EXPECT_EQ(scope, notifications_[3].scope);
EXPECT_EQ(registration_id, notifications_[3].registration_id);
EXPECT_EQ(REGISTRATION_STORED, notifications_[4].type);
EXPECT_EQ(scope, notifications_[4].scope);
EXPECT_EQ(registration_id, notifications_[4].registration_id);
}
INSTANTIATE_TEST_SUITE_P(ServiceWorkerContextRecoveryTest,
ServiceWorkerContextRecoveryTest,
testing::Bool() /* is_storage_on_disk */);
} // namespace content