| 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" |
| 11 #include "content/browser/service_worker/embedded_worker_registry.h" | 11 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 12 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
| 13 #include "content/browser/service_worker/service_worker_process_manager.h" |
| 13 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
| 14 #include "content/browser/service_worker/service_worker_utils.h" | 15 #include "content/browser/service_worker/service_worker_utils.h" |
| 15 #include "content/common/service_worker/service_worker_messages.h" | 16 #include "content/common/service_worker/service_worker_messages.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 typedef ServiceWorkerVersion::StatusCallback StatusCallback; | 22 typedef ServiceWorkerVersion::StatusCallback StatusCallback; |
| 22 typedef ServiceWorkerVersion::MessageCallback MessageCallback; | 23 typedef ServiceWorkerVersion::MessageCallback MessageCallback; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 return ServiceWorkerVersionInfo( | 152 return ServiceWorkerVersionInfo( |
| 152 running_status(), | 153 running_status(), |
| 153 status(), | 154 status(), |
| 154 version_id(), | 155 version_id(), |
| 155 embedded_worker()->process_id(), | 156 embedded_worker()->process_id(), |
| 156 embedded_worker()->thread_id(), | 157 embedded_worker()->thread_id(), |
| 157 embedded_worker()->worker_devtools_agent_route_id()); | 158 embedded_worker()->worker_devtools_agent_route_id()); |
| 158 } | 159 } |
| 159 | 160 |
| 160 void ServiceWorkerVersion::StartWorker(const StatusCallback& callback) { | 161 void ServiceWorkerVersion::StartWorker(const StatusCallback& callback) { |
| 161 StartWorkerWithCandidateProcesses(std::vector<int>(), false, callback); | 162 StartWorker(false, callback); |
| 162 } | 163 } |
| 163 | 164 |
| 164 void ServiceWorkerVersion::StartWorkerWithCandidateProcesses( | 165 void ServiceWorkerVersion::StartWorker( |
| 165 const std::vector<int>& possible_process_ids, | |
| 166 bool pause_after_download, | 166 bool pause_after_download, |
| 167 const StatusCallback& callback) { | 167 const StatusCallback& callback) { |
| 168 switch (running_status()) { | 168 switch (running_status()) { |
| 169 case RUNNING: | 169 case RUNNING: |
| 170 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | 170 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); |
| 171 return; | 171 return; |
| 172 case STOPPING: | 172 case STOPPING: |
| 173 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); | 173 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); |
| 174 return; | 174 return; |
| 175 case STOPPED: | 175 case STOPPED: |
| 176 case STARTING: | 176 case STARTING: |
| 177 start_callbacks_.push_back(callback); | 177 start_callbacks_.push_back(callback); |
| 178 if (running_status() == STOPPED) { | 178 if (running_status() == STOPPED) { |
| 179 embedded_worker_->Start( | 179 embedded_worker_->Start( |
| 180 version_id_, | 180 version_id_, |
| 181 scope_, | 181 scope_, |
| 182 script_url_, | 182 script_url_, |
| 183 pause_after_download, | 183 pause_after_download, |
| 184 possible_process_ids, | |
| 185 base::Bind(&ServiceWorkerVersion::RunStartWorkerCallbacksOnError, | 184 base::Bind(&ServiceWorkerVersion::RunStartWorkerCallbacksOnError, |
| 186 weak_factory_.GetWeakPtr())); | 185 weak_factory_.GetWeakPtr())); |
| 187 } | 186 } |
| 188 return; | 187 return; |
| 189 } | 188 } |
| 190 } | 189 } |
| 191 | 190 |
| 192 void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { | 191 void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) { |
| 193 if (running_status() == STOPPED) { | 192 if (running_status() == STOPPED) { |
| 194 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | 193 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 int request_id = push_callbacks_.Add(new StatusCallback(callback)); | 362 int request_id = push_callbacks_.Add(new StatusCallback(callback)); |
| 364 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( | 363 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( |
| 365 ServiceWorkerMsg_PushEvent(request_id, data)); | 364 ServiceWorkerMsg_PushEvent(request_id, data)); |
| 366 if (status != SERVICE_WORKER_OK) { | 365 if (status != SERVICE_WORKER_OK) { |
| 367 push_callbacks_.Remove(request_id); | 366 push_callbacks_.Remove(request_id); |
| 368 RunSoon(base::Bind(callback, status)); | 367 RunSoon(base::Bind(callback, status)); |
| 369 } | 368 } |
| 370 } | 369 } |
| 371 | 370 |
| 372 void ServiceWorkerVersion::AddProcessToWorker(int process_id) { | 371 void ServiceWorkerVersion::AddProcessToWorker(int process_id) { |
| 373 embedded_worker_->AddProcessReference(process_id); | 372 if (context_ && context_->process_manager()) { |
| 373 context_->process_manager()->AddScopeProcessReference( |
| 374 scope_, process_id); |
| 375 } |
| 374 } | 376 } |
| 375 | 377 |
| 376 void ServiceWorkerVersion::RemoveProcessFromWorker(int process_id) { | 378 void ServiceWorkerVersion::RemoveProcessFromWorker(int process_id) { |
| 377 embedded_worker_->ReleaseProcessReference(process_id); | 379 if (context_ && context_->process_manager()) { |
| 378 } | 380 context_->process_manager()->RemoveScopeProcessReference( |
| 379 | 381 scope_, process_id); |
| 380 bool ServiceWorkerVersion::HasProcessToRun() const { | 382 } |
| 381 return embedded_worker_->HasProcessToRun(); | |
| 382 } | 383 } |
| 383 | 384 |
| 384 void ServiceWorkerVersion::AddControllee( | 385 void ServiceWorkerVersion::AddControllee( |
| 385 ServiceWorkerProviderHost* provider_host) { | 386 ServiceWorkerProviderHost* provider_host) { |
| 386 DCHECK(!ContainsKey(controllee_map_, provider_host)); | 387 DCHECK(!ContainsKey(controllee_map_, provider_host)); |
| 387 int controllee_id = controllee_by_id_.Add(provider_host); | 388 int controllee_id = controllee_by_id_.Add(provider_host); |
| 388 controllee_map_[provider_host] = controllee_id; | 389 controllee_map_[provider_host] = controllee_id; |
| 389 AddProcessToWorker(provider_host->process_id()); | 390 AddProcessToWorker(provider_host->process_id()); |
| 390 if (stop_worker_timer_.IsRunning()) | 391 if (stop_worker_timer_.IsRunning()) |
| 391 stop_worker_timer_.Stop(); | 392 stop_worker_timer_.Stop(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 411 void ServiceWorkerVersion::AddPotentialControllee( | 412 void ServiceWorkerVersion::AddPotentialControllee( |
| 412 ServiceWorkerProviderHost* provider_host) { | 413 ServiceWorkerProviderHost* provider_host) { |
| 413 AddProcessToWorker(provider_host->process_id()); | 414 AddProcessToWorker(provider_host->process_id()); |
| 414 } | 415 } |
| 415 | 416 |
| 416 void ServiceWorkerVersion::RemovePotentialControllee( | 417 void ServiceWorkerVersion::RemovePotentialControllee( |
| 417 ServiceWorkerProviderHost* provider_host) { | 418 ServiceWorkerProviderHost* provider_host) { |
| 418 RemoveProcessFromWorker(provider_host->process_id()); | 419 RemoveProcessFromWorker(provider_host->process_id()); |
| 419 } | 420 } |
| 420 | 421 |
| 422 void ServiceWorkerVersion::AddPendingProcesses( |
| 423 const std::vector<int>& pending_processes) { |
| 424 if (context_ && context_->process_manager() && !pending_processes.empty()) { |
| 425 context_->process_manager()->AddScopePendingProcesses( |
| 426 scope_, pending_processes); |
| 427 } |
| 428 } |
| 429 |
| 421 void ServiceWorkerVersion::AddListener(Listener* listener) { | 430 void ServiceWorkerVersion::AddListener(Listener* listener) { |
| 422 listeners_.AddObserver(listener); | 431 listeners_.AddObserver(listener); |
| 423 } | 432 } |
| 424 | 433 |
| 425 void ServiceWorkerVersion::RemoveListener(Listener* listener) { | 434 void ServiceWorkerVersion::RemoveListener(Listener* listener) { |
| 426 listeners_.RemoveObserver(listener); | 435 listeners_.RemoveObserver(listener); |
| 427 } | 436 } |
| 428 | 437 |
| 429 void ServiceWorkerVersion::Doom() { | 438 void ServiceWorkerVersion::Doom() { |
| 430 if (is_doomed_) | 439 if (is_doomed_) |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 SetStatus(REDUNDANT); | 699 SetStatus(REDUNDANT); |
| 691 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 700 StopWorker(base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 692 if (!context_) | 701 if (!context_) |
| 693 return; | 702 return; |
| 694 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; | 703 std::vector<ServiceWorkerDatabase::ResourceRecord> resources; |
| 695 script_cache_map_.GetResources(&resources); | 704 script_cache_map_.GetResources(&resources); |
| 696 context_->storage()->PurgeResources(resources); | 705 context_->storage()->PurgeResources(resources); |
| 697 } | 706 } |
| 698 | 707 |
| 699 } // namespace content | 708 } // namespace content |
| OLD | NEW |