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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 virtual void TearDown() { | 66 virtual void TearDown() { |
67 helper_.reset(); | 67 helper_.reset(); |
68 } | 68 } |
69 | 69 |
70 ServiceWorkerContextCore* context() { return helper_->context(); } | 70 ServiceWorkerContextCore* context() { return helper_->context(); } |
71 ServiceWorkerContextWrapper* context_wrapper() { | 71 ServiceWorkerContextWrapper* context_wrapper() { |
72 return helper_->context_wrapper(); | 72 return helper_->context_wrapper(); |
73 } | 73 } |
74 | 74 |
| 75 void SendRegister(int64 provider_id, GURL pattern, GURL worker_url) { |
| 76 dispatcher_host_->OnMessageReceived( |
| 77 ServiceWorkerHostMsg_RegisterServiceWorker( |
| 78 -1, -1, provider_id, pattern, worker_url)); |
| 79 base::RunLoop().RunUntilIdle(); |
| 80 } |
| 81 |
75 void Register(int64 provider_id, | 82 void Register(int64 provider_id, |
76 GURL pattern, | 83 GURL pattern, |
77 GURL worker_url, | 84 GURL worker_url, |
78 uint32 expected_message) { | 85 uint32 expected_message) { |
79 dispatcher_host_->OnMessageReceived( | 86 SendRegister(provider_id, pattern, worker_url); |
80 ServiceWorkerHostMsg_RegisterServiceWorker( | |
81 -1, -1, provider_id, pattern, worker_url)); | |
82 base::RunLoop().RunUntilIdle(); | |
83 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | 87 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
84 expected_message)); | 88 expected_message)); |
85 dispatcher_host_->ipc_sink()->ClearMessages(); | 89 dispatcher_host_->ipc_sink()->ClearMessages(); |
86 } | 90 } |
87 | 91 |
88 void Unregister(int64 provider_id, GURL pattern, uint32 expected_message) { | 92 void SendUnregister(int64 provider_id, GURL pattern) { |
89 dispatcher_host_->OnMessageReceived( | 93 dispatcher_host_->OnMessageReceived( |
90 ServiceWorkerHostMsg_UnregisterServiceWorker( | 94 ServiceWorkerHostMsg_UnregisterServiceWorker( |
91 -1, -1, provider_id, pattern)); | 95 -1, -1, provider_id, pattern)); |
92 base::RunLoop().RunUntilIdle(); | 96 base::RunLoop().RunUntilIdle(); |
| 97 } |
| 98 |
| 99 void Unregister(int64 provider_id, GURL pattern, uint32 expected_message) { |
| 100 SendUnregister(provider_id, pattern); |
93 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | 101 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
94 expected_message)); | 102 expected_message)); |
95 dispatcher_host_->ipc_sink()->ClearMessages(); | 103 dispatcher_host_->ipc_sink()->ClearMessages(); |
96 } | 104 } |
97 | 105 |
98 TestBrowserThreadBundle browser_thread_bundle_; | 106 TestBrowserThreadBundle browser_thread_bundle_; |
99 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 107 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
100 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; | 108 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; |
101 }; | 109 }; |
102 | 110 |
103 TEST_F(ServiceWorkerDispatcherHostTest, RegisterSameOrigin) { | 111 TEST_F(ServiceWorkerDispatcherHostTest, Register_SameOrigin) { |
104 const int64 kProviderId = 99; // Dummy value | 112 const int64 kProviderId = 99; // Dummy value |
105 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( | 113 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
106 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL)); | 114 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL)); |
107 host->SetDocumentUrl(GURL("http://www.example.com/foo")); | 115 host->SetDocumentUrl(GURL("https://www.example.com/foo")); |
108 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); | 116 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); |
109 context()->AddProviderHost(host.Pass()); | 117 context()->AddProviderHost(host.Pass()); |
110 | 118 |
111 Register(kProviderId, | 119 Register(kProviderId, |
112 GURL("http://www.example.com/*"), | 120 GURL("https://www.example.com/*"), |
113 GURL("http://foo.example.com/bar"), | 121 GURL("https://www.example.com/bar"), |
114 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); | |
115 Register(kProviderId, | |
116 GURL("http://foo.example.com/*"), | |
117 GURL("http://www.example.com/bar"), | |
118 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); | |
119 Register(kProviderId, | |
120 GURL("http://foo.example.com/*"), | |
121 GURL("http://foo.example.com/bar"), | |
122 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); | |
123 Register(kProviderId, | |
124 GURL("http://www.example.com/*"), | |
125 GURL("http://www.example.com/bar"), | |
126 ServiceWorkerMsg_ServiceWorkerRegistered::ID); | 122 ServiceWorkerMsg_ServiceWorkerRegistered::ID); |
127 } | 123 } |
128 | 124 |
129 TEST_F(ServiceWorkerDispatcherHostTest, UnregisterSameOrigin) { | 125 TEST_F(ServiceWorkerDispatcherHostTest, Register_CrossOrigin) { |
130 const int64 kProviderId = 99; // Dummy value | 126 const int64 kProviderId = 99; // Dummy value |
131 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( | 127 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
132 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL)); | 128 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL)); |
| 129 host->SetDocumentUrl(GURL("https://www.example.com/foo")); |
| 130 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); |
| 131 context()->AddProviderHost(host.Pass()); |
| 132 |
| 133 // Script has a different host |
| 134 SendRegister(kProviderId, |
| 135 GURL("https://www.example.com/*"), |
| 136 GURL("https://foo.example.com/bar")); |
| 137 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); |
| 138 |
| 139 // Scope has a different host |
| 140 SendRegister(kProviderId, |
| 141 GURL("https://foo.example.com/*"), |
| 142 GURL("https://www.example.com/bar")); |
| 143 EXPECT_EQ(2, dispatcher_host_->bad_messages_received_count_); |
| 144 |
| 145 // Script has a different port |
| 146 SendRegister(kProviderId, |
| 147 GURL("https://www.example.com/*"), |
| 148 GURL("https://www.example.com:8080/bar")); |
| 149 EXPECT_EQ(3, dispatcher_host_->bad_messages_received_count_); |
| 150 |
| 151 // Scope has a different transport |
| 152 SendRegister(kProviderId, |
| 153 GURL("wss://www.example.com/*"), |
| 154 GURL("https://www.example.com/bar")); |
| 155 EXPECT_EQ(4, dispatcher_host_->bad_messages_received_count_); |
| 156 |
| 157 // Script and scope have different hosts |
| 158 SendRegister(kProviderId, |
| 159 GURL("https://foo.example.com/*"), |
| 160 GURL("https://foo.example.com/bar")); |
| 161 EXPECT_EQ(5, dispatcher_host_->bad_messages_received_count_); |
| 162 |
| 163 // Script and scope URLs are invalid |
| 164 SendRegister(kProviderId, |
| 165 GURL(), |
| 166 GURL("h@ttps://@")); |
| 167 EXPECT_EQ(6, dispatcher_host_->bad_messages_received_count_); |
| 168 } |
| 169 |
| 170 TEST_F(ServiceWorkerDispatcherHostTest, Unregister_SameOrigin) { |
| 171 const int64 kProviderId = 99; // Dummy value |
| 172 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
| 173 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL)); |
133 host->SetDocumentUrl(GURL("http://www.example.com/foo")); | 174 host->SetDocumentUrl(GURL("http://www.example.com/foo")); |
134 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); | 175 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); |
135 context()->AddProviderHost(host.Pass()); | 176 context()->AddProviderHost(host.Pass()); |
136 | 177 |
137 Unregister(kProviderId, | 178 Unregister(kProviderId, |
138 GURL("http://foo.example.com/*"), | |
139 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); | |
140 Unregister(kProviderId, | |
141 GURL("http://www.example.com/*"), | 179 GURL("http://www.example.com/*"), |
142 ServiceWorkerMsg_ServiceWorkerUnregistered::ID); | 180 ServiceWorkerMsg_ServiceWorkerUnregistered::ID); |
143 } | 181 } |
144 | 182 |
| 183 TEST_F(ServiceWorkerDispatcherHostTest, Unregister_CrossOrigin) { |
| 184 const int64 kProviderId = 99; // Dummy value |
| 185 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
| 186 kRenderProcessId, kProviderId, context()->AsWeakPtr(), NULL)); |
| 187 host->SetDocumentUrl(GURL("http://www.example.com/foo")); |
| 188 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); |
| 189 context()->AddProviderHost(host.Pass()); |
| 190 |
| 191 SendUnregister(kProviderId, GURL("http://foo.example.com/*")); |
| 192 EXPECT_EQ(1, dispatcher_host_->bad_messages_received_count_); |
| 193 } |
| 194 |
145 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { | 195 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { |
146 helper_->ShutdownContext(); | 196 helper_->ShutdownContext(); |
147 | 197 |
148 // Let the shutdown reach the simulated IO thread. | 198 // Let the shutdown reach the simulated IO thread. |
149 base::RunLoop().RunUntilIdle(); | 199 base::RunLoop().RunUntilIdle(); |
150 | 200 |
151 dispatcher_host_->OnMessageReceived( | 201 Register(-1, |
152 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, -1, GURL(), GURL())); | 202 GURL(), |
153 | 203 GURL(), |
154 // TODO(alecflett): Pump the message loop when this becomes async. | 204 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID); |
155 ASSERT_EQ(1UL, dispatcher_host_->ipc_sink()->message_count()); | |
156 EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( | |
157 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); | |
158 } | 205 } |
159 | 206 |
160 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 207 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
161 const int kProviderId = 1001; // Test with a value != kRenderProcessId. | 208 const int kProviderId = 1001; // Test with a value != kRenderProcessId. |
162 | 209 |
163 dispatcher_host_->OnMessageReceived( | 210 dispatcher_host_->OnMessageReceived( |
164 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); | 211 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
165 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 212 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
166 | 213 |
167 // Two with the same ID should be seen as a bad message. | 214 // Two with the same ID should be seen as a bad message. |
(...skipping 14 matching lines...) Expand all Loading... |
182 // process to get deleted as well. | 229 // process to get deleted as well. |
183 dispatcher_host_->OnMessageReceived( | 230 dispatcher_host_->OnMessageReceived( |
184 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); | 231 ServiceWorkerHostMsg_ProviderCreated(kProviderId)); |
185 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 232 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
186 EXPECT_TRUE(dispatcher_host_->HasOneRef()); | 233 EXPECT_TRUE(dispatcher_host_->HasOneRef()); |
187 dispatcher_host_ = NULL; | 234 dispatcher_host_ = NULL; |
188 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 235 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
189 } | 236 } |
190 | 237 |
191 } // namespace content | 238 } // namespace content |
OLD | NEW |