Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/child/service_worker/service_worker_dispatcher.h" | |
| 6 | |
| 7 #include "base/lazy_instance.h" | |
| 8 #include "base/threading/thread_local.h" | |
| 9 #include "content/child/service_worker/service_worker_message_filter.h" | |
| 10 #include "content/child/service_worker/web_service_worker_proxy.h" | |
| 11 #include "content/child/thread_safe_sender.h" | |
| 12 #include "content/common/service_worker_messages.h" | |
| 13 | |
| 14 using WebKit::WebServiceWorkerRegistry; | |
| 15 using base::ThreadLocalPointer; | |
| 16 using webkit_glue::WorkerTaskRunner; | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 static base::LazyInstance<ThreadLocalPointer<ServiceWorkerDispatcher> >::Leaky | |
| 21 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; | |
| 22 | |
| 23 ServiceWorkerDispatcher* const kHasBeenDeleted = | |
| 24 reinterpret_cast<ServiceWorkerDispatcher*>(0x1); | |
| 25 | |
| 26 ServiceWorkerDispatcher::ServiceWorkerDispatcher( | |
| 27 ThreadSafeSender* thread_safe_sender, | |
| 28 ServiceWorkerMessageFilter* message_filter) | |
| 29 : thread_safe_sender_(thread_safe_sender), message_filter_(message_filter) { | |
| 30 g_dispatcher_tls.Pointer()->Set(this); | |
| 31 } | |
| 32 | |
| 33 ServiceWorkerDispatcher::~ServiceWorkerDispatcher() { | |
| 34 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); | |
| 35 } | |
| 36 | |
| 37 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
| 38 bool handled = true; | |
| 39 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) | |
| 40 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, | |
| 41 OnServiceWorkerRegistered) | |
| 42 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, | |
| 43 OnServiceWorkerUnregistered) | |
| 44 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 45 IPC_END_MESSAGE_MAP() | |
| 46 DCHECK(handled) << "Unhandled message:" << msg.type(); | |
| 47 } | |
| 48 | |
| 49 bool ServiceWorkerDispatcher::Send(IPC::Message* msg) { | |
| 50 return thread_safe_sender_->Send(msg); | |
| 51 } | |
| 52 | |
| 53 static int CurrentWorkerId() { | |
| 54 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | |
| 55 } | |
| 56 | |
| 57 void ServiceWorkerDispatcher::RegisterServiceWorker( | |
| 58 const string16& pattern, | |
| 59 const GURL& scriptUrl, | |
| 60 WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks) { | |
| 61 DCHECK(callbacks); | |
| 62 int request_id = pending_callbacks_.Add(callbacks); | |
| 63 message_filter_->RegisterRequestID(request_id, CurrentWorkerId()); | |
| 64 thread_safe_sender_->Send(new ServiceWorkerHostMsg_RegisterServiceWorker( | |
| 65 request_id, pattern, scriptUrl)); | |
| 66 } | |
| 67 | |
| 68 void ServiceWorkerDispatcher::UnregisterServiceWorker( | |
| 69 const string16& pattern, | |
| 70 WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks) { | |
| 71 DCHECK(callbacks); | |
| 72 int request_id = pending_callbacks_.Add(callbacks); | |
| 73 message_filter_->RegisterRequestID(request_id, CurrentWorkerId()); | |
| 74 thread_safe_sender_->Send( | |
| 75 new ServiceWorkerHostMsg_UnregisterServiceWorker(request_id, pattern)); | |
| 76 } | |
| 77 | |
| 78 ServiceWorkerDispatcher* ServiceWorkerDispatcher::ThreadSpecificInstance( | |
| 79 ThreadSafeSender* thread_safe_sender, | |
| 80 ServiceWorkerMessageFilter* message_filter) { | |
| 81 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { | |
| 82 NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; | |
| 83 g_dispatcher_tls.Pointer()->Set(NULL); | |
| 84 } | |
| 85 if (g_dispatcher_tls.Pointer()->Get()) | |
| 86 return g_dispatcher_tls.Pointer()->Get(); | |
| 87 | |
| 88 ServiceWorkerDispatcher* dispatcher = | |
| 89 new ServiceWorkerDispatcher(thread_safe_sender, message_filter); | |
| 90 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) | |
| 91 webkit_glue::WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); | |
| 92 return dispatcher; | |
| 93 } | |
| 94 | |
| 95 void ServiceWorkerDispatcher::OnServiceWorkerRegistered(int32 request_id, | |
| 96 int32 worker_id) { | |
| 97 WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks = | |
| 98 pending_callbacks_.Lookup(request_id); | |
| 99 DCHECK(callbacks); | |
| 100 if (!callbacks) | |
| 101 return; | |
| 102 | |
| 103 // the browser has to generate the worker_id so the same worker can | |
| 104 // be called from different renderer contexts. However, the proxy | |
| 105 // object doesn't have to be consistent unless we require the DOM | |
| 106 // objects to be identical if there's a duplicate registration. So | |
| 107 // for now we mint a new object each time. | |
| 108 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
| |
| 109 callbacks->onSuccess(worker); | |
| 110 } | |
| 111 | |
| 112 void ServiceWorkerDispatcher::OnServiceWorkerUnregistered(int32 request_id, | |
| 113 int32 worker_id) { | |
| 114 WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks = | |
| 115 pending_callbacks_.Lookup(request_id); | |
| 116 DCHECK(callbacks); | |
| 117 if (!callbacks) | |
| 118 return; | |
| 119 | |
| 120 // see notes in ServiceWorkerRegistered | |
| 121 WebServiceWorkerImpl* worker = new WebServiceWorkerImpl(worker_id, false); | |
| 122 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
| |
| 123 } | |
| 124 | |
| 125 void ServiceWorkerDispatcher::OnWorkerRunLoopStopped() { delete this; } | |
| 126 | |
| 127 } // namespace content | |
| OLD | NEW |