1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/public/browser/service_worker_context.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/service_worker/embedded_worker_registry.h"
12 #include "content/browser/service_worker/embedded_worker_test_helper.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_context_observer.h"
15 #include "content/browser/service_worker/service_worker_context_wrapper.h"
16 #include "content/browser/service_worker/service_worker_provider_host.h"
17 #include "content/browser/service_worker/service_worker_registration.h"
18 #include "content/browser/service_worker/service_worker_storage.h"
19 #include "content/common/service_worker/embedded_worker_messages.h"
20 #include "content/common/service_worker/service_worker_messages.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "content/public/test/test_utils.h"
23 #include "testing/gtest/include/gtest/gtest.h"
29 void SaveResponseCallback(bool* called
,
30 int64
* store_registration_id
,
31 ServiceWorkerStatusCode status
,
32 const std::string
& status_message
,
33 int64 registration_id
) {
34 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
36 *store_registration_id
= registration_id
;
39 ServiceWorkerContextCore::RegistrationCallback
MakeRegisteredCallback(
41 int64
* store_registration_id
) {
42 return base::Bind(&SaveResponseCallback
, called
, store_registration_id
);
45 void CallCompletedCallback(bool* called
, ServiceWorkerStatusCode
) {
49 ServiceWorkerContextCore::UnregistrationCallback
MakeUnregisteredCallback(
51 return base::Bind(&CallCompletedCallback
, called
);
54 void ExpectRegisteredWorkers(
55 ServiceWorkerStatusCode expect_status
,
58 ServiceWorkerStatusCode status
,
59 const scoped_refptr
<ServiceWorkerRegistration
>& registration
) {
60 ASSERT_EQ(expect_status
, status
);
61 if (status
!= SERVICE_WORKER_OK
) {
62 EXPECT_FALSE(registration
.get());
67 EXPECT_TRUE(registration
->waiting_version());
69 EXPECT_FALSE(registration
->waiting_version());
73 EXPECT_TRUE(registration
->active_version());
75 EXPECT_FALSE(registration
->active_version());
79 class RejectInstallTestHelper
: public EmbeddedWorkerTestHelper
{
81 explicit RejectInstallTestHelper(int mock_render_process_id
)
82 : EmbeddedWorkerTestHelper(base::FilePath(), mock_render_process_id
) {}
84 void OnInstallEvent(int embedded_worker_id
,
85 int request_id
) override
{
87 new ServiceWorkerHostMsg_InstallEventFinished(
88 embedded_worker_id
, request_id
,
89 blink::WebServiceWorkerEventResultRejected
));
93 class RejectActivateTestHelper
: public EmbeddedWorkerTestHelper
{
95 explicit RejectActivateTestHelper(int mock_render_process_id
)
96 : EmbeddedWorkerTestHelper(base::FilePath(), mock_render_process_id
) {}
98 void OnActivateEvent(int embedded_worker_id
, int request_id
) override
{
100 new ServiceWorkerHostMsg_ActivateEventFinished(
101 embedded_worker_id
, request_id
,
102 blink::WebServiceWorkerEventResultRejected
));
106 enum NotificationType
{
108 REGISTRATION_DELETED
,
112 struct NotificationLog
{
113 NotificationType type
;
115 int64 registration_id
;
120 class ServiceWorkerContextTest
: public ServiceWorkerContextObserver
,
121 public testing::Test
{
123 ServiceWorkerContextTest()
124 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
125 render_process_id_(99) {}
127 void SetUp() override
{
129 new EmbeddedWorkerTestHelper(base::FilePath(), render_process_id_
));
130 helper_
->context_wrapper()->AddObserver(this);
133 void TearDown() override
{ helper_
.reset(); }
135 // ServiceWorkerContextObserver overrides.
136 void OnRegistrationStored(int64 registration_id
,
137 const GURL
& pattern
) override
{
139 log
.type
= REGISTRATION_STORED
;
140 log
.pattern
= pattern
;
141 log
.registration_id
= registration_id
;
142 notifications_
.push_back(log
);
144 void OnRegistrationDeleted(int64 registration_id
,
145 const GURL
& pattern
) override
{
147 log
.type
= REGISTRATION_DELETED
;
148 log
.pattern
= pattern
;
149 log
.registration_id
= registration_id
;
150 notifications_
.push_back(log
);
152 void OnStorageWiped() override
{
154 log
.type
= STORAGE_RECOVERED
;
155 notifications_
.push_back(log
);
158 ServiceWorkerContextCore
* context() { return helper_
->context(); }
161 TestBrowserThreadBundle browser_thread_bundle_
;
162 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
163 const int render_process_id_
;
164 std::vector
<NotificationLog
> notifications_
;
167 // Make sure basic registration is working.
168 TEST_F(ServiceWorkerContextTest
, Register
) {
169 GURL
pattern("http://www.example.com/");
170 GURL
script_url("http://www.example.com/service_worker.js");
172 int64 registration_id
= kInvalidServiceWorkerRegistrationId
;
174 context()->RegisterServiceWorker(
178 MakeRegisteredCallback(&called
, ®istration_id
));
180 ASSERT_FALSE(called
);
181 base::RunLoop().RunUntilIdle();
184 EXPECT_EQ(4UL, helper_
->ipc_sink()->message_count());
185 EXPECT_TRUE(helper_
->ipc_sink()->GetUniqueMessageMatching(
186 EmbeddedWorkerMsg_StartWorker::ID
));
187 EXPECT_TRUE(helper_
->inner_ipc_sink()->GetUniqueMessageMatching(
188 ServiceWorkerMsg_InstallEvent::ID
));
189 EXPECT_TRUE(helper_
->inner_ipc_sink()->GetUniqueMessageMatching(
190 ServiceWorkerMsg_ActivateEvent::ID
));
191 EXPECT_TRUE(helper_
->ipc_sink()->GetUniqueMessageMatching(
192 EmbeddedWorkerMsg_StopWorker::ID
));
193 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, registration_id
);
195 context()->storage()->FindRegistrationForId(
198 base::Bind(&ExpectRegisteredWorkers
,
200 false /* expect_waiting */,
201 true /* expect_active */));
202 base::RunLoop().RunUntilIdle();
204 ASSERT_EQ(1u, notifications_
.size());
205 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[0].type
);
206 EXPECT_EQ(pattern
, notifications_
[0].pattern
);
207 EXPECT_EQ(registration_id
, notifications_
[0].registration_id
);
210 // Test registration when the service worker rejects the install event. The
211 // registration callback should indicate success, but there should be no waiting
212 // or active worker in the registration.
213 TEST_F(ServiceWorkerContextTest
, Register_RejectInstall
) {
214 GURL
pattern("http://www.example.com/");
215 GURL
script_url("http://www.example.com/service_worker.js");
217 helper_
.reset(); // Make sure the process lookups stay overridden.
218 helper_
.reset(new RejectInstallTestHelper(render_process_id_
));
219 int64 registration_id
= kInvalidServiceWorkerRegistrationId
;
221 context()->RegisterServiceWorker(
225 MakeRegisteredCallback(&called
, ®istration_id
));
227 ASSERT_FALSE(called
);
228 base::RunLoop().RunUntilIdle();
231 EXPECT_EQ(3UL, helper_
->ipc_sink()->message_count());
232 EXPECT_TRUE(helper_
->ipc_sink()->GetUniqueMessageMatching(
233 EmbeddedWorkerMsg_StartWorker::ID
));
234 EXPECT_TRUE(helper_
->inner_ipc_sink()->GetUniqueMessageMatching(
235 ServiceWorkerMsg_InstallEvent::ID
));
236 EXPECT_FALSE(helper_
->inner_ipc_sink()->GetUniqueMessageMatching(
237 ServiceWorkerMsg_ActivateEvent::ID
));
238 EXPECT_TRUE(helper_
->ipc_sink()->GetUniqueMessageMatching(
239 EmbeddedWorkerMsg_StopWorker::ID
));
240 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, registration_id
);
242 context()->storage()->FindRegistrationForId(
245 base::Bind(&ExpectRegisteredWorkers
,
246 SERVICE_WORKER_ERROR_NOT_FOUND
,
247 false /* expect_waiting */,
248 false /* expect_active */));
249 base::RunLoop().RunUntilIdle();
251 EXPECT_TRUE(notifications_
.empty());
254 // Test registration when the service worker rejects the activate event. The
255 // registration callback should indicate success, but there should be no waiting
256 // or active worker in the registration.
257 TEST_F(ServiceWorkerContextTest
, Register_RejectActivate
) {
258 helper_
.reset(); // Make sure the process lookups stay overridden.
259 helper_
.reset(new RejectActivateTestHelper(render_process_id_
));
260 int64 registration_id
= kInvalidServiceWorkerRegistrationId
;
262 context()->RegisterServiceWorker(
263 GURL("http://www.example.com/"),
264 GURL("http://www.example.com/service_worker.js"),
266 MakeRegisteredCallback(&called
, ®istration_id
));
268 ASSERT_FALSE(called
);
269 base::RunLoop().RunUntilIdle();
272 EXPECT_EQ(4UL, helper_
->ipc_sink()->message_count());
273 EXPECT_TRUE(helper_
->ipc_sink()->GetUniqueMessageMatching(
274 EmbeddedWorkerMsg_StartWorker::ID
));
275 EXPECT_TRUE(helper_
->inner_ipc_sink()->GetUniqueMessageMatching(
276 ServiceWorkerMsg_InstallEvent::ID
));
277 EXPECT_TRUE(helper_
->inner_ipc_sink()->GetUniqueMessageMatching(
278 ServiceWorkerMsg_ActivateEvent::ID
));
279 EXPECT_TRUE(helper_
->ipc_sink()->GetUniqueMessageMatching(
280 EmbeddedWorkerMsg_StopWorker::ID
));
281 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, registration_id
);
283 context()->storage()->FindRegistrationForId(
285 GURL("http://www.example.com"),
286 base::Bind(&ExpectRegisteredWorkers
,
287 SERVICE_WORKER_ERROR_NOT_FOUND
,
288 false /* expect_waiting */,
289 false /* expect_active */));
290 base::RunLoop().RunUntilIdle();
292 EXPECT_TRUE(notifications_
.empty());
295 // Make sure registrations are cleaned up when they are unregistered.
296 TEST_F(ServiceWorkerContextTest
, Unregister
) {
297 GURL
pattern("http://www.example.com/");
300 int64 registration_id
= kInvalidServiceWorkerRegistrationId
;
301 context()->RegisterServiceWorker(
303 GURL("http://www.example.com/service_worker.js"),
305 MakeRegisteredCallback(&called
, ®istration_id
));
307 ASSERT_FALSE(called
);
308 base::RunLoop().RunUntilIdle();
310 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, registration_id
);
313 context()->UnregisterServiceWorker(pattern
,
314 MakeUnregisteredCallback(&called
));
316 ASSERT_FALSE(called
);
317 base::RunLoop().RunUntilIdle();
320 context()->storage()->FindRegistrationForId(
323 base::Bind(&ExpectRegisteredWorkers
,
324 SERVICE_WORKER_ERROR_NOT_FOUND
,
325 false /* expect_waiting */,
326 false /* expect_active */));
327 base::RunLoop().RunUntilIdle();
329 ASSERT_EQ(2u, notifications_
.size());
330 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[0].type
);
331 EXPECT_EQ(pattern
, notifications_
[0].pattern
);
332 EXPECT_EQ(registration_id
, notifications_
[0].registration_id
);
333 EXPECT_EQ(REGISTRATION_DELETED
, notifications_
[1].type
);
334 EXPECT_EQ(pattern
, notifications_
[1].pattern
);
335 EXPECT_EQ(registration_id
, notifications_
[1].registration_id
);
338 // Make sure registrations are cleaned up when they are unregistered in bulk.
339 TEST_F(ServiceWorkerContextTest
, UnregisterMultiple
) {
340 GURL
origin1_p1("http://www.example.com/test");
341 GURL
origin1_p2("http://www.example.com/hello");
342 GURL
origin2_p1("http://www.example.com:8080/again");
343 GURL
origin3_p1("http://www.other.com/");
346 int64 registration_id1
= kInvalidServiceWorkerRegistrationId
;
347 int64 registration_id2
= kInvalidServiceWorkerRegistrationId
;
348 int64 registration_id3
= kInvalidServiceWorkerRegistrationId
;
349 int64 registration_id4
= kInvalidServiceWorkerRegistrationId
;
350 context()->RegisterServiceWorker(
352 GURL("http://www.example.com/service_worker.js"),
354 MakeRegisteredCallback(&called
, ®istration_id1
));
355 context()->RegisterServiceWorker(
357 GURL("http://www.example.com/service_worker2.js"),
359 MakeRegisteredCallback(&called
, ®istration_id2
));
360 context()->RegisterServiceWorker(
362 GURL("http://www.example.com:8080/service_worker3.js"),
364 MakeRegisteredCallback(&called
, ®istration_id3
));
365 context()->RegisterServiceWorker(
367 GURL("http://www.other.com/service_worker4.js"),
369 MakeRegisteredCallback(&called
, ®istration_id4
));
371 ASSERT_FALSE(called
);
372 base::RunLoop().RunUntilIdle();
375 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, registration_id1
);
376 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, registration_id2
);
377 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, registration_id3
);
378 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, registration_id4
);
381 context()->UnregisterServiceWorkers(origin1_p1
.GetOrigin(),
382 MakeUnregisteredCallback(&called
));
384 ASSERT_FALSE(called
);
385 base::RunLoop().RunUntilIdle();
388 context()->storage()->FindRegistrationForId(
390 origin1_p1
.GetOrigin(),
391 base::Bind(&ExpectRegisteredWorkers
,
392 SERVICE_WORKER_ERROR_NOT_FOUND
,
393 false /* expect_waiting */,
394 false /* expect_active */));
395 context()->storage()->FindRegistrationForId(
397 origin1_p2
.GetOrigin(),
398 base::Bind(&ExpectRegisteredWorkers
,
399 SERVICE_WORKER_ERROR_NOT_FOUND
,
400 false /* expect_waiting */,
401 false /* expect_active */));
402 context()->storage()->FindRegistrationForId(
404 origin2_p1
.GetOrigin(),
405 base::Bind(&ExpectRegisteredWorkers
,
407 false /* expect_waiting */,
408 true /* expect_active */));
410 context()->storage()->FindRegistrationForId(
412 origin3_p1
.GetOrigin(),
413 base::Bind(&ExpectRegisteredWorkers
,
415 false /* expect_waiting */,
416 true /* expect_active */));
418 base::RunLoop().RunUntilIdle();
420 ASSERT_EQ(6u, notifications_
.size());
421 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[0].type
);
422 EXPECT_EQ(registration_id1
, notifications_
[0].registration_id
);
423 EXPECT_EQ(origin1_p1
, notifications_
[0].pattern
);
424 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[1].type
);
425 EXPECT_EQ(origin1_p2
, notifications_
[1].pattern
);
426 EXPECT_EQ(registration_id2
, notifications_
[1].registration_id
);
427 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[2].type
);
428 EXPECT_EQ(origin2_p1
, notifications_
[2].pattern
);
429 EXPECT_EQ(registration_id3
, notifications_
[2].registration_id
);
430 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[3].type
);
431 EXPECT_EQ(origin3_p1
, notifications_
[3].pattern
);
432 EXPECT_EQ(registration_id4
, notifications_
[3].registration_id
);
433 EXPECT_EQ(REGISTRATION_DELETED
, notifications_
[4].type
);
434 EXPECT_EQ(origin1_p2
, notifications_
[4].pattern
);
435 EXPECT_EQ(registration_id2
, notifications_
[4].registration_id
);
436 EXPECT_EQ(REGISTRATION_DELETED
, notifications_
[5].type
);
437 EXPECT_EQ(origin1_p1
, notifications_
[5].pattern
);
438 EXPECT_EQ(registration_id1
, notifications_
[5].registration_id
);
441 // Make sure registering a new script shares an existing registration.
442 TEST_F(ServiceWorkerContextTest
, RegisterNewScript
) {
443 GURL
pattern("http://www.example.com/");
446 int64 old_registration_id
= kInvalidServiceWorkerRegistrationId
;
447 context()->RegisterServiceWorker(
449 GURL("http://www.example.com/service_worker.js"),
451 MakeRegisteredCallback(&called
, &old_registration_id
));
453 ASSERT_FALSE(called
);
454 base::RunLoop().RunUntilIdle();
456 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, old_registration_id
);
459 int64 new_registration_id
= kInvalidServiceWorkerRegistrationId
;
460 context()->RegisterServiceWorker(
462 GURL("http://www.example.com/service_worker_new.js"),
464 MakeRegisteredCallback(&called
, &new_registration_id
));
466 ASSERT_FALSE(called
);
467 base::RunLoop().RunUntilIdle();
470 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, new_registration_id
);
471 EXPECT_EQ(old_registration_id
, new_registration_id
);
473 ASSERT_EQ(2u, notifications_
.size());
474 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[0].type
);
475 EXPECT_EQ(pattern
, notifications_
[0].pattern
);
476 EXPECT_EQ(old_registration_id
, notifications_
[0].registration_id
);
477 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[1].type
);
478 EXPECT_EQ(pattern
, notifications_
[1].pattern
);
479 EXPECT_EQ(new_registration_id
, notifications_
[1].registration_id
);
482 // Make sure that when registering a duplicate pattern+script_url
483 // combination, that the same registration is used.
484 TEST_F(ServiceWorkerContextTest
, RegisterDuplicateScript
) {
485 GURL
pattern("http://www.example.com/");
486 GURL
script_url("http://www.example.com/service_worker.js");
489 int64 old_registration_id
= kInvalidServiceWorkerRegistrationId
;
490 context()->RegisterServiceWorker(
494 MakeRegisteredCallback(&called
, &old_registration_id
));
496 ASSERT_FALSE(called
);
497 base::RunLoop().RunUntilIdle();
499 EXPECT_NE(kInvalidServiceWorkerRegistrationId
, old_registration_id
);
502 int64 new_registration_id
= kInvalidServiceWorkerRegistrationId
;
503 context()->RegisterServiceWorker(
507 MakeRegisteredCallback(&called
, &new_registration_id
));
509 ASSERT_FALSE(called
);
510 base::RunLoop().RunUntilIdle();
512 EXPECT_EQ(old_registration_id
, new_registration_id
);
514 ASSERT_EQ(2u, notifications_
.size());
515 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[0].type
);
516 EXPECT_EQ(pattern
, notifications_
[0].pattern
);
517 EXPECT_EQ(old_registration_id
, notifications_
[0].registration_id
);
518 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[1].type
);
519 EXPECT_EQ(pattern
, notifications_
[1].pattern
);
520 EXPECT_EQ(old_registration_id
, notifications_
[1].registration_id
);
523 TEST_F(ServiceWorkerContextTest
, DeleteAndStartOver
) {
524 GURL
pattern("http://www.example.com/");
525 GURL
script_url("http://www.example.com/service_worker.js");
527 // Reinitialize the helper to test on-disk storage.
528 base::ScopedTempDir user_data_directory
;
529 ASSERT_TRUE(user_data_directory
.CreateUniqueTempDir());
530 helper_
.reset(new EmbeddedWorkerTestHelper(user_data_directory
.path(),
531 render_process_id_
));
532 helper_
->context_wrapper()->AddObserver(this);
534 int64 registration_id
= kInvalidServiceWorkerRegistrationId
;
536 context()->RegisterServiceWorker(
540 MakeRegisteredCallback(&called
, ®istration_id
));
542 ASSERT_FALSE(called
);
543 base::RunLoop().RunUntilIdle();
546 context()->storage()->FindRegistrationForId(
549 base::Bind(&ExpectRegisteredWorkers
,
551 false /* expect_waiting */,
552 true /* expect_active */));
553 base::RunLoop().RunUntilIdle();
555 // Next handle ids should be 0 (the next call should return 1).
556 EXPECT_EQ(0, context()->GetNewServiceWorkerHandleId());
557 EXPECT_EQ(0, context()->GetNewRegistrationHandleId());
559 context()->ScheduleDeleteAndStartOver();
561 // The storage is disabled while the recovery process is running, so the
562 // operation should be failed.
563 context()->storage()->FindRegistrationForId(
566 base::Bind(&ExpectRegisteredWorkers
,
567 SERVICE_WORKER_ERROR_FAILED
,
568 false /* expect_waiting */,
569 true /* expect_active */));
570 base::RunLoop().RunUntilIdle();
572 // The context started over and the storage was re-initialized, so the
573 // registration should not be found.
574 context()->storage()->FindRegistrationForId(
577 base::Bind(&ExpectRegisteredWorkers
,
578 SERVICE_WORKER_ERROR_NOT_FOUND
,
579 false /* expect_waiting */,
580 true /* expect_active */));
581 base::RunLoop().RunUntilIdle();
584 context()->RegisterServiceWorker(
588 MakeRegisteredCallback(&called
, ®istration_id
));
590 ASSERT_FALSE(called
);
591 base::RunLoop().RunUntilIdle();
594 context()->storage()->FindRegistrationForId(
597 base::Bind(&ExpectRegisteredWorkers
,
599 false /* expect_waiting */,
600 true /* expect_active */));
601 base::RunLoop().RunUntilIdle();
603 // The new context should take over next handle ids.
604 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId());
605 EXPECT_EQ(1, context()->GetNewRegistrationHandleId());
607 ASSERT_EQ(3u, notifications_
.size());
608 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[0].type
);
609 EXPECT_EQ(pattern
, notifications_
[0].pattern
);
610 EXPECT_EQ(registration_id
, notifications_
[0].registration_id
);
611 EXPECT_EQ(STORAGE_RECOVERED
, notifications_
[1].type
);
612 EXPECT_EQ(REGISTRATION_STORED
, notifications_
[2].type
);
613 EXPECT_EQ(pattern
, notifications_
[2].pattern
);
614 EXPECT_EQ(registration_id
, notifications_
[2].registration_id
);
617 TEST_F(ServiceWorkerContextTest
, ProviderHostIterator
) {
618 const int kRenderProcessId1
= 1;
619 const int kRenderProcessId2
= 2;
620 const GURL kOrigin1
= GURL("http://www.example.com/");
621 const GURL kOrigin2
= GURL("https://www.example.com/");
624 // Host1 (provider_id=1): process_id=1, origin1.
625 ServiceWorkerProviderHost
* host1(new ServiceWorkerProviderHost(
626 kRenderProcessId1
, MSG_ROUTING_NONE
, provider_id
++,
627 SERVICE_WORKER_PROVIDER_FOR_WINDOW
, context()->AsWeakPtr(), nullptr));
628 host1
->SetDocumentUrl(kOrigin1
);
630 // Host2 (provider_id=2): process_id=2, origin2.
631 ServiceWorkerProviderHost
* host2(new ServiceWorkerProviderHost(
632 kRenderProcessId2
, MSG_ROUTING_NONE
, provider_id
++,
633 SERVICE_WORKER_PROVIDER_FOR_WINDOW
, context()->AsWeakPtr(), nullptr));
634 host2
->SetDocumentUrl(kOrigin2
);
636 // Host3 (provider_id=3): process_id=2, origin1.
637 ServiceWorkerProviderHost
* host3(new ServiceWorkerProviderHost(
638 kRenderProcessId2
, MSG_ROUTING_NONE
, provider_id
++,
639 SERVICE_WORKER_PROVIDER_FOR_WINDOW
, context()->AsWeakPtr(), nullptr));
640 host3
->SetDocumentUrl(kOrigin1
);
642 // Host4 (provider_id=4): process_id=2, origin2, for ServiceWorker.
643 ServiceWorkerProviderHost
* host4(new ServiceWorkerProviderHost(
644 kRenderProcessId2
, MSG_ROUTING_NONE
, provider_id
++,
645 SERVICE_WORKER_PROVIDER_FOR_CONTROLLER
, context()->AsWeakPtr(), nullptr));
646 host4
->SetDocumentUrl(kOrigin2
);
648 context()->AddProviderHost(make_scoped_ptr(host1
));
649 context()->AddProviderHost(make_scoped_ptr(host2
));
650 context()->AddProviderHost(make_scoped_ptr(host3
));
651 context()->AddProviderHost(make_scoped_ptr(host4
));
653 // Iterate over all provider hosts.
654 std::set
<ServiceWorkerProviderHost
*> results
;
655 for (auto it
= context()->GetProviderHostIterator(); !it
->IsAtEnd();
657 results
.insert(it
->GetProviderHost());
659 EXPECT_EQ(4u, results
.size());
660 EXPECT_TRUE(ContainsKey(results
, host1
));
661 EXPECT_TRUE(ContainsKey(results
, host2
));
662 EXPECT_TRUE(ContainsKey(results
, host3
));
663 EXPECT_TRUE(ContainsKey(results
, host4
));
665 // Iterate over the client provider hosts that belong to kOrigin1.
667 for (auto it
= context()->GetClientProviderHostIterator(kOrigin1
);
668 !it
->IsAtEnd(); it
->Advance()) {
669 results
.insert(it
->GetProviderHost());
671 EXPECT_EQ(2u, results
.size());
672 EXPECT_TRUE(ContainsKey(results
, host1
));
673 EXPECT_TRUE(ContainsKey(results
, host3
));
675 // Iterate over the provider hosts that belong to kOrigin2.
676 // (This should not include host4 as it's not for controllee.)
678 for (auto it
= context()->GetClientProviderHostIterator(kOrigin2
);
679 !it
->IsAtEnd(); it
->Advance()) {
680 results
.insert(it
->GetProviderHost());
682 EXPECT_EQ(1u, results
.size());
683 EXPECT_TRUE(ContainsKey(results
, host2
));
685 context()->RemoveProviderHost(kRenderProcessId1
, 1);
686 context()->RemoveProviderHost(kRenderProcessId2
, 2);
687 context()->RemoveProviderHost(kRenderProcessId2
, 3);
688 context()->RemoveProviderHost(kRenderProcessId2
, 4);
691 } // namespace content