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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 helper_.reset(); | 111 helper_.reset(); |
112 } | 112 } |
113 | 113 |
114 ServiceWorkerContextCore* context() { return helper_->context(); } | 114 ServiceWorkerContextCore* context() { return helper_->context(); } |
115 ServiceWorkerContextWrapper* context_wrapper() { | 115 ServiceWorkerContextWrapper* context_wrapper() { |
116 return helper_->context_wrapper(); | 116 return helper_->context_wrapper(); |
117 } | 117 } |
118 | 118 |
119 void Initialize(std::unique_ptr<EmbeddedWorkerTestHelper> helper) { | 119 void Initialize(std::unique_ptr<EmbeddedWorkerTestHelper> helper) { |
120 helper_.reset(helper.release()); | 120 helper_.reset(helper.release()); |
| 121 // Replace the default dispatcher host. |
| 122 int process_id = helper_->mock_render_process_id(); |
| 123 context()->RemoveDispatcherHost(process_id); |
121 dispatcher_host_ = new TestingServiceWorkerDispatcherHost( | 124 dispatcher_host_ = new TestingServiceWorkerDispatcherHost( |
122 helper_->mock_render_process_id(), context_wrapper(), | 125 process_id, context_wrapper(), &resource_context_, helper_.get()); |
123 &resource_context_, helper_.get()); | |
124 } | 126 } |
125 | 127 |
126 void SetUpRegistration(const GURL& scope, const GURL& script_url) { | 128 void SetUpRegistration(const GURL& scope, const GURL& script_url) { |
127 registration_ = new ServiceWorkerRegistration( | 129 registration_ = new ServiceWorkerRegistration( |
128 scope, 1L, helper_->context()->AsWeakPtr()); | 130 scope, 1L, helper_->context()->AsWeakPtr()); |
129 version_ = new ServiceWorkerVersion(registration_.get(), script_url, 1L, | 131 version_ = new ServiceWorkerVersion(registration_.get(), script_url, 1L, |
130 helper_->context()->AsWeakPtr()); | 132 helper_->context()->AsWeakPtr()); |
131 std::vector<ServiceWorkerDatabase::ResourceRecord> records; | 133 std::vector<ServiceWorkerDatabase::ResourceRecord> records; |
132 records.push_back( | 134 records.push_back( |
133 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100)); | 135 ServiceWorkerDatabase::ResourceRecord(10, version_->script_url(), 100)); |
(...skipping 13 matching lines...) Expand all Loading... |
147 registration_.get(), version_.get(), | 149 registration_.get(), version_.get(), |
148 base::Bind(&SaveStatusCallback, &called, &status)); | 150 base::Bind(&SaveStatusCallback, &called, &status)); |
149 base::RunLoop().RunUntilIdle(); | 151 base::RunLoop().RunUntilIdle(); |
150 EXPECT_TRUE(called); | 152 EXPECT_TRUE(called); |
151 EXPECT_EQ(SERVICE_WORKER_OK, status); | 153 EXPECT_EQ(SERVICE_WORKER_OK, status); |
152 } | 154 } |
153 | 155 |
154 void SendSetHostedVersionId(int provider_id, | 156 void SendSetHostedVersionId(int provider_id, |
155 int64_t version_id, | 157 int64_t version_id, |
156 int embedded_worker_id) { | 158 int embedded_worker_id) { |
157 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_SetVersionId( | 159 dispatcher_host_->OnSetHostedVersionId(provider_id, version_id, |
158 provider_id, version_id, embedded_worker_id)); | 160 embedded_worker_id); |
159 } | 161 } |
160 | 162 |
161 void SendProviderCreated(ServiceWorkerProviderType type, | 163 void SendProviderCreated(ServiceWorkerProviderType type, |
162 const GURL& pattern) { | 164 const GURL& pattern) { |
163 const int64_t kProviderId = 99; | 165 const int64_t kProviderId = 99; |
164 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 166 ServiceWorkerProviderHostInfo info(kProviderId, MSG_ROUTING_NONE, type, |
165 kProviderId, MSG_ROUTING_NONE, type, | 167 true /* is_parent_frame_secure */); |
166 true /* is_parent_frame_secure */)); | 168 dispatcher_host_->OnProviderCreated(std::move(info)); |
167 helper_->SimulateAddProcessToPattern(pattern, | 169 helper_->SimulateAddProcessToPattern(pattern, |
168 helper_->mock_render_process_id()); | 170 helper_->mock_render_process_id()); |
169 provider_host_ = context()->GetProviderHost( | 171 provider_host_ = context()->GetProviderHost( |
170 helper_->mock_render_process_id(), kProviderId); | 172 helper_->mock_render_process_id(), kProviderId); |
171 } | 173 } |
172 | 174 |
173 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { | 175 void SendRegister(int64_t provider_id, GURL pattern, GURL worker_url) { |
174 dispatcher_host_->OnMessageReceived( | 176 dispatcher_host_->OnMessageReceived( |
175 ServiceWorkerHostMsg_RegisterServiceWorker( | 177 ServiceWorkerHostMsg_RegisterServiceWorker( |
176 -1, -1, provider_id, pattern, worker_url)); | 178 -1, -1, provider_id, pattern, worker_url)); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 const base::string16& message, | 239 const base::string16& message, |
238 const url::Origin& source_origin, | 240 const url::Origin& source_origin, |
239 const std::vector<MessagePort>& sent_message_ports, | 241 const std::vector<MessagePort>& sent_message_ports, |
240 ServiceWorkerProviderHost* sender_provider_host, | 242 ServiceWorkerProviderHost* sender_provider_host, |
241 const ServiceWorkerDispatcherHost::StatusCallback& callback) { | 243 const ServiceWorkerDispatcherHost::StatusCallback& callback) { |
242 dispatcher_host_->DispatchExtendableMessageEvent( | 244 dispatcher_host_->DispatchExtendableMessageEvent( |
243 std::move(worker), message, source_origin, sent_message_ports, | 245 std::move(worker), message, source_origin, sent_message_ports, |
244 sender_provider_host, callback); | 246 sender_provider_host, callback); |
245 } | 247 } |
246 | 248 |
247 ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { | 249 std::unique_ptr<ServiceWorkerProviderHost> CreateServiceWorkerProviderHost( |
248 return new ServiceWorkerProviderHost( | 250 int provider_id) { |
249 helper_->mock_render_process_id(), kRenderFrameId, provider_id, | 251 return CreateProviderHostWithDispatcherHost( |
250 SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 252 helper_->mock_render_process_id(), provider_id, context()->AsWeakPtr(), |
251 ServiceWorkerProviderHost::FrameSecurityLevel::SECURE, | 253 kRenderFrameId, dispatcher_host_.get()); |
252 context()->AsWeakPtr(), dispatcher_host_.get()); | |
253 } | 254 } |
254 | 255 |
255 TestBrowserThreadBundle browser_thread_bundle_; | 256 TestBrowserThreadBundle browser_thread_bundle_; |
256 content::MockResourceContext resource_context_; | 257 content::MockResourceContext resource_context_; |
257 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 258 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
258 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; | 259 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; |
259 scoped_refptr<ServiceWorkerRegistration> registration_; | 260 scoped_refptr<ServiceWorkerRegistration> registration_; |
260 scoped_refptr<ServiceWorkerVersion> version_; | 261 scoped_refptr<ServiceWorkerVersion> version_; |
261 ServiceWorkerProviderHost* provider_host_; | 262 ServiceWorkerProviderHost* provider_host_; |
262 }; | 263 }; |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 Register(-1, | 502 Register(-1, |
502 GURL(), | 503 GURL(), |
503 GURL(), | 504 GURL(), |
504 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); | 505 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
505 } | 506 } |
506 | 507 |
507 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 508 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
508 const int kProviderId = 1001; | 509 const int kProviderId = 1001; |
509 int process_id = helper_->mock_render_process_id(); | 510 int process_id = helper_->mock_render_process_id(); |
510 | 511 |
511 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 512 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo( |
512 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 513 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
513 true /* is_parent_frame_secure */)); | 514 true /* is_parent_frame_secure */)); |
514 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); | 515 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); |
515 | 516 |
516 // Two with the same ID should be seen as a bad message. | 517 // Two with the same ID should be seen as a bad message. |
517 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 518 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo( |
518 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 519 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
519 true /* is_parent_frame_secure */)); | 520 true /* is_parent_frame_secure */)); |
520 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); | 521 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); |
521 | 522 |
522 dispatcher_host_->OnMessageReceived( | 523 dispatcher_host_->OnProviderDestroyed(kProviderId); |
523 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | |
524 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); | 524 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); |
525 | 525 |
526 // Destroying an ID that does not exist warrants a bad message. | 526 // Destroying an ID that does not exist warrants a bad message. |
527 dispatcher_host_->OnMessageReceived( | 527 dispatcher_host_->OnProviderDestroyed(kProviderId); |
528 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | |
529 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); | 528 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); |
530 | 529 |
531 // Deletion of the dispatcher_host should cause providers for that | 530 // Deletion of the dispatcher_host should cause providers for that |
532 // process to get deleted as well. | 531 // process to get deleted as well. |
533 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 532 dispatcher_host_->OnProviderCreated(ServiceWorkerProviderHostInfo( |
534 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 533 kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
535 true /* is_parent_frame_secure */)); | 534 true /* is_parent_frame_secure */)); |
536 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); | 535 EXPECT_TRUE(context()->GetProviderHost(process_id, kProviderId)); |
537 EXPECT_TRUE(dispatcher_host_->HasOneRef()); | 536 EXPECT_TRUE(dispatcher_host_->HasOneRef()); |
538 dispatcher_host_ = NULL; | 537 dispatcher_host_ = nullptr; |
539 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); | 538 EXPECT_FALSE(context()->GetProviderHost(process_id, kProviderId)); |
540 } | 539 } |
541 | 540 |
542 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { | 541 TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { |
543 const int64_t kProviderId = 99; // Dummy value | 542 const int64_t kProviderId = 99; // Dummy value |
544 std::unique_ptr<ServiceWorkerProviderHost> host( | 543 std::unique_ptr<ServiceWorkerProviderHost> host( |
545 CreateServiceWorkerProviderHost(kProviderId)); | 544 CreateServiceWorkerProviderHost(kProviderId)); |
546 host->SetDocumentUrl(GURL("https://www.example.com/foo")); | 545 host->SetDocumentUrl(GURL("https://www.example.com/foo")); |
547 context()->AddProviderHost(std::move(host)); | 546 context()->AddProviderHost(std::move(host)); |
548 | 547 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 // We should be able to hook up a new dispatcher host although the old object | 659 // We should be able to hook up a new dispatcher host although the old object |
661 // is not yet destroyed. This is what the browser does when reusing a crashed | 660 // is not yet destroyed. This is what the browser does when reusing a crashed |
662 // render process. | 661 // render process. |
663 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( | 662 scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( |
664 new TestingServiceWorkerDispatcherHost( | 663 new TestingServiceWorkerDispatcherHost( |
665 process_id, context_wrapper(), &resource_context_, helper_.get())); | 664 process_id, context_wrapper(), &resource_context_, helper_.get())); |
666 | 665 |
667 // To show the new dispatcher can operate, simulate provider creation. Since | 666 // To show the new dispatcher can operate, simulate provider creation. Since |
668 // the old dispatcher cleaned up the old provider host, the new one won't | 667 // the old dispatcher cleaned up the old provider host, the new one won't |
669 // complain. | 668 // complain. |
670 new_dispatcher_host->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( | 669 new_dispatcher_host->OnProviderCreated(ServiceWorkerProviderHostInfo( |
671 provider_id, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, | 670 provider_id, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW, |
672 true /* is_parent_frame_secure */)); | 671 true /* is_parent_frame_secure */)); |
673 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); | 672 EXPECT_EQ(0, new_dispatcher_host->bad_messages_received_count_); |
674 } | 673 } |
675 | 674 |
676 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { | 675 TEST_F(ServiceWorkerDispatcherHostTest, DispatchExtendableMessageEvent) { |
677 GURL pattern = GURL("http://www.example.com/"); | 676 GURL pattern = GURL("http://www.example.com/"); |
678 GURL script_url = GURL("http://www.example.com/service_worker.js"); | 677 GURL script_url = GURL("http://www.example.com/service_worker.js"); |
679 | 678 |
680 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); | 679 SendProviderCreated(SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, pattern); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
832 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( | 831 dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_FetchEventResponse( |
833 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, | 832 version_->embedded_worker()->embedded_worker_id(), kFetchEventId, |
834 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(), | 833 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(), |
835 base::Time::Now())); | 834 base::Time::Now())); |
836 | 835 |
837 base::RunLoop().RunUntilIdle(); | 836 base::RunLoop().RunUntilIdle(); |
838 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); | 837 EXPECT_EQ(0, dispatcher_host_->bad_messages_received_count_); |
839 } | 838 } |
840 | 839 |
841 } // namespace content | 840 } // namespace content |
OLD | NEW |