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()); | |
|
michaeln
2013/10/04 01:26:07
Looks like two different dispatchers can generate
michaeln
2013/10/04 01:51:52
In case it's not obvious, i have a preference for
kinuko
2013/10/04 14:55:48
:( I have to agree that map stuff has some error-p
alecflett
2013/10/07 18:33:58
oh wow yeah that means the quota api has this prob
alecflett
2013/10/07 18:33:58
Given the choice of using the global vs passing th
| |
| 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( | |
| 96 int32 request_id, | |
| 97 int32 service_worker_id) { | |
| 98 WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks = | |
| 99 pending_callbacks_.Lookup(request_id); | |
| 100 DCHECK(callbacks); | |
| 101 if (!callbacks) | |
| 102 return; | |
| 103 | |
| 104 // the browser has to generate the service_worker_id so the same | |
| 105 // worker can be called from different renderer contexts. However, | |
| 106 // the proxy object doesn't have to be consistent unless we require | |
| 107 // the DOM objects to be identical when there's a duplicate | |
| 108 // registration. So for now we mint a new object each time. | |
| 109 WebServiceWorkerImpl* worker = | |
| 110 new WebServiceWorkerImpl(service_worker_id, true); | |
| 111 callbacks->onSuccess(worker); | |
|
kinuko
2013/10/04 14:55:48
pending_callbacks_.Remove(request_id) here?
Curre
| |
| 112 } | |
| 113 | |
| 114 void ServiceWorkerDispatcher::OnServiceWorkerUnregistered( | |
| 115 int32 request_id, | |
| 116 int32 service_worker_id) { | |
| 117 WebServiceWorkerRegistry::WebServiceWorkerCallbacks* callbacks = | |
| 118 pending_callbacks_.Lookup(request_id); | |
| 119 DCHECK(callbacks); | |
| 120 if (!callbacks) | |
| 121 return; | |
| 122 | |
| 123 // see notes in ServiceWorkerRegistered | |
| 124 WebServiceWorkerImpl* worker = | |
| 125 new WebServiceWorkerImpl(service_worker_id, false); | |
| 126 callbacks->onSuccess(worker); | |
| 127 } | |
| 128 | |
| 129 void ServiceWorkerDispatcher::OnWorkerRunLoopStopped() { | |
| 130 message_filter_->ClearThreadRequests(CurrentWorkerId()); | |
| 131 delete this; | |
| 132 } | |
| 133 | |
| 134 } // namespace content | |
| OLD | NEW |