| 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_registration.h" | 5 #include "content/browser/service_worker/service_worker_registration.h" |
| 6 | 6 |
| 7 #include "content/browser/service_worker/service_worker_context_core.h" | 7 #include "content/browser/service_worker/service_worker_context_core.h" |
| 8 #include "content/browser/service_worker/service_worker_info.h" | 8 #include "content/browser/service_worker/service_worker_info.h" |
| 9 #include "content/browser/service_worker/service_worker_register_job.h" | 9 #include "content/browser/service_worker/service_worker_register_job.h" |
| 10 #include "content/browser/service_worker/service_worker_utils.h" | 10 #include "content/browser/service_worker/service_worker_utils.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 155 |
| 156 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { | 156 void ServiceWorkerRegistration::ActivateWaitingVersionWhenReady() { |
| 157 DCHECK(waiting_version()); | 157 DCHECK(waiting_version()); |
| 158 should_activate_when_ready_ = true; | 158 should_activate_when_ready_ = true; |
| 159 | 159 |
| 160 if (!active_version() || !active_version()->HasControllee() || | 160 if (!active_version() || !active_version()->HasControllee() || |
| 161 waiting_version()->skip_waiting()) | 161 waiting_version()->skip_waiting()) |
| 162 ActivateWaitingVersion(); | 162 ActivateWaitingVersion(); |
| 163 } | 163 } |
| 164 | 164 |
| 165 void ServiceWorkerRegistration::ClaimClients(const StatusCallback& callback) { | 165 void ServiceWorkerRegistration::ClaimClients() { |
| 166 DCHECK(context_); | 166 DCHECK(context_); |
| 167 DCHECK(active_version()); | 167 DCHECK(active_version()); |
| 168 // TODO(xiang): Should better not hit the database http://crbug.com/454250. | 168 |
| 169 context_->storage()->GetRegistrationsForOrigin( | 169 for (scoped_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = |
| 170 pattern_.GetOrigin(), | 170 context_->GetProviderHostIterator(); |
| 171 base::Bind(&ServiceWorkerRegistration::DidGetRegistrationsForClaimClients, | 171 !it->IsAtEnd(); it->Advance()) { |
| 172 this, callback, active_version_)); | 172 ServiceWorkerProviderHost* host = it->GetProviderHost(); |
| 173 if (host->IsHostToRunningServiceWorker()) |
| 174 continue; |
| 175 if (host->controlling_version() == active_version()) |
| 176 continue; |
| 177 if (host->MatchRegistration() == this) |
| 178 host->ClaimedByRegistration(this); |
| 179 } |
| 173 } | 180 } |
| 174 | 181 |
| 175 void ServiceWorkerRegistration::ClearWhenReady() { | 182 void ServiceWorkerRegistration::ClearWhenReady() { |
| 176 DCHECK(context_); | 183 DCHECK(context_); |
| 177 if (is_uninstalling_) | 184 if (is_uninstalling_) |
| 178 return; | 185 return; |
| 179 is_uninstalling_ = true; | 186 is_uninstalling_ = true; |
| 180 | 187 |
| 181 context_->storage()->NotifyUninstallingRegistration(this); | 188 context_->storage()->NotifyUninstallingRegistration(this); |
| 182 context_->storage()->DeleteRegistration( | 189 context_->storage()->DeleteRegistration( |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 ServiceWorkerStatusCode status) { | 375 ServiceWorkerStatusCode status) { |
| 369 if (!context_) { | 376 if (!context_) { |
| 370 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 377 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 371 return; | 378 return; |
| 372 } | 379 } |
| 373 context_->storage()->NotifyDoneInstallingRegistration( | 380 context_->storage()->NotifyDoneInstallingRegistration( |
| 374 this, version.get(), status); | 381 this, version.get(), status); |
| 375 callback.Run(status); | 382 callback.Run(status); |
| 376 } | 383 } |
| 377 | 384 |
| 378 void ServiceWorkerRegistration::DidGetRegistrationsForClaimClients( | |
| 379 const StatusCallback& callback, | |
| 380 scoped_refptr<ServiceWorkerVersion> version, | |
| 381 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { | |
| 382 if (!context_) { | |
| 383 callback.Run(SERVICE_WORKER_ERROR_ABORT); | |
| 384 return; | |
| 385 } | |
| 386 if (!active_version() || version != active_version()) { | |
| 387 callback.Run(SERVICE_WORKER_ERROR_STATE); | |
| 388 return; | |
| 389 } | |
| 390 | |
| 391 for (scoped_ptr<ServiceWorkerContextCore::ProviderHostIterator> it = | |
| 392 context_->GetProviderHostIterator(); | |
| 393 !it->IsAtEnd(); it->Advance()) { | |
| 394 ServiceWorkerProviderHost* host = it->GetProviderHost(); | |
| 395 if (ShouldClaim(host, registrations)) | |
| 396 host->ClaimedByRegistration(this); | |
| 397 } | |
| 398 callback.Run(SERVICE_WORKER_OK); | |
| 399 } | |
| 400 | |
| 401 bool ServiceWorkerRegistration::ShouldClaim( | |
| 402 ServiceWorkerProviderHost* provider_host, | |
| 403 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { | |
| 404 if (provider_host->IsHostToRunningServiceWorker()) | |
| 405 return false; | |
| 406 if (provider_host->controlling_version() == active_version()) | |
| 407 return false; | |
| 408 | |
| 409 LongestScopeMatcher matcher(provider_host->document_url()); | |
| 410 if (!matcher.MatchLongest(pattern_)) | |
| 411 return false; | |
| 412 for (const ServiceWorkerRegistrationInfo& info : registrations) { | |
| 413 ServiceWorkerRegistration* registration = | |
| 414 context_->GetLiveRegistration(info.registration_id); | |
| 415 if (registration && | |
| 416 (registration->is_uninstalling() || registration->is_uninstalled())) | |
| 417 continue; | |
| 418 if (matcher.MatchLongest(info.pattern)) | |
| 419 return false; | |
| 420 } | |
| 421 return true; | |
| 422 } | |
| 423 | |
| 424 } // namespace content | 385 } // namespace content |
| OLD | NEW |