| 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 "content/browser/child_process_security_policy_impl.h" |
| 7 #include "content/browser/service_worker/service_worker_context.h" | 8 #include "content/browser/service_worker/service_worker_context.h" |
| 8 #include "content/common/service_worker_messages.h" | 9 #include "content/common/service_worker_messages.h" |
| 9 #include "ipc/ipc_message_macros.h" | 10 #include "ipc/ipc_message_macros.h" |
| 10 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 11 | 12 |
| 12 namespace content { | 13 namespace content { |
| 13 | 14 |
| 14 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 15 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
| 16 int render_process_id, |
| 15 ServiceWorkerContext* context) | 17 ServiceWorkerContext* context) |
| 16 : context_(context) {} | 18 : render_process_id_(render_process_id), context_(context) {} |
| 17 | 19 |
| 18 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() {} | 20 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() {} |
| 19 | 21 |
| 20 bool ServiceWorkerDispatcherHost::OnMessageReceived(const IPC::Message& message, | 22 bool ServiceWorkerDispatcherHost::OnMessageReceived(const IPC::Message& message, |
| 21 bool* message_was_ok) { | 23 bool* message_was_ok) { |
| 22 | |
| 23 if (IPC_MESSAGE_CLASS(message) != ServiceWorkerMsgStart) | 24 if (IPC_MESSAGE_CLASS(message) != ServiceWorkerMsgStart) |
| 24 return false; | 25 return false; |
| 25 | 26 |
| 26 bool handled = true; | 27 bool handled = true; |
| 27 IPC_BEGIN_MESSAGE_MAP_EX( | 28 IPC_BEGIN_MESSAGE_MAP_EX( |
| 28 ServiceWorkerDispatcherHost, message, *message_was_ok) | 29 ServiceWorkerDispatcherHost, message, *message_was_ok) |
| 29 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, | 30 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, |
| 30 OnRegisterServiceWorker) | 31 OnRegisterServiceWorker) |
| 31 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 32 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
| 32 OnUnregisterServiceWorker) | 33 OnUnregisterServiceWorker) |
| 33 IPC_MESSAGE_UNHANDLED(handled = false) | 34 IPC_MESSAGE_UNHANDLED(handled = false) |
| 34 IPC_END_MESSAGE_MAP() | 35 IPC_END_MESSAGE_MAP() |
| 35 | 36 |
| 36 return handled; | 37 return handled; |
| 37 } | 38 } |
| 38 | 39 |
| 39 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker(int32 registry_id, | 40 // TODO(alecflett): Store the worker_id keyed by (domain+pattern, |
| 40 const string16& scope, | 41 // script) so we don't always return a new service worker id. |
| 41 const GURL& script_url) { | 42 static int32 NextWorkerId() { |
| 42 // TODO(alecflett): Enforce that script_url must have the same | 43 static int32 worker_id = 0; |
| 43 // origin as the registering document. | 44 return worker_id++; |
| 45 } |
| 46 |
| 47 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( |
| 48 int32 thread_id, |
| 49 int32 request_id, |
| 50 const GURL& security_origin, |
| 51 const string16& scope, |
| 52 const GURL& script_url) { |
| 53 // We're using cookie access as a proxy for ServiceWorker |
| 54 // registration permission. |
| 55 // TODO(alecflett): add a ServiceWorker-specific policy query. |
| 56 ChildProcessSecurityPolicyImpl* policy = |
| 57 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 58 if (!policy->CanAccessCookiesForOrigin(render_process_id_, security_origin)) |
| 59 return; |
| 60 |
| 61 Send(new ServiceWorkerMsg_ServiceWorkerRegistered( |
| 62 thread_id, request_id, NextWorkerId())); |
| 44 } | 63 } |
| 45 | 64 |
| 46 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( | 65 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( |
| 47 int32 registry_id, | 66 int32 thread_id, |
| 48 const string16& scope) {} | 67 int32 request_id, |
| 68 const GURL& security_origin, |
| 69 const string16& scope) { |
| 70 // We're using cookie access as a proxy for ServiceWorker |
| 71 // registration permission. |
| 72 // TODO(alecflett): add a ServiceWorker-specific policy query. |
| 73 ChildProcessSecurityPolicyImpl* policy = |
| 74 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 75 if (!policy->CanAccessCookiesForOrigin(render_process_id_, security_origin)) |
| 76 return; |
| 77 |
| 78 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered( |
| 79 thread_id, request_id, NextWorkerId())); |
| 80 } |
| 49 | 81 |
| 50 } // namespace content | 82 } // namespace content |
| OLD | NEW |