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