Upstream version 5.34.92.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 "base/bind.h"
12 #include "content/browser/service_worker/service_worker_register_job.h"
13 #include "content/browser/service_worker/service_worker_registration_status.h"
14 #include "content/browser/service_worker/service_worker_storage.h"
15 #include "content/common/content_export.h"
16 #include "url/gurl.h"
17
18 namespace content {
19
20 class ServiceWorkerRegistration;
21
22 // This class manages all in-flight jobs. Any asynchronous
23 // operations are run through instances of ServiceWorkerRegisterJob.
24 class CONTENT_EXPORT ServiceWorkerJobCoordinator {
25  public:
26   explicit ServiceWorkerJobCoordinator(ServiceWorkerStorage* storage);
27   ~ServiceWorkerJobCoordinator();
28
29   void Register(const GURL& pattern,
30                 const GURL& script_url,
31                 const ServiceWorkerRegisterJob::RegistrationCallback& callback);
32
33   void Unregister(
34       const GURL& pattern,
35       const ServiceWorkerRegisterJob::UnregistrationCallback& callback);
36
37   // Jobs are removed whenever they are finished or canceled.
38   void FinishJob(const GURL& pattern, ServiceWorkerRegisterJob* job);
39
40  private:
41   friend class ServiceWorkerRegisterJob;
42
43   class JobQueue {
44    public:
45     JobQueue();
46     ~JobQueue();
47
48     void Push(scoped_ptr<ServiceWorkerRegisterJob> job,
49               const ServiceWorkerRegisterJob::RegistrationCallback& callback);
50
51     void Pop(ServiceWorkerRegisterJob* job);
52
53     bool empty() { return jobs_.empty(); }
54
55    private:
56     std::deque<ServiceWorkerRegisterJob*> jobs_;
57   };
58
59   typedef std::map<GURL, JobQueue> RegistrationJobMap;
60
61   // Called at ServiceWorkerRegisterJob completion.
62   void UnregisterComplete(
63       const ServiceWorkerRegisterJob::UnregistrationCallback& callback,
64       ServiceWorkerStatusCode status,
65       const scoped_refptr<ServiceWorkerRegistration>& registration);
66
67   // The ServiceWorkerStorage object should always outlive this
68   ServiceWorkerStorage* storage_;
69   base::WeakPtrFactory<ServiceWorkerJobCoordinator> weak_factory_;
70
71   RegistrationJobMap jobs_;
72
73   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerJobCoordinator);
74 };
75
76 }  // namespace content
77
78 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_COORDINATOR_H_