Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_job_coordinator.h
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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_COORDINATOR_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_COORDINATOR_H_
7
8 #include <deque>
9 #include <map>
10
11 #include "content/browser/service_worker/service_worker_register_job.h"
12 #include "content/browser/service_worker/service_worker_unregister_job.h"
13 #include "content/common/content_export.h"
14 #include "url/gurl.h"
15
16 namespace content {
17
18 class EmbeddedWorkerRegistry;
19 class ServiceWorkerRegistration;
20 class ServiceWorkerStorage;
21
22 // This class manages all in-flight registration or unregistration jobs.
23 class CONTENT_EXPORT ServiceWorkerJobCoordinator {
24  public:
25   explicit ServiceWorkerJobCoordinator(
26       base::WeakPtr<ServiceWorkerContextCore> context);
27   ~ServiceWorkerJobCoordinator();
28
29   void Register(const GURL& pattern,
30                 const GURL& script_url,
31                 int source_process_id,
32                 const ServiceWorkerRegisterJob::RegistrationCallback& callback);
33
34   void Unregister(
35       const GURL& pattern,
36       const ServiceWorkerUnregisterJob::UnregistrationCallback& callback);
37
38   // Jobs are removed whenever they are finished or canceled.
39   void FinishJob(const GURL& pattern, ServiceWorkerRegisterJobBase* job);
40
41  private:
42   class JobQueue {
43    public:
44     JobQueue();
45     ~JobQueue();
46
47     // Adds a job to the queue. If an identical job is already in the queue, no
48     // new job is added. Returns the job in the queue, regardless of whether it
49     // was newly added.
50     ServiceWorkerRegisterJobBase* Push(
51         scoped_ptr<ServiceWorkerRegisterJobBase> job);
52
53     // Removes a job from the queue.
54     void Pop(ServiceWorkerRegisterJobBase* job);
55
56     bool empty() { return jobs_.empty(); }
57
58    private:
59     std::deque<ServiceWorkerRegisterJobBase*> jobs_;
60   };
61
62   typedef std::map<GURL, JobQueue> RegistrationJobMap;
63
64   // The ServiceWorkerContextCore object should always outlive the
65   // job coordinator, the core owns the coordinator.
66   base::WeakPtr<ServiceWorkerContextCore> context_;
67   RegistrationJobMap jobs_;
68
69   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerJobCoordinator);
70 };
71
72 }  // namespace content
73
74 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_COORDINATOR_H_