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/web_service_worker_impl.h" | |
| 10 #include "content/child/thread_safe_sender.h" | |
| 11 #include "content/common/service_worker_messages.h" | |
| 12 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | |
| 13 | |
| 14 using WebKit::WebServiceWorkerProvider; | |
| 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 = | |
|
michaeln
2013/10/24 21:57:39
this wants to be either static or in an anon names
alecflett
2013/10/24 22:41:22
Done.
| |
| 24 reinterpret_cast<ServiceWorkerDispatcher*>(0x1); | |
| 25 | |
| 26 ServiceWorkerDispatcher::ServiceWorkerDispatcher( | |
| 27 ThreadSafeSender* thread_safe_sender) | |
| 28 : thread_safe_sender_(thread_safe_sender) { | |
| 29 g_dispatcher_tls.Pointer()->Set(this); | |
| 30 } | |
| 31 | |
| 32 ServiceWorkerDispatcher::~ServiceWorkerDispatcher() { | |
| 33 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); | |
| 34 } | |
| 35 | |
| 36 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
| 37 bool handled = true; | |
| 38 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) | |
| 39 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, | |
| 40 OnServiceWorkerRegistered) | |
| 41 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, | |
| 42 OnServiceWorkerUnregistered) | |
| 43 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 44 IPC_END_MESSAGE_MAP() | |
| 45 DCHECK(handled) << "Unhandled message:" << msg.type(); | |
| 46 } | |
| 47 | |
| 48 bool ServiceWorkerDispatcher::Send(IPC::Message* msg) { | |
| 49 return thread_safe_sender_->Send(msg); | |
| 50 } | |
| 51 | |
| 52 static int CurrentWorkerId() { | |
|
michaeln
2013/10/24 21:57:39
please move this helper up to where the other non-
alecflett
2013/10/24 22:41:22
Done.
| |
| 53 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | |
| 54 } | |
| 55 | |
| 56 void ServiceWorkerDispatcher::RegisterServiceWorker( | |
| 57 const GURL& pattern, | |
| 58 const GURL& scriptUrl, | |
| 59 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks) { | |
| 60 DCHECK(callbacks); | |
| 61 int request_id = pending_callbacks_.Add(callbacks); | |
| 62 thread_safe_sender_->Send(new ServiceWorkerHostMsg_RegisterServiceWorker( | |
| 63 CurrentWorkerId(), request_id, pattern, scriptUrl)); | |
| 64 } | |
| 65 | |
| 66 void ServiceWorkerDispatcher::UnregisterServiceWorker( | |
| 67 const GURL& pattern, | |
| 68 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks) { | |
| 69 DCHECK(callbacks); | |
| 70 int request_id = pending_callbacks_.Add(callbacks); | |
| 71 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( | |
| 72 CurrentWorkerId(), request_id, pattern)); | |
| 73 } | |
| 74 | |
| 75 ServiceWorkerDispatcher* ServiceWorkerDispatcher::ThreadSpecificInstance( | |
| 76 ThreadSafeSender* thread_safe_sender) { | |
| 77 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { | |
| 78 NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; | |
| 79 g_dispatcher_tls.Pointer()->Set(NULL); | |
| 80 } | |
| 81 if (g_dispatcher_tls.Pointer()->Get()) | |
| 82 return g_dispatcher_tls.Pointer()->Get(); | |
| 83 | |
| 84 ServiceWorkerDispatcher* dispatcher = | |
| 85 new ServiceWorkerDispatcher(thread_safe_sender); | |
| 86 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) | |
| 87 webkit_glue::WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); | |
| 88 return dispatcher; | |
| 89 } | |
| 90 | |
| 91 void ServiceWorkerDispatcher::OnServiceWorkerRegistered( | |
| 92 int32 thread_id, | |
| 93 int32 request_id, | |
| 94 int64 service_worker_id) { | |
| 95 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks = | |
| 96 pending_callbacks_.Lookup(request_id); | |
| 97 DCHECK(callbacks); | |
| 98 if (!callbacks) | |
| 99 return; | |
| 100 | |
| 101 // the browser has to generate the service_worker_id so the same | |
| 102 // worker can be called from different renderer contexts. However, | |
| 103 // the impl object doesn't have to be the same instance across calls | |
| 104 // unless we require the DOM objects to be identical when there's a | |
| 105 // duplicate registration. So for now we mint a new object each | |
| 106 // time. | |
| 107 WebServiceWorkerImpl* worker = | |
| 108 new WebServiceWorkerImpl(service_worker_id, true); | |
| 109 callbacks->onSuccess(worker); | |
| 110 pending_callbacks_.Remove(request_id); | |
| 111 } | |
| 112 | |
| 113 void ServiceWorkerDispatcher::OnServiceWorkerUnregistered( | |
| 114 int32 thread_id, | |
| 115 int32 request_id) { | |
| 116 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks = | |
| 117 pending_callbacks_.Lookup(request_id); | |
| 118 DCHECK(callbacks); | |
| 119 if (!callbacks) | |
| 120 return; | |
| 121 | |
| 122 callbacks->onSuccess(nullptr); | |
|
michaeln
2013/10/24 21:57:39
use NULL here
alecflett
2013/10/24 22:41:22
Done.
| |
| 123 pending_callbacks_.Remove(request_id); | |
| 124 } | |
| 125 | |
| 126 void ServiceWorkerDispatcher::OnWorkerRunLoopStopped() { delete this; } | |
| 127 | |
| 128 } // namespace content | |
| OLD | NEW |