| 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_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "content/browser/service_worker/embedded_worker_instance.h" | 10 #include "content/browser/service_worker/embedded_worker_instance.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 92 |
| 93 ServiceWorkerVersion::ServiceWorkerVersion( | 93 ServiceWorkerVersion::ServiceWorkerVersion( |
| 94 ServiceWorkerRegistration* registration, | 94 ServiceWorkerRegistration* registration, |
| 95 int64 version_id, | 95 int64 version_id, |
| 96 base::WeakPtr<ServiceWorkerContextCore> context) | 96 base::WeakPtr<ServiceWorkerContextCore> context) |
| 97 : version_id_(version_id), | 97 : version_id_(version_id), |
| 98 registration_id_(kInvalidServiceWorkerVersionId), | 98 registration_id_(kInvalidServiceWorkerVersionId), |
| 99 status_(NEW), | 99 status_(NEW), |
| 100 context_(context), | 100 context_(context), |
| 101 script_cache_map_(this, context), | 101 script_cache_map_(this, context), |
| 102 is_doomed_(false), |
| 102 weak_factory_(this) { | 103 weak_factory_(this) { |
| 103 DCHECK(context_); | 104 DCHECK(context_); |
| 104 DCHECK(registration); | 105 DCHECK(registration); |
| 105 if (registration) { | 106 if (registration) { |
| 106 registration_id_ = registration->id(); | 107 registration_id_ = registration->id(); |
| 107 script_url_ = registration->script_url(); | 108 script_url_ = registration->script_url(); |
| 108 scope_ = registration->pattern(); | 109 scope_ = registration->pattern(); |
| 109 } | 110 } |
| 110 context_->AddLiveVersion(this); | 111 context_->AddLiveVersion(this); |
| 111 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); | 112 embedded_worker_ = context_->embedded_worker_registry()->CreateWorker(); |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 stop_worker_timer_.Stop(); | 380 stop_worker_timer_.Stop(); |
| 380 } | 381 } |
| 381 | 382 |
| 382 void ServiceWorkerVersion::RemoveControllee( | 383 void ServiceWorkerVersion::RemoveControllee( |
| 383 ServiceWorkerProviderHost* provider_host) { | 384 ServiceWorkerProviderHost* provider_host) { |
| 384 ControlleeMap::iterator found = controllee_map_.find(provider_host); | 385 ControlleeMap::iterator found = controllee_map_.find(provider_host); |
| 385 DCHECK(found != controllee_map_.end()); | 386 DCHECK(found != controllee_map_.end()); |
| 386 controllee_by_id_.Remove(found->second); | 387 controllee_by_id_.Remove(found->second); |
| 387 controllee_map_.erase(found); | 388 controllee_map_.erase(found); |
| 388 RemoveProcessFromWorker(provider_host->process_id()); | 389 RemoveProcessFromWorker(provider_host->process_id()); |
| 389 if (!HasControllee()) { | 390 if (HasControllee()) |
| 390 ScheduleStopWorker(); | 391 return; |
| 391 FOR_EACH_OBSERVER(Listener, listeners_, OnNoControllees(this)); | 392 ScheduleStopWorker(); |
| 392 } | 393 if (is_doomed_) |
| 394 DoomInternal(); |
| 395 FOR_EACH_OBSERVER(Listener, listeners_, OnNoControllees(this)); |
| 393 } | 396 } |
| 394 | 397 |
| 395 void ServiceWorkerVersion::AddPotentialControllee( | 398 void ServiceWorkerVersion::AddPotentialControllee( |
| 396 ServiceWorkerProviderHost* provider_host) { | 399 ServiceWorkerProviderHost* provider_host) { |
| 397 AddProcessToWorker(provider_host->process_id()); | 400 AddProcessToWorker(provider_host->process_id()); |
| 398 } | 401 } |
| 399 | 402 |
| 400 void ServiceWorkerVersion::RemovePotentialControllee( | 403 void ServiceWorkerVersion::RemovePotentialControllee( |
| 401 ServiceWorkerProviderHost* provider_host) { | 404 ServiceWorkerProviderHost* provider_host) { |
| 402 RemoveProcessFromWorker(provider_host->process_id()); | 405 RemoveProcessFromWorker(provider_host->process_id()); |
| 403 } | 406 } |
| 404 | 407 |
| 405 void ServiceWorkerVersion::AddListener(Listener* listener) { | 408 void ServiceWorkerVersion::AddListener(Listener* listener) { |
| 406 listeners_.AddObserver(listener); | 409 listeners_.AddObserver(listener); |
| 407 } | 410 } |
| 408 | 411 |
| 409 void ServiceWorkerVersion::RemoveListener(Listener* listener) { | 412 void ServiceWorkerVersion::RemoveListener(Listener* listener) { |
| 410 listeners_.RemoveObserver(listener); | 413 listeners_.RemoveObserver(listener); |
| 411 } | 414 } |
| 412 | 415 |
| 416 void ServiceWorkerVersion::Doom() { |
| 417 if (is_doomed_) |
| 418 return; |
| 419 is_doomed_ = true; |
| 420 if (!HasControllee()) |
| 421 DoomInternal(); |
| 422 } |
| 423 |
| 424 void ServiceWorkerVersion::DoomInternal() { |
| 425 SetStatus(REDUNDANT); |
| 426 if (!context_) |
| 427 return; |
| 428 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 429 script_cache_map_.GetResources(&resources); |
| 430 context_->storage()->PurgeResources(resources); |
| 431 } |
| 432 |
| 413 void ServiceWorkerVersion::OnStarted() { | 433 void ServiceWorkerVersion::OnStarted() { |
| 414 DCHECK_EQ(RUNNING, running_status()); | 434 DCHECK_EQ(RUNNING, running_status()); |
| 415 // Fire all start callbacks. | 435 // Fire all start callbacks. |
| 416 RunCallbacks(this, &start_callbacks_, SERVICE_WORKER_OK); | 436 RunCallbacks(this, &start_callbacks_, SERVICE_WORKER_OK); |
| 417 FOR_EACH_OBSERVER(Listener, listeners_, OnWorkerStarted(this)); | 437 FOR_EACH_OBSERVER(Listener, listeners_, OnWorkerStarted(this)); |
| 418 } | 438 } |
| 419 | 439 |
| 420 void ServiceWorkerVersion::OnStopped() { | 440 void ServiceWorkerVersion::OnStopped() { |
| 421 DCHECK_EQ(STOPPED, running_status()); | 441 DCHECK_EQ(STOPPED, running_status()); |
| 422 scoped_refptr<ServiceWorkerVersion> protect(this); | 442 scoped_refptr<ServiceWorkerVersion> protect(this); |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 return; | 670 return; |
| 651 } | 671 } |
| 652 stop_worker_timer_.Start( | 672 stop_worker_timer_.Start( |
| 653 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), | 673 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), |
| 654 base::Bind(&ServiceWorkerVersion::StopWorker, | 674 base::Bind(&ServiceWorkerVersion::StopWorker, |
| 655 weak_factory_.GetWeakPtr(), | 675 weak_factory_.GetWeakPtr(), |
| 656 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); | 676 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback))); |
| 657 } | 677 } |
| 658 | 678 |
| 659 } // namespace content | 679 } // namespace content |
| OLD | NEW |