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/child/service_worker/service_worker_dispatcher.h" | 5 #include "content/child/service_worker/service_worker_dispatcher.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/threading/thread_local.h" | 9 #include "base/threading/thread_local.h" |
| 10 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 11 #include "content/child/service_worker/service_worker_provider_context.h" |
10 #include "content/child/service_worker/web_service_worker_impl.h" | 12 #include "content/child/service_worker/web_service_worker_impl.h" |
11 #include "content/child/thread_safe_sender.h" | 13 #include "content/child/thread_safe_sender.h" |
12 #include "content/common/service_worker/service_worker_messages.h" | 14 #include "content/common/service_worker/service_worker_messages.h" |
| 15 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h" |
13 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 16 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
14 | 17 |
15 using blink::WebServiceWorkerError; | 18 using blink::WebServiceWorkerError; |
16 using blink::WebServiceWorkerProvider; | 19 using blink::WebServiceWorkerProvider; |
17 using base::ThreadLocalPointer; | 20 using base::ThreadLocalPointer; |
18 | 21 |
19 namespace content { | 22 namespace content { |
20 | 23 |
21 namespace { | 24 namespace { |
22 | 25 |
23 base::LazyInstance<ThreadLocalPointer<ServiceWorkerDispatcher> >::Leaky | 26 base::LazyInstance<ThreadLocalPointer<ServiceWorkerDispatcher> >::Leaky |
24 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; | 27 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; |
25 | 28 |
26 ServiceWorkerDispatcher* const kHasBeenDeleted = | 29 ServiceWorkerDispatcher* const kHasBeenDeleted = |
27 reinterpret_cast<ServiceWorkerDispatcher*>(0x1); | 30 reinterpret_cast<ServiceWorkerDispatcher*>(0x1); |
28 | 31 |
29 int CurrentWorkerId() { | 32 int CurrentWorkerId() { |
30 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | 33 return WorkerTaskRunner::Instance()->CurrentWorkerId(); |
31 } | 34 } |
32 | 35 |
33 } // namespace | 36 } // namespace |
34 | 37 |
35 ServiceWorkerDispatcher::ServiceWorkerDispatcher( | 38 ServiceWorkerDispatcher::ServiceWorkerDispatcher( |
36 ThreadSafeSender* thread_safe_sender) | 39 ThreadSafeSender* thread_safe_sender) |
37 : thread_safe_sender_(thread_safe_sender) { | 40 : thread_safe_sender_(thread_safe_sender) { |
38 g_dispatcher_tls.Pointer()->Set(this); | 41 g_dispatcher_tls.Pointer()->Set(this); |
39 } | 42 } |
40 | 43 |
41 ServiceWorkerDispatcher::~ServiceWorkerDispatcher() { | 44 ServiceWorkerDispatcher::~ServiceWorkerDispatcher() { |
42 for (ScriptClientMap::iterator it = script_clients_.begin(); | |
43 it != script_clients_.end(); | |
44 ++it) { | |
45 Send(new ServiceWorkerHostMsg_RemoveScriptClient( | |
46 CurrentWorkerId(), it->first)); | |
47 } | |
48 | |
49 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); | 45 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); |
50 } | 46 } |
51 | 47 |
52 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { | 48 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { |
53 bool handled = true; | 49 bool handled = true; |
54 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) | 50 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) |
55 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered) | 51 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered) |
56 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, | 52 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, |
57 OnUnregistered) | 53 OnUnregistered) |
58 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError, | 54 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError, |
(...skipping 25 matching lines...) Expand all Loading... |
84 void ServiceWorkerDispatcher::UnregisterServiceWorker( | 80 void ServiceWorkerDispatcher::UnregisterServiceWorker( |
85 int provider_id, | 81 int provider_id, |
86 const GURL& pattern, | 82 const GURL& pattern, |
87 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks) { | 83 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks) { |
88 DCHECK(callbacks); | 84 DCHECK(callbacks); |
89 int request_id = pending_callbacks_.Add(callbacks); | 85 int request_id = pending_callbacks_.Add(callbacks); |
90 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( | 86 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( |
91 CurrentWorkerId(), request_id, provider_id, pattern)); | 87 CurrentWorkerId(), request_id, provider_id, pattern)); |
92 } | 88 } |
93 | 89 |
| 90 void ServiceWorkerDispatcher::AddProviderContext( |
| 91 ServiceWorkerProviderContext* provider_context) { |
| 92 DCHECK(provider_context); |
| 93 int provider_id = provider_context->provider_id(); |
| 94 DCHECK(!ContainsKey(provider_contexts_, provider_id)); |
| 95 provider_contexts_[provider_id] = provider_context; |
| 96 } |
| 97 |
| 98 void ServiceWorkerDispatcher::RemoveProviderContext( |
| 99 ServiceWorkerProviderContext* provider_context) { |
| 100 DCHECK(provider_context); |
| 101 DCHECK(ContainsKey(provider_contexts_, provider_context->provider_id())); |
| 102 provider_contexts_.erase(provider_context->provider_id()); |
| 103 worker_to_provider_.erase(provider_context->current_handle_id()); |
| 104 } |
| 105 |
94 void ServiceWorkerDispatcher::AddScriptClient( | 106 void ServiceWorkerDispatcher::AddScriptClient( |
95 int provider_id, | 107 int provider_id, |
96 blink::WebServiceWorkerProviderClient* client) { | 108 blink::WebServiceWorkerProviderClient* client) { |
97 DCHECK(client); | 109 DCHECK(client); |
98 DCHECK(!ContainsKey(script_clients_, provider_id)); | 110 DCHECK(!ContainsKey(script_clients_, provider_id)); |
99 script_clients_[provider_id] = client; | 111 script_clients_[provider_id] = client; |
100 thread_safe_sender_->Send(new ServiceWorkerHostMsg_AddScriptClient( | |
101 CurrentWorkerId(), provider_id)); | |
102 } | 112 } |
103 | 113 |
104 void ServiceWorkerDispatcher::RemoveScriptClient(int provider_id) { | 114 void ServiceWorkerDispatcher::RemoveScriptClient(int provider_id) { |
105 // This could be possibly called multiple times to ensure termination. | 115 // This could be possibly called multiple times to ensure termination. |
106 if (ContainsKey(script_clients_, provider_id)) { | 116 if (ContainsKey(script_clients_, provider_id)) |
107 script_clients_.erase(provider_id); | 117 script_clients_.erase(provider_id); |
108 thread_safe_sender_->Send(new ServiceWorkerHostMsg_RemoveScriptClient( | |
109 CurrentWorkerId(), provider_id)); | |
110 } | |
111 } | 118 } |
112 | 119 |
113 ServiceWorkerDispatcher* | 120 ServiceWorkerDispatcher* |
114 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | 121 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
115 ThreadSafeSender* thread_safe_sender) { | 122 ThreadSafeSender* thread_safe_sender) { |
116 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { | 123 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { |
117 NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; | 124 NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; |
118 g_dispatcher_tls.Pointer()->Set(NULL); | 125 g_dispatcher_tls.Pointer()->Set(NULL); |
119 } | 126 } |
120 if (g_dispatcher_tls.Pointer()->Get()) | 127 if (g_dispatcher_tls.Pointer()->Get()) |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 scoped_ptr<WebServiceWorkerError> error( | 195 scoped_ptr<WebServiceWorkerError> error( |
189 new WebServiceWorkerError(error_type, message)); | 196 new WebServiceWorkerError(error_type, message)); |
190 callbacks->onError(error.release()); | 197 callbacks->onError(error.release()); |
191 pending_callbacks_.Remove(request_id); | 198 pending_callbacks_.Remove(request_id); |
192 } | 199 } |
193 | 200 |
194 void ServiceWorkerDispatcher::OnServiceWorkerStateChanged( | 201 void ServiceWorkerDispatcher::OnServiceWorkerStateChanged( |
195 int thread_id, | 202 int thread_id, |
196 int handle_id, | 203 int handle_id, |
197 blink::WebServiceWorkerState state) { | 204 blink::WebServiceWorkerState state) { |
198 ServiceWorkerMap::iterator found = service_workers_.find(handle_id); | 205 WorkerObjectMap::iterator worker = service_workers_.find(handle_id); |
199 if (found == service_workers_.end()) | 206 if (worker != service_workers_.end()) |
200 return; | 207 worker->second->OnStateChanged(state); |
201 found->second->OnStateChanged(state); | 208 |
| 209 WorkerToProviderMap::iterator provider = worker_to_provider_.find(handle_id); |
| 210 if (provider != worker_to_provider_.end()) |
| 211 provider->second->OnServiceWorkerStateChanged(thread_id, handle_id, state); |
202 } | 212 } |
203 | 213 |
204 void ServiceWorkerDispatcher::OnSetCurrentServiceWorker( | 214 void ServiceWorkerDispatcher::OnSetCurrentServiceWorker( |
205 int thread_id, | 215 int thread_id, |
206 int provider_id, | 216 int provider_id, |
207 const ServiceWorkerObjectInfo& info) { | 217 const ServiceWorkerObjectInfo& info) { |
208 scoped_ptr<WebServiceWorkerImpl> worker( | 218 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
209 new WebServiceWorkerImpl(info, thread_safe_sender_)); | 219 if (provider != provider_contexts_.end()) { |
| 220 provider->second->OnSetCurrentServiceWorker(thread_id, provider_id, info); |
| 221 worker_to_provider_[info.handle_id] = provider->second; |
| 222 } |
| 223 |
210 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 224 ScriptClientMap::iterator found = script_clients_.find(provider_id); |
211 if (found == script_clients_.end()) { | 225 if (found != script_clients_.end()) { |
212 // Note that |worker|'s destructor sends a ServiceWorkerObjectDestroyed | 226 // Populate the .current field with the new worker object. |
213 // message so the browser-side can clean up the ServiceWorkerHandle it | 227 scoped_ptr<ServiceWorkerHandleReference> handle_ref( |
214 // created when sending us this message. | 228 ServiceWorkerHandleReference::Create(info, thread_safe_sender_)); |
215 return; | 229 found->second->setCurrentServiceWorker( |
| 230 new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_)); |
216 } | 231 } |
217 // TODO(falken): Call client->setCurrentServiceWorker(worker) when the Blink | |
218 // change to add that function rolls in. | |
219 } | 232 } |
220 | 233 |
221 void ServiceWorkerDispatcher::AddServiceWorker( | 234 void ServiceWorkerDispatcher::AddServiceWorker( |
222 int handle_id, WebServiceWorkerImpl* worker) { | 235 int handle_id, WebServiceWorkerImpl* worker) { |
223 DCHECK(!ContainsKey(service_workers_, handle_id)); | 236 DCHECK(!ContainsKey(service_workers_, handle_id)); |
224 service_workers_[handle_id] = worker; | 237 service_workers_[handle_id] = worker; |
225 } | 238 } |
226 | 239 |
227 void ServiceWorkerDispatcher::RemoveServiceWorker(int handle_id) { | 240 void ServiceWorkerDispatcher::RemoveServiceWorker(int handle_id) { |
228 DCHECK(ContainsKey(service_workers_, handle_id)); | 241 DCHECK(ContainsKey(service_workers_, handle_id)); |
229 service_workers_.erase(handle_id); | 242 service_workers_.erase(handle_id); |
230 } | 243 } |
231 | 244 |
232 } // namespace content | 245 } // namespace content |
OLD | NEW |