| 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/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "content/browser/service_worker/service_worker_context.h" | 8 #include "content/browser/service_worker/service_worker_context_core.h" |
| 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 9 #include "content/common/service_worker_messages.h" | 10 #include "content/common/service_worker_messages.h" |
| 10 #include "ipc/ipc_message_macros.h" | 11 #include "ipc/ipc_message_macros.h" |
| 11 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" | 12 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" |
| 12 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 13 | 14 |
| 14 using WebKit::WebServiceWorkerError; | 15 using WebKit::WebServiceWorkerError; |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 | 18 |
| 18 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 19 namespace { |
| 19 int render_process_id, | |
| 20 ServiceWorkerContext* context) : context_(context) {} | |
| 21 | 20 |
| 22 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() {} | 21 const char kDisabledErrorMessage[] = |
| 23 | 22 "ServiceWorker is disabled"; |
| 24 namespace { | |
| 25 const char kDomainMismatchErrorMessage[] = | 23 const char kDomainMismatchErrorMessage[] = |
| 26 "Scope and scripts do not have the same origin"; | 24 "Scope and scripts do not have the same origin"; |
| 25 |
| 26 // TODO(alecflett): Store the service_worker_id keyed by (domain+pattern, |
| 27 // script) so we don't always return a new service worker id. |
| 28 int64 NextWorkerId() { |
| 29 static int64 service_worker_id = 0; |
| 30 return service_worker_id++; |
| 27 } | 31 } |
| 28 | 32 |
| 29 bool ServiceWorkerDispatcherHost::OnMessageReceived(const IPC::Message& message, | 33 } // namespace |
| 30 bool* message_was_ok) { | 34 |
| 35 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
| 36 int render_process_id) { |
| 37 } |
| 38 |
| 39 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
| 40 } |
| 41 |
| 42 void ServiceWorkerDispatcherHost::Init( |
| 43 ServiceWorkerContextWrapper* context_wrapper) { |
| 44 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 45 BrowserThread::PostTask( |
| 46 BrowserThread::IO, FROM_HERE, |
| 47 base::Bind(&ServiceWorkerDispatcherHost::Init, |
| 48 this, context_wrapper)); |
| 49 return; |
| 50 } |
| 51 context_ = context_wrapper->context()->AsWeakPtr(); |
| 52 } |
| 53 |
| 54 bool ServiceWorkerDispatcherHost::OnMessageReceived( |
| 55 const IPC::Message& message, |
| 56 bool* message_was_ok) { |
| 31 if (IPC_MESSAGE_CLASS(message) != ServiceWorkerMsgStart) | 57 if (IPC_MESSAGE_CLASS(message) != ServiceWorkerMsgStart) |
| 32 return false; | 58 return false; |
| 33 | 59 |
| 34 bool handled = true; | 60 bool handled = true; |
| 35 IPC_BEGIN_MESSAGE_MAP_EX( | 61 IPC_BEGIN_MESSAGE_MAP_EX( |
| 36 ServiceWorkerDispatcherHost, message, *message_was_ok) | 62 ServiceWorkerDispatcherHost, message, *message_was_ok) |
| 37 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, | 63 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, |
| 38 OnRegisterServiceWorker) | 64 OnRegisterServiceWorker) |
| 39 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 65 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
| 40 OnUnregisterServiceWorker) | 66 OnUnregisterServiceWorker) |
| 41 IPC_MESSAGE_UNHANDLED(handled = false) | 67 IPC_MESSAGE_UNHANDLED(handled = false) |
| 42 IPC_END_MESSAGE_MAP() | 68 IPC_END_MESSAGE_MAP() |
| 43 | 69 |
| 44 return handled; | 70 return handled; |
| 45 } | 71 } |
| 46 | 72 |
| 47 // TODO(alecflett): Store the service_worker_id keyed by (domain+pattern, | |
| 48 // script) so we don't always return a new service worker id. | |
| 49 static int64 NextWorkerId() { | |
| 50 static int64 service_worker_id = 0; | |
| 51 return service_worker_id++; | |
| 52 } | |
| 53 | |
| 54 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( | 73 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( |
| 55 int32 thread_id, | 74 int32 thread_id, |
| 56 int32 request_id, | 75 int32 request_id, |
| 57 const GURL& scope, | 76 const GURL& scope, |
| 58 const GURL& script_url) { | 77 const GURL& script_url) { |
| 59 if (!context_->IsEnabled()) { | 78 if (!context_ || !context_->IsEnabled()) { |
| 60 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 79 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 61 thread_id, | 80 thread_id, |
| 62 request_id, | 81 request_id, |
| 63 WebKit::WebServiceWorkerError::DisabledError, | 82 WebKit::WebServiceWorkerError::DisabledError, |
| 64 ASCIIToUTF16("ServiceWorker is disabled"))); | 83 ASCIIToUTF16(kDisabledErrorMessage))); |
| 65 return; | 84 return; |
| 66 } | 85 } |
| 67 | 86 |
| 68 // TODO(alecflett): This check is insufficient for release. Add a | 87 // TODO(alecflett): This check is insufficient for release. Add a |
| 69 // ServiceWorker-specific policy query in | 88 // ServiceWorker-specific policy query in |
| 70 // ChildProcessSecurityImpl. See http://crbug.com/311631. | 89 // ChildProcessSecurityImpl. See http://crbug.com/311631. |
| 71 if (scope.GetOrigin() != script_url.GetOrigin()) { | 90 if (scope.GetOrigin() != script_url.GetOrigin()) { |
| 72 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 91 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 73 thread_id, | 92 thread_id, |
| 74 request_id, | 93 request_id, |
| 75 WebKit::WebServiceWorkerError::SecurityError, | 94 WebKit::WebServiceWorkerError::SecurityError, |
| 76 ASCIIToUTF16(kDomainMismatchErrorMessage))); | 95 ASCIIToUTF16(kDomainMismatchErrorMessage))); |
| 77 return; | 96 return; |
| 78 } | 97 } |
| 79 | 98 |
| 80 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( | 99 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( |
| 81 thread_id, request_id, NextWorkerId())); | 100 thread_id, request_id, NextWorkerId())); |
| 82 } | 101 } |
| 83 | 102 |
| 84 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker(int32 thread_id, | 103 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker(int32 thread_id, |
| 85 int32 request_id, | 104 int32 request_id, |
| 86 const GURL& scope) { | 105 const GURL& scope) { |
| 87 // TODO(alecflett): This check is insufficient for release. Add a | 106 // TODO(alecflett): This check is insufficient for release. Add a |
| 88 // ServiceWorker-specific policy query in | 107 // ServiceWorker-specific policy query in |
| 89 // ChildProcessSecurityImpl. See http://crbug.com/311631. | 108 // ChildProcessSecurityImpl. See http://crbug.com/311631. |
| 90 if (!context_->IsEnabled()) { | 109 if (!context_ || !context_->IsEnabled()) { |
| 91 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( | 110 Send(new ServiceWorkerMsg_ServiceWorkerRegistrationError( |
| 92 thread_id, | 111 thread_id, |
| 93 request_id, | 112 request_id, |
| 94 WebServiceWorkerError::DisabledError, | 113 WebServiceWorkerError::DisabledError, |
| 95 ASCIIToUTF16("ServiceWorker is disabled"))); | 114 ASCIIToUTF16("ServiceWorker is disabled"))); |
| 96 return; | 115 return; |
| 97 } | 116 } |
| 98 | 117 |
| 99 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id)); | 118 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id)); |
| 100 } | 119 } |
| 101 | 120 |
| 102 } // namespace content | 121 } // namespace content |
| OLD | NEW |