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/service_worker_dispatcher.h" | 5 #include "content/child/service_worker/service_worker_dispatcher.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/threading/thread_local.h" | 9 #include "base/threading/thread_local.h" |
10 #include "content/child/child_thread.h" | 10 #include "content/child/child_thread.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 ServiceWorkerDispatcher* ServiceWorkerDispatcher::GetThreadSpecificInstance() { | 141 ServiceWorkerDispatcher* ServiceWorkerDispatcher::GetThreadSpecificInstance() { |
142 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) | 142 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) |
143 return NULL; | 143 return NULL; |
144 return g_dispatcher_tls.Pointer()->Get(); | 144 return g_dispatcher_tls.Pointer()->Get(); |
145 } | 145 } |
146 | 146 |
147 void ServiceWorkerDispatcher::OnWorkerRunLoopStopped() { | 147 void ServiceWorkerDispatcher::OnWorkerRunLoopStopped() { |
148 delete this; | 148 delete this; |
149 } | 149 } |
150 | 150 |
| 151 WebServiceWorkerImpl* ServiceWorkerDispatcher::GetServiceWorker( |
| 152 const ServiceWorkerObjectInfo& info, |
| 153 bool adopt_handle) { |
| 154 WorkerObjectMap::iterator existing_worker = |
| 155 service_workers_.find(info.handle_id); |
| 156 |
| 157 if (existing_worker != service_workers_.end()) { |
| 158 if (adopt_handle) { |
| 159 // We are instructed to adopt a handle but we already have one, so |
| 160 // adopt and destroy a handle ref. |
| 161 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_); |
| 162 } |
| 163 return existing_worker->second; |
| 164 } |
| 165 |
| 166 scoped_ptr<ServiceWorkerHandleReference> handle_ref = |
| 167 adopt_handle |
| 168 ? ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_) |
| 169 : ServiceWorkerHandleReference::Create(info, thread_safe_sender_); |
| 170 // WebServiceWorkerImpl constructor calls AddServiceWorker. |
| 171 return new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_); |
| 172 } |
| 173 |
151 void ServiceWorkerDispatcher::OnRegistered( | 174 void ServiceWorkerDispatcher::OnRegistered( |
152 int thread_id, | 175 int thread_id, |
153 int request_id, | 176 int request_id, |
154 const ServiceWorkerObjectInfo& info) { | 177 const ServiceWorkerObjectInfo& info) { |
155 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks = | 178 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks = |
156 pending_callbacks_.Lookup(request_id); | 179 pending_callbacks_.Lookup(request_id); |
157 DCHECK(callbacks); | 180 DCHECK(callbacks); |
158 if (!callbacks) | 181 if (!callbacks) |
159 return; | 182 return; |
160 | 183 |
161 // The browser has to generate the registration_id so the same | 184 callbacks->onSuccess(GetServiceWorker(info, true)); |
162 // worker can be called from different renderer contexts. However, | |
163 // the impl object doesn't have to be the same instance across calls | |
164 // unless we require the DOM objects to be identical when there's a | |
165 // duplicate registration. So for now we mint a new object each | |
166 // time. | |
167 // | |
168 // WebServiceWorkerImpl's ctor internally calls AddServiceWorker. | |
169 scoped_ptr<WebServiceWorkerImpl> worker( | |
170 new WebServiceWorkerImpl(info, thread_safe_sender_)); | |
171 callbacks->onSuccess(worker.release()); | |
172 pending_callbacks_.Remove(request_id); | 185 pending_callbacks_.Remove(request_id); |
173 } | 186 } |
174 | 187 |
175 void ServiceWorkerDispatcher::OnUnregistered( | 188 void ServiceWorkerDispatcher::OnUnregistered( |
176 int thread_id, | 189 int thread_id, |
177 int request_id) { | 190 int request_id) { |
178 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks = | 191 WebServiceWorkerProvider::WebServiceWorkerCallbacks* callbacks = |
179 pending_callbacks_.Lookup(request_id); | 192 pending_callbacks_.Lookup(request_id); |
180 DCHECK(callbacks); | 193 DCHECK(callbacks); |
181 if (!callbacks) | 194 if (!callbacks) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 const ServiceWorkerObjectInfo& info) { | 234 const ServiceWorkerObjectInfo& info) { |
222 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 235 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
223 if (provider != provider_contexts_.end()) { | 236 if (provider != provider_contexts_.end()) { |
224 provider->second->OnSetCurrentServiceWorker(provider_id, info); | 237 provider->second->OnSetCurrentServiceWorker(provider_id, info); |
225 worker_to_provider_[info.handle_id] = provider->second; | 238 worker_to_provider_[info.handle_id] = provider->second; |
226 } | 239 } |
227 | 240 |
228 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 241 ScriptClientMap::iterator found = script_clients_.find(provider_id); |
229 if (found != script_clients_.end()) { | 242 if (found != script_clients_.end()) { |
230 // Populate the .current field with the new worker object. | 243 // Populate the .current field with the new worker object. |
231 scoped_ptr<ServiceWorkerHandleReference> handle_ref( | 244 found->second->setCurrentServiceWorker(GetServiceWorker(info, false)); |
232 ServiceWorkerHandleReference::Create(info, thread_safe_sender_)); | |
233 found->second->setCurrentServiceWorker( | |
234 new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_)); | |
235 } | 245 } |
236 } | 246 } |
237 | 247 |
238 void ServiceWorkerDispatcher::OnPostMessage( | 248 void ServiceWorkerDispatcher::OnPostMessage( |
239 int thread_id, | 249 int thread_id, |
240 int provider_id, | 250 int provider_id, |
241 const base::string16& message, | 251 const base::string16& message, |
242 const std::vector<int>& sent_message_port_ids, | 252 const std::vector<int>& sent_message_port_ids, |
243 const std::vector<int>& new_routing_ids) { | 253 const std::vector<int>& new_routing_ids) { |
244 // Make sure we're on the main document thread. (That must be the only | 254 // Make sure we're on the main document thread. (That must be the only |
(...skipping 25 matching lines...) Expand all Loading... |
270 DCHECK(!ContainsKey(service_workers_, handle_id)); | 280 DCHECK(!ContainsKey(service_workers_, handle_id)); |
271 service_workers_[handle_id] = worker; | 281 service_workers_[handle_id] = worker; |
272 } | 282 } |
273 | 283 |
274 void ServiceWorkerDispatcher::RemoveServiceWorker(int handle_id) { | 284 void ServiceWorkerDispatcher::RemoveServiceWorker(int handle_id) { |
275 DCHECK(ContainsKey(service_workers_, handle_id)); | 285 DCHECK(ContainsKey(service_workers_, handle_id)); |
276 service_workers_.erase(handle_id); | 286 service_workers_.erase(handle_id); |
277 } | 287 } |
278 | 288 |
279 } // namespace content | 289 } // namespace content |
OLD | NEW |