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" |
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_loop_proxy_(base::MessageLoopProxy::current()) { |
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 sender_ = new ServiceWorkerMessageSender( |
| 27 ChildThreadImpl::current()->thread_safe_sender()); |
26 ServiceWorkerDispatcher* dispatcher = | 28 ServiceWorkerDispatcher* dispatcher = |
27 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | 29 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(sender_.get()); |
28 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 } |
(...skipping 23 matching lines...) Expand all Loading... |
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_loop_proxy_->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, 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, 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, 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_loop_proxy_->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_ = |
94 info, thread_safe_sender_.get()); | 95 ServiceWorkerRegistrationHandleReference::Adopt(info, sender_.get()); |
95 installing_ = ServiceWorkerHandleReference::Adopt( | 96 installing_ = |
96 attrs.installing, thread_safe_sender_.get()); | 97 ServiceWorkerHandleReference::Adopt(attrs.installing, sender_.get()); |
97 waiting_ = ServiceWorkerHandleReference::Adopt( | 98 waiting_ = ServiceWorkerHandleReference::Adopt(attrs.waiting, sender_.get()); |
98 attrs.waiting, thread_safe_sender_.get()); | 99 active_ = ServiceWorkerHandleReference::Adopt(attrs.active, sender_.get()); |
99 active_ = ServiceWorkerHandleReference::Adopt( | |
100 attrs.active, thread_safe_sender_.get()); | |
101 } | 100 } |
102 | 101 |
103 void ServiceWorkerProviderContext::OnDisassociateRegistration() { | 102 void ServiceWorkerProviderContext::OnDisassociateRegistration() { |
104 base::AutoLock lock(lock_); | 103 base::AutoLock lock(lock_); |
105 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 104 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
106 | 105 |
107 controller_.reset(); | 106 controller_.reset(); |
108 active_.reset(); | 107 active_.reset(); |
109 waiting_.reset(); | 108 waiting_.reset(); |
110 installing_.reset(); | 109 installing_.reset(); |
(...skipping 26 matching lines...) Expand all Loading... |
137 // when we support navigator.serviceWorker in dedicated workers. | 136 // when we support navigator.serviceWorker in dedicated workers. |
138 } | 137 } |
139 | 138 |
140 void ServiceWorkerProviderContext::OnSetControllerServiceWorker( | 139 void ServiceWorkerProviderContext::OnSetControllerServiceWorker( |
141 const ServiceWorkerObjectInfo& info) { | 140 const ServiceWorkerObjectInfo& info) { |
142 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 141 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
143 DCHECK(registration_); | 142 DCHECK(registration_); |
144 | 143 |
145 // This context is is the primary owner of this handle, keeps the | 144 // This context is is the primary owner of this handle, keeps the |
146 // initial reference until it goes away. | 145 // initial reference until it goes away. |
147 controller_ = | 146 controller_ = ServiceWorkerHandleReference::Adopt(info, sender_.get()); |
148 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | |
149 | 147 |
150 // TODO(kinuko): We can forward the message to other threads here | 148 // TODO(kinuko): We can forward the message to other threads here |
151 // when we support navigator.serviceWorker in dedicated workers. | 149 // when we support navigator.serviceWorker in dedicated workers. |
152 } | 150 } |
153 | 151 |
154 int ServiceWorkerProviderContext::installing_handle_id() const { | 152 int ServiceWorkerProviderContext::installing_handle_id() const { |
155 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); | 153 DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread()); |
156 return installing_ ? installing_->info().handle_id | 154 return installing_ ? installing_->info().handle_id |
157 : kInvalidServiceWorkerHandleId; | 155 : kInvalidServiceWorkerHandleId; |
158 } | 156 } |
(...skipping 24 matching lines...) Expand all Loading... |
183 | 181 |
184 void ServiceWorkerProviderContext::DestructOnMainThread() const { | 182 void ServiceWorkerProviderContext::DestructOnMainThread() const { |
185 if (!main_thread_loop_proxy_->RunsTasksOnCurrentThread() && | 183 if (!main_thread_loop_proxy_->RunsTasksOnCurrentThread() && |
186 main_thread_loop_proxy_->DeleteSoon(FROM_HERE, this)) { | 184 main_thread_loop_proxy_->DeleteSoon(FROM_HERE, this)) { |
187 return; | 185 return; |
188 } | 186 } |
189 delete this; | 187 delete this; |
190 } | 188 } |
191 | 189 |
192 } // namespace content | 190 } // namespace content |
OLD | NEW |