Upstream version 7.36.149.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 #include <vector>
10
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/id_map.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list_threadsafe.h"
17 #include "content/browser/service_worker/service_worker_info.h"
18 #include "content/browser/service_worker/service_worker_process_manager.h"
19 #include "content/browser/service_worker/service_worker_provider_host.h"
20 #include "content/browser/service_worker/service_worker_registration_status.h"
21 #include "content/browser/service_worker/service_worker_storage.h"
22 #include "content/common/content_export.h"
23
24 class GURL;
25
26 namespace base {
27 class FilePath;
28 class SequencedTaskRunner;
29 }
30
31 namespace quota {
32 class QuotaManagerProxy;
33 }
34
35 namespace content {
36
37 class EmbeddedWorkerRegistry;
38 class ServiceWorkerContextObserver;
39 class ServiceWorkerHandle;
40 class ServiceWorkerJobCoordinator;
41 class ServiceWorkerProviderHost;
42 class ServiceWorkerRegistration;
43 class ServiceWorkerStorage;
44
45 // This class manages data associated with service workers.
46 // The class is single threaded and should only be used on the IO thread.
47 // In chromium, there is one instance per storagepartition. This class
48 // is the root of the containment hierarchy for service worker data
49 // associated with a particular partition.
50 class CONTENT_EXPORT ServiceWorkerContextCore
51     : NON_EXPORTED_BASE(public base::SupportsWeakPtr<ServiceWorkerContextCore>),
52       public ServiceWorkerVersion::Listener {
53  public:
54   typedef base::Callback<void(ServiceWorkerStatusCode status,
55                               int64 registration_id,
56                               int64 version_id)> RegistrationCallback;
57   typedef base::Callback<
58       void(ServiceWorkerStatusCode status)> UnregistrationCallback;
59   typedef IDMap<ServiceWorkerProviderHost, IDMapOwnPointer> ProviderMap;
60   typedef IDMap<ProviderMap, IDMapOwnPointer> ProcessToProviderMap;
61
62   // Iterates over ServiceWorkerProviderHost objects in a ProcessToProviderMap.
63   class ProviderHostIterator {
64    public:
65     ~ProviderHostIterator();
66     ServiceWorkerProviderHost* GetProviderHost();
67     void Advance();
68     bool IsAtEnd();
69
70    private:
71     friend class ServiceWorkerContextCore;
72     explicit ProviderHostIterator(ProcessToProviderMap* map);
73     void Initialize();
74
75     ProcessToProviderMap* map_;
76     scoped_ptr<ProcessToProviderMap::iterator> provider_iterator_;
77     scoped_ptr<ProviderMap::iterator> provider_host_iterator_;
78
79     DISALLOW_COPY_AND_ASSIGN(ProviderHostIterator);
80   };
81
82   // This is owned by the StoragePartition, which will supply it with
83   // the local path on disk. Given an empty |user_data_directory|,
84   // nothing will be stored on disk. |observer_list| is created in
85   // ServiceWorkerContextWrapper. When Notify() of |observer_list| is called in
86   // ServiceWorkerContextCore, the methods of ServiceWorkerContextObserver will
87   // be called on the thread which called AddObserver() of |observer_list|.
88   ServiceWorkerContextCore(
89       const base::FilePath& user_data_directory,
90       base::SequencedTaskRunner* database_task_runner,
91       quota::QuotaManagerProxy* quota_manager_proxy,
92       ObserverListThreadSafe<ServiceWorkerContextObserver>* observer_list,
93       scoped_ptr<ServiceWorkerProcessManager> process_manager);
94   virtual ~ServiceWorkerContextCore();
95
96   // ServiceWorkerVersion::Listener overrides.
97   virtual void OnWorkerStarted(ServiceWorkerVersion* version) OVERRIDE;
98   virtual void OnWorkerStopped(ServiceWorkerVersion* version) OVERRIDE;
99   virtual void OnVersionStateChanged(ServiceWorkerVersion* version) OVERRIDE;
100   virtual void OnErrorReported(ServiceWorkerVersion* version,
101                                const base::string16& error_message,
102                                int line_number,
103                                int column_number,
104                                const GURL& source_url) OVERRIDE;
105   virtual void OnReportConsoleMessage(ServiceWorkerVersion* version,
106                                       int source_identifier,
107                                       int message_level,
108                                       const base::string16& message,
109                                       int line_number,
110                                       const GURL& source_url) OVERRIDE;
111
112   ServiceWorkerStorage* storage() { return storage_.get(); }
113   ServiceWorkerProcessManager* process_manager() {
114     return process_manager_.get();
115   }
116   EmbeddedWorkerRegistry* embedded_worker_registry() {
117     return embedded_worker_registry_.get();
118   }
119   ServiceWorkerJobCoordinator* job_coordinator() {
120     return job_coordinator_.get();
121   }
122
123   // The context class owns the set of ProviderHosts.
124   ServiceWorkerProviderHost* GetProviderHost(int process_id, int provider_id);
125   void AddProviderHost(scoped_ptr<ServiceWorkerProviderHost> provider_host);
126   void RemoveProviderHost(int process_id, int provider_id);
127   void RemoveAllProviderHostsForProcess(int process_id);
128   scoped_ptr<ProviderHostIterator> GetProviderHostIterator();
129
130   // The callback will be called on the IO thread.
131   // A child process of |source_process_id| may be used to run the created
132   // worker for initial installation.
133   // Non-null |provider_host| must be given if this is called from a document,
134   // whose process_id() must match with |source_process_id|.
135   void RegisterServiceWorker(const GURL& pattern,
136                              const GURL& script_url,
137                              int source_process_id,
138                              ServiceWorkerProviderHost* provider_host,
139                              const RegistrationCallback& callback);
140
141   // The callback will be called on the IO thread.
142   void UnregisterServiceWorker(const GURL& pattern,
143                                const UnregistrationCallback& callback);
144
145   // This class maintains collections of live instances, this class
146   // does not own these object or influence their lifetime.
147   ServiceWorkerRegistration* GetLiveRegistration(int64 registration_id);
148   void AddLiveRegistration(ServiceWorkerRegistration* registration);
149   void RemoveLiveRegistration(int64 registration_id);
150   ServiceWorkerVersion* GetLiveVersion(int64 version_id);
151   void AddLiveVersion(ServiceWorkerVersion* version);
152   void RemoveLiveVersion(int64 registration_id);
153
154   // Returns new context-local unique ID for ServiceWorkerHandle.
155   int GetNewServiceWorkerHandleId();
156
157   // Allows tests to change how processes are created.
158   void SetProcessManagerForTest(
159       scoped_ptr<ServiceWorkerProcessManager> new_process_manager) {
160     process_manager_ = new_process_manager.Pass();
161   }
162
163  private:
164   typedef std::map<int64, ServiceWorkerRegistration*> RegistrationsMap;
165   typedef std::map<int64, ServiceWorkerVersion*> VersionMap;
166
167   ProviderMap* GetProviderMapForProcess(int process_id) {
168     return providers_.Lookup(process_id);
169   }
170
171   void RegistrationComplete(
172       const RegistrationCallback& callback,
173       ServiceWorkerStatusCode status,
174       ServiceWorkerRegistration* registration,
175       ServiceWorkerVersion* version);
176
177   ProcessToProviderMap providers_;
178   scoped_ptr<ServiceWorkerStorage> storage_;
179   scoped_refptr<EmbeddedWorkerRegistry> embedded_worker_registry_;
180   scoped_ptr<ServiceWorkerJobCoordinator> job_coordinator_;
181   scoped_ptr<ServiceWorkerProcessManager> process_manager_;
182   std::map<int64, ServiceWorkerRegistration*> live_registrations_;
183   std::map<int64, ServiceWorkerVersion*> live_versions_;
184   int next_handle_id_;
185
186   scoped_refptr<ObserverListThreadSafe<ServiceWorkerContextObserver> >
187       observer_list_;
188
189   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContextCore);
190 };
191
192 }  // namespace content
193
194 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CORE_H_