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/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { | 54 void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) { |
55 bool handled = true; | 55 bool handled = true; |
56 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) | 56 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcher, msg) |
57 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_AssociateRegistration, | 57 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_AssociateRegistration, |
58 OnAssociateRegistration) | 58 OnAssociateRegistration) |
59 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DisassociateRegistration, | 59 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DisassociateRegistration, |
60 OnDisassociateRegistration) | 60 OnDisassociateRegistration) |
61 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered) | 61 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered) |
62 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, | 62 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, |
63 OnUnregistered) | 63 OnUnregistered) |
| 64 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetRegistration, |
| 65 OnDidGetRegistration) |
64 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError, | 66 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError, |
65 OnRegistrationError) | 67 OnRegistrationError) |
66 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistrationError, | 68 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistrationError, |
67 OnUnregistrationError) | 69 OnUnregistrationError) |
| 70 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerGetRegistrationError, |
| 71 OnGetRegistrationError) |
68 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerStateChanged, | 72 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerStateChanged, |
69 OnServiceWorkerStateChanged) | 73 OnServiceWorkerStateChanged) |
70 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetVersionAttributes, | 74 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetVersionAttributes, |
71 OnSetVersionAttributes) | 75 OnSetVersionAttributes) |
72 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_UpdateFound, | 76 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_UpdateFound, |
73 OnUpdateFound) | 77 OnUpdateFound) |
74 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetControllerServiceWorker, | 78 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetControllerServiceWorker, |
75 OnSetControllerServiceWorker) | 79 OnSetControllerServiceWorker) |
76 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToDocument, | 80 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToDocument, |
77 OnPostMessage) | 81 OnPostMessage) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 132 |
129 int request_id = pending_unregistration_callbacks_.Add(callbacks); | 133 int request_id = pending_unregistration_callbacks_.Add(callbacks); |
130 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", | 134 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", |
131 "ServiceWorkerDispatcher::UnregisterServiceWorker", | 135 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
132 request_id, | 136 request_id, |
133 "Pettern", pattern.spec()); | 137 "Pettern", pattern.spec()); |
134 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( | 138 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( |
135 CurrentWorkerId(), request_id, provider_id, pattern)); | 139 CurrentWorkerId(), request_id, provider_id, pattern)); |
136 } | 140 } |
137 | 141 |
| 142 void ServiceWorkerDispatcher::GetRegistration( |
| 143 int provider_id, |
| 144 const GURL& document_url, |
| 145 WebServiceWorkerRegistrationCallbacks* callbacks) { |
| 146 DCHECK(callbacks); |
| 147 |
| 148 if (document_url.possibly_invalid_spec().size() > GetMaxURLChars()) { |
| 149 scoped_ptr<WebServiceWorkerRegistrationCallbacks> |
| 150 owned_callbacks(callbacks); |
| 151 scoped_ptr<WebServiceWorkerError> error(new WebServiceWorkerError( |
| 152 WebServiceWorkerError::ErrorTypeSecurity, "URL too long")); |
| 153 callbacks->onError(error.release()); |
| 154 return; |
| 155 } |
| 156 |
| 157 int request_id = pending_get_registration_callbacks_.Add(callbacks); |
| 158 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", |
| 159 "ServiceWorkerDispatcher::GetRegistration", |
| 160 request_id, |
| 161 "Document URL", document_url.spec()); |
| 162 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetRegistration( |
| 163 CurrentWorkerId(), request_id, provider_id, document_url)); |
| 164 } |
| 165 |
138 void ServiceWorkerDispatcher::AddProviderContext( | 166 void ServiceWorkerDispatcher::AddProviderContext( |
139 ServiceWorkerProviderContext* provider_context) { | 167 ServiceWorkerProviderContext* provider_context) { |
140 DCHECK(provider_context); | 168 DCHECK(provider_context); |
141 int provider_id = provider_context->provider_id(); | 169 int provider_id = provider_context->provider_id(); |
142 DCHECK(!ContainsKey(provider_contexts_, provider_id)); | 170 DCHECK(!ContainsKey(provider_contexts_, provider_id)); |
143 provider_contexts_[provider_id] = provider_context; | 171 provider_contexts_[provider_id] = provider_context; |
144 } | 172 } |
145 | 173 |
146 void ServiceWorkerDispatcher::RemoveProviderContext( | 174 void ServiceWorkerDispatcher::RemoveProviderContext( |
147 ServiceWorkerProviderContext* provider_context) { | 175 ServiceWorkerProviderContext* provider_context) { |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", | 324 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", |
297 "ServiceWorkerDispatcher::RegisterServiceWorker", | 325 "ServiceWorkerDispatcher::RegisterServiceWorker", |
298 request_id, | 326 request_id, |
299 "OnRegistered"); | 327 "OnRegistered"); |
300 WebServiceWorkerRegistrationCallbacks* callbacks = | 328 WebServiceWorkerRegistrationCallbacks* callbacks = |
301 pending_registration_callbacks_.Lookup(request_id); | 329 pending_registration_callbacks_.Lookup(request_id); |
302 DCHECK(callbacks); | 330 DCHECK(callbacks); |
303 if (!callbacks) | 331 if (!callbacks) |
304 return; | 332 return; |
305 | 333 |
306 WebServiceWorkerRegistrationImpl* registration = | 334 callbacks->onSuccess(FindOrCreateRegistration(info, attrs)); |
307 FindServiceWorkerRegistration(info, true); | |
308 if (!registration) { | |
309 registration = CreateServiceWorkerRegistration(info, true); | |
310 registration->SetInstalling(GetServiceWorker(attrs.installing, true)); | |
311 registration->SetWaiting(GetServiceWorker(attrs.waiting, true)); | |
312 registration->SetActive(GetServiceWorker(attrs.active, true)); | |
313 } else { | |
314 // |registration| must already have version attributes, so adopt and destroy | |
315 // handle refs for them. | |
316 ServiceWorkerHandleReference::Adopt( | |
317 attrs.installing, thread_safe_sender_.get()); | |
318 ServiceWorkerHandleReference::Adopt( | |
319 attrs.waiting, thread_safe_sender_.get()); | |
320 ServiceWorkerHandleReference::Adopt( | |
321 attrs.active, thread_safe_sender_.get()); | |
322 } | |
323 | |
324 callbacks->onSuccess(registration); | |
325 pending_registration_callbacks_.Remove(request_id); | 335 pending_registration_callbacks_.Remove(request_id); |
326 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 336 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
327 "ServiceWorkerDispatcher::RegisterServiceWorker", | 337 "ServiceWorkerDispatcher::RegisterServiceWorker", |
328 request_id); | 338 request_id); |
329 } | 339 } |
330 | 340 |
331 void ServiceWorkerDispatcher::OnUnregistered(int thread_id, | 341 void ServiceWorkerDispatcher::OnUnregistered(int thread_id, |
332 int request_id, | 342 int request_id, |
333 bool is_success) { | 343 bool is_success) { |
334 WebServiceWorkerUnregistrationCallbacks* callbacks = | 344 WebServiceWorkerUnregistrationCallbacks* callbacks = |
335 pending_unregistration_callbacks_.Lookup(request_id); | 345 pending_unregistration_callbacks_.Lookup(request_id); |
336 TRACE_EVENT_ASYNC_STEP_INTO0( | 346 TRACE_EVENT_ASYNC_STEP_INTO0( |
337 "ServiceWorker", | 347 "ServiceWorker", |
338 "ServiceWorkerDispatcher::UnregisterServiceWorker", | 348 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
339 request_id, | 349 request_id, |
340 "OnUnregistered"); | 350 "OnUnregistered"); |
341 DCHECK(callbacks); | 351 DCHECK(callbacks); |
342 if (!callbacks) | 352 if (!callbacks) |
343 return; | 353 return; |
344 callbacks->onSuccess(&is_success); | 354 callbacks->onSuccess(&is_success); |
345 pending_unregistration_callbacks_.Remove(request_id); | 355 pending_unregistration_callbacks_.Remove(request_id); |
346 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 356 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
347 "ServiceWorkerDispatcher::UnregisterServiceWorker", | 357 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
348 request_id); | 358 request_id); |
349 } | 359 } |
350 | 360 |
| 361 void ServiceWorkerDispatcher::OnDidGetRegistration( |
| 362 int thread_id, |
| 363 int request_id, |
| 364 const ServiceWorkerRegistrationObjectInfo& info, |
| 365 const ServiceWorkerVersionAttributes& attrs) { |
| 366 WebServiceWorkerRegistrationCallbacks* callbacks = |
| 367 pending_get_registration_callbacks_.Lookup(request_id); |
| 368 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 369 "ServiceWorker", |
| 370 "ServiceWorkerDispatcher::GetRegistration", |
| 371 request_id, |
| 372 "OnDidGetRegistration"); |
| 373 DCHECK(callbacks); |
| 374 if (!callbacks) |
| 375 return; |
| 376 |
| 377 WebServiceWorkerRegistrationImpl* registration = NULL; |
| 378 if (info.handle_id != kInvalidServiceWorkerHandleId) |
| 379 registration = FindOrCreateRegistration(info, attrs); |
| 380 |
| 381 callbacks->onSuccess(registration); |
| 382 pending_get_registration_callbacks_.Remove(request_id); |
| 383 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 384 "ServiceWorkerDispatcher::GetRegistration", |
| 385 request_id); |
| 386 } |
| 387 |
351 void ServiceWorkerDispatcher::OnRegistrationError( | 388 void ServiceWorkerDispatcher::OnRegistrationError( |
352 int thread_id, | 389 int thread_id, |
353 int request_id, | 390 int request_id, |
354 WebServiceWorkerError::ErrorType error_type, | 391 WebServiceWorkerError::ErrorType error_type, |
355 const base::string16& message) { | 392 const base::string16& message) { |
356 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", | 393 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", |
357 "ServiceWorkerDispatcher::RegisterServiceWorker", | 394 "ServiceWorkerDispatcher::RegisterServiceWorker", |
358 request_id, | 395 request_id, |
359 "OnRegistrationError"); | 396 "OnRegistrationError"); |
360 WebServiceWorkerRegistrationCallbacks* callbacks = | 397 WebServiceWorkerRegistrationCallbacks* callbacks = |
(...skipping 29 matching lines...) Expand all Loading... |
390 | 427 |
391 scoped_ptr<WebServiceWorkerError> error( | 428 scoped_ptr<WebServiceWorkerError> error( |
392 new WebServiceWorkerError(error_type, message)); | 429 new WebServiceWorkerError(error_type, message)); |
393 callbacks->onError(error.release()); | 430 callbacks->onError(error.release()); |
394 pending_unregistration_callbacks_.Remove(request_id); | 431 pending_unregistration_callbacks_.Remove(request_id); |
395 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 432 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
396 "ServiceWorkerDispatcher::UnregisterServiceWorker", | 433 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
397 request_id); | 434 request_id); |
398 } | 435 } |
399 | 436 |
| 437 void ServiceWorkerDispatcher::OnGetRegistrationError( |
| 438 int thread_id, |
| 439 int request_id, |
| 440 WebServiceWorkerError::ErrorType error_type, |
| 441 const base::string16& message) { |
| 442 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 443 "ServiceWorker", |
| 444 "ServiceWorkerDispatcher::GetRegistration", |
| 445 request_id, |
| 446 "OnGetRegistrationError"); |
| 447 WebServiceWorkerGetRegistrationCallbacks* callbacks = |
| 448 pending_get_registration_callbacks_.Lookup(request_id); |
| 449 DCHECK(callbacks); |
| 450 if (!callbacks) |
| 451 return; |
| 452 |
| 453 scoped_ptr<WebServiceWorkerError> error( |
| 454 new WebServiceWorkerError(error_type, message)); |
| 455 callbacks->onError(error.release()); |
| 456 pending_get_registration_callbacks_.Remove(request_id); |
| 457 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 458 "ServiceWorkerDispatcher::GetRegistration", |
| 459 request_id); |
| 460 } |
| 461 |
400 void ServiceWorkerDispatcher::OnServiceWorkerStateChanged( | 462 void ServiceWorkerDispatcher::OnServiceWorkerStateChanged( |
401 int thread_id, | 463 int thread_id, |
402 int handle_id, | 464 int handle_id, |
403 blink::WebServiceWorkerState state) { | 465 blink::WebServiceWorkerState state) { |
404 TRACE_EVENT2("ServiceWorker", | 466 TRACE_EVENT2("ServiceWorker", |
405 "ServiceWorkerDispatcher::OnServiceWorkerStateChanged", | 467 "ServiceWorkerDispatcher::OnServiceWorkerStateChanged", |
406 "Thread ID", thread_id, | 468 "Thread ID", thread_id, |
407 "State", state); | 469 "State", state); |
408 WorkerObjectMap::iterator worker = service_workers_.find(handle_id); | 470 WorkerObjectMap::iterator worker = service_workers_.find(handle_id); |
409 if (worker != service_workers_.end()) | 471 if (worker != service_workers_.end()) |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 DCHECK(!ContainsKey(registrations_, registration_handle_id)); | 705 DCHECK(!ContainsKey(registrations_, registration_handle_id)); |
644 registrations_[registration_handle_id] = registration; | 706 registrations_[registration_handle_id] = registration; |
645 } | 707 } |
646 | 708 |
647 void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration( | 709 void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration( |
648 int registration_handle_id) { | 710 int registration_handle_id) { |
649 DCHECK(ContainsKey(registrations_, registration_handle_id)); | 711 DCHECK(ContainsKey(registrations_, registration_handle_id)); |
650 registrations_.erase(registration_handle_id); | 712 registrations_.erase(registration_handle_id); |
651 } | 713 } |
652 | 714 |
| 715 WebServiceWorkerRegistrationImpl* |
| 716 ServiceWorkerDispatcher::FindOrCreateRegistration( |
| 717 const ServiceWorkerRegistrationObjectInfo& info, |
| 718 const ServiceWorkerVersionAttributes& attrs) { |
| 719 WebServiceWorkerRegistrationImpl* registration = |
| 720 FindServiceWorkerRegistration(info, true); |
| 721 if (!registration) { |
| 722 registration = CreateServiceWorkerRegistration(info, true); |
| 723 registration->SetInstalling(GetServiceWorker(attrs.installing, true)); |
| 724 registration->SetWaiting(GetServiceWorker(attrs.waiting, true)); |
| 725 registration->SetActive(GetServiceWorker(attrs.active, true)); |
| 726 } else { |
| 727 // |registration| must already have version attributes, so adopt and destroy |
| 728 // handle refs for them. |
| 729 ServiceWorkerHandleReference::Adopt( |
| 730 attrs.installing, thread_safe_sender_.get()); |
| 731 ServiceWorkerHandleReference::Adopt( |
| 732 attrs.waiting, thread_safe_sender_.get()); |
| 733 ServiceWorkerHandleReference::Adopt( |
| 734 attrs.active, thread_safe_sender_.get()); |
| 735 } |
| 736 return registration; |
| 737 } |
| 738 |
653 } // namespace content | 739 } // namespace content |
OLD | NEW |