Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_storage.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_STORAGE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_
7
8 #include <map>
9 #include <set>
10 #include <vector>
11
12 #include "base/bind.h"
13 #include "base/files/file_path.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/memory/weak_ptr.h"
17 #include "content/browser/service_worker/service_worker_database.h"
18 #include "content/common/content_export.h"
19 #include "content/common/service_worker/service_worker_status_code.h"
20 #include "url/gurl.h"
21
22 namespace base {
23 class SequencedTaskRunner;
24 }
25
26 namespace quota {
27 class QuotaManagerProxy;
28 }
29
30 namespace content {
31
32 class ServiceWorkerContextCore;
33 class ServiceWorkerDiskCache;
34 class ServiceWorkerRegistration;
35 class ServiceWorkerRegistrationInfo;
36 class ServiceWorkerResponseReader;
37 class ServiceWorkerResponseWriter;
38 class ServiceWorkerVersion;
39
40 // This class provides an interface to store and retrieve ServiceWorker
41 // registration data.
42 class CONTENT_EXPORT ServiceWorkerStorage {
43  public:
44   typedef base::Callback<void(ServiceWorkerStatusCode status)> StatusCallback;
45   typedef base::Callback<void(ServiceWorkerStatusCode status,
46                               const scoped_refptr<ServiceWorkerRegistration>&
47                                   registration)> FindRegistrationCallback;
48   typedef base::Callback<
49       void(const std::vector<ServiceWorkerRegistrationInfo>& registrations)>
50           GetAllRegistrationInfosCallback;
51   typedef base::Callback<
52       void(ServiceWorkerStatusCode status, int result)>
53           CompareCallback;
54
55   struct InitialData {
56     int64 next_registration_id;
57     int64 next_version_id;
58     int64 next_resource_id;
59     std::set<GURL> origins;
60
61     InitialData();
62     ~InitialData();
63   };
64
65   ServiceWorkerStorage(const base::FilePath& path,
66                        base::WeakPtr<ServiceWorkerContextCore> context,
67                        base::SequencedTaskRunner* database_task_runner,
68                        quota::QuotaManagerProxy* quota_manager_proxy);
69   ~ServiceWorkerStorage();
70
71   // Finds registration for |document_url| or |pattern| or |registration_id|.
72   // The Find methods will find stored and initially installing registrations.
73   // Returns SERVICE_WORKER_OK with non-null registration if registration
74   // is found, or returns SERVICE_WORKER_ERROR_NOT_FOUND if no matching
75   // registration is found.  The FindRegistrationForPattern method is
76   // guaranteed to return asynchronously. However, the methods to find
77   // for |document_url| or |registration_id| may complete immediately
78   // (the callback may be called prior to the method returning) or
79   // asynchronously.
80   void FindRegistrationForDocument(const GURL& document_url,
81                                    const FindRegistrationCallback& callback);
82   void FindRegistrationForPattern(const GURL& scope,
83                                   const FindRegistrationCallback& callback);
84   void FindRegistrationForId(int64 registration_id,
85                              const GURL& origin,
86                              const FindRegistrationCallback& callback);
87
88   // Returns info about all stored and initially installing registrations.
89   void GetAllRegistrations(const GetAllRegistrationInfosCallback& callback);
90
91   // Commits |registration| with the installed but not activated |version|
92   // to storage, overwritting any pre-existing registration data for the scope.
93   // A pre-existing version's script resources will remain available until
94   // either a browser restart or DeleteVersionResources is called.
95   void StoreRegistration(
96       ServiceWorkerRegistration* registration,
97       ServiceWorkerVersion* version,
98       const StatusCallback& callback);
99
100   // Updates the state of the registration's stored version to active.
101   void UpdateToActiveState(
102       ServiceWorkerRegistration* registration,
103       const StatusCallback& callback);
104
105   // Deletes the registration data for |registration_id|, the
106   // script resources for the registration's stored version
107   // will remain available until either a browser restart or
108   // DeleteVersionResources is called.
109   void DeleteRegistration(int64 registration_id,
110                           const GURL& origin,
111                           const StatusCallback& callback);
112
113   scoped_ptr<ServiceWorkerResponseReader> CreateResponseReader(
114       int64 response_id);
115   scoped_ptr<ServiceWorkerResponseWriter> CreateResponseWriter(
116       int64 response_id);
117
118   // Returns new IDs which are guaranteed to be unique in the storage.
119   int64 NewRegistrationId();
120   int64 NewVersionId();
121   int64 NewResourceId();
122
123   // Intended for use only by ServiceWorkerRegisterJob.
124   void NotifyInstallingRegistration(
125       ServiceWorkerRegistration* registration);
126   void NotifyDoneInstallingRegistration(
127       ServiceWorkerRegistration* registration);
128
129  private:
130   friend class ServiceWorkerStorageTest;
131
132   typedef std::vector<ServiceWorkerDatabase::RegistrationData> RegistrationList;
133   typedef std::vector<ServiceWorkerDatabase::ResourceRecord> ResourceList;
134
135   bool LazyInitialize(
136       const base::Closure& callback);
137   void DidReadInitialData(
138       InitialData* data,
139       bool success);
140   void DidGetRegistrationsForPattern(
141       const GURL& scope,
142       const FindRegistrationCallback& callback,
143       RegistrationList* registrations,
144       bool succcess);
145   void DidGetRegistrationsForDocument(
146       const GURL& scope,
147       const FindRegistrationCallback& callback,
148       RegistrationList* registrations,
149       bool succcess);
150   void DidReadRegistrationForId(
151       const FindRegistrationCallback& callback,
152       const ServiceWorkerDatabase::RegistrationData& registration,
153       const ResourceList& resources,
154       ServiceWorkerStatusCode status);
155   void DidGetAllRegistrations(
156       const GetAllRegistrationInfosCallback& callback,
157       RegistrationList* registrations,
158       bool success);
159   void DidStoreRegistration(
160       const GURL& origin,
161       const StatusCallback& callback,
162       bool success);
163   void DidDeleteRegistration(
164       const GURL& origin,
165       const StatusCallback& callback,
166       bool origin_is_deletable,
167       ServiceWorkerStatusCode status);
168
169   scoped_refptr<ServiceWorkerRegistration> CreateRegistration(
170       const ServiceWorkerDatabase::RegistrationData& data);
171   ServiceWorkerRegistration* FindInstallingRegistrationForDocument(
172       const GURL& document_url);
173   ServiceWorkerRegistration* FindInstallingRegistrationForPattern(
174       const GURL& scope);
175   ServiceWorkerRegistration* FindInstallingRegistrationForId(
176       int64 registration_id);
177
178   // For finding registrations being installed.
179   typedef std::map<int64, scoped_refptr<ServiceWorkerRegistration> >
180       RegistrationRefsById;
181   RegistrationRefsById installing_registrations_;
182
183   // Lazy disk_cache getter.
184   ServiceWorkerDiskCache* disk_cache();
185
186   // Origins having registations.
187   std::set<GURL> registered_origins_;
188
189   // Pending database tasks waiting for initialization.
190   std::vector<base::Closure> pending_tasks_;
191
192   int64 next_registration_id_;
193   int64 next_version_id_;
194   int64 next_resource_id_;
195
196   enum State {
197     UNINITIALIZED,
198     INITIALIZING,
199     INITIALIZED,
200     DISABLED,
201   };
202   State state_;
203
204   base::FilePath path_;
205   base::WeakPtr<ServiceWorkerContextCore> context_;
206
207   // Only accessed on |database_task_runner_|.
208   scoped_ptr<ServiceWorkerDatabase> database_;
209
210   scoped_refptr<base::SequencedTaskRunner> database_task_runner_;
211   scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_;
212   scoped_ptr<ServiceWorkerDiskCache> disk_cache_;
213
214   base::WeakPtrFactory<ServiceWorkerStorage> weak_factory_;
215
216   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage);
217 };
218
219 }  // namespace content
220
221 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_