f72187e1bdfd2520816a660e6f3020f706910321
[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/stl_util.h"
8 #include "content/browser/service_worker/embedded_worker_registry.h"
9 #include "content/browser/service_worker/service_worker_registration.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 void ServiceWorkerJobCoordinator::JobQueue::Push(
22     scoped_ptr<ServiceWorkerRegisterJob> job,
23     int source_process_id,
24     const ServiceWorkerRegisterJob::RegistrationCallback& callback) {
25   if (jobs_.empty()) {
26     job->Start();
27     jobs_.push_back(job.release());
28   } else if (!job->Equals(jobs_.back())) {
29     jobs_.push_back(job.release());
30   }
31   // Note we are releasing 'job' here.
32
33   DCHECK(!jobs_.empty());
34   jobs_.back()->AddCallback(callback, source_process_id);
35 }
36
37 void ServiceWorkerJobCoordinator::JobQueue::Pop(ServiceWorkerRegisterJob* job) {
38   DCHECK(job == jobs_.front());
39   jobs_.pop_front();
40   delete job;
41   if (!jobs_.empty())
42     jobs_.front()->Start();
43 }
44
45 ServiceWorkerJobCoordinator::ServiceWorkerJobCoordinator(
46     ServiceWorkerStorage* storage,
47     EmbeddedWorkerRegistry* worker_registry)
48     : storage_(storage),
49       worker_registry_(worker_registry),
50       weak_factory_(this) {}
51
52 ServiceWorkerJobCoordinator::~ServiceWorkerJobCoordinator() {
53   DCHECK(jobs_.empty()) << "Destroying ServiceWorkerJobCoordinator with "
54                         << jobs_.size() << " job queues";
55 }
56
57 void ServiceWorkerJobCoordinator::Register(
58     const GURL& pattern,
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_,
64                                    worker_registry_,
65                                    this,
66                                    pattern,
67                                    script_url,
68                                    ServiceWorkerRegisterJob::REGISTER));
69   jobs_[pattern].Push(job.Pass(), source_process_id, callback);
70 }
71
72 void ServiceWorkerJobCoordinator::Unregister(
73     const GURL& pattern,
74     int source_process_id,
75     const ServiceWorkerRegisterJob::UnregistrationCallback& callback) {
76
77   scoped_ptr<ServiceWorkerRegisterJob> job = make_scoped_ptr(
78       new ServiceWorkerRegisterJob(storage_,
79                                    worker_registry_,
80                                    this,
81                                    pattern,
82                                    GURL(),
83                                    ServiceWorkerRegisterJob::UNREGISTER));
84   jobs_[pattern]
85       .Push(job.Pass(),
86             source_process_id,
87             base::Bind(&ServiceWorkerJobCoordinator::UnregisterComplete,
88                        weak_factory_.GetWeakPtr(),
89                        callback));
90 }
91
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);
99 }
100
101 void ServiceWorkerJobCoordinator::UnregisterComplete(
102     const ServiceWorkerRegisterJob::UnregistrationCallback& callback,
103     ServiceWorkerStatusCode status,
104     const scoped_refptr<ServiceWorkerRegistration>& previous_registration) {
105   callback.Run(status);
106 }
107
108 }  // namespace content