| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 bool CanUnregisterServiceWorker(const GURL& document_url, | 71 bool CanUnregisterServiceWorker(const GURL& document_url, |
| 72 const GURL& pattern) { | 72 const GURL& pattern) { |
| 73 DCHECK(document_url.is_valid()); | 73 DCHECK(document_url.is_valid()); |
| 74 DCHECK(pattern.is_valid()); | 74 DCHECK(pattern.is_valid()); |
| 75 return document_url.GetOrigin() == pattern.GetOrigin() && | 75 return document_url.GetOrigin() == pattern.GetOrigin() && |
| 76 OriginCanAccessServiceWorkers(document_url) && | 76 OriginCanAccessServiceWorkers(document_url) && |
| 77 OriginCanAccessServiceWorkers(pattern); | 77 OriginCanAccessServiceWorkers(pattern); |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool CanUpdateServiceWorker(const GURL& document_url, const GURL& pattern) { |
| 81 DCHECK(document_url.is_valid()); |
| 82 DCHECK(pattern.is_valid()); |
| 83 DCHECK(OriginCanAccessServiceWorkers(document_url)); |
| 84 DCHECK(OriginCanAccessServiceWorkers(pattern)); |
| 85 return document_url.GetOrigin() == pattern.GetOrigin(); |
| 86 } |
| 87 |
| 80 bool CanGetRegistration(const GURL& document_url, | 88 bool CanGetRegistration(const GURL& document_url, |
| 81 const GURL& given_document_url) { | 89 const GURL& given_document_url) { |
| 82 DCHECK(document_url.is_valid()); | 90 DCHECK(document_url.is_valid()); |
| 83 DCHECK(given_document_url.is_valid()); | 91 DCHECK(given_document_url.is_valid()); |
| 84 return document_url.GetOrigin() == given_document_url.GetOrigin() && | 92 return document_url.GetOrigin() == given_document_url.GetOrigin() && |
| 85 OriginCanAccessServiceWorkers(document_url) && | 93 OriginCanAccessServiceWorkers(document_url) && |
| 86 OriginCanAccessServiceWorkers(given_document_url); | 94 OriginCanAccessServiceWorkers(given_document_url); |
| 87 } | 95 } |
| 88 | 96 |
| 89 } // namespace | 97 } // namespace |
| (...skipping 61 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_GetRegistrations, | 175 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrations, |
| 166 OnGetRegistrations) | 176 OnGetRegistrations) |
| 167 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady, | 177 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetRegistrationForReady, |
| 168 OnGetRegistrationForReady) | 178 OnGetRegistrationForReady) |
| 169 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, | 179 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, |
| 170 OnProviderCreated) | 180 OnProviderCreated) |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 pattern, | 363 pattern, |
| 354 script_url, | 364 script_url, |
| 355 provider_host, | 365 provider_host, |
| 356 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, | 366 base::Bind(&ServiceWorkerDispatcherHost::RegistrationComplete, |
| 357 this, | 367 this, |
| 358 thread_id, | 368 thread_id, |
| 359 provider_id, | 369 provider_id, |
| 360 request_id)); | 370 request_id)); |
| 361 } | 371 } |
| 362 | 372 |
| 373 void ServiceWorkerDispatcherHost::OnUpdateServiceWorker(int provider_id, |
| 374 int64 registration_id) { |
| 375 TRACE_EVENT0("ServiceWorker", |
| 376 "ServiceWorkerDispatcherHost::OnUpdateServiceWorker"); |
| 377 if (!GetContext()) |
| 378 return; |
| 379 |
| 380 ServiceWorkerProviderHost* provider_host = |
| 381 GetContext()->GetProviderHost(render_process_id_, provider_id); |
| 382 if (!provider_host) { |
| 383 bad_message::ReceivedBadMessage(this, bad_message::SWDH_UPDATE_NO_HOST); |
| 384 return; |
| 385 } |
| 386 if (!provider_host->IsContextAlive()) |
| 387 return; |
| 388 |
| 389 // TODO(ksakamoto): This check can be removed once crbug.com/439697 is fixed. |
| 390 if (provider_host->document_url().is_empty()) |
| 391 return; |
| 392 |
| 393 ServiceWorkerRegistration* registration = |
| 394 GetContext()->GetLiveRegistration(registration_id); |
| 395 if (!registration) { |
| 396 // |registration| must be alive because a renderer retains a registration |
| 397 // reference at this point. |
| 398 bad_message::ReceivedBadMessage( |
| 399 this, bad_message::SWDH_UPDATE_BAD_REGISTRATION_ID); |
| 400 return; |
| 401 } |
| 402 |
| 403 if (!CanUpdateServiceWorker(provider_host->document_url(), |
| 404 registration->pattern())) { |
| 405 bad_message::ReceivedBadMessage(this, bad_message::SWDH_UPDATE_CANNOT); |
| 406 return; |
| 407 } |
| 408 |
| 409 if (!GetContentClient()->browser()->AllowServiceWorker( |
| 410 registration->pattern(), provider_host->topmost_frame_url(), |
| 411 resource_context_, render_process_id_, provider_host->frame_id())) { |
| 412 return; |
| 413 } |
| 414 |
| 415 if (!registration->GetNewestVersion()) { |
| 416 // This can happen if update() is called during initial script evaluation. |
| 417 // Abort the following steps according to the spec. |
| 418 return; |
| 419 } |
| 420 |
| 421 // The spec says, "update() pings the server for an updated version of this |
| 422 // script without consulting caches", so set |force_bypass_cache| to true. |
| 423 GetContext()->UpdateServiceWorker(registration, |
| 424 true /* force_bypass_cache */); |
| 425 } |
| 426 |
| 363 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( | 427 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker( |
| 364 int thread_id, | 428 int thread_id, |
| 365 int request_id, | 429 int request_id, |
| 366 int provider_id, | 430 int provider_id, |
| 367 const GURL& pattern) { | 431 const GURL& pattern) { |
| 368 TRACE_EVENT0("ServiceWorker", | 432 TRACE_EVENT0("ServiceWorker", |
| 369 "ServiceWorkerDispatcherHost::OnUnregisterServiceWorker"); | 433 "ServiceWorkerDispatcherHost::OnUnregisterServiceWorker"); |
| 370 if (!GetContext()) { | 434 if (!GetContext()) { |
| 371 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( | 435 Send(new ServiceWorkerMsg_ServiceWorkerUnregistrationError( |
| 372 thread_id, | 436 thread_id, |
| (...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 if (!handle) { | 1236 if (!handle) { |
| 1173 bad_message::ReceivedBadMessage(this, | 1237 bad_message::ReceivedBadMessage(this, |
| 1174 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1238 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1175 return; | 1239 return; |
| 1176 } | 1240 } |
| 1177 handle->version()->StopWorker( | 1241 handle->version()->StopWorker( |
| 1178 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1242 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1179 } | 1243 } |
| 1180 | 1244 |
| 1181 } // namespace content | 1245 } // namespace content |
| OLD | NEW |