| 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/web_service_worker_provider_impl.h" | 5 #include "content/child/service_worker/web_service_worker_provider_impl.h" |
| 6 | 6 |
| 7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "content/child/child_thread.h" | 9 #include "content/child/child_thread.h" |
| 10 #include "content/child/service_worker/scoped_service_worker_reference.h" |
| 10 #include "content/child/service_worker/service_worker_dispatcher.h" | 11 #include "content/child/service_worker/service_worker_dispatcher.h" |
| 12 #include "content/child/service_worker/service_worker_provider_context.h" |
| 13 #include "content/child/service_worker/web_service_worker_impl.h" |
| 11 #include "content/child/thread_safe_sender.h" | 14 #include "content/child/thread_safe_sender.h" |
| 15 #include "content/child/worker_task_runner.h" |
| 16 #include "content/common/service_worker/service_worker_messages.h" |
| 17 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h" |
| 12 #include "third_party/WebKit/public/platform/WebURL.h" | 18 #include "third_party/WebKit/public/platform/WebURL.h" |
| 13 | 19 |
| 14 using blink::WebURL; | 20 using blink::WebURL; |
| 15 | 21 |
| 16 namespace content { | 22 namespace content { |
| 17 | 23 |
| 24 namespace { |
| 25 |
| 26 int CurrentWorkerId() { |
| 27 return WorkerTaskRunner::Instance()->CurrentWorkerId(); |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 18 WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl( | 32 WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl( |
| 19 ThreadSafeSender* thread_safe_sender, | 33 ThreadSafeSender* thread_safe_sender, |
| 20 int provider_id) | 34 ServiceWorkerProviderContext* context) |
| 21 : thread_safe_sender_(thread_safe_sender), | 35 : thread_safe_sender_(thread_safe_sender), |
| 22 provider_id_(provider_id) { | 36 context_(context), |
| 37 provider_id_(context->provider_id()) { |
| 23 } | 38 } |
| 24 | 39 |
| 25 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() { | 40 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() { |
| 26 // Make sure the script client is removed. | 41 // Make sure the script client is removed. |
| 27 RemoveScriptClient(); | 42 RemoveScriptClient(); |
| 28 } | 43 } |
| 29 | 44 |
| 30 void WebServiceWorkerProviderImpl::setClient( | 45 void WebServiceWorkerProviderImpl::setClient( |
| 31 blink::WebServiceWorkerProviderClient* client) { | 46 blink::WebServiceWorkerProviderClient* client) { |
| 32 if (client) | 47 if (!client) { |
| 33 GetDispatcher()->AddScriptClient(provider_id_, client); | |
| 34 else | |
| 35 RemoveScriptClient(); | 48 RemoveScriptClient(); |
| 49 return; |
| 50 } |
| 51 |
| 52 scoped_ptr<ScopedServiceWorkerReference> current; |
| 53 context_->AddProviderThreadAndGetInitializationInfo( |
| 54 CurrentWorkerId(), ¤t); |
| 55 GetDispatcher()->AddScriptClient(provider_id_, client); |
| 56 if (!current) |
| 57 return; |
| 58 |
| 59 int handle_id = current->info().handle_id; |
| 60 if (handle_id != kInvalidServiceWorkerHandleId) { |
| 61 scoped_ptr<WebServiceWorkerImpl> worker( |
| 62 new WebServiceWorkerImpl(current.Pass(), thread_safe_sender_)); |
| 63 client->setCurrentServiceWorker(worker.release()); |
| 64 } |
| 36 } | 65 } |
| 37 | 66 |
| 38 void WebServiceWorkerProviderImpl::registerServiceWorker( | 67 void WebServiceWorkerProviderImpl::registerServiceWorker( |
| 39 const WebURL& pattern, | 68 const WebURL& pattern, |
| 40 const WebURL& script_url, | 69 const WebURL& script_url, |
| 41 WebServiceWorkerCallbacks* callbacks) { | 70 WebServiceWorkerCallbacks* callbacks) { |
| 42 GetDispatcher()->RegisterServiceWorker( | 71 GetDispatcher()->RegisterServiceWorker( |
| 43 provider_id_, pattern, script_url, callbacks); | 72 provider_id_, pattern, script_url, callbacks); |
| 44 } | 73 } |
| 45 | 74 |
| 46 void WebServiceWorkerProviderImpl::unregisterServiceWorker( | 75 void WebServiceWorkerProviderImpl::unregisterServiceWorker( |
| 47 const WebURL& pattern, | 76 const WebURL& pattern, |
| 48 WebServiceWorkerCallbacks* callbacks) { | 77 WebServiceWorkerCallbacks* callbacks) { |
| 49 GetDispatcher()->UnregisterServiceWorker( | 78 GetDispatcher()->UnregisterServiceWorker( |
| 50 provider_id_, pattern, callbacks); | 79 provider_id_, pattern, callbacks); |
| 51 } | 80 } |
| 52 | 81 |
| 53 void WebServiceWorkerProviderImpl::RemoveScriptClient() { | 82 void WebServiceWorkerProviderImpl::RemoveScriptClient() { |
| 54 // Remove the script client, but only if the dispatcher is still there. | 83 // Remove the script client, but only if the dispatcher is still there. |
| 55 // (For cleanup path we don't need to bother creating a new dispatcher) | 84 // (For cleanup path we don't need to bother creating a new dispatcher) |
| 56 ServiceWorkerDispatcher* dispatcher = | 85 ServiceWorkerDispatcher* dispatcher = |
| 57 ServiceWorkerDispatcher::GetThreadSpecificInstance(); | 86 ServiceWorkerDispatcher::GetThreadSpecificInstance(); |
| 58 if (dispatcher) | 87 if (dispatcher) |
| 59 dispatcher->RemoveScriptClient(provider_id_); | 88 dispatcher->RemoveScriptClient(provider_id_); |
| 89 context_->RemoveProviderThread(CurrentWorkerId()); |
| 60 } | 90 } |
| 61 | 91 |
| 62 ServiceWorkerDispatcher* WebServiceWorkerProviderImpl::GetDispatcher() { | 92 ServiceWorkerDispatcher* WebServiceWorkerProviderImpl::GetDispatcher() { |
| 63 return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | 93 return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
| 64 thread_safe_sender_); | 94 thread_safe_sender_); |
| 65 } | 95 } |
| 66 | 96 |
| 67 } // namespace content | 97 } // namespace content |
| OLD | NEW |