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