OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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_provider_context.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "base/stl_util.h" |
| 10 #include "content/child/child_thread.h" |
| 11 #include "content/child/service_worker/service_worker_dispatcher.h" |
| 12 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 13 #include "content/child/thread_safe_sender.h" |
| 14 #include "content/child/worker_task_runner.h" |
| 15 #include "content/common/service_worker/service_worker_messages.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 ServiceWorkerProviderContext::ServiceWorkerProviderContext(int provider_id) |
| 20 : provider_id_(provider_id), |
| 21 main_thread_loop_proxy_(base::MessageLoopProxy::current()) { |
| 22 if (!ChildThread::current()) |
| 23 return; // May be null in some tests. |
| 24 thread_safe_sender_ = ChildThread::current()->thread_safe_sender(); |
| 25 ServiceWorkerDispatcher* dispatcher = |
| 26 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
| 27 thread_safe_sender_); |
| 28 DCHECK(dispatcher); |
| 29 dispatcher->AddProviderContext(this); |
| 30 } |
| 31 |
| 32 ServiceWorkerProviderContext::~ServiceWorkerProviderContext() { |
| 33 if (ServiceWorkerDispatcher* dispatcher = |
| 34 ServiceWorkerDispatcher::GetThreadSpecificInstance()) { |
| 35 dispatcher->RemoveProviderContext(this); |
| 36 } |
| 37 } |
| 38 |
| 39 scoped_ptr<ServiceWorkerHandleReference> |
| 40 ServiceWorkerProviderContext::GetCurrentServiceWorkerHandle() { |
| 41 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 42 if (!current_) |
| 43 return scoped_ptr<ServiceWorkerHandleReference>(); |
| 44 return ServiceWorkerHandleReference::Create( |
| 45 current_->info(), thread_safe_sender_); |
| 46 } |
| 47 |
| 48 void ServiceWorkerProviderContext::OnServiceWorkerStateChanged( |
| 49 int handle_id, |
| 50 blink::WebServiceWorkerState state) { |
| 51 // Currently .current is the only ServiceWorker associated to this provider. |
| 52 DCHECK_EQ(current_handle_id(), handle_id); |
| 53 current_->set_state(state); |
| 54 |
| 55 // TODO(kinuko): We can forward the message to other threads here |
| 56 // when we support navigator.serviceWorker in dedicated workers. |
| 57 } |
| 58 |
| 59 void ServiceWorkerProviderContext::OnSetCurrentServiceWorker( |
| 60 int provider_id, |
| 61 const ServiceWorkerObjectInfo& info) { |
| 62 DCHECK_EQ(provider_id_, provider_id); |
| 63 |
| 64 // This context is is the primary owner of this handle, keeps the |
| 65 // initial reference until it goes away. |
| 66 current_ = ServiceWorkerHandleReference::CreateForDeleter( |
| 67 info, thread_safe_sender_); |
| 68 |
| 69 // TODO(kinuko): We can forward the message to other threads here |
| 70 // when we support navigator.serviceWorker in dedicated workers. |
| 71 } |
| 72 |
| 73 int ServiceWorkerProviderContext::current_handle_id() const { |
| 74 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 75 return current_ ? current_->info().handle_id : kInvalidServiceWorkerHandleId; |
| 76 } |
| 77 |
| 78 } // namespace content |
OLD | NEW |