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" |
11 #include "content/child/service_worker/service_worker_handle_reference.h" | 11 #include "content/child/service_worker/service_worker_handle_reference.h" |
12 #include "content/child/service_worker/service_worker_provider_context.h" | 12 #include "content/child/service_worker/service_worker_provider_context.h" |
| 13 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" |
13 #include "content/child/service_worker/web_service_worker_impl.h" | 14 #include "content/child/service_worker/web_service_worker_impl.h" |
14 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 15 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
15 #include "content/child/thread_safe_sender.h" | 16 #include "content/child/thread_safe_sender.h" |
16 #include "content/child/webmessageportchannel_impl.h" | 17 #include "content/child/webmessageportchannel_impl.h" |
17 #include "content/common/service_worker/service_worker_messages.h" | 18 #include "content/common/service_worker/service_worker_messages.h" |
18 #include "content/public/common/url_utils.h" | 19 #include "content/public/common/url_utils.h" |
19 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h" | 20 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h" |
20 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 21 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
21 | 22 |
22 using blink::WebServiceWorkerError; | 23 using blink::WebServiceWorkerError; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 } | 196 } |
196 | 197 |
197 scoped_ptr<ServiceWorkerHandleReference> handle_ref = | 198 scoped_ptr<ServiceWorkerHandleReference> handle_ref = |
198 adopt_handle | 199 adopt_handle |
199 ? ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_) | 200 ? ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_) |
200 : ServiceWorkerHandleReference::Create(info, thread_safe_sender_); | 201 : ServiceWorkerHandleReference::Create(info, thread_safe_sender_); |
201 // WebServiceWorkerImpl constructor calls AddServiceWorker. | 202 // WebServiceWorkerImpl constructor calls AddServiceWorker. |
202 return new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_); | 203 return new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_); |
203 } | 204 } |
204 | 205 |
| 206 WebServiceWorkerRegistrationImpl* |
| 207 ServiceWorkerDispatcher::GetServiceWorkerRegistration( |
| 208 int registration_handle_id, |
| 209 const ServiceWorkerObjectInfo& info, |
| 210 bool adopt_handle) { |
| 211 if (registration_handle_id == kInvalidServiceWorkerRegistrationHandleId) |
| 212 return NULL; |
| 213 |
| 214 RegistrationObjectMap::iterator existing_registration = |
| 215 registrations_.find(registration_handle_id); |
| 216 |
| 217 if (existing_registration != registrations_.end()) { |
| 218 if (adopt_handle) { |
| 219 // We are instructed to adopt a handle but we already have one, so |
| 220 // adopt and destroy a handle ref. |
| 221 ServiceWorkerRegistrationHandleReference::Adopt( |
| 222 registration_handle_id, info, thread_safe_sender_); |
| 223 } |
| 224 return existing_registration->second; |
| 225 } |
| 226 |
| 227 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref = |
| 228 adopt_handle |
| 229 ? ServiceWorkerRegistrationHandleReference::Adopt( |
| 230 registration_handle_id, info, thread_safe_sender_) |
| 231 : ServiceWorkerRegistrationHandleReference::Create( |
| 232 registration_handle_id, info, thread_safe_sender_); |
| 233 |
| 234 // WebServiceWorkerRegistrationImpl constructor calls |
| 235 // AddServiceWorkerRegistration. |
| 236 return new WebServiceWorkerRegistrationImpl(handle_ref.Pass()); |
| 237 } |
| 238 |
205 void ServiceWorkerDispatcher::OnRegistered( | 239 void ServiceWorkerDispatcher::OnRegistered( |
206 int thread_id, | 240 int thread_id, |
207 int request_id, | 241 int request_id, |
| 242 int registration_handle_id, |
208 const ServiceWorkerObjectInfo& info) { | 243 const ServiceWorkerObjectInfo& info) { |
209 WebServiceWorkerRegistrationCallbacks* callbacks = | 244 WebServiceWorkerRegistrationCallbacks* callbacks = |
210 pending_callbacks_.Lookup(request_id); | 245 pending_callbacks_.Lookup(request_id); |
211 DCHECK(callbacks); | 246 DCHECK(callbacks); |
212 if (!callbacks) | 247 if (!callbacks) |
213 return; | 248 return; |
214 | 249 |
215 #ifdef DISABLE_SERVICE_WORKER_REGISTRATION | 250 #ifdef DISABLE_SERVICE_WORKER_REGISTRATION |
216 callbacks->onSuccess(GetServiceWorker(info, true)); | 251 callbacks->onSuccess(GetServiceWorker(info, true)); |
| 252 // We should adopt and destroy an unused handle ref. |
| 253 ServiceWorkerRegistrationHandleReference::Adopt( |
| 254 registration_handle_id, info, thread_safe_sender_); |
217 #else | 255 #else |
218 callbacks->onSuccess(new WebServiceWorkerRegistrationImpl(info)); | 256 callbacks->onSuccess(GetServiceWorkerRegistration( |
| 257 registration_handle_id, info, true)); |
| 258 // We should adopt and destroy an unused handle ref. |
| 259 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_); |
219 #endif | 260 #endif |
220 pending_callbacks_.Remove(request_id); | 261 pending_callbacks_.Remove(request_id); |
221 } | 262 } |
222 | 263 |
223 void ServiceWorkerDispatcher::OnUnregistered( | 264 void ServiceWorkerDispatcher::OnUnregistered( |
224 int thread_id, | 265 int thread_id, |
225 int request_id) { | 266 int request_id) { |
226 WebServiceWorkerRegistrationCallbacks* callbacks = | 267 WebServiceWorkerRegistrationCallbacks* callbacks = |
227 pending_callbacks_.Lookup(request_id); | 268 pending_callbacks_.Lookup(request_id); |
228 DCHECK(callbacks); | 269 DCHECK(callbacks); |
(...skipping 30 matching lines...) Expand all Loading... |
259 worker->second->OnStateChanged(state); | 300 worker->second->OnStateChanged(state); |
260 | 301 |
261 WorkerToProviderMap::iterator provider = worker_to_provider_.find(handle_id); | 302 WorkerToProviderMap::iterator provider = worker_to_provider_.find(handle_id); |
262 if (provider != worker_to_provider_.end()) | 303 if (provider != worker_to_provider_.end()) |
263 provider->second->OnServiceWorkerStateChanged(handle_id, state); | 304 provider->second->OnServiceWorkerStateChanged(handle_id, state); |
264 } | 305 } |
265 | 306 |
266 void ServiceWorkerDispatcher::OnSetVersionAttributes( | 307 void ServiceWorkerDispatcher::OnSetVersionAttributes( |
267 int thread_id, | 308 int thread_id, |
268 int provider_id, | 309 int provider_id, |
| 310 int registration_handle_id, |
269 int changed_mask, | 311 int changed_mask, |
270 const ServiceWorkerVersionAttributes& attributes) { | 312 const ServiceWorkerVersionAttributes& attributes) { |
271 ChangedVersionAttributesMask mask(changed_mask); | 313 ChangedVersionAttributesMask mask(changed_mask); |
272 if (mask.installing_changed()) | 314 if (mask.installing_changed()) { |
273 SetInstallingServiceWorker(provider_id, attributes.installing); | 315 SetInstallingServiceWorker(provider_id, |
274 if (mask.waiting_changed()) | 316 registration_handle_id, |
275 SetWaitingServiceWorker(provider_id, attributes.waiting); | 317 attributes.installing); |
276 if (mask.active_changed()) | 318 } |
277 SetActiveServiceWorker(provider_id, attributes.active); | 319 if (mask.waiting_changed()) { |
| 320 SetWaitingServiceWorker(provider_id, |
| 321 registration_handle_id, |
| 322 attributes.waiting); |
| 323 } |
| 324 if (mask.active_changed()) { |
| 325 SetActiveServiceWorker(provider_id, |
| 326 registration_handle_id, |
| 327 attributes.active); |
| 328 } |
278 } | 329 } |
279 | 330 |
280 void ServiceWorkerDispatcher::SetInstallingServiceWorker( | 331 void ServiceWorkerDispatcher::SetInstallingServiceWorker( |
281 int provider_id, | 332 int provider_id, |
| 333 int registration_handle_id, |
282 const ServiceWorkerObjectInfo& info) { | 334 const ServiceWorkerObjectInfo& info) { |
283 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 335 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
284 if (provider != provider_contexts_.end()) { | 336 if (provider != provider_contexts_.end()) { |
285 int existing_installing_id = provider->second->installing_handle_id(); | 337 int existing_installing_id = provider->second->installing_handle_id(); |
286 if (existing_installing_id != info.handle_id && | 338 if (existing_installing_id != info.handle_id && |
287 existing_installing_id != kInvalidServiceWorkerHandleId) { | 339 existing_installing_id != kInvalidServiceWorkerHandleId) { |
288 WorkerToProviderMap::iterator associated_provider = | 340 WorkerToProviderMap::iterator associated_provider = |
289 worker_to_provider_.find(existing_installing_id); | 341 worker_to_provider_.find(existing_installing_id); |
290 DCHECK(associated_provider != worker_to_provider_.end()); | 342 DCHECK(associated_provider != worker_to_provider_.end()); |
291 DCHECK(associated_provider->second->provider_id() == provider_id); | 343 DCHECK(associated_provider->second->provider_id() == provider_id); |
292 worker_to_provider_.erase(associated_provider); | 344 worker_to_provider_.erase(associated_provider); |
293 } | 345 } |
294 provider->second->OnSetInstallingServiceWorker(provider_id, info); | 346 provider->second->OnSetInstallingServiceWorker(provider_id, info); |
295 if (info.handle_id != kInvalidServiceWorkerHandleId) | 347 if (info.handle_id != kInvalidServiceWorkerHandleId) |
296 worker_to_provider_[info.handle_id] = provider->second; | 348 worker_to_provider_[info.handle_id] = provider->second; |
297 } | 349 } |
298 | 350 |
| 351 #ifdef DISABLE_SERVICE_WORKER_REGISTRATION |
299 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 352 ScriptClientMap::iterator found = script_clients_.find(provider_id); |
300 if (found != script_clients_.end()) { | 353 if (found != script_clients_.end()) { |
301 // Populate the .installing field with the new worker object. | 354 // Populate the .installing field with the new worker object. |
302 found->second->setInstalling(GetServiceWorker(info, false)); | 355 found->second->setInstalling(GetServiceWorker(info, false)); |
303 } | 356 } |
| 357 #else |
| 358 RegistrationObjectMap::iterator found = |
| 359 registrations_.find(registration_handle_id); |
| 360 if (found != registrations_.end()) { |
| 361 found->second->setInstalling(GetServiceWorker(info, false)); |
| 362 if (info.handle_id != kInvalidServiceWorkerHandleId) |
| 363 found->second->OnUpdateFound(); |
| 364 } |
| 365 #endif |
304 } | 366 } |
305 | 367 |
306 void ServiceWorkerDispatcher::SetWaitingServiceWorker( | 368 void ServiceWorkerDispatcher::SetWaitingServiceWorker( |
307 int provider_id, | 369 int provider_id, |
| 370 int registration_handle_id, |
308 const ServiceWorkerObjectInfo& info) { | 371 const ServiceWorkerObjectInfo& info) { |
309 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 372 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
310 if (provider != provider_contexts_.end()) { | 373 if (provider != provider_contexts_.end()) { |
311 int existing_waiting_id = provider->second->waiting_handle_id(); | 374 int existing_waiting_id = provider->second->waiting_handle_id(); |
312 if (existing_waiting_id != info.handle_id && | 375 if (existing_waiting_id != info.handle_id && |
313 existing_waiting_id != kInvalidServiceWorkerHandleId) { | 376 existing_waiting_id != kInvalidServiceWorkerHandleId) { |
314 WorkerToProviderMap::iterator associated_provider = | 377 WorkerToProviderMap::iterator associated_provider = |
315 worker_to_provider_.find(existing_waiting_id); | 378 worker_to_provider_.find(existing_waiting_id); |
316 DCHECK(associated_provider != worker_to_provider_.end()); | 379 DCHECK(associated_provider != worker_to_provider_.end()); |
317 DCHECK(associated_provider->second->provider_id() == provider_id); | 380 DCHECK(associated_provider->second->provider_id() == provider_id); |
318 worker_to_provider_.erase(associated_provider); | 381 worker_to_provider_.erase(associated_provider); |
319 } | 382 } |
320 provider->second->OnSetWaitingServiceWorker(provider_id, info); | 383 provider->second->OnSetWaitingServiceWorker(provider_id, info); |
321 if (info.handle_id != kInvalidServiceWorkerHandleId) | 384 if (info.handle_id != kInvalidServiceWorkerHandleId) |
322 worker_to_provider_[info.handle_id] = provider->second; | 385 worker_to_provider_[info.handle_id] = provider->second; |
323 } | 386 } |
324 | 387 |
| 388 #ifdef DISABLE_SERVICE_WORKER_REGISTRATION |
325 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 389 ScriptClientMap::iterator found = script_clients_.find(provider_id); |
326 if (found != script_clients_.end()) { | 390 if (found != script_clients_.end()) { |
327 // Populate the .waiting field with the new worker object. | 391 // Populate the .waiting field with the new worker object. |
328 found->second->setWaiting(GetServiceWorker(info, false)); | 392 found->second->setWaiting(GetServiceWorker(info, false)); |
329 } | 393 } |
| 394 #else |
| 395 RegistrationObjectMap::iterator found = |
| 396 registrations_.find(registration_handle_id); |
| 397 if (found != registrations_.end()) |
| 398 found->second->setWaiting(GetServiceWorker(info, false)); |
| 399 #endif |
330 } | 400 } |
331 | 401 |
332 void ServiceWorkerDispatcher::SetActiveServiceWorker( | 402 void ServiceWorkerDispatcher::SetActiveServiceWorker( |
333 int provider_id, | 403 int provider_id, |
| 404 int registration_handle_id, |
334 const ServiceWorkerObjectInfo& info) { | 405 const ServiceWorkerObjectInfo& info) { |
335 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 406 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
336 if (provider != provider_contexts_.end()) { | 407 if (provider != provider_contexts_.end()) { |
337 int existing_active_id = provider->second->active_handle_id(); | 408 int existing_active_id = provider->second->active_handle_id(); |
338 if (existing_active_id != info.handle_id && | 409 if (existing_active_id != info.handle_id && |
339 existing_active_id != kInvalidServiceWorkerHandleId) { | 410 existing_active_id != kInvalidServiceWorkerHandleId) { |
340 WorkerToProviderMap::iterator associated_provider = | 411 WorkerToProviderMap::iterator associated_provider = |
341 worker_to_provider_.find(existing_active_id); | 412 worker_to_provider_.find(existing_active_id); |
342 DCHECK(associated_provider != worker_to_provider_.end()); | 413 DCHECK(associated_provider != worker_to_provider_.end()); |
343 DCHECK(associated_provider->second->provider_id() == provider_id); | 414 DCHECK(associated_provider->second->provider_id() == provider_id); |
344 worker_to_provider_.erase(associated_provider); | 415 worker_to_provider_.erase(associated_provider); |
345 } | 416 } |
346 provider->second->OnSetActiveServiceWorker(provider_id, info); | 417 provider->second->OnSetActiveServiceWorker(provider_id, info); |
347 if (info.handle_id != kInvalidServiceWorkerHandleId) | 418 if (info.handle_id != kInvalidServiceWorkerHandleId) |
348 worker_to_provider_[info.handle_id] = provider->second; | 419 worker_to_provider_[info.handle_id] = provider->second; |
349 } | 420 } |
350 | 421 |
| 422 #ifdef DISABLE_SERVICE_WORKER_REGISTRATION |
351 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 423 ScriptClientMap::iterator found = script_clients_.find(provider_id); |
352 if (found != script_clients_.end()) { | 424 if (found != script_clients_.end()) { |
353 // Populate the .active field with the new worker object. | 425 // Populate the .active field with the new worker object. |
354 found->second->setActive(GetServiceWorker(info, false)); | 426 found->second->setActive(GetServiceWorker(info, false)); |
355 } | 427 } |
| 428 #else |
| 429 RegistrationObjectMap::iterator found = |
| 430 registrations_.find(registration_handle_id); |
| 431 if (found != registrations_.end()) |
| 432 found->second->setActive(GetServiceWorker(info, false)); |
| 433 #endif |
356 } | 434 } |
357 | 435 |
358 void ServiceWorkerDispatcher::OnSetControllerServiceWorker( | 436 void ServiceWorkerDispatcher::OnSetControllerServiceWorker( |
359 int thread_id, | 437 int thread_id, |
360 int provider_id, | 438 int provider_id, |
361 const ServiceWorkerObjectInfo& info) { | 439 const ServiceWorkerObjectInfo& info) { |
362 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 440 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
363 if (provider != provider_contexts_.end()) { | 441 if (provider != provider_contexts_.end()) { |
364 provider->second->OnSetControllerServiceWorker(provider_id, info); | 442 provider->second->OnSetControllerServiceWorker(provider_id, info); |
365 worker_to_provider_[info.handle_id] = provider->second; | 443 worker_to_provider_[info.handle_id] = provider->second; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 int handle_id, WebServiceWorkerImpl* worker) { | 484 int handle_id, WebServiceWorkerImpl* worker) { |
407 DCHECK(!ContainsKey(service_workers_, handle_id)); | 485 DCHECK(!ContainsKey(service_workers_, handle_id)); |
408 service_workers_[handle_id] = worker; | 486 service_workers_[handle_id] = worker; |
409 } | 487 } |
410 | 488 |
411 void ServiceWorkerDispatcher::RemoveServiceWorker(int handle_id) { | 489 void ServiceWorkerDispatcher::RemoveServiceWorker(int handle_id) { |
412 DCHECK(ContainsKey(service_workers_, handle_id)); | 490 DCHECK(ContainsKey(service_workers_, handle_id)); |
413 service_workers_.erase(handle_id); | 491 service_workers_.erase(handle_id); |
414 } | 492 } |
415 | 493 |
| 494 void ServiceWorkerDispatcher::AddServiceWorkerRegistration( |
| 495 int registration_handle_id, |
| 496 WebServiceWorkerRegistrationImpl* registration) { |
| 497 DCHECK(!ContainsKey(registrations_, registration_handle_id)); |
| 498 registrations_[registration_handle_id] = registration; |
| 499 } |
| 500 |
| 501 void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration( |
| 502 int registration_handle_id) { |
| 503 DCHECK(ContainsKey(registrations_, registration_handle_id)); |
| 504 registrations_.erase(registration_handle_id); |
| 505 } |
| 506 |
416 } // namespace content | 507 } // namespace content |
OLD | NEW |