| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 using blink::WebServiceWorkerProvider; | 35 using blink::WebServiceWorkerProvider; |
| 36 using base::ThreadLocalPointer; | 36 using base::ThreadLocalPointer; |
| 37 | 37 |
| 38 namespace content { | 38 namespace content { |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 | 41 |
| 42 base::LazyInstance<ThreadLocalPointer<void>>::Leaky g_dispatcher_tls = | 42 base::LazyInstance<ThreadLocalPointer<void>>::Leaky g_dispatcher_tls = |
| 43 LAZY_INSTANCE_INITIALIZER; | 43 LAZY_INSTANCE_INITIALIZER; |
| 44 | 44 |
| 45 void* const kHasBeenDeleted = reinterpret_cast<void*>(0x1); | 45 void* const kSWDispatcherHasBeenDeleted = reinterpret_cast<void*>(0x1); |
| 46 | 46 |
| 47 int CurrentWorkerId() { | 47 int ServiceWorkerCurrentWorkerId() { |
| 48 return WorkerThread::GetCurrentId(); | 48 return WorkerThread::GetCurrentId(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 ServiceWorkerDispatcher::ServiceWorkerDispatcher( | 53 ServiceWorkerDispatcher::ServiceWorkerDispatcher( |
| 54 ThreadSafeSender* thread_safe_sender, | 54 ThreadSafeSender* thread_safe_sender, |
| 55 base::SingleThreadTaskRunner* main_thread_task_runner) | 55 base::SingleThreadTaskRunner* main_thread_task_runner) |
| 56 : thread_safe_sender_(thread_safe_sender), | 56 : thread_safe_sender_(thread_safe_sender), |
| 57 main_thread_task_runner_(main_thread_task_runner) { | 57 main_thread_task_runner_(main_thread_task_runner) { |
| 58 g_dispatcher_tls.Pointer()->Set(static_cast<void*>(this)); | 58 g_dispatcher_tls.Pointer()->Set(static_cast<void*>(this)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 ServiceWorkerDispatcher::~ServiceWorkerDispatcher() { | 61 ServiceWorkerDispatcher::~ServiceWorkerDispatcher() { |
| 62 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); | 62 g_dispatcher_tls.Pointer()->Set(kSWDispatcherHasBeenDeleted); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { | 65 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { |
| 66 bool handled = true; | 66 bool handled = true; |
| 67 | 67 |
| 68 // When you add a new message handler, you should consider adding a similar | 68 // When you add a new message handler, you should consider adding a similar |
| 69 // handler in ServiceWorkerMessageFilter to release references passed from | 69 // handler in ServiceWorkerMessageFilter to release references passed from |
| 70 // the browser process in case we fail to post task to the thread. | 70 // the browser process in case we fail to post task to the thread. |
| 71 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) | 71 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) |
| 72 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUpdated, OnUpdated) | 72 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUpdated, OnUpdated) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 DCHECK(handled) << "Unhandled message:" << msg.type(); | 104 DCHECK(handled) << "Unhandled message:" << msg.type(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void ServiceWorkerDispatcher::UpdateServiceWorker( | 107 void ServiceWorkerDispatcher::UpdateServiceWorker( |
| 108 int provider_id, | 108 int provider_id, |
| 109 int64_t registration_id, | 109 int64_t registration_id, |
| 110 std::unique_ptr<WebServiceWorkerUpdateCallbacks> callbacks) { | 110 std::unique_ptr<WebServiceWorkerUpdateCallbacks> callbacks) { |
| 111 DCHECK(callbacks); | 111 DCHECK(callbacks); |
| 112 int request_id = pending_update_callbacks_.Add(std::move(callbacks)); | 112 int request_id = pending_update_callbacks_.Add(std::move(callbacks)); |
| 113 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UpdateServiceWorker( | 113 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UpdateServiceWorker( |
| 114 CurrentWorkerId(), request_id, provider_id, registration_id)); | 114 ServiceWorkerCurrentWorkerId(), request_id, provider_id, registration_id))
; |
| 115 } | 115 } |
| 116 | 116 |
| 117 void ServiceWorkerDispatcher::UnregisterServiceWorker( | 117 void ServiceWorkerDispatcher::UnregisterServiceWorker( |
| 118 int provider_id, | 118 int provider_id, |
| 119 int64_t registration_id, | 119 int64_t registration_id, |
| 120 std::unique_ptr<WebServiceWorkerUnregistrationCallbacks> callbacks) { | 120 std::unique_ptr<WebServiceWorkerUnregistrationCallbacks> callbacks) { |
| 121 DCHECK(callbacks); | 121 DCHECK(callbacks); |
| 122 int request_id = pending_unregistration_callbacks_.Add(std::move(callbacks)); | 122 int request_id = pending_unregistration_callbacks_.Add(std::move(callbacks)); |
| 123 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", | 123 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", |
| 124 "ServiceWorkerDispatcher::UnregisterServiceWorker", | 124 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
| 125 request_id, "Registration ID", registration_id); | 125 request_id, "Registration ID", registration_id); |
| 126 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( | 126 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( |
| 127 CurrentWorkerId(), request_id, provider_id, registration_id)); | 127 ServiceWorkerCurrentWorkerId(), request_id, provider_id, registration_id))
; |
| 128 } | 128 } |
| 129 | 129 |
| 130 void ServiceWorkerDispatcher::EnableNavigationPreload( | 130 void ServiceWorkerDispatcher::EnableNavigationPreload( |
| 131 int provider_id, | 131 int provider_id, |
| 132 int64_t registration_id, | 132 int64_t registration_id, |
| 133 bool enable, | 133 bool enable, |
| 134 std::unique_ptr<WebEnableNavigationPreloadCallbacks> callbacks) { | 134 std::unique_ptr<WebEnableNavigationPreloadCallbacks> callbacks) { |
| 135 DCHECK(callbacks); | 135 DCHECK(callbacks); |
| 136 int request_id = | 136 int request_id = |
| 137 enable_navigation_preload_callbacks_.Add(std::move(callbacks)); | 137 enable_navigation_preload_callbacks_.Add(std::move(callbacks)); |
| 138 thread_safe_sender_->Send(new ServiceWorkerHostMsg_EnableNavigationPreload( | 138 thread_safe_sender_->Send(new ServiceWorkerHostMsg_EnableNavigationPreload( |
| 139 CurrentWorkerId(), request_id, provider_id, registration_id, enable)); | 139 ServiceWorkerCurrentWorkerId(), request_id, provider_id, registration_id,
enable)); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void ServiceWorkerDispatcher::GetNavigationPreloadState( | 142 void ServiceWorkerDispatcher::GetNavigationPreloadState( |
| 143 int provider_id, | 143 int provider_id, |
| 144 int64_t registration_id, | 144 int64_t registration_id, |
| 145 std::unique_ptr<WebGetNavigationPreloadStateCallbacks> callbacks) { | 145 std::unique_ptr<WebGetNavigationPreloadStateCallbacks> callbacks) { |
| 146 DCHECK(callbacks); | 146 DCHECK(callbacks); |
| 147 int request_id = | 147 int request_id = |
| 148 get_navigation_preload_state_callbacks_.Add(std::move(callbacks)); | 148 get_navigation_preload_state_callbacks_.Add(std::move(callbacks)); |
| 149 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetNavigationPreloadState( | 149 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetNavigationPreloadState( |
| 150 CurrentWorkerId(), request_id, provider_id, registration_id)); | 150 ServiceWorkerCurrentWorkerId(), request_id, provider_id, registration_id))
; |
| 151 } | 151 } |
| 152 | 152 |
| 153 void ServiceWorkerDispatcher::SetNavigationPreloadHeader( | 153 void ServiceWorkerDispatcher::SetNavigationPreloadHeader( |
| 154 int provider_id, | 154 int provider_id, |
| 155 int64_t registration_id, | 155 int64_t registration_id, |
| 156 const std::string& value, | 156 const std::string& value, |
| 157 std::unique_ptr<WebSetNavigationPreloadHeaderCallbacks> callbacks) { | 157 std::unique_ptr<WebSetNavigationPreloadHeaderCallbacks> callbacks) { |
| 158 DCHECK(callbacks); | 158 DCHECK(callbacks); |
| 159 int request_id = | 159 int request_id = |
| 160 set_navigation_preload_header_callbacks_.Add(std::move(callbacks)); | 160 set_navigation_preload_header_callbacks_.Add(std::move(callbacks)); |
| 161 thread_safe_sender_->Send(new ServiceWorkerHostMsg_SetNavigationPreloadHeader( | 161 thread_safe_sender_->Send(new ServiceWorkerHostMsg_SetNavigationPreloadHeader( |
| 162 CurrentWorkerId(), request_id, provider_id, registration_id, value)); | 162 ServiceWorkerCurrentWorkerId(), request_id, provider_id, registration_id,
value)); |
| 163 } | 163 } |
| 164 | 164 |
| 165 void ServiceWorkerDispatcher::AddProviderContext( | 165 void ServiceWorkerDispatcher::AddProviderContext( |
| 166 ServiceWorkerProviderContext* provider_context) { | 166 ServiceWorkerProviderContext* provider_context) { |
| 167 DCHECK(provider_context); | 167 DCHECK(provider_context); |
| 168 int provider_id = provider_context->provider_id(); | 168 int provider_id = provider_context->provider_id(); |
| 169 DCHECK(!base::ContainsKey(provider_contexts_, provider_id)); | 169 DCHECK(!base::ContainsKey(provider_contexts_, provider_id)); |
| 170 provider_contexts_[provider_id] = provider_context; | 170 provider_contexts_[provider_id] = provider_context; |
| 171 } | 171 } |
| 172 | 172 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 189 void ServiceWorkerDispatcher::RemoveProviderClient(int provider_id) { | 189 void ServiceWorkerDispatcher::RemoveProviderClient(int provider_id) { |
| 190 // This could be possibly called multiple times to ensure termination. | 190 // This could be possibly called multiple times to ensure termination. |
| 191 if (base::ContainsKey(provider_clients_, provider_id)) | 191 if (base::ContainsKey(provider_clients_, provider_id)) |
| 192 provider_clients_.erase(provider_id); | 192 provider_clients_.erase(provider_id); |
| 193 } | 193 } |
| 194 | 194 |
| 195 ServiceWorkerDispatcher* | 195 ServiceWorkerDispatcher* |
| 196 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | 196 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
| 197 ThreadSafeSender* thread_safe_sender, | 197 ThreadSafeSender* thread_safe_sender, |
| 198 base::SingleThreadTaskRunner* main_thread_task_runner) { | 198 base::SingleThreadTaskRunner* main_thread_task_runner) { |
| 199 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { | 199 if (g_dispatcher_tls.Pointer()->Get() == kSWDispatcherHasBeenDeleted) { |
| 200 NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; | 200 NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; |
| 201 g_dispatcher_tls.Pointer()->Set(NULL); | 201 g_dispatcher_tls.Pointer()->Set(NULL); |
| 202 } | 202 } |
| 203 if (g_dispatcher_tls.Pointer()->Get()) | 203 if (g_dispatcher_tls.Pointer()->Get()) |
| 204 return static_cast<ServiceWorkerDispatcher*>( | 204 return static_cast<ServiceWorkerDispatcher*>( |
| 205 g_dispatcher_tls.Pointer()->Get()); | 205 g_dispatcher_tls.Pointer()->Get()); |
| 206 | 206 |
| 207 ServiceWorkerDispatcher* dispatcher = | 207 ServiceWorkerDispatcher* dispatcher = |
| 208 new ServiceWorkerDispatcher(thread_safe_sender, main_thread_task_runner); | 208 new ServiceWorkerDispatcher(thread_safe_sender, main_thread_task_runner); |
| 209 if (WorkerThread::GetCurrentId()) | 209 if (WorkerThread::GetCurrentId()) |
| 210 WorkerThread::AddObserver(dispatcher); | 210 WorkerThread::AddObserver(dispatcher); |
| 211 return dispatcher; | 211 return dispatcher; |
| 212 } | 212 } |
| 213 | 213 |
| 214 ServiceWorkerDispatcher* ServiceWorkerDispatcher::GetThreadSpecificInstance() { | 214 ServiceWorkerDispatcher* ServiceWorkerDispatcher::GetThreadSpecificInstance() { |
| 215 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) | 215 if (g_dispatcher_tls.Pointer()->Get() == kSWDispatcherHasBeenDeleted) |
| 216 return NULL; | 216 return NULL; |
| 217 return static_cast<ServiceWorkerDispatcher*>( | 217 return static_cast<ServiceWorkerDispatcher*>( |
| 218 g_dispatcher_tls.Pointer()->Get()); | 218 g_dispatcher_tls.Pointer()->Get()); |
| 219 } | 219 } |
| 220 | 220 |
| 221 void ServiceWorkerDispatcher::WillStopCurrentWorkerThread() { | 221 void ServiceWorkerDispatcher::WillStopCurrentWorkerThread() { |
| 222 delete this; | 222 delete this; |
| 223 } | 223 } |
| 224 | 224 |
| 225 scoped_refptr<WebServiceWorkerImpl> | 225 scoped_refptr<WebServiceWorkerImpl> |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 return ServiceWorkerRegistrationHandleReference::Adopt( | 629 return ServiceWorkerRegistrationHandleReference::Adopt( |
| 630 info, thread_safe_sender_.get()); | 630 info, thread_safe_sender_.get()); |
| 631 } | 631 } |
| 632 | 632 |
| 633 std::unique_ptr<ServiceWorkerHandleReference> ServiceWorkerDispatcher::Adopt( | 633 std::unique_ptr<ServiceWorkerHandleReference> ServiceWorkerDispatcher::Adopt( |
| 634 const ServiceWorkerObjectInfo& info) { | 634 const ServiceWorkerObjectInfo& info) { |
| 635 return ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | 635 return ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); |
| 636 } | 636 } |
| 637 | 637 |
| 638 } // namespace content | 638 } // namespace content |
| OLD | NEW |