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/memory/scoped_ptr.h"
8 #include "base/stl_util.h"
9 #include "content/browser/service_worker/service_worker_register_job_base.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 ServiceWorkerRegisterJobBase* ServiceWorkerJobCoordinator::JobQueue::Push(
22 scoped_ptr<ServiceWorkerRegisterJobBase> job) {
25 jobs_.push_back(job.release());
26 } else if (!job->Equals(jobs_.back())) {
27 jobs_.push_back(job.release());
29 // Note we are releasing 'job' here.
31 DCHECK(!jobs_.empty());
35 void ServiceWorkerJobCoordinator::JobQueue::Pop(
36 ServiceWorkerRegisterJobBase* job) {
37 DCHECK(job == jobs_.front());
41 jobs_.front()->Start();
44 ServiceWorkerJobCoordinator::ServiceWorkerJobCoordinator(
45 base::WeakPtr<ServiceWorkerContextCore> context)
49 ServiceWorkerJobCoordinator::~ServiceWorkerJobCoordinator() {
50 DCHECK(jobs_.empty()) << "Destroying ServiceWorkerJobCoordinator with "
51 << jobs_.size() << " job queues";
54 void ServiceWorkerJobCoordinator::Register(
56 const GURL& script_url,
57 int source_process_id,
58 const ServiceWorkerRegisterJob::RegistrationCallback& callback) {
59 scoped_ptr<ServiceWorkerRegisterJobBase> job(
60 new ServiceWorkerRegisterJob(context_, pattern, script_url));
61 ServiceWorkerRegisterJob* queued_job =
62 static_cast<ServiceWorkerRegisterJob*>(jobs_[pattern].Push(job.Pass()));
63 queued_job->AddCallback(callback, source_process_id);
66 void ServiceWorkerJobCoordinator::Unregister(
68 const ServiceWorkerUnregisterJob::UnregistrationCallback& callback) {
69 scoped_ptr<ServiceWorkerRegisterJobBase> job(
70 new ServiceWorkerUnregisterJob(context_, pattern));
71 ServiceWorkerUnregisterJob* queued_job =
72 static_cast<ServiceWorkerUnregisterJob*>(jobs_[pattern].Push(job.Pass()));
73 queued_job->AddCallback(callback);
76 void ServiceWorkerJobCoordinator::FinishJob(const GURL& pattern,
77 ServiceWorkerRegisterJobBase* job) {
78 RegistrationJobMap::iterator pending_jobs = jobs_.find(pattern);
79 DCHECK(pending_jobs != jobs_.end()) << "Deleting non-existent job.";
80 pending_jobs->second.Pop(job);
81 if (pending_jobs->second.empty())
82 jobs_.erase(pending_jobs);
85 } // namespace content