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.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CORE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CORE_H_
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"
27 class QuotaManagerProxy;
32 class EmbeddedWorkerRegistry;
33 class ServiceWorkerJobCoordinator;
34 class ServiceWorkerProviderHost;
35 class ServiceWorkerRegistration;
36 class ServiceWorkerStorage;
38 // This class manages data associated with service workers.
39 // The class is single threaded and should only be used on the IO thread.
40 // In chromium, there is one instance per storagepartition. This class
41 // is the root of the containment hierarchy for service worker data
42 // associated with a particular partition.
43 class CONTENT_EXPORT ServiceWorkerContextCore
45 public base::SupportsWeakPtr<ServiceWorkerContextCore>) {
47 typedef base::Callback<void(ServiceWorkerStatusCode status,
48 int64 registration_id)> RegistrationCallback;
49 typedef base::Callback<
50 void(ServiceWorkerStatusCode status)> UnregistrationCallback;
52 // This is owned by the StoragePartition, which will supply it with
53 // the local path on disk. Given an empty |user_data_directory|,
54 // nothing will be stored on disk.
55 ServiceWorkerContextCore(const base::FilePath& user_data_directory,
56 quota::QuotaManagerProxy* quota_manager_proxy);
57 ~ServiceWorkerContextCore();
59 ServiceWorkerStorage* storage() { return storage_.get(); }
61 // The context class owns the set of ProviderHosts.
62 ServiceWorkerProviderHost* GetProviderHost(int process_id, int provider_id);
63 void AddProviderHost(scoped_ptr<ServiceWorkerProviderHost> provider_host);
64 void RemoveProviderHost(int process_id, int provider_id);
65 void RemoveAllProviderHostsForProcess(int process_id);
67 // Checks the cmdline flag.
70 // The callback will be called on the IO thread.
71 void RegisterServiceWorker(const GURL& pattern,
72 const GURL& script_url,
73 int source_process_id,
74 const RegistrationCallback& callback);
76 // The callback will be called on the IO thread.
77 void UnregisterServiceWorker(const GURL& pattern,
78 int source_process_id,
79 const UnregistrationCallback& callback);
81 EmbeddedWorkerRegistry* embedded_worker_registry() {
82 return embedded_worker_registry_.get();
85 ServiceWorkerJobCoordinator* job_coordinator() {
86 return job_coordinator_.get();
90 typedef IDMap<ServiceWorkerProviderHost, IDMapOwnPointer> ProviderMap;
91 typedef IDMap<ProviderMap, IDMapOwnPointer> ProcessToProviderMap;
93 ProviderMap* GetProviderMapForProcess(int process_id) {
94 return providers_.Lookup(process_id);
97 void RegistrationComplete(
98 const RegistrationCallback& callback,
99 ServiceWorkerStatusCode status,
100 const scoped_refptr<ServiceWorkerRegistration>& registration);
102 ProcessToProviderMap providers_;
103 scoped_ptr<ServiceWorkerStorage> storage_;
104 scoped_refptr<EmbeddedWorkerRegistry> embedded_worker_registry_;
105 scoped_ptr<ServiceWorkerJobCoordinator> job_coordinator_;
107 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContextCore);
110 } // namespace content
112 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CORE_H_