Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_job_coordinator.cc
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.
4
5 #include "content/browser/service_worker/service_worker_job_coordinator.h"
6
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"
10
11 namespace content {
12
13 ServiceWorkerJobCoordinator::JobQueue::JobQueue() {}
14
15 ServiceWorkerJobCoordinator::JobQueue::~JobQueue() {
16   DCHECK(jobs_.empty()) << "Destroying JobQueue with " << jobs_.size()
17                         << " unfinished jobs";
18   STLDeleteElements(&jobs_);
19 }
20
21 ServiceWorkerRegisterJobBase* ServiceWorkerJobCoordinator::JobQueue::Push(
22     scoped_ptr<ServiceWorkerRegisterJobBase> job) {
23   if (jobs_.empty()) {
24     job->Start();
25     jobs_.push_back(job.release());
26   } else if (!job->Equals(jobs_.back())) {
27     jobs_.push_back(job.release());
28   }
29   // Note we are releasing 'job' here.
30
31   DCHECK(!jobs_.empty());
32   return jobs_.back();
33 }
34
35 void ServiceWorkerJobCoordinator::JobQueue::Pop(
36     ServiceWorkerRegisterJobBase* job) {
37   DCHECK(job == jobs_.front());
38   jobs_.pop_front();
39   delete job;
40   if (!jobs_.empty())
41     jobs_.front()->Start();
42 }
43
44 ServiceWorkerJobCoordinator::ServiceWorkerJobCoordinator(
45     base::WeakPtr<ServiceWorkerContextCore> context)
46     : context_(context) {
47 }
48
49 ServiceWorkerJobCoordinator::~ServiceWorkerJobCoordinator() {
50   DCHECK(jobs_.empty()) << "Destroying ServiceWorkerJobCoordinator with "
51                         << jobs_.size() << " job queues";
52 }
53
54 void ServiceWorkerJobCoordinator::Register(
55     const GURL& pattern,
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);
64 }
65
66 void ServiceWorkerJobCoordinator::Unregister(
67     const GURL& pattern,
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);
74 }
75
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);
83 }
84
85 }  // namespace content