| 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/location.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 10 #include "content/child/child_thread_impl.h" | 11 #include "content/child/child_thread_impl.h" |
| 11 #include "content/child/service_worker/service_worker_dispatcher.h" | 12 #include "content/child/service_worker/service_worker_dispatcher.h" |
| 12 #include "content/child/service_worker/service_worker_handle_reference.h" | 13 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 13 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" | 14 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" |
| 14 #include "content/child/thread_safe_sender.h" | 15 #include "content/child/thread_safe_sender.h" |
| 15 #include "content/child/worker_task_runner.h" | 16 #include "content/child/worker_task_runner.h" |
| 16 #include "content/common/service_worker/service_worker_messages.h" | 17 #include "content/common/service_worker/service_worker_messages.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 ServiceWorkerProviderContext::ServiceWorkerProviderContext(int provider_id) | 21 ServiceWorkerProviderContext::ServiceWorkerProviderContext(int provider_id) |
| 21 : provider_id_(provider_id), | 22 : provider_id_(provider_id), |
| 22 main_thread_loop_proxy_(base::MessageLoopProxy::current()) { | 23 main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 23 if (!ChildThreadImpl::current()) | 24 if (!ChildThreadImpl::current()) |
| 24 return; // May be null in some tests. | 25 return; // May be null in some tests. |
| 25 thread_safe_sender_ = ChildThreadImpl::current()->thread_safe_sender(); | 26 thread_safe_sender_ = ChildThreadImpl::current()->thread_safe_sender(); |
| 26 ServiceWorkerDispatcher* dispatcher = | 27 ServiceWorkerDispatcher* dispatcher = |
| 27 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | 28 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
| 28 thread_safe_sender_.get()); | 29 thread_safe_sender_.get()); |
| 29 DCHECK(dispatcher); | 30 DCHECK(dispatcher); |
| 30 dispatcher->AddProviderContext(this); | 31 dispatcher->AddProviderContext(this); |
| 31 } | 32 } |
| 32 | 33 |
| 33 ServiceWorkerProviderContext::~ServiceWorkerProviderContext() { | 34 ServiceWorkerProviderContext::~ServiceWorkerProviderContext() { |
| 34 if (ServiceWorkerDispatcher* dispatcher = | 35 if (ServiceWorkerDispatcher* dispatcher = |
| 35 ServiceWorkerDispatcher::GetThreadSpecificInstance()) { | 36 ServiceWorkerDispatcher::GetThreadSpecificInstance()) { |
| 36 // Remove this context from the dispatcher living on the main thread. | 37 // Remove this context from the dispatcher living on the main thread. |
| 37 dispatcher->RemoveProviderContext(this); | 38 dispatcher->RemoveProviderContext(this); |
| 38 } | 39 } |
| 39 } | 40 } |
| 40 | 41 |
| 41 ServiceWorkerHandleReference* ServiceWorkerProviderContext::controller() { | 42 ServiceWorkerHandleReference* ServiceWorkerProviderContext::controller() { |
| 42 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 43 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 43 return controller_.get(); | 44 return controller_.get(); |
| 44 } | 45 } |
| 45 | 46 |
| 46 bool ServiceWorkerProviderContext::GetRegistrationInfoAndVersionAttributes( | 47 bool ServiceWorkerProviderContext::GetRegistrationInfoAndVersionAttributes( |
| 47 ServiceWorkerRegistrationObjectInfo* info, | 48 ServiceWorkerRegistrationObjectInfo* info, |
| 48 ServiceWorkerVersionAttributes* attrs) { | 49 ServiceWorkerVersionAttributes* attrs) { |
| 49 base::AutoLock lock(lock_); | 50 base::AutoLock lock(lock_); |
| 50 if (!registration_) | 51 if (!registration_) |
| 51 return false; | 52 return false; |
| 52 | 53 |
| 53 *info = registration_->info(); | 54 *info = registration_->info(); |
| 54 if (installing_) | 55 if (installing_) |
| 55 attrs->installing = installing_->info(); | 56 attrs->installing = installing_->info(); |
| 56 if (waiting_) | 57 if (waiting_) |
| 57 attrs->waiting = waiting_->info(); | 58 attrs->waiting = waiting_->info(); |
| 58 if (active_) | 59 if (active_) |
| 59 attrs->active = active_->info(); | 60 attrs->active = active_->info(); |
| 60 return true; | 61 return true; |
| 61 } | 62 } |
| 62 | 63 |
| 63 void ServiceWorkerProviderContext::SetVersionAttributes( | 64 void ServiceWorkerProviderContext::SetVersionAttributes( |
| 64 ChangedVersionAttributesMask mask, | 65 ChangedVersionAttributesMask mask, |
| 65 const ServiceWorkerVersionAttributes& attrs) { | 66 const ServiceWorkerVersionAttributes& attrs) { |
| 66 base::AutoLock lock(lock_); | 67 base::AutoLock lock(lock_); |
| 67 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 68 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 68 DCHECK(registration_); | 69 DCHECK(registration_); |
| 69 | 70 |
| 70 if (mask.installing_changed()) { | 71 if (mask.installing_changed()) { |
| 71 installing_ = ServiceWorkerHandleReference::Adopt( | 72 installing_ = ServiceWorkerHandleReference::Adopt( |
| 72 attrs.installing, thread_safe_sender_.get()); | 73 attrs.installing, thread_safe_sender_.get()); |
| 73 } | 74 } |
| 74 if (mask.waiting_changed()) { | 75 if (mask.waiting_changed()) { |
| 75 waiting_ = ServiceWorkerHandleReference::Adopt( | 76 waiting_ = ServiceWorkerHandleReference::Adopt( |
| 76 attrs.waiting, thread_safe_sender_.get()); | 77 attrs.waiting, thread_safe_sender_.get()); |
| 77 } | 78 } |
| 78 if (mask.active_changed()) { | 79 if (mask.active_changed()) { |
| 79 active_ = ServiceWorkerHandleReference::Adopt( | 80 active_ = ServiceWorkerHandleReference::Adopt( |
| 80 attrs.active, thread_safe_sender_.get()); | 81 attrs.active, thread_safe_sender_.get()); |
| 81 } | 82 } |
| 82 } | 83 } |
| 83 | 84 |
| 84 void ServiceWorkerProviderContext::OnAssociateRegistration( | 85 void ServiceWorkerProviderContext::OnAssociateRegistration( |
| 85 const ServiceWorkerRegistrationObjectInfo& info, | 86 const ServiceWorkerRegistrationObjectInfo& info, |
| 86 const ServiceWorkerVersionAttributes& attrs) { | 87 const ServiceWorkerVersionAttributes& attrs) { |
| 87 base::AutoLock lock(lock_); | 88 base::AutoLock lock(lock_); |
| 88 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 89 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 89 DCHECK(!registration_); | 90 DCHECK(!registration_); |
| 90 DCHECK_NE(kInvalidServiceWorkerRegistrationId, info.registration_id); | 91 DCHECK_NE(kInvalidServiceWorkerRegistrationId, info.registration_id); |
| 91 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId, info.handle_id); | 92 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId, info.handle_id); |
| 92 | 93 |
| 93 registration_ = ServiceWorkerRegistrationHandleReference::Adopt( | 94 registration_ = ServiceWorkerRegistrationHandleReference::Adopt( |
| 94 info, thread_safe_sender_.get()); | 95 info, thread_safe_sender_.get()); |
| 95 installing_ = ServiceWorkerHandleReference::Adopt( | 96 installing_ = ServiceWorkerHandleReference::Adopt( |
| 96 attrs.installing, thread_safe_sender_.get()); | 97 attrs.installing, thread_safe_sender_.get()); |
| 97 waiting_ = ServiceWorkerHandleReference::Adopt( | 98 waiting_ = ServiceWorkerHandleReference::Adopt( |
| 98 attrs.waiting, thread_safe_sender_.get()); | 99 attrs.waiting, thread_safe_sender_.get()); |
| 99 active_ = ServiceWorkerHandleReference::Adopt( | 100 active_ = ServiceWorkerHandleReference::Adopt( |
| 100 attrs.active, thread_safe_sender_.get()); | 101 attrs.active, thread_safe_sender_.get()); |
| 101 } | 102 } |
| 102 | 103 |
| 103 void ServiceWorkerProviderContext::OnDisassociateRegistration() { | 104 void ServiceWorkerProviderContext::OnDisassociateRegistration() { |
| 104 base::AutoLock lock(lock_); | 105 base::AutoLock lock(lock_); |
| 105 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 106 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 106 | 107 |
| 107 controller_.reset(); | 108 controller_.reset(); |
| 108 active_.reset(); | 109 active_.reset(); |
| 109 waiting_.reset(); | 110 waiting_.reset(); |
| 110 installing_.reset(); | 111 installing_.reset(); |
| 111 registration_.reset(); | 112 registration_.reset(); |
| 112 } | 113 } |
| 113 | 114 |
| 114 void ServiceWorkerProviderContext::OnServiceWorkerStateChanged( | 115 void ServiceWorkerProviderContext::OnServiceWorkerStateChanged( |
| 115 int handle_id, | 116 int handle_id, |
| 116 blink::WebServiceWorkerState state) { | 117 blink::WebServiceWorkerState state) { |
| 117 base::AutoLock lock(lock_); | 118 base::AutoLock lock(lock_); |
| 118 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 119 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 119 | 120 |
| 120 ServiceWorkerHandleReference* which = NULL; | 121 ServiceWorkerHandleReference* which = NULL; |
| 121 if (handle_id == controller_handle_id()) | 122 if (handle_id == controller_handle_id()) |
| 122 which = controller_.get(); | 123 which = controller_.get(); |
| 123 else if (handle_id == active_handle_id()) | 124 else if (handle_id == active_handle_id()) |
| 124 which = active_.get(); | 125 which = active_.get(); |
| 125 else if (handle_id == waiting_handle_id()) | 126 else if (handle_id == waiting_handle_id()) |
| 126 which = waiting_.get(); | 127 which = waiting_.get(); |
| 127 else if (handle_id == installing_handle_id()) | 128 else if (handle_id == installing_handle_id()) |
| 128 which = installing_.get(); | 129 which = installing_.get(); |
| 129 | 130 |
| 130 // We should only get messages for ServiceWorkers associated with | 131 // We should only get messages for ServiceWorkers associated with |
| 131 // this provider. | 132 // this provider. |
| 132 DCHECK(which); | 133 DCHECK(which); |
| 133 | 134 |
| 134 which->set_state(state); | 135 which->set_state(state); |
| 135 | 136 |
| 136 // TODO(kinuko): We can forward the message to other threads here | 137 // TODO(kinuko): We can forward the message to other threads here |
| 137 // when we support navigator.serviceWorker in dedicated workers. | 138 // when we support navigator.serviceWorker in dedicated workers. |
| 138 } | 139 } |
| 139 | 140 |
| 140 void ServiceWorkerProviderContext::OnSetControllerServiceWorker( | 141 void ServiceWorkerProviderContext::OnSetControllerServiceWorker( |
| 141 const ServiceWorkerObjectInfo& info) { | 142 const ServiceWorkerObjectInfo& info) { |
| 142 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 143 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 143 DCHECK(registration_); | 144 DCHECK(registration_); |
| 144 | 145 |
| 145 // This context is is the primary owner of this handle, keeps the | 146 // This context is is the primary owner of this handle, keeps the |
| 146 // initial reference until it goes away. | 147 // initial reference until it goes away. |
| 147 controller_ = | 148 controller_ = |
| 148 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | 149 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); |
| 149 | 150 |
| 150 // TODO(kinuko): We can forward the message to other threads here | 151 // TODO(kinuko): We can forward the message to other threads here |
| 151 // when we support navigator.serviceWorker in dedicated workers. | 152 // when we support navigator.serviceWorker in dedicated workers. |
| 152 } | 153 } |
| 153 | 154 |
| 154 int ServiceWorkerProviderContext::installing_handle_id() const { | 155 int ServiceWorkerProviderContext::installing_handle_id() const { |
| 155 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 156 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 156 return installing_ ? installing_->info().handle_id | 157 return installing_ ? installing_->info().handle_id |
| 157 : kInvalidServiceWorkerHandleId; | 158 : kInvalidServiceWorkerHandleId; |
| 158 } | 159 } |
| 159 | 160 |
| 160 int ServiceWorkerProviderContext::waiting_handle_id() const { | 161 int ServiceWorkerProviderContext::waiting_handle_id() const { |
| 161 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 162 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 162 return waiting_ ? waiting_->info().handle_id | 163 return waiting_ ? waiting_->info().handle_id |
| 163 : kInvalidServiceWorkerHandleId; | 164 : kInvalidServiceWorkerHandleId; |
| 164 } | 165 } |
| 165 | 166 |
| 166 int ServiceWorkerProviderContext::active_handle_id() const { | 167 int ServiceWorkerProviderContext::active_handle_id() const { |
| 167 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 168 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 168 return active_ ? active_->info().handle_id | 169 return active_ ? active_->info().handle_id |
| 169 : kInvalidServiceWorkerHandleId; | 170 : kInvalidServiceWorkerHandleId; |
| 170 } | 171 } |
| 171 | 172 |
| 172 int ServiceWorkerProviderContext::controller_handle_id() const { | 173 int ServiceWorkerProviderContext::controller_handle_id() const { |
| 173 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 174 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 174 return controller_ ? controller_->info().handle_id | 175 return controller_ ? controller_->info().handle_id |
| 175 : kInvalidServiceWorkerHandleId; | 176 : kInvalidServiceWorkerHandleId; |
| 176 } | 177 } |
| 177 | 178 |
| 178 int ServiceWorkerProviderContext::registration_handle_id() const { | 179 int ServiceWorkerProviderContext::registration_handle_id() const { |
| 179 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 180 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 180 return registration_ ? registration_->info().handle_id | 181 return registration_ ? registration_->info().handle_id |
| 181 : kInvalidServiceWorkerRegistrationHandleId; | 182 : kInvalidServiceWorkerRegistrationHandleId; |
| 182 } | 183 } |
| 183 | 184 |
| 184 void ServiceWorkerProviderContext::DestructOnMainThread() const { | 185 void ServiceWorkerProviderContext::DestructOnMainThread() const { |
| 185 if (!main_thread_loop_proxy_->RunsTasksOnCurrentThread() && | 186 if (!main_thread_task_runner_->RunsTasksOnCurrentThread() && |
| 186 main_thread_loop_proxy_->DeleteSoon(FROM_HERE, this)) { | 187 main_thread_task_runner_->DeleteSoon(FROM_HERE, this)) { |
| 187 return; | 188 return; |
| 188 } | 189 } |
| 189 delete this; | 190 delete this; |
| 190 } | 191 } |
| 191 | 192 |
| 192 } // namespace content | 193 } // namespace content |
| OLD | NEW |