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 // Calls ServiceWorkerRegisterJobBase::Abort() on all jobs and removes them. 39 void AbortAll(); 40 41 // Removes the job. A job that was not aborted must call FinishJob when it is 42 // done. 43 void FinishJob(const GURL& pattern, ServiceWorkerRegisterJobBase* job); 44 45 private: 46 class JobQueue { 47 public: 48 JobQueue(); 49 ~JobQueue(); 50 51 // Adds a job to the queue. If an identical job is already in the queue, no 52 // new job is added. Returns the job in the queue, regardless of whether it 53 // was newly added. 54 ServiceWorkerRegisterJobBase* Push( 55 scoped_ptr<ServiceWorkerRegisterJobBase> job); 56 57 // Removes a job from the queue. 58 void Pop(ServiceWorkerRegisterJobBase* job); 59 empty()60 bool empty() { return jobs_.empty(); } 61 62 // Aborts all jobs in the queue and removes them. 63 void AbortAll(); 64 65 // Marks that the browser is shutting down, so jobs may be destroyed before 66 // finishing. 67 void ClearForShutdown(); 68 69 private: 70 std::deque<ServiceWorkerRegisterJobBase*> jobs_; 71 }; 72 73 typedef std::map<GURL, JobQueue> RegistrationJobMap; 74 75 // The ServiceWorkerContextCore object should always outlive the 76 // job coordinator, the core owns the coordinator. 77 base::WeakPtr<ServiceWorkerContextCore> context_; 78 RegistrationJobMap job_queues_; 79 80 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerJobCoordinator); 81 }; 82 83 } // namespace content 84 85 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_COORDINATOR_H_ 86