| 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/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 cross_origin_connect_callbacks_.Remove(request_id); | 589 cross_origin_connect_callbacks_.Remove(request_id); |
| 590 RunSoon(base::Bind(callback, status, false)); | 590 RunSoon(base::Bind(callback, status, false)); |
| 591 } | 591 } |
| 592 } | 592 } |
| 593 | 593 |
| 594 void ServiceWorkerVersion::AddControllee( | 594 void ServiceWorkerVersion::AddControllee( |
| 595 ServiceWorkerProviderHost* provider_host) { | 595 ServiceWorkerProviderHost* provider_host) { |
| 596 DCHECK(!ContainsKey(controllee_map_, provider_host)); | 596 DCHECK(!ContainsKey(controllee_map_, provider_host)); |
| 597 int controllee_id = controllee_by_id_.Add(provider_host); | 597 int controllee_id = controllee_by_id_.Add(provider_host); |
| 598 controllee_map_[provider_host] = controllee_id; | 598 controllee_map_[provider_host] = controllee_id; |
| 599 if (stop_worker_timer_.IsRunning()) | 599 // Reset the timer if it's running (so that it's kept alive a bit longer |
| 600 stop_worker_timer_.Stop(); | 600 // right after a new controllee is added). |
| 601 ScheduleStopWorker(); |
| 601 } | 602 } |
| 602 | 603 |
| 603 void ServiceWorkerVersion::RemoveControllee( | 604 void ServiceWorkerVersion::RemoveControllee( |
| 604 ServiceWorkerProviderHost* provider_host) { | 605 ServiceWorkerProviderHost* provider_host) { |
| 605 ControlleeMap::iterator found = controllee_map_.find(provider_host); | 606 ControlleeMap::iterator found = controllee_map_.find(provider_host); |
| 606 DCHECK(found != controllee_map_.end()); | 607 DCHECK(found != controllee_map_.end()); |
| 607 controllee_by_id_.Remove(found->second); | 608 controllee_by_id_.Remove(found->second); |
| 608 controllee_map_.erase(found); | 609 controllee_map_.erase(found); |
| 609 if (HasControllee()) | 610 if (HasControllee()) |
| 610 return; | 611 return; |
| 611 FOR_EACH_OBSERVER(Listener, listeners_, OnNoControllees(this)); | 612 FOR_EACH_OBSERVER(Listener, listeners_, OnNoControllees(this)); |
| 612 if (is_doomed_) { | 613 if (is_doomed_) { |
| 613 DoomInternal(); | 614 DoomInternal(); |
| 614 return; | 615 return; |
| 615 } | 616 } |
| 616 ScheduleStopWorker(); | 617 // Schedule the stop-worker-timer if it's not running. |
| 618 if (!stop_worker_timer_.IsRunning()) |
| 619 ScheduleStopWorker(); |
| 617 } | 620 } |
| 618 | 621 |
| 619 void ServiceWorkerVersion::AddListener(Listener* listener) { | 622 void ServiceWorkerVersion::AddListener(Listener* listener) { |
| 620 listeners_.AddObserver(listener); | 623 listeners_.AddObserver(listener); |
| 621 } | 624 } |
| 622 | 625 |
| 623 void ServiceWorkerVersion::RemoveListener(Listener* listener) { | 626 void ServiceWorkerVersion::RemoveListener(Listener* listener) { |
| 624 listeners_.RemoveObserver(listener); | 627 listeners_.RemoveObserver(listener); |
| 625 } | 628 } |
| 626 | 629 |
| (...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1057 if (pending_skip_waiting_requests_.size() == 1) | 1060 if (pending_skip_waiting_requests_.size() == 1) |
| 1058 registration->ActivateWaitingVersionWhenReady(); | 1061 registration->ActivateWaitingVersionWhenReady(); |
| 1059 } | 1062 } |
| 1060 | 1063 |
| 1061 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { | 1064 void ServiceWorkerVersion::DidSkipWaiting(int request_id) { |
| 1062 if (running_status() == STARTING || running_status() == RUNNING) | 1065 if (running_status() == STARTING || running_status() == RUNNING) |
| 1063 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); | 1066 embedded_worker_->SendMessage(ServiceWorkerMsg_DidSkipWaiting(request_id)); |
| 1064 } | 1067 } |
| 1065 | 1068 |
| 1066 void ServiceWorkerVersion::ScheduleStopWorker() { | 1069 void ServiceWorkerVersion::ScheduleStopWorker() { |
| 1067 // TODO(kinuko): Currently we don't schedule stop-time-worker when the SW has | 1070 if (running_status() != RUNNING) |
| 1068 // controllee, but we should change this default behavior (crbug.com/440259) | |
| 1069 if (running_status() != RUNNING || HasControllee()) | |
| 1070 return; | 1071 return; |
| 1071 if (stop_worker_timer_.IsRunning()) { | 1072 if (stop_worker_timer_.IsRunning()) { |
| 1072 stop_worker_timer_.Reset(); | 1073 stop_worker_timer_.Reset(); |
| 1073 return; | 1074 return; |
| 1074 } | 1075 } |
| 1075 stop_worker_timer_.Start( | 1076 stop_worker_timer_.Start( |
| 1076 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), | 1077 FROM_HERE, base::TimeDelta::FromSeconds(kStopWorkerDelay), |
| 1077 base::Bind(&ServiceWorkerVersion::StopWorkerIfIdle, | 1078 base::Bind(&ServiceWorkerVersion::StopWorkerIfIdle, |
| 1078 weak_factory_.GetWeakPtr())); | 1079 weak_factory_.GetWeakPtr())); |
| 1079 } | 1080 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1107 SetStatus(REDUNDANT); | 1108 SetStatus(REDUNDANT); |
| 1108 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1109 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1109 if (!context_) | 1110 if (!context_) |
| 1110 return; | 1111 return; |
| 1111 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | 1112 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 1112 script_cache_map_.GetResources(&resources); | 1113 script_cache_map_.GetResources(&resources); |
| 1113 context_->storage()->PurgeResources(resources); | 1114 context_->storage()->PurgeResources(resources); |
| 1114 } | 1115 } |
| 1115 | 1116 |
| 1116 } // namespace content | 1117 } // namespace content |
| OLD | NEW |