Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_context_core.h
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 ServiceWorkerJobCoordinator;
34 class ServiceWorkerProviderHost;
35 class ServiceWorkerRegistration;
36 class ServiceWorkerStorage;
37
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
44     : NON_EXPORTED_BASE(
45           public base::SupportsWeakPtr<ServiceWorkerContextCore>) {
46  public:
47   typedef base::Callback<void(ServiceWorkerStatusCode status,
48                               int64 registration_id)> RegistrationCallback;
49   typedef base::Callback<
50       void(ServiceWorkerStatusCode status)> UnregistrationCallback;
51
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();
58
59   ServiceWorkerStorage* storage() { return storage_.get(); }
60
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);
66
67   // Checks the cmdline flag.
68   bool IsEnabled();
69
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);
75
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);
80
81   EmbeddedWorkerRegistry* embedded_worker_registry() {
82     return embedded_worker_registry_.get();
83   }
84
85   ServiceWorkerJobCoordinator* job_coordinator() {
86     return job_coordinator_.get();
87   }
88
89  private:
90   typedef IDMap<ServiceWorkerProviderHost, IDMapOwnPointer> ProviderMap;
91   typedef IDMap<ProviderMap, IDMapOwnPointer> ProcessToProviderMap;
92
93   ProviderMap* GetProviderMapForProcess(int process_id) {
94     return providers_.Lookup(process_id);
95   }
96
97   void RegistrationComplete(
98       const RegistrationCallback& callback,
99       ServiceWorkerStatusCode status,
100       const scoped_refptr<ServiceWorkerRegistration>& registration);
101
102   ProcessToProviderMap providers_;
103   scoped_ptr<ServiceWorkerStorage> storage_;
104   scoped_refptr<EmbeddedWorkerRegistry> embedded_worker_registry_;
105   scoped_ptr<ServiceWorkerJobCoordinator> job_coordinator_;
106
107   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContextCore);
108 };
109
110 }  // namespace content
111
112 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CORE_H_