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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "content/browser/message_port_message_filter.h" | 9 #include "content/browser/message_port_message_filter.h" |
10 #include "content/browser/message_port_service.h" | 10 #include "content/browser/message_port_service.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 const char kDisabledErrorMessage[] = | 29 const char kDisabledErrorMessage[] = |
30 "ServiceWorker is disabled"; | 30 "ServiceWorker is disabled"; |
31 const char kDomainMismatchErrorMessage[] = | 31 const char kDomainMismatchErrorMessage[] = |
32 "Scope and scripts do not have the same origin"; | 32 "Scope and scripts do not have the same origin"; |
33 | 33 |
34 const uint32 kFilteredMessageClasses[] = { | 34 const uint32 kFilteredMessageClasses[] = { |
35 ServiceWorkerMsgStart, | 35 ServiceWorkerMsgStart, |
36 EmbeddedWorkerMsgStart, | 36 EmbeddedWorkerMsgStart, |
37 }; | 37 }; |
38 | 38 |
| 39 bool CanRegisterServiceWorker(const GURL& document_url, |
| 40 const GURL& pattern, |
| 41 const GURL& script_url) { |
| 42 // TODO: Respect Chrome's content settings, if we add a setting for |
| 43 // controlling whether Service Worker is allowed. |
| 44 return document_url.GetOrigin() == pattern.GetOrigin() && |
| 45 document_url.GetOrigin() == script_url.GetOrigin(); |
| 46 } |
| 47 |
| 48 bool CanUnregisterServiceWorker(const GURL& document_url, |
| 49 const GURL& pattern) { |
| 50 // TODO: Respect Chrome's content settings, if we add a setting for |
| 51 // controlling whether Service Worker is allowed. |
| 52 return document_url.GetOrigin() == pattern.GetOrigin(); |
| 53 } |
| 54 |
39 } // namespace | 55 } // namespace |
40 | 56 |
41 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 57 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
42 int render_process_id, | 58 int render_process_id, |
43 MessagePortMessageFilter* message_port_message_filter) | 59 MessagePortMessageFilter* message_port_message_filter) |
44 : BrowserMessageFilter(kFilteredMessageClasses, | 60 : BrowserMessageFilter(kFilteredMessageClasses, |
45 arraysize(kFilteredMessageClasses)), | 61 arraysize(kFilteredMessageClasses)), |
46 render_process_id_(render_process_id), | 62 render_process_id_(render_process_id), |
47 message_port_message_filter_(message_port_message_filter), | 63 message_port_message_filter_(message_port_message_filter), |
48 channel_ready_(false) { | 64 channel_ready_(false) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 const GURL& script_url) { | 170 const GURL& script_url) { |
155 if (!GetContext() || !ServiceWorkerUtils::IsFeatureEnabled()) { | 171 if (!GetContext() || !ServiceWorkerUtils::IsFeatureEnabled()) { |
156 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 172 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
157 thread_id, | 173 thread_id, |
158 request_id, | 174 request_id, |
159 WebServiceWorkerError::ErrorTypeDisabled, | 175 WebServiceWorkerError::ErrorTypeDisabled, |
160 base::ASCIIToUTF16(kDisabledErrorMessage))); | 176 base::ASCIIToUTF16(kDisabledErrorMessage))); |
161 return; | 177 return; |
162 } | 178 } |
163 | 179 |
164 // TODO(alecflett): This check is insufficient for release. Add a | |
165 // ServiceWorker-specific policy query in | |
166 // ChildProcessSecurityImpl. See http://crbug.com/311631. | |
167 if (pattern.GetOrigin() != script_url.GetOrigin()) { | |
168 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | |
169 thread_id, | |
170 request_id, | |
171 WebServiceWorkerError::ErrorTypeSecurity, | |
172 base::ASCIIToUTF16(kDomainMismatchErrorMessage))); | |
173 return; | |
174 } | |
175 | |
176 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost( | 180 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost( |
177 render_process_id_, provider_id); | 181 render_process_id_, provider_id); |
178 if (!provider_host) { | 182 if (!provider_host) { |
179 BadMessageReceived(); | 183 BadMessageReceived(); |
180 return; | 184 return; |
181 } | 185 } |
182 if (!provider_host->IsContextAlive()) { | 186 if (!provider_host->IsContextAlive()) { |
183 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 187 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
184 thread_id, | 188 thread_id, |
185 request_id, | 189 request_id, |
186 WebServiceWorkerError::ErrorTypeDisabled, | 190 WebServiceWorkerError::ErrorTypeDisabled, |
187 base::ASCIIToUTF16(kDisabledErrorMessage))); | 191 base::ASCIIToUTF16(kDisabledErrorMessage))); |
188 return; | 192 return; |
189 } | 193 } |
190 | 194 |
| 195 if (!CanRegisterServiceWorker( |
| 196 provider_host->document_url(), pattern, script_url)) { |
| 197 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 198 thread_id, |
| 199 request_id, |
| 200 WebServiceWorkerError::ErrorTypeSecurity, |
| 201 base::ASCIIToUTF16(kDomainMismatchErrorMessage))); |
| 202 return; |
| 203 } |
191 GetContext()->RegisterServiceWorker( | 204 GetContext()->RegisterServiceWorker( |
192 pattern, | 205 pattern, |
193 script_url, | 206 script_url, |
194 render_process_id_, | 207 render_process_id_, |
195 provider_host, | 208 provider_host, |
196 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, | 209 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, |
197 this, | 210 this, |
198 thread_id, | 211 thread_id, |
199 request_id)); | 212 request_id)); |
200 } | 213 } |
201 | 214 |
202 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( | 215 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( |
203 int thread_id, | 216 int thread_id, |
204 int request_id, | 217 int request_id, |
205 int provider_id, | 218 int provider_id, |
206 const GURL& pattern) { | 219 const GURL& pattern) { |
207 // TODO(alecflett): This check is insufficient for release. Add a | |
208 // ServiceWorker-specific policy query in | |
209 // ChildProcessSecurityImpl. See http://crbug.com/311631. | |
210 if (!GetContext() || !ServiceWorkerUtils::IsFeatureEnabled()) { | 220 if (!GetContext() || !ServiceWorkerUtils::IsFeatureEnabled()) { |
211 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 221 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
212 thread_id, | 222 thread_id, |
213 request_id, | 223 request_id, |
214 blink::WebServiceWorkerError::ErrorTypeDisabled, | 224 blink::WebServiceWorkerError::ErrorTypeDisabled, |
215 base::ASCIIToUTF16(kDisabledErrorMessage))); | 225 base::ASCIIToUTF16(kDisabledErrorMessage))); |
216 return; | 226 return; |
217 } | 227 } |
218 | 228 |
219 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost( | 229 ServiceWorkerProviderHost* provider_host = GetContext()->GetProviderHost( |
220 render_process_id_, provider_id); | 230 render_process_id_, provider_id); |
221 if (!provider_host) { | 231 if (!provider_host) { |
222 BadMessageReceived(); | 232 BadMessageReceived(); |
223 return; | 233 return; |
224 } | 234 } |
225 if (!provider_host->IsContextAlive()) { | 235 if (!provider_host->IsContextAlive()) { |
226 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 236 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
227 thread_id, | 237 thread_id, |
228 request_id, | 238 request_id, |
229 blink::WebServiceWorkerError::ErrorTypeDisabled, | 239 blink::WebServiceWorkerError::ErrorTypeDisabled, |
230 base::ASCIIToUTF16(kDisabledErrorMessage))); | 240 base::ASCIIToUTF16(kDisabledErrorMessage))); |
231 return; | 241 return; |
232 } | 242 } |
233 | 243 |
| 244 if (!CanUnregisterServiceWorker(provider_host->document_url(), pattern)) { |
| 245 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 246 thread_id, |
| 247 request_id, |
| 248 WebServiceWorkerError::ErrorTypeSecurity, |
| 249 base::ASCIIToUTF16(kDomainMismatchErrorMessage))); |
| 250 return; |
| 251 } |
| 252 |
234 GetContext()->UnregisterServiceWorker( | 253 GetContext()->UnregisterServiceWorker( |
235 pattern, | 254 pattern, |
236 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, | 255 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, |
237 this, | 256 this, |
238 thread_id, | 257 thread_id, |
239 request_id)); | 258 request_id)); |
240 } | 259 } |
241 | 260 |
242 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( | 261 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( |
243 int handle_id, | 262 int handle_id, |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 status, &error_type, &error_message); | 452 status, &error_type, &error_message); |
434 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 453 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
435 thread_id, request_id, error_type, error_message)); | 454 thread_id, request_id, error_type, error_message)); |
436 } | 455 } |
437 | 456 |
438 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { | 457 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { |
439 return context_wrapper_->context(); | 458 return context_wrapper_->context(); |
440 } | 459 } |
441 | 460 |
442 } // namespace content | 461 } // namespace content |
OLD | NEW |