OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 void GetRegistration(int64 provider_id, | 123 void GetRegistration(int64 provider_id, |
124 GURL document_url, | 124 GURL document_url, |
125 uint32 expected_message) { | 125 uint32 expected_message) { |
126 SendGetRegistration(provider_id, document_url); | 126 SendGetRegistration(provider_id, document_url); |
127 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | 127 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
128 expected_message)); | 128 expected_message)); |
129 dispatcher_host_->ipc_sink()->ClearMessages(); | 129 dispatcher_host_->ipc_sink()->ClearMessages(); |
130 } | 130 } |
131 | 131 |
132 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { | 132 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { |
133 return new ServiceWorkerProviderHost(kRenderProcessId, | 133 return new ServiceWorkerProviderHost( |
134 kRenderFrameId, | 134 kRenderProcessId, kRenderFrameId, provider_id, |
135 provider_id, | 135 SERVICE_WORKER_PROVIDER_FOR_WINDOW, context()->AsWeakPtr(), |
136 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, | 136 dispatcher_host_.get()); |
137 context()->AsWeakPtr(), | |
138 dispatcher_host_.get()); | |
139 } | 137 } |
140 | 138 |
141 | 139 |
142 TestBrowserThreadBundle browser_thread_bundle_; | 140 TestBrowserThreadBundle browser_thread_bundle_; |
143 content::MockResourceContext resource_context_; | 141 content::MockResourceContext resource_context_; |
144 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 142 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
145 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; | 143 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; |
146 }; | 144 }; |
147 | 145 |
148 class ServiceWorkerTestContentBrowserClient : public TestContentBrowserClient { | 146 class ServiceWorkerTestContentBrowserClient : public TestContentBrowserClient { |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 | 399 |
402 Register(-1, | 400 Register(-1, |
403 GURL(), | 401 GURL(), |
404 GURL(), | 402 GURL(), |
405 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); | 403 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
406 } | 404 } |
407 | 405 |
408 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 406 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
409 const int kProviderId = 1001; // Test with a value != kRenderProcessId. | 407 const int kProviderId = 1001; // Test with a value != kRenderProcessId. |
410 | 408 |
411 dispatcher_host_->OnMessageReceived( | 409 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
412 ServiceWorkerHostMsg_ProviderCreated( | 410 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); |
413 kProviderId, MSG_ROUTING_NONE, | |
414 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE)); | |
415 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 411 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
416 | 412 |
417 // Two with the same ID should be seen as a bad message. | 413 // Two with the same ID should be seen as a bad message. |
418 dispatcher_host_->OnMessageReceived( | 414 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
419 ServiceWorkerHostMsg_ProviderCreated( | 415 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); |
420 kProviderId, MSG_ROUTING_NONE, | |
421 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE)); | |
422 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); | 416 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); |
423 | 417 |
424 dispatcher_host_->OnMessageReceived( | 418 dispatcher_host_->OnMessageReceived( |
425 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 419 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
426 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 420 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
427 | 421 |
428 // Destroying an ID that does not exist warrants a bad message. | 422 // Destroying an ID that does not exist warrants a bad message. |
429 dispatcher_host_->OnMessageReceived( | 423 dispatcher_host_->OnMessageReceived( |
430 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 424 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
431 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); | 425 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); |
432 | 426 |
433 // Deletion of the dispatcher_host should cause providers for that | 427 // Deletion of the dispatcher_host should cause providers for that |
434 // process to get deleted as well. | 428 // process to get deleted as well. |
435 dispatcher_host_->OnMessageReceived( | 429 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
436 ServiceWorkerHostMsg_ProviderCreated( | 430 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); |
437 kProviderId, MSG_ROUTING_NONE, | |
438 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE)); | |
439 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 431 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
440 EXPECT_TRUE(dispatcher_host_->HasOneRef()); | 432 EXPECT_TRUE(dispatcher_host_->HasOneRef()); |
441 dispatcher_host_ = NULL; | 433 dispatcher_host_ = NULL; |
442 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 434 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
443 } | 435 } |
444 | 436 |
445 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { | 437 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { |
446 const int64 kProviderId = 99; // Dummy value | 438 const int64 kProviderId = 99; // Dummy value |
447 scoped_ptr<ServiceWorkerProviderHost> host( | 439 scoped_ptr<ServiceWorkerProviderHost> host( |
448 CreateServiceWorkerProviderHost(kProviderId)); | 440 CreateServiceWorkerProviderHost(kProviderId)); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 base::RunLoop().RunUntilIdle(); | 488 base::RunLoop().RunUntilIdle(); |
497 | 489 |
498 GetRegistration(-1, | 490 GetRegistration(-1, |
499 GURL(), | 491 GURL(), |
500 ServiceWorkerMsg_ServiceWorkerGetRegistrationError::ID); | 492 ServiceWorkerMsg_ServiceWorkerGetRegistrationError::ID); |
501 } | 493 } |
502 | 494 |
503 TEST_F(ServiceWorkerDispatcherHostTest, CleanupOnRendererCrash) { | 495 TEST_F(ServiceWorkerDispatcherHostTest, CleanupOnRendererCrash) { |
504 // Add a provider and worker. | 496 // Add a provider and worker. |
505 const int64 kProviderId = 99; // Dummy value | 497 const int64 kProviderId = 99; // Dummy value |
506 dispatcher_host_->OnMessageReceived( | 498 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
507 ServiceWorkerHostMsg_ProviderCreated( | 499 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); |
508 kProviderId, MSG_ROUTING_NONE, | |
509 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE)); | |
510 | 500 |
511 GURL pattern = GURL("http://www.example.com/"); | 501 GURL pattern = GURL("http://www.example.com/"); |
512 scoped_refptr<ServiceWorkerRegistration> registration( | 502 scoped_refptr<ServiceWorkerRegistration> registration( |
513 new ServiceWorkerRegistration(pattern, | 503 new ServiceWorkerRegistration(pattern, |
514 1L, | 504 1L, |
515 helper_->context()->AsWeakPtr())); | 505 helper_->context()->AsWeakPtr())); |
516 scoped_refptr<ServiceWorkerVersion> version( | 506 scoped_refptr<ServiceWorkerVersion> version( |
517 new ServiceWorkerVersion(registration.get(), | 507 new ServiceWorkerVersion(registration.get(), |
518 GURL("http://www.example.com/service_worker.js"), | 508 GURL("http://www.example.com/service_worker.js"), |
519 1L, | 509 1L, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 // render process. | 547 // render process. |
558 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( | 548 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( |
559 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 549 new TestingServiceWorkerDispatcherHost(kRenderProcessId, |
560 context_wrapper(), | 550 context_wrapper(), |
561 &resource_context_, | 551 &resource_context_, |
562 helper_.get())); | 552 helper_.get())); |
563 | 553 |
564 // To show the new dispatcher can operate, simulate provider creation. Since | 554 // To show the new dispatcher can operate, simulate provider creation. Since |
565 // the old dispatcher cleaned up the old provider host, the new one won't | 555 // the old dispatcher cleaned up the old provider host, the new one won't |
566 // complain. | 556 // complain. |
567 new_dispatcher_host->OnMessageReceived( | 557 new_dispatcher_host->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
568 ServiceWorkerHostMsg_ProviderCreated( | 558 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); |
569 kProviderId, MSG_ROUTING_NONE, | |
570 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE)); | |
571 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); | 559 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); |
572 } | 560 } |
573 | 561 |
574 } // namespace content | 562 } // namespace content |
OLD | NEW |