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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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( | 133 return new ServiceWorkerProviderHost( |
134 kRenderProcessId, kRenderFrameId, provider_id, | 134 kRenderProcessId, kRenderFrameId, |
135 SERVICE_WORKER_PROVIDER_FOR_WINDOW, context()->AsWeakPtr(), | 135 MSG_ROUTING_NONE /* shared_worker_route_id */, provider_id, |
136 dispatcher_host_.get()); | 136 context()->AsWeakPtr(), dispatcher_host_.get()); |
137 } | 137 } |
138 | 138 |
139 | 139 |
140 TestBrowserThreadBundle browser_thread_bundle_; | 140 TestBrowserThreadBundle browser_thread_bundle_; |
141 content::MockResourceContext resource_context_; | 141 content::MockResourceContext resource_context_; |
142 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 142 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
143 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; | 143 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; |
144 }; | 144 }; |
145 | 145 |
146 class ServiceWorkerTestContentBrowserClient : public TestContentBrowserClient { | 146 class ServiceWorkerTestContentBrowserClient : public TestContentBrowserClient { |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 base::RunLoop().RunUntilIdle(); | 400 base::RunLoop().RunUntilIdle(); |
401 | 401 |
402 Register(-1, | 402 Register(-1, |
403 GURL(), | 403 GURL(), |
404 GURL(), | 404 GURL(), |
405 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); | 405 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
406 } | 406 } |
407 | 407 |
408 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 408 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
409 const int kProviderId = 1001; // Test with a value != kRenderProcessId. | 409 const int kProviderId = 1001; // Test with a value != kRenderProcessId. |
| 410 const int kRenderFrameId = 1; |
410 | 411 |
411 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 412 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
412 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); | 413 kProviderId, kRenderFrameId, MSG_ROUTING_NONE)); |
413 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 414 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
414 | 415 |
415 // Two with the same ID should be seen as a bad message. | 416 // Two with the same ID should be seen as a bad message. |
416 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 417 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
417 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); | 418 kProviderId, kRenderFrameId, MSG_ROUTING_NONE)); |
418 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); | 419 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); |
419 | 420 |
420 dispatcher_host_->OnMessageReceived( | 421 dispatcher_host_->OnMessageReceived( |
421 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 422 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
422 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 423 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
423 | 424 |
424 // Destroying an ID that does not exist warrants a bad message. | 425 // Destroying an ID that does not exist warrants a bad message. |
425 dispatcher_host_->OnMessageReceived( | 426 dispatcher_host_->OnMessageReceived( |
426 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 427 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
427 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); | 428 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); |
428 | 429 |
429 // Deletion of the dispatcher_host should cause providers for that | 430 // Deletion of the dispatcher_host should cause providers for that |
430 // process to get deleted as well. | 431 // process to get deleted as well. |
431 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 432 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
432 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); | 433 kProviderId, kRenderFrameId, MSG_ROUTING_NONE)); |
433 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 434 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
434 EXPECT_TRUE(dispatcher_host_->HasOneRef()); | 435 EXPECT_TRUE(dispatcher_host_->HasOneRef()); |
435 dispatcher_host_ = NULL; | 436 dispatcher_host_ = NULL; |
436 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 437 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
437 } | 438 } |
438 | 439 |
439 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { | 440 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { |
440 const int64 kProviderId = 99; // Dummy value | 441 const int64 kProviderId = 99; // Dummy value |
441 scoped_ptr<ServiceWorkerProviderHost> host( | 442 scoped_ptr<ServiceWorkerProviderHost> host( |
442 CreateServiceWorkerProviderHost(kProviderId)); | 443 CreateServiceWorkerProviderHost(kProviderId)); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 base::RunLoop().RunUntilIdle(); | 491 base::RunLoop().RunUntilIdle(); |
491 | 492 |
492 GetRegistration(-1, | 493 GetRegistration(-1, |
493 GURL(), | 494 GURL(), |
494 ServiceWorkerMsg_ServiceWorkerGetRegistrationError::ID); | 495 ServiceWorkerMsg_ServiceWorkerGetRegistrationError::ID); |
495 } | 496 } |
496 | 497 |
497 TEST_F(ServiceWorkerDispatcherHostTest, CleanupOnRendererCrash) { | 498 TEST_F(ServiceWorkerDispatcherHostTest, CleanupOnRendererCrash) { |
498 // Add a provider and worker. | 499 // Add a provider and worker. |
499 const int64 kProviderId = 99; // Dummy value | 500 const int64 kProviderId = 99; // Dummy value |
| 501 const int64 kRenderFrameId = 1; // Dummy value |
500 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 502 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
501 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); | 503 kProviderId, kRenderFrameId, MSG_ROUTING_NONE)); |
502 | 504 |
503 GURL pattern = GURL("http://www.example.com/"); | 505 GURL pattern = GURL("http://www.example.com/"); |
504 scoped_refptr<ServiceWorkerRegistration> registration( | 506 scoped_refptr<ServiceWorkerRegistration> registration( |
505 new ServiceWorkerRegistration(pattern, | 507 new ServiceWorkerRegistration(pattern, |
506 1L, | 508 1L, |
507 helper_->context()->AsWeakPtr())); | 509 helper_->context()->AsWeakPtr())); |
508 scoped_refptr<ServiceWorkerVersion> version( | 510 scoped_refptr<ServiceWorkerVersion> version( |
509 new ServiceWorkerVersion(registration.get(), | 511 new ServiceWorkerVersion(registration.get(), |
510 GURL("http://www.example.com/service_worker.js"), | 512 GURL("http://www.example.com/service_worker.js"), |
511 1L, | 513 1L, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( | 556 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( |
555 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 557 new TestingServiceWorkerDispatcherHost(kRenderProcessId, |
556 context_wrapper(), | 558 context_wrapper(), |
557 &resource_context_, | 559 &resource_context_, |
558 helper_.get())); | 560 helper_.get())); |
559 | 561 |
560 // To show the new dispatcher can operate, simulate provider creation. Since | 562 // To show the new dispatcher can operate, simulate provider creation. Since |
561 // the old dispatcher cleaned up the old provider host, the new one won't | 563 // the old dispatcher cleaned up the old provider host, the new one won't |
562 // complain. | 564 // complain. |
563 new_dispatcher_host->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 565 new_dispatcher_host->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
564 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); | 566 kProviderId, kRenderFrameId, MSG_ROUTING_NONE)); |
565 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); | 567 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); |
566 } | 568 } |
567 | 569 |
568 } // namespace content | 570 } // namespace content |
OLD | NEW |