1 // Copyright 2013 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_CONTEXT_CORE_H_ 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CORE_H_ 7 8 #include <map> 9 10 #include "base/callback.h" 11 #include "base/files/file_path.h" 12 #include "base/id_map.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "content/browser/service_worker/service_worker_provider_host.h" 16 #include "content/browser/service_worker/service_worker_registration_status.h" 17 #include "content/browser/service_worker/service_worker_storage.h" 18 #include "content/common/content_export.h" 19 20 class GURL; 21 22 namespace base { 23 class FilePath; 24 } 25 26 namespace quota { 27 class QuotaManagerProxy; 28 } 29 30 namespace content { 31 32 class EmbeddedWorkerRegistry; 33 class ServiceWorkerProviderHost; 34 class ServiceWorkerRegistration; 35 class ServiceWorkerStorage; 36 37 // This class manages data associated with service workers. 38 // The class is single threaded and should only be used on the IO thread. 39 // In chromium, there is one instance per storagepartition. This class 40 // is the root of the containment hierarchy for service worker data 41 // associated with a particular partition. 42 class CONTENT_EXPORT ServiceWorkerContextCore NON_EXPORTED_BASE(public base::SupportsWeakPtr<ServiceWorkerContextCore>)43 : NON_EXPORTED_BASE( 44 public base::SupportsWeakPtr<ServiceWorkerContextCore>) { 45 public: 46 typedef base::Callback<void(ServiceWorkerRegistrationStatus status, 47 int64 registration_id)> RegistrationCallback; 48 typedef base::Callback< 49 void(ServiceWorkerRegistrationStatus status)> UnregistrationCallback; 50 51 // This is owned by the StoragePartition, which will supply it with 52 // the local path on disk. Given an empty |user_data_directory|, 53 // nothing will be stored on disk. 54 ServiceWorkerContextCore(const base::FilePath& user_data_directory, 55 quota::QuotaManagerProxy* quota_manager_proxy); 56 ~ServiceWorkerContextCore(); 57 58 ServiceWorkerStorage* storage() { return storage_.get(); } 59 60 // The context class owns the set of ProviderHosts. 61 ServiceWorkerProviderHost* GetProviderHost(int process_id, int provider_id); 62 void AddProviderHost(scoped_ptr<ServiceWorkerProviderHost> provider_host); 63 void RemoveProviderHost(int process_id, int provider_id); 64 void RemoveAllProviderHostsForProcess(int process_id); 65 66 // Checks the cmdline flag. 67 bool IsEnabled(); 68 69 // The callback will be called on the IO thread. 70 void RegisterServiceWorker(const GURL& pattern, 71 const GURL& script_url, 72 const RegistrationCallback& callback); 73 74 // The callback will be called on the IO thread. 75 void UnregisterServiceWorker(const GURL& pattern, 76 const UnregistrationCallback& callback); 77 78 EmbeddedWorkerRegistry* embedded_worker_registry() { 79 return embedded_worker_registry_.get(); 80 } 81 82 private: 83 typedef IDMap<ServiceWorkerProviderHost, IDMapOwnPointer> ProviderMap; 84 typedef IDMap<ProviderMap, IDMapOwnPointer> ProcessToProviderMap; 85 86 ProviderMap* GetProviderMapForProcess(int process_id) { 87 return providers_.Lookup(process_id); 88 } 89 90 void RegistrationComplete( 91 const RegistrationCallback& callback, 92 ServiceWorkerRegistrationStatus status, 93 const scoped_refptr<ServiceWorkerRegistration>& registration); 94 95 ProcessToProviderMap providers_; 96 scoped_ptr<ServiceWorkerStorage> storage_; 97 scoped_refptr<EmbeddedWorkerRegistry> embedded_worker_registry_; 98 99 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContextCore); 100 }; 101 102 } // namespace content 103 104 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CORE_H_ 105