Chromium Code Reviews| 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/browser/service_worker/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/profiler/scoped_tracker.h" | 8 #include "base/profiler/scoped_tracker.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 const GURL& script_url) { | 61 const GURL& script_url) { |
| 62 DCHECK(document_url.is_valid()); | 62 DCHECK(document_url.is_valid()); |
| 63 DCHECK(pattern.is_valid()); | 63 DCHECK(pattern.is_valid()); |
| 64 DCHECK(script_url.is_valid()); | 64 DCHECK(script_url.is_valid()); |
| 65 return AllOriginsMatch(document_url, pattern, script_url) && | 65 return AllOriginsMatch(document_url, pattern, script_url) && |
| 66 OriginCanAccessServiceWorkers(document_url) && | 66 OriginCanAccessServiceWorkers(document_url) && |
| 67 OriginCanAccessServiceWorkers(pattern) && | 67 OriginCanAccessServiceWorkers(pattern) && |
| 68 OriginCanAccessServiceWorkers(script_url); | 68 OriginCanAccessServiceWorkers(script_url); |
| 69 } | 69 } |
| 70 | 70 |
| 71 bool CanUpdateServiceWorker(const GURL& document_url, const GURL& pattern) { | |
| 72 DCHECK(document_url.is_valid()); | |
| 73 DCHECK(pattern.is_valid()); | |
| 74 return document_url.GetOrigin() == pattern.GetOrigin() && | |
| 75 OriginCanAccessServiceWorkers(document_url) && | |
| 76 OriginCanAccessServiceWorkers(pattern); | |
| 77 } | |
| 78 | |
| 71 bool CanUnregisterServiceWorker(const GURL& document_url, | 79 bool CanUnregisterServiceWorker(const GURL& document_url, |
| 72 const GURL& pattern) { | 80 const GURL& pattern) { |
| 73 DCHECK(document_url.is_valid()); | 81 DCHECK(document_url.is_valid()); |
| 74 DCHECK(pattern.is_valid()); | 82 DCHECK(pattern.is_valid()); |
| 75 return document_url.GetOrigin() == pattern.GetOrigin() && | 83 return document_url.GetOrigin() == pattern.GetOrigin() && |
| 76 OriginCanAccessServiceWorkers(document_url) && | 84 OriginCanAccessServiceWorkers(document_url) && |
| 77 OriginCanAccessServiceWorkers(pattern); | 85 OriginCanAccessServiceWorkers(pattern); |
| 78 } | 86 } |
| 79 | 87 |
| 80 bool CanGetRegistration(const GURL& document_url, | 88 bool CanGetRegistration(const GURL& document_url, |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 void ServiceWorkerDispatcherHost::OnDestruct() const { | 159 void ServiceWorkerDispatcherHost::OnDestruct() const { |
| 152 BrowserThread::DeleteOnIOThread::Destruct(this); | 160 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 153 } | 161 } |
| 154 | 162 |
| 155 bool ServiceWorkerDispatcherHost::OnMessageReceived( | 163 bool ServiceWorkerDispatcherHost::OnMessageReceived( |
| 156 const IPC::Message& message) { | 164 const IPC::Message& message) { |
| 157 bool handled = true; | 165 bool handled = true; |
| 158 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message) | 166 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerDispatcherHost, message) |
| 159 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, | 167 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, |
| 160 OnRegisterServiceWorker) | 168 OnRegisterServiceWorker) |
| 169 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UpdateServiceWorker, | |
| 170 OnUpdateServiceWorker) | |
| 161 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 171 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
| 162 OnUnregisterServiceWorker) | 172 OnUnregisterServiceWorker) |
| 163 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration, | 173 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistration, |
| 164 OnGetRegistration) | 174 OnGetRegistration) |
| 165 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady, | 175 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady, |
| 166 OnGetRegistrationForReady) | 176 OnGetRegistrationForReady) |
| 167 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, | 177 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, |
| 168 OnProviderCreated) | 178 OnProviderCreated) |
| 169 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, | 179 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, |
| 170 OnProviderDestroyed) | 180 OnProviderDestroyed) |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 pattern, | 361 pattern, |
| 352 script_url, | 362 script_url, |
| 353 provider_host, | 363 provider_host, |
| 354 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, | 364 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, |
| 355 this, | 365 this, |
| 356 thread_id, | 366 thread_id, |
| 357 provider_id, | 367 provider_id, |
| 358 request_id)); | 368 request_id)); |
| 359 } | 369 } |
| 360 | 370 |
| 371 void ServiceWorkerDispatcherHost::OnUpdateServiceWorker(int provider_id, | |
| 372 const GURL& pattern) { | |
| 373 TRACE_EVENT0("ServiceWorker", | |
| 374 "ServiceWorkerDispatcherHost::OnUpdateServiceWorker"); | |
| 375 if (!GetContext()) | |
| 376 return; | |
| 377 if (!pattern.is_valid()) { | |
| 378 bad_message::ReceivedBadMessage(this, bad_message::SWDH_UPDATE_BAD_URL); | |
| 379 return; | |
| 380 } | |
| 381 | |
| 382 ServiceWorkerProviderHost* provider_host = | |
| 383 GetContext()->GetProviderHost(render_process_id_, provider_id); | |
| 384 if (!provider_host) { | |
| 385 bad_message::ReceivedBadMessage(this, bad_message::SWDH_UPDATE_NO_HOST); | |
| 386 return; | |
| 387 } | |
| 388 if (!provider_host->IsContextAlive()) | |
| 389 return; | |
| 390 | |
| 391 // TODO(ksakamoto): This check can be removed once crbug.com/439697 is fixed. | |
| 392 if (provider_host->document_url().is_empty()) | |
| 393 return; | |
| 394 | |
| 395 if (!CanUpdateServiceWorker(provider_host->document_url(), pattern)) { | |
| 396 bad_message::ReceivedBadMessage(this, bad_message::SWDH_UPDATE_CANNOT); | |
| 397 return; | |
| 398 } | |
| 399 | |
| 400 if (!GetContentClient()->browser()->AllowServiceWorker( | |
| 401 pattern, provider_host->topmost_frame_url(), resource_context_, | |
| 402 render_process_id_, provider_host->frame_id())) { | |
| 403 return; | |
| 404 } | |
| 405 | |
| 406 GetContext()->storage()->FindRegistrationForPattern( | |
| 407 pattern, | |
| 408 base::Bind(&ServiceWorkerDispatcherHost::DidFindRegistrationForUpdate, | |
| 409 this)); | |
| 410 } | |
| 411 | |
| 361 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( | 412 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( |
| 362 int thread_id, | 413 int thread_id, |
| 363 int request_id, | 414 int request_id, |
| 364 int provider_id, | 415 int provider_id, |
| 365 const GURL& pattern) { | 416 const GURL& pattern) { |
| 366 TRACE_EVENT0("ServiceWorker", | 417 TRACE_EVENT0("ServiceWorker", |
| 367 "ServiceWorkerDispatcherHost::OnUnregisterServiceWorker"); | 418 "ServiceWorkerDispatcherHost::OnUnregisterServiceWorker"); |
| 368 if (!GetContext()) { | 419 if (!GetContext()) { |
| 369 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( | 420 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( |
| 370 thread_id, | 421 thread_id, |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 641 | 692 |
| 642 ServiceWorkerRegistrationObjectInfo info; | 693 ServiceWorkerRegistrationObjectInfo info; |
| 643 ServiceWorkerVersionAttributes attrs; | 694 ServiceWorkerVersionAttributes attrs; |
| 644 GetRegistrationObjectInfoAndVersionAttributes( | 695 GetRegistrationObjectInfoAndVersionAttributes( |
| 645 provider_host->AsWeakPtr(), registration, &info, &attrs); | 696 provider_host->AsWeakPtr(), registration, &info, &attrs); |
| 646 | 697 |
| 647 Send(new ServiceWorkerMsg_AssociateRegistrationWithServiceWorker( | 698 Send(new ServiceWorkerMsg_AssociateRegistrationWithServiceWorker( |
| 648 kDocumentMainThreadId, provider_id, info, attrs)); | 699 kDocumentMainThreadId, provider_id, info, attrs)); |
| 649 } | 700 } |
| 650 | 701 |
| 702 void ServiceWorkerDispatcherHost::DidFindRegistrationForUpdate( | |
| 703 ServiceWorkerStatusCode status, | |
| 704 const scoped_refptr<ServiceWorkerRegistration>& registration) { | |
| 705 if (status != SERVICE_WORKER_OK) | |
| 706 return; | |
| 707 if (!GetContext()) | |
| 708 return; | |
| 709 if (!registration->GetNewestVersion()) { | |
| 710 // This can happen if update() is called during the script evaluation. Abort | |
|
falken
2015/06/10 04:04:40
nit: during initial script evaluation
nhiroki
2015/06/10 06:36:32
Done.
| |
| 711 // the following steps according to the spec. | |
| 712 return; | |
| 713 } | |
| 714 GetContext()->UpdateServiceWorker(registration.get(), | |
| 715 false /* force_bypass_cache */); | |
|
falken
2015/06/10 04:08:48
Ah this should be true, as per spec's non-normativ
nhiroki
2015/06/10 06:36:32
Good catch! Fixed.
| |
| 716 } | |
| 717 | |
| 651 ServiceWorkerRegistrationHandle* | 718 ServiceWorkerRegistrationHandle* |
| 652 ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id, | 719 ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id, |
| 653 int64 registration_id) { | 720 int64 registration_id) { |
| 654 for (IDMap<ServiceWorkerRegistrationHandle, IDMapOwnPointer>::iterator | 721 for (IDMap<ServiceWorkerRegistrationHandle, IDMapOwnPointer>::iterator |
| 655 iter(®istration_handles_); | 722 iter(®istration_handles_); |
| 656 !iter.IsAtEnd(); | 723 !iter.IsAtEnd(); |
| 657 iter.Advance()) { | 724 iter.Advance()) { |
| 658 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue(); | 725 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue(); |
| 659 DCHECK(handle); | 726 DCHECK(handle); |
| 660 DCHECK(handle->registration()); | 727 DCHECK(handle->registration()); |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1048 if (!handle) { | 1115 if (!handle) { |
| 1049 bad_message::ReceivedBadMessage(this, | 1116 bad_message::ReceivedBadMessage(this, |
| 1050 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1117 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1051 return; | 1118 return; |
| 1052 } | 1119 } |
| 1053 handle->version()->StopWorker( | 1120 handle->version()->StopWorker( |
| 1054 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1121 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1055 } | 1122 } |
| 1056 | 1123 |
| 1057 } // namespace content | 1124 } // namespace content |
| OLD | NEW |