1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_job_coordinator.h"
7 #include "base/stl_util.h"
8 #include "content/browser/service_worker/embedded_worker_registry.h"
9 #include "content/browser/service_worker/service_worker_registration.h"
13 ServiceWorkerJobCoordinator::JobQueue::JobQueue() {}
15 ServiceWorkerJobCoordinator::JobQueue::~JobQueue() {
16 DCHECK(jobs_.empty()) << "Destroying JobQueue with " << jobs_.size()
17 << " unfinished jobs";
18 STLDeleteElements(&jobs_);
21 void ServiceWorkerJobCoordinator::JobQueue::Push(
22 scoped_ptr<ServiceWorkerRegisterJob> job,
23 int source_process_id,
24 const ServiceWorkerRegisterJob::RegistrationCallback& callback) {
27 jobs_.push_back(job.release());
28 } else if (!job->Equals(jobs_.back())) {
29 jobs_.push_back(job.release());
31 // Note we are releasing 'job' here.
33 DCHECK(!jobs_.empty());
34 jobs_.back()->AddCallback(callback, source_process_id);
37 void ServiceWorkerJobCoordinator::JobQueue::Pop(ServiceWorkerRegisterJob* job) {
38 DCHECK(job == jobs_.front());
42 jobs_.front()->Start();
45 ServiceWorkerJobCoordinator::ServiceWorkerJobCoordinator(
46 ServiceWorkerStorage* storage,
47 EmbeddedWorkerRegistry* worker_registry)
49 worker_registry_(worker_registry),
50 weak_factory_(this) {}
52 ServiceWorkerJobCoordinator::~ServiceWorkerJobCoordinator() {
53 DCHECK(jobs_.empty()) << "Destroying ServiceWorkerJobCoordinator with "
54 << jobs_.size() << " job queues";
57 void ServiceWorkerJobCoordinator::Register(
59 const GURL& script_url,
60 int source_process_id,
61 const ServiceWorkerRegisterJob::RegistrationCallback& callback) {
62 scoped_ptr<ServiceWorkerRegisterJob> job = make_scoped_ptr(
63 new ServiceWorkerRegisterJob(storage_,
68 ServiceWorkerRegisterJob::REGISTER));
69 jobs_[pattern].Push(job.Pass(), source_process_id, callback);
72 void ServiceWorkerJobCoordinator::Unregister(
74 int source_process_id,
75 const ServiceWorkerRegisterJob::UnregistrationCallback& callback) {
77 scoped_ptr<ServiceWorkerRegisterJob> job = make_scoped_ptr(
78 new ServiceWorkerRegisterJob(storage_,
83 ServiceWorkerRegisterJob::UNREGISTER));
87 base::Bind(&ServiceWorkerJobCoordinator::UnregisterComplete,
88 weak_factory_.GetWeakPtr(),
92 void ServiceWorkerJobCoordinator::FinishJob(const GURL& pattern,
93 ServiceWorkerRegisterJob* job) {
94 RegistrationJobMap::iterator pending_jobs = jobs_.find(pattern);
95 DCHECK(pending_jobs != jobs_.end()) << "Deleting non-existent job.";
96 pending_jobs->second.Pop(job);
97 if (pending_jobs->second.empty())
98 jobs_.erase(pending_jobs);
101 void ServiceWorkerJobCoordinator::UnregisterComplete(
102 const ServiceWorkerRegisterJob::UnregistrationCallback& callback,
103 ServiceWorkerStatusCode status,
104 const scoped_refptr<ServiceWorkerRegistration>& previous_registration) {
105 callback.Run(status);
108 } // namespace content