| 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/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/browser/message_port_message_filter.h" | 10 #include "content/browser/message_port_message_filter.h" |
| 11 #include "content/browser/message_port_service.h" | 11 #include "content/browser/message_port_service.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/service_worker_context_core.h" | 13 #include "content/browser/service_worker/service_worker_context_core.h" |
| 14 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 14 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 15 #include "content/browser/service_worker/service_worker_handle.h" | 15 #include "content/browser/service_worker/service_worker_handle.h" |
| 16 #include "content/browser/service_worker/service_worker_registration.h" | 16 #include "content/browser/service_worker/service_worker_registration.h" |
| 17 #include "content/browser/service_worker/service_worker_registration_handle.h" | 17 #include "content/browser/service_worker/service_worker_registration_handle.h" |
| 18 #include "content/browser/service_worker/service_worker_utils.h" | 18 #include "content/browser/service_worker/service_worker_utils.h" |
| 19 #include "content/common/service_worker/embedded_worker_messages.h" | 19 #include "content/common/service_worker/embedded_worker_messages.h" |
| 20 #include "content/common/service_worker/service_worker_messages.h" | 20 #include "content/common/service_worker/service_worker_messages.h" |
| 21 #include "content/public/browser/content_browser_client.h" |
| 22 #include "content/public/common/content_client.h" |
| 21 #include "ipc/ipc_message_macros.h" | 23 #include "ipc/ipc_message_macros.h" |
| 22 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 23 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" | 25 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" |
| 24 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 25 | 27 |
| 26 using blink::WebServiceWorkerError; | 28 using blink::WebServiceWorkerError; |
| 27 | 29 |
| 28 namespace content { | 30 namespace content { |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 const char kShutdownErrorMessage[] = | 34 const char kShutdownErrorMessage[] = |
| 33 "The Service Worker system has shutdown."; | 35 "The Service Worker system has shutdown."; |
| 36 const char kDisabledErrorMessage[] = "The browser has disabled Service Worker."; |
| 34 | 37 |
| 35 const uint32 kFilteredMessageClasses[] = { | 38 const uint32 kFilteredMessageClasses[] = { |
| 36 ServiceWorkerMsgStart, | 39 ServiceWorkerMsgStart, |
| 37 EmbeddedWorkerMsgStart, | 40 EmbeddedWorkerMsgStart, |
| 38 }; | 41 }; |
| 39 | 42 |
| 40 bool AllOriginsMatch(const GURL& url_a, const GURL& url_b, const GURL& url_c) { | 43 bool AllOriginsMatch(const GURL& url_a, const GURL& url_b, const GURL& url_c) { |
| 41 return url_a.GetOrigin() == url_b.GetOrigin() && | 44 return url_a.GetOrigin() == url_b.GetOrigin() && |
| 42 url_a.GetOrigin() == url_c.GetOrigin(); | 45 url_a.GetOrigin() == url_c.GetOrigin(); |
| 43 } | 46 } |
| 44 | 47 |
| 45 // TODO(dominicc): When crbug.com/362214 is fixed use that to be | 48 // TODO(dominicc): When crbug.com/362214 is fixed use that to be |
| 46 // consistent with Blink's | 49 // consistent with Blink's |
| 47 // SecurityOrigin::canAccessFeatureRequiringSecureOrigin. | 50 // SecurityOrigin::canAccessFeatureRequiringSecureOrigin. |
| 48 bool OriginCanAccessServiceWorkers(const GURL& url) { | 51 bool OriginCanAccessServiceWorkers(const GURL& url) { |
| 49 return url.SchemeIsSecure() || net::IsLocalhost(url.host()); | 52 return url.SchemeIsSecure() || net::IsLocalhost(url.host()); |
| 50 } | 53 } |
| 51 | 54 |
| 52 bool CanRegisterServiceWorker(const GURL& document_url, | 55 bool CanRegisterServiceWorker(const GURL& document_url, |
| 53 const GURL& pattern, | 56 const GURL& pattern, |
| 54 const GURL& script_url) { | 57 const GURL& script_url) { |
| 55 // TODO: Respect Chrome's content settings, if we add a setting for | |
| 56 // controlling whether Service Worker is allowed. | |
| 57 return AllOriginsMatch(document_url, pattern, script_url) && | 58 return AllOriginsMatch(document_url, pattern, script_url) && |
| 58 OriginCanAccessServiceWorkers(document_url); | 59 OriginCanAccessServiceWorkers(document_url); |
| 59 } | 60 } |
| 60 | 61 |
| 61 bool CanUnregisterServiceWorker(const GURL& document_url, | 62 bool CanUnregisterServiceWorker(const GURL& document_url, |
| 62 const GURL& pattern) { | 63 const GURL& pattern) { |
| 63 // TODO: Respect Chrome's content settings, if we add a setting for | |
| 64 // controlling whether Service Worker is allowed. | |
| 65 return document_url.GetOrigin() == pattern.GetOrigin() && | 64 return document_url.GetOrigin() == pattern.GetOrigin() && |
| 66 OriginCanAccessServiceWorkers(document_url); | 65 OriginCanAccessServiceWorkers(document_url); |
| 67 } | 66 } |
| 68 | 67 |
| 69 bool CanGetRegistration(const GURL& document_url, | 68 bool CanGetRegistration(const GURL& document_url, |
| 70 const GURL& given_document_url) { | 69 const GURL& given_document_url) { |
| 71 // TODO: Respect Chrome's content settings, if we add a setting for | |
| 72 // controlling whether Service Worker is allowed. | |
| 73 return document_url.GetOrigin() == given_document_url.GetOrigin() && | 70 return document_url.GetOrigin() == given_document_url.GetOrigin() && |
| 74 OriginCanAccessServiceWorkers(document_url); | 71 OriginCanAccessServiceWorkers(document_url); |
| 75 } | 72 } |
| 76 | 73 |
| 77 } // namespace | 74 } // namespace |
| 78 | 75 |
| 79 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 76 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
| 80 int render_process_id, | 77 int render_process_id, |
| 81 MessagePortMessageFilter* message_port_message_filter) | 78 MessagePortMessageFilter* message_port_message_filter, |
| 79 ResourceContext* resource_context) |
| 82 : BrowserMessageFilter(kFilteredMessageClasses, | 80 : BrowserMessageFilter(kFilteredMessageClasses, |
| 83 arraysize(kFilteredMessageClasses)), | 81 arraysize(kFilteredMessageClasses)), |
| 84 render_process_id_(render_process_id), | 82 render_process_id_(render_process_id), |
| 85 message_port_message_filter_(message_port_message_filter), | 83 message_port_message_filter_(message_port_message_filter), |
| 84 resource_context_(resource_context), |
| 86 channel_ready_(false) { | 85 channel_ready_(false) { |
| 87 } | 86 } |
| 88 | 87 |
| 89 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { | 88 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
| 90 if (GetContext()) { | 89 if (GetContext()) { |
| 91 GetContext()->RemoveAllProviderHostsForProcess(render_process_id_); | 90 GetContext()->RemoveAllProviderHostsForProcess(render_process_id_); |
| 92 GetContext()->embedded_worker_registry()->RemoveChildProcessSender( | 91 GetContext()->embedded_worker_registry()->RemoveChildProcessSender( |
| 93 render_process_id_); | 92 render_process_id_); |
| 94 } | 93 } |
| 95 } | 94 } |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 WebServiceWorkerError::ErrorTypeAbort, | 250 WebServiceWorkerError::ErrorTypeAbort, |
| 252 base::ASCIIToUTF16(kShutdownErrorMessage))); | 251 base::ASCIIToUTF16(kShutdownErrorMessage))); |
| 253 return; | 252 return; |
| 254 } | 253 } |
| 255 | 254 |
| 256 if (!CanRegisterServiceWorker( | 255 if (!CanRegisterServiceWorker( |
| 257 provider_host->document_url(), pattern, script_url)) { | 256 provider_host->document_url(), pattern, script_url)) { |
| 258 BadMessageReceived(); | 257 BadMessageReceived(); |
| 259 return; | 258 return; |
| 260 } | 259 } |
| 260 |
| 261 if (!GetContentClient()->browser()->AllowServiceWorker( |
| 262 pattern, provider_host->topmost_frame_url(), resource_context_)) { |
| 263 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 264 thread_id, |
| 265 request_id, |
| 266 WebServiceWorkerError::ErrorTypeDisabled, |
| 267 base::ASCIIToUTF16(kDisabledErrorMessage))); |
| 268 return; |
| 269 } |
| 270 |
| 261 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", | 271 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", |
| 262 "ServiceWorkerDispatcherHost::RegisterServiceWorker", | 272 "ServiceWorkerDispatcherHost::RegisterServiceWorker", |
| 263 request_id, | 273 request_id, |
| 264 "Pattern", pattern.spec(), | 274 "Pattern", pattern.spec(), |
| 265 "Script URL", script_url.spec()); | 275 "Script URL", script_url.spec()); |
| 266 GetContext()->RegisterServiceWorker( | 276 GetContext()->RegisterServiceWorker( |
| 267 pattern, | 277 pattern, |
| 268 script_url, | 278 script_url, |
| 269 provider_host, | 279 provider_host, |
| 270 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, | 280 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 blink::WebServiceWorkerError::ErrorTypeAbort, | 313 blink::WebServiceWorkerError::ErrorTypeAbort, |
| 304 base::ASCIIToUTF16(kShutdownErrorMessage))); | 314 base::ASCIIToUTF16(kShutdownErrorMessage))); |
| 305 return; | 315 return; |
| 306 } | 316 } |
| 307 | 317 |
| 308 if (!CanUnregisterServiceWorker(provider_host->document_url(), pattern)) { | 318 if (!CanUnregisterServiceWorker(provider_host->document_url(), pattern)) { |
| 309 BadMessageReceived(); | 319 BadMessageReceived(); |
| 310 return; | 320 return; |
| 311 } | 321 } |
| 312 | 322 |
| 323 if (!GetContentClient()->browser()->AllowServiceWorker( |
| 324 pattern, provider_host->topmost_frame_url(), resource_context_)) { |
| 325 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( |
| 326 thread_id, |
| 327 request_id, |
| 328 WebServiceWorkerError::ErrorTypeDisabled, |
| 329 base::ASCIIToUTF16(kDisabledErrorMessage))); |
| 330 return; |
| 331 } |
| 332 |
| 313 TRACE_EVENT_ASYNC_BEGIN1( | 333 TRACE_EVENT_ASYNC_BEGIN1( |
| 314 "ServiceWorker", | 334 "ServiceWorker", |
| 315 "ServiceWorkerDispatcherHost::UnregisterServiceWorker", | 335 "ServiceWorkerDispatcherHost::UnregisterServiceWorker", |
| 316 request_id, | 336 request_id, |
| 317 "Pattern", pattern.spec()); | 337 "Pattern", pattern.spec()); |
| 318 GetContext()->UnregisterServiceWorker( | 338 GetContext()->UnregisterServiceWorker( |
| 319 pattern, | 339 pattern, |
| 320 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, | 340 base::Bind(&ServiceWorkerDispatcherHost::UnregistrationComplete, |
| 321 this, | 341 this, |
| 322 thread_id, | 342 thread_id, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 352 blink::WebServiceWorkerError::ErrorTypeAbort, | 372 blink::WebServiceWorkerError::ErrorTypeAbort, |
| 353 base::ASCIIToUTF16(kShutdownErrorMessage))); | 373 base::ASCIIToUTF16(kShutdownErrorMessage))); |
| 354 return; | 374 return; |
| 355 } | 375 } |
| 356 | 376 |
| 357 if (!CanGetRegistration(provider_host->document_url(), document_url)) { | 377 if (!CanGetRegistration(provider_host->document_url(), document_url)) { |
| 358 BadMessageReceived(); | 378 BadMessageReceived(); |
| 359 return; | 379 return; |
| 360 } | 380 } |
| 361 | 381 |
| 382 if (!GetContentClient()->browser()->AllowServiceWorker( |
| 383 provider_host->document_url(), |
| 384 provider_host->topmost_frame_url(), |
| 385 resource_context_)) { |
| 386 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
| 387 thread_id, |
| 388 request_id, |
| 389 WebServiceWorkerError::ErrorTypeDisabled, |
| 390 base::ASCIIToUTF16(kDisabledErrorMessage))); |
| 391 return; |
| 392 } |
| 393 |
| 362 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 394 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 363 if (GetContext()->storage()->IsDisabled()) { | 395 if (GetContext()->storage()->IsDisabled()) { |
| 364 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT); | 396 SendGetRegistrationError(thread_id, request_id, SERVICE_WORKER_ERROR_ABORT); |
| 365 return; | 397 return; |
| 366 } | 398 } |
| 367 | 399 |
| 368 TRACE_EVENT_ASYNC_BEGIN1( | 400 TRACE_EVENT_ASYNC_BEGIN1( |
| 369 "ServiceWorker", | 401 "ServiceWorker", |
| 370 "ServiceWorkerDispatcherHost::GetRegistration", | 402 "ServiceWorkerDispatcherHost::GetRegistration", |
| 371 request_id, | 403 request_id, |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 status, &error_type, &error_message); | 794 status, &error_type, &error_message); |
| 763 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( | 795 Send(new ServiceWorkerMsg_ServiceWorkerGetRegistrationError( |
| 764 thread_id, request_id, error_type, error_message)); | 796 thread_id, request_id, error_type, error_message)); |
| 765 } | 797 } |
| 766 | 798 |
| 767 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { | 799 ServiceWorkerContextCore* ServiceWorkerDispatcherHost::GetContext() { |
| 768 return context_wrapper_->context(); | 800 return context_wrapper_->context(); |
| 769 } | 801 } |
| 770 | 802 |
| 771 } // namespace content | 803 } // namespace content |
| OLD | NEW |