| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/service_worker_provider_context.h" | 5 #include "content/child/service_worker/service_worker_provider_context.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "content/child/child_thread_impl.h" | 10 #include "content/child/child_thread_impl.h" |
| 11 #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_handle_reference.h" | 12 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 13 #include "content/child/service_worker/service_worker_message_sender.h" | |
| 14 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" | 13 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" |
| 15 #include "content/child/thread_safe_sender.h" | 14 #include "content/child/thread_safe_sender.h" |
| 16 #include "content/child/worker_task_runner.h" | 15 #include "content/child/worker_task_runner.h" |
| 17 #include "content/common/service_worker/service_worker_messages.h" | 16 #include "content/common/service_worker/service_worker_messages.h" |
| 18 | 17 |
| 19 namespace content { | 18 namespace content { |
| 20 | 19 |
| 21 ServiceWorkerProviderContext::ServiceWorkerProviderContext(int provider_id) | 20 ServiceWorkerProviderContext::ServiceWorkerProviderContext(int provider_id) |
| 22 : provider_id_(provider_id), | 21 : provider_id_(provider_id), |
| 23 main_thread_loop_proxy_(base::MessageLoopProxy::current()) { | 22 main_thread_loop_proxy_(base::MessageLoopProxy::current()) { |
| 24 if (!ChildThreadImpl::current()) | 23 if (!ChildThreadImpl::current()) |
| 25 return; // May be null in some tests. | 24 return; // May be null in some tests. |
| 26 sender_ = new ServiceWorkerMessageSender( | 25 thread_safe_sender_ = ChildThreadImpl::current()->thread_safe_sender(); |
| 27 ChildThreadImpl::current()->thread_safe_sender()); | |
| 28 ServiceWorkerDispatcher* dispatcher = | 26 ServiceWorkerDispatcher* dispatcher = |
| 29 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(sender_.get()); | 27 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
| 28 thread_safe_sender_.get()); |
| 30 DCHECK(dispatcher); | 29 DCHECK(dispatcher); |
| 31 dispatcher->AddProviderContext(this); | 30 dispatcher->AddProviderContext(this); |
| 32 } | 31 } |
| 33 | 32 |
| 34 ServiceWorkerProviderContext::~ServiceWorkerProviderContext() { | 33 ServiceWorkerProviderContext::~ServiceWorkerProviderContext() { |
| 35 if (ServiceWorkerDispatcher* dispatcher = | 34 if (ServiceWorkerDispatcher* dispatcher = |
| 36 ServiceWorkerDispatcher::GetThreadSpecificInstance()) { | 35 ServiceWorkerDispatcher::GetThreadSpecificInstance()) { |
| 37 // Remove this context from the dispatcher living on the main thread. | 36 // Remove this context from the dispatcher living on the main thread. |
| 38 dispatcher->RemoveProviderContext(this); | 37 dispatcher->RemoveProviderContext(this); |
| 39 } | 38 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 63 | 62 |
| 64 void ServiceWorkerProviderContext::SetVersionAttributes( | 63 void ServiceWorkerProviderContext::SetVersionAttributes( |
| 65 ChangedVersionAttributesMask mask, | 64 ChangedVersionAttributesMask mask, |
| 66 const ServiceWorkerVersionAttributes& attrs) { | 65 const ServiceWorkerVersionAttributes& attrs) { |
| 67 base::AutoLock lock(lock_); | 66 base::AutoLock lock(lock_); |
| 68 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 67 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 69 DCHECK(registration_); | 68 DCHECK(registration_); |
| 70 | 69 |
| 71 if (mask.installing_changed()) { | 70 if (mask.installing_changed()) { |
| 72 installing_ = ServiceWorkerHandleReference::Adopt( | 71 installing_ = ServiceWorkerHandleReference::Adopt( |
| 73 attrs.installing, sender_.get()); | 72 attrs.installing, thread_safe_sender_.get()); |
| 74 } | 73 } |
| 75 if (mask.waiting_changed()) { | 74 if (mask.waiting_changed()) { |
| 76 waiting_ = ServiceWorkerHandleReference::Adopt( | 75 waiting_ = ServiceWorkerHandleReference::Adopt( |
| 77 attrs.waiting, sender_.get()); | 76 attrs.waiting, thread_safe_sender_.get()); |
| 78 } | 77 } |
| 79 if (mask.active_changed()) { | 78 if (mask.active_changed()) { |
| 80 active_ = ServiceWorkerHandleReference::Adopt( | 79 active_ = ServiceWorkerHandleReference::Adopt( |
| 81 attrs.active, sender_.get()); | 80 attrs.active, thread_safe_sender_.get()); |
| 82 } | 81 } |
| 83 } | 82 } |
| 84 | 83 |
| 85 void ServiceWorkerProviderContext::OnAssociateRegistration( | 84 void ServiceWorkerProviderContext::OnAssociateRegistration( |
| 86 const ServiceWorkerRegistrationObjectInfo& info, | 85 const ServiceWorkerRegistrationObjectInfo& info, |
| 87 const ServiceWorkerVersionAttributes& attrs) { | 86 const ServiceWorkerVersionAttributes& attrs) { |
| 88 base::AutoLock lock(lock_); | 87 base::AutoLock lock(lock_); |
| 89 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 88 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 90 DCHECK(!registration_); | 89 DCHECK(!registration_); |
| 91 DCHECK_NE(kInvalidServiceWorkerRegistrationId, info.registration_id); | 90 DCHECK_NE(kInvalidServiceWorkerRegistrationId, info.registration_id); |
| 92 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId, info.handle_id); | 91 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId, info.handle_id); |
| 93 | 92 |
| 94 registration_ = | 93 registration_ = ServiceWorkerRegistrationHandleReference::Adopt( |
| 95 ServiceWorkerRegistrationHandleReference::Adopt(info, sender_.get()); | 94 info, thread_safe_sender_.get()); |
| 96 installing_ = | 95 installing_ = ServiceWorkerHandleReference::Adopt( |
| 97 ServiceWorkerHandleReference::Adopt(attrs.installing, sender_.get()); | 96 attrs.installing, thread_safe_sender_.get()); |
| 98 waiting_ = ServiceWorkerHandleReference::Adopt(attrs.waiting, sender_.get()); | 97 waiting_ = ServiceWorkerHandleReference::Adopt( |
| 99 active_ = ServiceWorkerHandleReference::Adopt(attrs.active, sender_.get()); | 98 attrs.waiting, thread_safe_sender_.get()); |
| 99 active_ = ServiceWorkerHandleReference::Adopt( |
| 100 attrs.active, thread_safe_sender_.get()); |
| 100 } | 101 } |
| 101 | 102 |
| 102 void ServiceWorkerProviderContext::OnDisassociateRegistration() { | 103 void ServiceWorkerProviderContext::OnDisassociateRegistration() { |
| 103 base::AutoLock lock(lock_); | 104 base::AutoLock lock(lock_); |
| 104 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 105 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 105 | 106 |
| 106 controller_.reset(); | 107 controller_.reset(); |
| 107 active_.reset(); | 108 active_.reset(); |
| 108 waiting_.reset(); | 109 waiting_.reset(); |
| 109 installing_.reset(); | 110 installing_.reset(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 136 // when we support navigator.serviceWorker in dedicated workers. | 137 // when we support navigator.serviceWorker in dedicated workers. |
| 137 } | 138 } |
| 138 | 139 |
| 139 void ServiceWorkerProviderContext::OnSetControllerServiceWorker( | 140 void ServiceWorkerProviderContext::OnSetControllerServiceWorker( |
| 140 const ServiceWorkerObjectInfo& info) { | 141 const ServiceWorkerObjectInfo& info) { |
| 141 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 142 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 142 DCHECK(registration_); | 143 DCHECK(registration_); |
| 143 | 144 |
| 144 // This context is is the primary owner of this handle, keeps the | 145 // This context is is the primary owner of this handle, keeps the |
| 145 // initial reference until it goes away. | 146 // initial reference until it goes away. |
| 146 controller_ = ServiceWorkerHandleReference::Adopt(info, sender_.get()); | 147 controller_ = |
| 148 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); |
| 147 | 149 |
| 148 // TODO(kinuko): We can forward the message to other threads here | 150 // TODO(kinuko): We can forward the message to other threads here |
| 149 // when we support navigator.serviceWorker in dedicated workers. | 151 // when we support navigator.serviceWorker in dedicated workers. |
| 150 } | 152 } |
| 151 | 153 |
| 152 int ServiceWorkerProviderContext::installing_handle_id() const { | 154 int ServiceWorkerProviderContext::installing_handle_id() const { |
| 153 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 155 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
| 154 return installing_ ? installing_->info().handle_id | 156 return installing_ ? installing_->info().handle_id |
| 155 : kInvalidServiceWorkerHandleId; | 157 : kInvalidServiceWorkerHandleId; |
| 156 } | 158 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 181 | 183 |
| 182 void ServiceWorkerProviderContext::DestructOnMainThread() const { | 184 void ServiceWorkerProviderContext::DestructOnMainThread() const { |
| 183 if (!main_thread_loop_proxy_->RunsTasksOnCurrentThread() && | 185 if (!main_thread_loop_proxy_->RunsTasksOnCurrentThread() && |
| 184 main_thread_loop_proxy_->DeleteSoon(FROM_HERE, this)) { | 186 main_thread_loop_proxy_->DeleteSoon(FROM_HERE, this)) { |
| 185 return; | 187 return; |
| 186 } | 188 } |
| 187 delete this; | 189 delete this; |
| 188 } | 190 } |
| 189 | 191 |
| 190 } // namespace content | 192 } // namespace content |
| OLD | NEW |