Index: content/child/service_worker/service_worker_dispatcher.cc |
diff --git a/content/child/service_worker/service_worker_dispatcher.cc b/content/child/service_worker/service_worker_dispatcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..96c559f4bde186b8b41a773f3dce1cf67edccaac |
--- /dev/null |
+++ b/content/child/service_worker/service_worker_dispatcher.cc |
@@ -0,0 +1,127 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/child/service_worker/service_worker_dispatcher.h" |
+ |
+#include "base/lazy_instance.h" |
+#include "base/threading/thread_local.h" |
+#include "content/child/service_worker/service_worker_message_filter.h" |
+#include "content/child/service_worker/web_service_worker_proxy.h" |
+#include "content/child/thread_safe_sender.h" |
+#include "content/common/service_worker_messages.h" |
+ |
+using WebKit::WebServiceWorkerRegistry; |
+using base::ThreadLocalPointer; |
+using webkit_glue::WorkerTaskRunner; |
+ |
+namespace content { |
+ |
+static base::LazyInstance<ThreadLocalPointer<ServiceWorkerDispatcher> >::Leaky |
+ g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; |
+ |
+ServiceWorkerDispatcher* const kHasBeenDeleted = |
+ reinterpret_cast<ServiceWorkerDispatcher*>(0x1); |
+ |
+ServiceWorkerDispatcher::ServiceWorkerDispatcher( |
+ ThreadSafeSender* thread_safe_sender, |
+ ServiceWorkerMessageFilter* message_filter) |
+ : thread_safe_sender_(thread_safe_sender), message_filter_(message_filter) { |
+ g_dispatcher_tls.Pointer()->Set(this); |
+} |
+ |
+ServiceWorkerDispatcher::~ServiceWorkerDispatcher() { |
+ g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); |
+} |
+ |
+void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) |
+ IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, |
+ OnServiceWorkerRegistered) |
+ IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, |
+ OnServiceWorkerUnregistered) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ DCHECK(handled) << "Unhandled message:" << msg.type(); |
+} |
+ |
+bool ServiceWorkerDispatcher::Send(IPC::Message* msg) { |
+ return thread_safe_sender_->Send(msg); |
+} |
+ |
+static int CurrentWorkerId() { |
+ return WorkerTaskRunner::Instance()->CurrentWorkerId(); |
+} |
+ |
+void ServiceWorkerDispatcher::RegisterServiceWorker( |
+ const string16& pattern, |
+ const GURL& scriptUrl, |
+ WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks) { |
+ DCHECK(callbacks); |
+ int request_id = pending_callbacks_.Add(callbacks); |
+ message_filter_->RegisterRequestID(request_id, CurrentWorkerId()); |
+ thread_safe_sender_->Send(new ServiceWorkerHostMsg_RegisterServiceWorker( |
+ request_id, pattern, scriptUrl)); |
+} |
+ |
+void ServiceWorkerDispatcher::UnregisterServiceWorker( |
+ const string16& pattern, |
+ WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks) { |
+ DCHECK(callbacks); |
+ int request_id = pending_callbacks_.Add(callbacks); |
+ message_filter_->RegisterRequestID(request_id, CurrentWorkerId()); |
+ thread_safe_sender_->Send( |
+ new ServiceWorkerHostMsg_UnregisterServiceWorker(request_id, pattern)); |
+} |
+ |
+ServiceWorkerDispatcher* ServiceWorkerDispatcher::ThreadSpecificInstance( |
+ ThreadSafeSender* thread_safe_sender, |
+ ServiceWorkerMessageFilter* message_filter) { |
+ if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { |
+ NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; |
+ g_dispatcher_tls.Pointer()->Set(NULL); |
+ } |
+ if (g_dispatcher_tls.Pointer()->Get()) |
+ return g_dispatcher_tls.Pointer()->Get(); |
+ |
+ ServiceWorkerDispatcher* dispatcher = |
+ new ServiceWorkerDispatcher(thread_safe_sender, message_filter); |
+ if (WorkerTaskRunner::Instance()->CurrentWorkerId()) |
+ webkit_glue::WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); |
+ return dispatcher; |
+} |
+ |
+void ServiceWorkerDispatcher::OnServiceWorkerRegistered(int32 request_id, |
+ int32 worker_id) { |
+ WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks = |
+ pending_callbacks_.Lookup(request_id); |
+ DCHECK(callbacks); |
+ if (!callbacks) |
+ return; |
+ |
+ // the browser has to generate the worker_id so the same worker can |
+ // be called from different renderer contexts. However, the proxy |
+ // object doesn't have to be consistent unless we require the DOM |
+ // objects to be identical if there's a duplicate registration. So |
+ // for now we mint a new object each time. |
+ WebServiceWorkerImpl* worker = new WebServiceWorkerImpl(worker_id, true); |
kinuko
2013/09/30 12:41:17
(we could think more about this later, but anyway)
alecflett
2013/10/01 00:17:04
Yeah, I'm not totally excited about that either. I
|
+ callbacks->onSuccess(worker); |
+} |
+ |
+void ServiceWorkerDispatcher::OnServiceWorkerUnregistered(int32 request_id, |
+ int32 worker_id) { |
+ WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks = |
+ pending_callbacks_.Lookup(request_id); |
+ DCHECK(callbacks); |
+ if (!callbacks) |
+ return; |
+ |
+ // see notes in ServiceWorkerRegistered |
+ WebServiceWorkerImpl* worker = new WebServiceWorkerImpl(worker_id, false); |
+ callbacks->onSuccess(worker); |
michaeln
2013/09/30 23:41:27
Why is a Worker passed as a result of unregistrati
alecflett
2013/10/01 00:17:04
It's unspecified right now but it seems the most c
|
+} |
+ |
+void ServiceWorkerDispatcher::OnWorkerRunLoopStopped() { delete this; } |
+ |
+} // namespace content |