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_core.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/browser/service_worker/service_worker_context_wrapper.h" |
| 10 #include "content/browser/service_worker/service_worker_provider_host.h" |
10 #include "content/common/service_worker_messages.h" | 11 #include "content/common/service_worker_messages.h" |
11 #include "ipc/ipc_message_macros.h" | 12 #include "ipc/ipc_message_macros.h" |
12 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" | 13 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" |
13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 const char kDisabledErrorMessage[] = | 20 const char kDisabledErrorMessage[] = |
20 "ServiceWorker is disabled"; | 21 "ServiceWorker is disabled"; |
21 const char kDomainMismatchErrorMessage[] = | 22 const char kDomainMismatchErrorMessage[] = |
22 "Scope and scripts do not have the same origin"; | 23 "Scope and scripts do not have the same origin"; |
23 | 24 |
24 // TODO(alecflett): Store the service_worker_id keyed by (domain+pattern, | 25 // 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 // script) so we don't always return a new service worker id. |
26 int64 NextWorkerId() { | 27 int64 NextWorkerId() { |
27 static int64 service_worker_id = 0; | 28 static int64 service_worker_id = 0; |
28 return service_worker_id++; | 29 return service_worker_id++; |
29 } | 30 } |
30 | 31 |
31 } // namespace | 32 } // namespace |
32 | 33 |
33 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 34 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
34 int render_process_id) { | 35 int render_process_id) |
| 36 : render_process_id_(render_process_id) { |
35 } | 37 } |
36 | 38 |
37 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { | 39 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
| 40 if (context_) |
| 41 context_->RemoveAllProviderHostsForProcess(render_process_id_); |
38 } | 42 } |
39 | 43 |
40 void ServiceWorkerDispatcherHost::Init( | 44 void ServiceWorkerDispatcherHost::Init( |
41 ServiceWorkerContextWrapper* context_wrapper) { | 45 ServiceWorkerContextWrapper* context_wrapper) { |
42 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 46 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
43 BrowserThread::PostTask( | 47 BrowserThread::PostTask( |
44 BrowserThread::IO, FROM_HERE, | 48 BrowserThread::IO, FROM_HERE, |
45 base::Bind(&ServiceWorkerDispatcherHost::Init, | 49 base::Bind(&ServiceWorkerDispatcherHost::Init, |
46 this, make_scoped_refptr(context_wrapper))); | 50 this, make_scoped_refptr(context_wrapper))); |
47 return; | 51 return; |
48 } | 52 } |
49 context_ = context_wrapper->context()->AsWeakPtr(); | 53 context_ = context_wrapper->context()->AsWeakPtr(); |
50 } | 54 } |
51 | 55 |
| 56 void ServiceWorkerDispatcherHost::OnDestruct() const { |
| 57 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 58 } |
| 59 |
52 bool ServiceWorkerDispatcherHost::OnMessageReceived( | 60 bool ServiceWorkerDispatcherHost::OnMessageReceived( |
53 const IPC::Message& message, | 61 const IPC::Message& message, |
54 bool* message_was_ok) { | 62 bool* message_was_ok) { |
55 if (IPC_MESSAGE_CLASS(message) != ServiceWorkerMsgStart) | 63 if (IPC_MESSAGE_CLASS(message) != ServiceWorkerMsgStart) |
56 return false; | 64 return false; |
57 | 65 |
58 bool handled = true; | 66 bool handled = true; |
59 IPC_BEGIN_MESSAGE_MAP_EX( | 67 IPC_BEGIN_MESSAGE_MAP_EX( |
60 ServiceWorkerDispatcherHost, message, *message_was_ok) | 68 ServiceWorkerDispatcherHost, message, *message_was_ok) |
61 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, | 69 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, |
62 OnRegisterServiceWorker) | 70 OnRegisterServiceWorker) |
63 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 71 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
64 OnUnregisterServiceWorker) | 72 OnUnregisterServiceWorker) |
| 73 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, |
| 74 OnProviderCreated) |
| 75 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, |
| 76 OnProviderDestroyed) |
65 IPC_MESSAGE_UNHANDLED(handled = false) | 77 IPC_MESSAGE_UNHANDLED(handled = false) |
66 IPC_END_MESSAGE_MAP() | 78 IPC_END_MESSAGE_MAP() |
67 | 79 |
68 return handled; | 80 return handled; |
69 } | 81 } |
70 | 82 |
71 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( | 83 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( |
72 int32 thread_id, | 84 int32 thread_id, |
73 int32 request_id, | 85 int32 request_id, |
74 const GURL& scope, | 86 const GURL& scope, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 thread_id, | 121 thread_id, |
110 request_id, | 122 request_id, |
111 blink::WebServiceWorkerError::DisabledError, | 123 blink::WebServiceWorkerError::DisabledError, |
112 ASCIIToUTF16(kDisabledErrorMessage))); | 124 ASCIIToUTF16(kDisabledErrorMessage))); |
113 return; | 125 return; |
114 } | 126 } |
115 | 127 |
116 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id)); | 128 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id)); |
117 } | 129 } |
118 | 130 |
| 131 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) { |
| 132 if (!context_) |
| 133 return; |
| 134 if (context_->GetProviderHost(render_process_id_, provider_id)) { |
| 135 BadMessageReceived(); |
| 136 return; |
| 137 } |
| 138 scoped_ptr<ServiceWorkerProviderHost> provider_host( |
| 139 new ServiceWorkerProviderHost(render_process_id_, provider_id)); |
| 140 context_->AddProviderHost(provider_host.Pass()); |
| 141 } |
| 142 |
| 143 void ServiceWorkerDispatcherHost::OnProviderDestroyed(int provider_id) { |
| 144 if (!context_) |
| 145 return; |
| 146 if (!context_->GetProviderHost(render_process_id_, provider_id)) { |
| 147 BadMessageReceived(); |
| 148 return; |
| 149 } |
| 150 context_->RemoveProviderHost(render_process_id_, provider_id); |
| 151 } |
| 152 |
119 } // namespace content | 153 } // namespace content |
OLD | NEW |