| 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" |
| 11 #include "content/browser/service_worker/embedded_worker_instance.h" | 11 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 12 #include "content/browser/service_worker/embedded_worker_registry.h" | 12 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 13 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 13 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 14 #include "content/browser/service_worker/service_worker_context_core.h" | 14 #include "content/browser/service_worker/service_worker_context_core.h" |
| 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 16 #include "content/browser/service_worker/service_worker_provider_host_registry.h
" |
| 16 #include "content/common/service_worker/embedded_worker_messages.h" | 17 #include "content/common/service_worker/embedded_worker_messages.h" |
| 17 #include "content/common/service_worker/service_worker_messages.h" | 18 #include "content/common/service_worker/service_worker_messages.h" |
| 18 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 | 24 |
| 24 static const int kRenderProcessId = 1; | 25 static const int kRenderProcessId = 1; |
| 25 | 26 |
| 26 class ServiceWorkerDispatcherHostTest : public testing::Test { | 27 class ServiceWorkerDispatcherHostTest : public testing::Test { |
| 27 protected: | 28 protected: |
| 28 ServiceWorkerDispatcherHostTest() | 29 ServiceWorkerDispatcherHostTest() |
| 29 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 30 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 30 | 31 |
| 31 virtual void SetUp() { | 32 virtual void SetUp() { |
| 32 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); | 33 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
| 33 } | 34 } |
| 34 | 35 |
| 35 virtual void TearDown() { | 36 virtual void TearDown() { |
| 36 helper_.reset(); | 37 helper_.reset(); |
| 37 } | 38 } |
| 38 | 39 |
| 39 ServiceWorkerContextCore* context() { return helper_->context(); } | |
| 40 ServiceWorkerContextWrapper* context_wrapper() { | 40 ServiceWorkerContextWrapper* context_wrapper() { |
| 41 return helper_->context_wrapper(); | 41 return helper_->context_wrapper(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 ServiceWorkerProviderHostRegistry* provider_registry() { |
| 45 return context_wrapper()->provider_registry(); |
| 46 } |
| 47 |
| 44 TestBrowserThreadBundle browser_thread_bundle_; | 48 TestBrowserThreadBundle browser_thread_bundle_; |
| 45 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 49 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 46 }; | 50 }; |
| 47 | 51 |
| 48 | 52 |
| 49 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | 53 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
| 50 public: | 54 public: |
| 51 TestingServiceWorkerDispatcherHost( | 55 TestingServiceWorkerDispatcherHost( |
| 52 int process_id, | 56 int process_id, |
| 53 ServiceWorkerContextWrapper* context_wrapper, | 57 ServiceWorkerContextWrapper* context_wrapper, |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 149 |
| 146 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 150 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
| 147 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 151 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 148 new TestingServiceWorkerDispatcherHost( | 152 new TestingServiceWorkerDispatcherHost( |
| 149 kRenderProcessId, context_wrapper(), helper_.get()); | 153 kRenderProcessId, context_wrapper(), helper_.get()); |
| 150 | 154 |
| 151 const int kProviderId = 1001; // Test with a value != kRenderProcessId. | 155 const int kProviderId = 1001; // Test with a value != kRenderProcessId. |
| 152 | 156 |
| 153 dispatcher_host->OnMessageReceived( | 157 dispatcher_host->OnMessageReceived( |
| 154 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); | 158 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
| 155 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 159 EXPECT_TRUE(provider_registry()->GetProviderHost(kRenderProcessId, |
| 160 kProviderId)); |
| 156 | 161 |
| 157 // Two with the same ID should be seen as a bad message. | 162 // Two with the same ID should be seen as a bad message. |
| 158 dispatcher_host->OnMessageReceived( | 163 dispatcher_host->OnMessageReceived( |
| 159 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); | 164 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
| 160 EXPECT_EQ(1, dispatcher_host->bad_messages_received_count_); | 165 EXPECT_EQ(1, dispatcher_host->bad_messages_received_count_); |
| 161 | 166 |
| 162 dispatcher_host->OnMessageReceived( | 167 dispatcher_host->OnMessageReceived( |
| 163 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 168 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
| 164 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 169 EXPECT_FALSE(provider_registry()->GetProviderHost(kRenderProcessId, |
| 170 kProviderId)); |
| 165 | 171 |
| 166 // Destroying an ID that does not exist warrants a bad message. | 172 // Destroying an ID that does not exist warrants a bad message. |
| 167 dispatcher_host->OnMessageReceived( | 173 dispatcher_host->OnMessageReceived( |
| 168 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 174 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
| 169 EXPECT_EQ(2, dispatcher_host->bad_messages_received_count_); | 175 EXPECT_EQ(2, dispatcher_host->bad_messages_received_count_); |
| 170 | 176 |
| 171 // Deletion of the dispatcher_host should cause providers for that | 177 // Deletion of the dispatcher_host should cause providers for that |
| 172 // process to get deleted as well. | 178 // process to get deleted as well. |
| 173 dispatcher_host->OnMessageReceived( | 179 dispatcher_host->OnMessageReceived( |
| 174 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); | 180 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
| 175 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 181 EXPECT_TRUE(provider_registry()->GetProviderHost(kRenderProcessId, |
| 182 kProviderId)); |
| 176 EXPECT_TRUE(dispatcher_host->HasOneRef()); | 183 EXPECT_TRUE(dispatcher_host->HasOneRef()); |
| 177 dispatcher_host = NULL; | 184 dispatcher_host = NULL; |
| 178 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 185 EXPECT_FALSE(provider_registry()->GetProviderHost(kRenderProcessId, |
| 186 kProviderId)); |
| 179 } | 187 } |
| 180 | 188 |
| 181 } // namespace content | 189 } // namespace content |
| OLD | NEW |