| 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/common/service_worker/embedded_worker_messages.h" | 16 #include "content/common/service_worker/embedded_worker_messages.h" |
| 17 #include "content/common/service_worker/service_worker_messages.h" | 17 #include "content/common/service_worker/service_worker_messages.h" |
| 18 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 | 23 |
| 24 static const int kRenderProcessId = 1; | 24 static const int kRenderProcessId = 1; |
| 25 | 25 |
| 26 class ServiceWorkerDispatcherHostTest : public testing::Test { | |
| 27 protected: | |
| 28 ServiceWorkerDispatcherHostTest() | |
| 29 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | |
| 30 | |
| 31 virtual void SetUp() { | |
| 32 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); | |
| 33 } | |
| 34 | |
| 35 virtual void TearDown() { | |
| 36 helper_.reset(); | |
| 37 } | |
| 38 | |
| 39 ServiceWorkerContextCore* context() { return helper_->context(); } | |
| 40 ServiceWorkerContextWrapper* context_wrapper() { | |
| 41 return helper_->context_wrapper(); | |
| 42 } | |
| 43 | |
| 44 TestBrowserThreadBundle browser_thread_bundle_; | |
| 45 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | |
| 46 }; | |
| 47 | |
| 48 | |
| 49 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | 26 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
| 50 public: | 27 public: |
| 51 TestingServiceWorkerDispatcherHost( | 28 TestingServiceWorkerDispatcherHost( |
| 52 int process_id, | 29 int process_id, |
| 53 ServiceWorkerContextWrapper* context_wrapper, | 30 ServiceWorkerContextWrapper* context_wrapper, |
| 54 EmbeddedWorkerTestHelper* helper) | 31 EmbeddedWorkerTestHelper* helper) |
| 55 : ServiceWorkerDispatcherHost(process_id, NULL), | 32 : ServiceWorkerDispatcherHost(process_id, NULL), |
| 56 bad_messages_received_count_(0), | 33 bad_messages_received_count_(0), |
| 57 helper_(helper) { | 34 helper_(helper) { |
| 58 Init(context_wrapper); | 35 Init(context_wrapper); |
| 59 } | 36 } |
| 60 | 37 |
| 61 virtual bool Send(IPC::Message* message) OVERRIDE { | 38 virtual bool Send(IPC::Message* message) OVERRIDE { |
| 62 return helper_->Send(message); | 39 return helper_->Send(message); |
| 63 } | 40 } |
| 64 | 41 |
| 65 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } | 42 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } |
| 66 | 43 |
| 67 virtual void BadMessageReceived() OVERRIDE { | 44 virtual void BadMessageReceived() OVERRIDE { |
| 68 ++bad_messages_received_count_; | 45 ++bad_messages_received_count_; |
| 69 } | 46 } |
| 70 | 47 |
| 71 int bad_messages_received_count_; | 48 int bad_messages_received_count_; |
| 72 | 49 |
| 73 protected: | 50 protected: |
| 74 EmbeddedWorkerTestHelper* helper_; | 51 EmbeddedWorkerTestHelper* helper_; |
| 75 virtual ~TestingServiceWorkerDispatcherHost() {} | 52 virtual ~TestingServiceWorkerDispatcherHost() {} |
| 76 }; | 53 }; |
| 77 | 54 |
| 55 class ServiceWorkerDispatcherHostTest : public testing::Test { |
| 56 protected: |
| 57 ServiceWorkerDispatcherHostTest() |
| 58 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 59 |
| 60 virtual void SetUp() { |
| 61 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
| 62 dispatcher_host_ = new TestingServiceWorkerDispatcherHost( |
| 63 kRenderProcessId, context_wrapper(), helper_.get()); |
| 64 } |
| 65 |
| 66 virtual void TearDown() { |
| 67 helper_.reset(); |
| 68 } |
| 69 |
| 70 ServiceWorkerContextCore* context() { return helper_->context(); } |
| 71 ServiceWorkerContextWrapper* context_wrapper() { |
| 72 return helper_->context_wrapper(); |
| 73 } |
| 74 |
| 75 void Register(int64 provider_id, |
| 76 GURL pattern, |
| 77 GURL worker_url, |
| 78 uint32 expected_message) { |
| 79 dispatcher_host_->OnMessageReceived( |
| 80 ServiceWorkerHostMsg_RegisterServiceWorker( |
| 81 -1, -1, provider_id, pattern, worker_url)); |
| 82 base::RunLoop().RunUntilIdle(); |
| 83 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
| 84 expected_message)); |
| 85 dispatcher_host_->ipc_sink()->ClearMessages(); |
| 86 } |
| 87 |
| 88 void Unregister(int64 provider_id, GURL pattern, uint32 expected_message) { |
| 89 dispatcher_host_->OnMessageReceived( |
| 90 ServiceWorkerHostMsg_UnregisterServiceWorker( |
| 91 -1, -1, provider_id, pattern)); |
| 92 base::RunLoop().RunUntilIdle(); |
| 93 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
| 94 expected_message)); |
| 95 dispatcher_host_->ipc_sink()->ClearMessages(); |
| 96 } |
| 97 |
| 98 TestBrowserThreadBundle browser_thread_bundle_; |
| 99 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 100 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; |
| 101 }; |
| 102 |
| 78 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { | 103 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { |
| 79 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 104 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 80 switches::kEnableServiceWorker)); | 105 switches::kEnableServiceWorker)); |
| 81 | 106 |
| 82 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 107 dispatcher_host_->OnMessageReceived( |
| 83 new TestingServiceWorkerDispatcherHost( | |
| 84 kRenderProcessId, context_wrapper(), helper_.get()); | |
| 85 | |
| 86 dispatcher_host->OnMessageReceived( | |
| 87 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); | 108 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); |
| 88 | 109 |
| 89 // TODO(alecflett): Pump the message loop when this becomes async. | 110 // TODO(alecflett): Pump the message loop when this becomes async. |
| 90 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); | 111 ASSERT_EQ(1UL, dispatcher_host_->ipc_sink()->message_count()); |
| 91 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 112 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
| 92 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); | 113 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
| 93 } | 114 } |
| 94 | 115 |
| 116 // TODO(falken): Enable this test when we remove the |
| 117 // --enable-service-worker-flag (see crbug.com/352581) |
| 118 TEST_F(ServiceWorkerDispatcherHostTest, DISABLED_RegisterSameOrigin) { |
| 119 const int64 kProviderId = 99; // Dummy value |
| 120 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
| 121 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL)); |
| 122 host->SetDocumentUrl(GURL("http://www.example.com/foo")); |
| 123 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); |
| 124 context()->AddProviderHost(host.Pass()); |
| 125 |
| 126 Register(kProviderId, |
| 127 GURL("http://www.example.com/*"), |
| 128 GURL("http://foo.example.com/bar"), |
| 129 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
| 130 Register(kProviderId, |
| 131 GURL("http://foo.example.com/*"), |
| 132 GURL("http://www.example.com/bar"), |
| 133 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
| 134 Register(kProviderId, |
| 135 GURL("http://foo.example.com/*"), |
| 136 GURL("http://foo.example.com/bar"), |
| 137 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
| 138 Register(kProviderId, |
| 139 GURL("http://www.example.com/*"), |
| 140 GURL("http://www.example.com/bar"), |
| 141 ServiceWorkerMsg_ServiceWorkerRegistered::ID); |
| 142 } |
| 143 |
| 144 // TODO(falken): Enable this test when we remove the |
| 145 // --enable-service-worker-flag (see crbug.com/352581) |
| 146 TEST_F(ServiceWorkerDispatcherHostTest, DISABLED_UnregisterSameOrigin) { |
| 147 const int64 kProviderId = 99; // Dummy value |
| 148 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
| 149 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL)); |
| 150 host->SetDocumentUrl(GURL("http://www.example.com/foo")); |
| 151 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); |
| 152 context()->AddProviderHost(host.Pass()); |
| 153 |
| 154 Unregister(kProviderId, |
| 155 GURL("http://foo.example.com/*"), |
| 156 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
| 157 Unregister(kProviderId, |
| 158 GURL("http://www.example.com/*"), |
| 159 ServiceWorkerMsg_ServiceWorkerUnregistered::ID); |
| 160 } |
| 161 |
| 95 // Disable this since now we cache command-line switch in | 162 // Disable this since now we cache command-line switch in |
| 96 // ServiceWorkerUtils::IsFeatureEnabled() and this could be flaky depending | 163 // ServiceWorkerUtils::IsFeatureEnabled() and this could be flaky depending |
| 97 // on testing order. (crbug.com/352581) | 164 // on testing order. (crbug.com/352581) |
| 98 // TODO(kinuko): Just remove DisabledCausesError test above and enable | 165 // TODO(kinuko): Just remove DisabledCausesError test above and enable |
| 99 // this test when we remove the --enable-service-worker flag. | 166 // this test when we remove the --enable-service-worker flag. |
| 100 TEST_F(ServiceWorkerDispatcherHostTest, DISABLED_Enabled) { | 167 TEST_F(ServiceWorkerDispatcherHostTest, DISABLED_Enabled) { |
| 101 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 168 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 102 switches::kEnableServiceWorker)); | 169 switches::kEnableServiceWorker)); |
| 103 CommandLine::ForCurrentProcess()->AppendSwitch( | 170 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 104 switches::kEnableServiceWorker); | 171 switches::kEnableServiceWorker); |
| 105 | 172 |
| 106 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 173 dispatcher_host_->OnMessageReceived( |
| 107 new TestingServiceWorkerDispatcherHost( | |
| 108 kRenderProcessId, context_wrapper(), helper_.get()); | |
| 109 | |
| 110 dispatcher_host->OnMessageReceived( | |
| 111 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); | 174 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); |
| 112 base::RunLoop().RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
| 113 | 176 |
| 114 // TODO(alecflett): Pump the message loop when this becomes async. | 177 // TODO(alecflett): Pump the message loop when this becomes async. |
| 115 ASSERT_EQ(2UL, dispatcher_host->ipc_sink()->message_count()); | 178 ASSERT_EQ(2UL, dispatcher_host_->ipc_sink()->message_count()); |
| 116 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 179 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
| 117 EmbeddedWorkerMsg_StartWorker::ID)); | 180 EmbeddedWorkerMsg_StartWorker::ID)); |
| 118 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 181 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
| 119 ServiceWorkerMsg_ServiceWorkerRegistered::ID)); | 182 ServiceWorkerMsg_ServiceWorkerRegistered::ID)); |
| 120 } | 183 } |
| 121 | 184 |
| 122 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { | 185 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { |
| 123 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 186 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 124 switches::kEnableServiceWorker)); | 187 switches::kEnableServiceWorker)); |
| 125 CommandLine::ForCurrentProcess()->AppendSwitch( | 188 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 126 switches::kEnableServiceWorker); | 189 switches::kEnableServiceWorker); |
| 127 | 190 |
| 128 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | |
| 129 new TestingServiceWorkerDispatcherHost( | |
| 130 kRenderProcessId, context_wrapper(), helper_.get()); | |
| 131 | |
| 132 helper_->ShutdownContext(); | 191 helper_->ShutdownContext(); |
| 133 | 192 |
| 134 // Let the shutdown reach the simulated IO thread. | 193 // Let the shutdown reach the simulated IO thread. |
| 135 base::RunLoop().RunUntilIdle(); | 194 base::RunLoop().RunUntilIdle(); |
| 136 | 195 |
| 137 dispatcher_host->OnMessageReceived( | 196 dispatcher_host_->OnMessageReceived( |
| 138 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); | 197 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); |
| 139 | 198 |
| 140 // TODO(alecflett): Pump the message loop when this becomes async. | 199 // TODO(alecflett): Pump the message loop when this becomes async. |
| 141 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); | 200 ASSERT_EQ(1UL, dispatcher_host_->ipc_sink()->message_count()); |
| 142 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( | 201 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
| 143 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); | 202 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
| 144 } | 203 } |
| 145 | 204 |
| 146 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 205 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
| 147 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | |
| 148 new TestingServiceWorkerDispatcherHost( | |
| 149 kRenderProcessId, context_wrapper(), helper_.get()); | |
| 150 | |
| 151 const int kProviderId = 1001; // Test with a value != kRenderProcessId. | 206 const int kProviderId = 1001; // Test with a value != kRenderProcessId. |
| 152 | 207 |
| 153 dispatcher_host->OnMessageReceived( | 208 dispatcher_host_->OnMessageReceived( |
| 154 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); | 209 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
| 155 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 210 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 156 | 211 |
| 157 // Two with the same ID should be seen as a bad message. | 212 // Two with the same ID should be seen as a bad message. |
| 158 dispatcher_host->OnMessageReceived( | 213 dispatcher_host_->OnMessageReceived( |
| 159 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); | 214 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
| 160 EXPECT_EQ(1, dispatcher_host->bad_messages_received_count_); | 215 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); |
| 161 | 216 |
| 162 dispatcher_host->OnMessageReceived( | 217 dispatcher_host_->OnMessageReceived( |
| 163 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 218 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
| 164 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 219 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 165 | 220 |
| 166 // Destroying an ID that does not exist warrants a bad message. | 221 // Destroying an ID that does not exist warrants a bad message. |
| 167 dispatcher_host->OnMessageReceived( | 222 dispatcher_host_->OnMessageReceived( |
| 168 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); | 223 ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
| 169 EXPECT_EQ(2, dispatcher_host->bad_messages_received_count_); | 224 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); |
| 170 | 225 |
| 171 // Deletion of the dispatcher_host should cause providers for that | 226 // Deletion of the dispatcher_host should cause providers for that |
| 172 // process to get deleted as well. | 227 // process to get deleted as well. |
| 173 dispatcher_host->OnMessageReceived( | 228 dispatcher_host_->OnMessageReceived( |
| 174 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); | 229 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
| 175 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 230 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 176 EXPECT_TRUE(dispatcher_host->HasOneRef()); | 231 EXPECT_TRUE(dispatcher_host_->HasOneRef()); |
| 177 dispatcher_host = NULL; | 232 dispatcher_host_ = NULL; |
| 178 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 233 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 179 } | 234 } |
| 180 | 235 |
| 181 } // namespace content | 236 } // namespace content |
| OLD | NEW |