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 16namespace content { 17 18class EmbeddedWorkerRegistry; 19class ServiceWorkerProviderHost; 20class ServiceWorkerRegistration; 21class ServiceWorkerStorage; 22 23// This class manages all in-flight registration or unregistration jobs. 24class CONTENT_EXPORT ServiceWorkerJobCoordinator { 25 public: 26 explicit ServiceWorkerJobCoordinator( 27 base::WeakPtr<ServiceWorkerContextCore> context); 28 ~ServiceWorkerJobCoordinator(); 29 30 void Register(const GURL& pattern, 31 const GURL& script_url, 32 ServiceWorkerProviderHost* provider_host, 33 const ServiceWorkerRegisterJob::RegistrationCallback& callback); 34 35 void Unregister( 36 const GURL& pattern, 37 const ServiceWorkerUnregisterJob::UnregistrationCallback& callback); 38 39 void Update(ServiceWorkerRegistration* registration); 40 41 // Calls ServiceWorkerRegisterJobBase::Abort() on all jobs and removes them. 42 void AbortAll(); 43 44 // Removes the job. A job that was not aborted must call FinishJob when it is 45 // done. 46 void FinishJob(const GURL& pattern, ServiceWorkerRegisterJobBase* job); 47 48 private: 49 class JobQueue { 50 public: 51 JobQueue(); 52 ~JobQueue(); 53 54 // Adds a job to the queue. If an identical job is already in the queue, no 55 // new job is added. Returns the job in the queue, regardless of whether it 56 // was newly added. 57 ServiceWorkerRegisterJobBase* Push( 58 scoped_ptr<ServiceWorkerRegisterJobBase> job); 59 60 // Removes a job from the queue. 61 void Pop(ServiceWorkerRegisterJobBase* job); 62 63 bool empty() { return jobs_.empty(); } 64 65 // Aborts all jobs in the queue and removes them. 66 void AbortAll(); 67 68 // Marks that the browser is shutting down, so jobs may be destroyed before 69 // finishing. 70 void ClearForShutdown(); 71 72 private: 73 std::deque<ServiceWorkerRegisterJobBase*> jobs_; 74 }; 75 76 typedef std::map<GURL, JobQueue> RegistrationJobMap; 77 78 // The ServiceWorkerContextCore object should always outlive the 79 // job coordinator, the core owns the coordinator. 80 base::WeakPtr<ServiceWorkerContextCore> context_; 81 RegistrationJobMap job_queues_; 82 83 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerJobCoordinator); 84}; 85 86} // namespace content 87 88#endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_JOB_COORDINATOR_H_ 89