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_STORAGE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_
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"
23 class SequencedTaskRunner;
27 class QuotaManagerProxy;
32 class ServiceWorkerContextCore;
33 class ServiceWorkerDiskCache;
34 class ServiceWorkerRegistration;
35 class ServiceWorkerRegistrationInfo;
36 class ServiceWorkerResponseReader;
37 class ServiceWorkerResponseWriter;
38 class ServiceWorkerVersion;
40 // This class provides an interface to store and retrieve ServiceWorker
42 class CONTENT_EXPORT ServiceWorkerStorage {
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)>
56 int64 next_registration_id;
57 int64 next_version_id;
58 int64 next_resource_id;
59 std::set<GURL> origins;
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();
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
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,
86 const FindRegistrationCallback& callback);
88 // Returns info about all stored and initially installing registrations.
89 void GetAllRegistrations(const GetAllRegistrationInfosCallback& callback);
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);
100 // Updates the state of the registration's stored version to active.
101 void UpdateToActiveState(
102 ServiceWorkerRegistration* registration,
103 const StatusCallback& callback);
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,
111 const StatusCallback& callback);
113 scoped_ptr<ServiceWorkerResponseReader> CreateResponseReader(
115 scoped_ptr<ServiceWorkerResponseWriter> CreateResponseWriter(
118 // Returns new IDs which are guaranteed to be unique in the storage.
119 int64 NewRegistrationId();
120 int64 NewVersionId();
121 int64 NewResourceId();
123 // Intended for use only by ServiceWorkerRegisterJob.
124 void NotifyInstallingRegistration(
125 ServiceWorkerRegistration* registration);
126 void NotifyDoneInstallingRegistration(
127 ServiceWorkerRegistration* registration);
130 friend class ServiceWorkerStorageTest;
132 typedef std::vector<ServiceWorkerDatabase::RegistrationData> RegistrationList;
133 typedef std::vector<ServiceWorkerDatabase::ResourceRecord> ResourceList;
136 const base::Closure& callback);
137 void DidReadInitialData(
140 void DidGetRegistrationsForPattern(
142 const FindRegistrationCallback& callback,
143 RegistrationList* registrations,
145 void DidGetRegistrationsForDocument(
147 const FindRegistrationCallback& callback,
148 RegistrationList* registrations,
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,
159 void DidStoreRegistration(
161 const StatusCallback& callback,
163 void DidDeleteRegistration(
165 const StatusCallback& callback,
166 bool origin_is_deletable,
167 ServiceWorkerStatusCode status);
169 scoped_refptr<ServiceWorkerRegistration> CreateRegistration(
170 const ServiceWorkerDatabase::RegistrationData& data);
171 ServiceWorkerRegistration* FindInstallingRegistrationForDocument(
172 const GURL& document_url);
173 ServiceWorkerRegistration* FindInstallingRegistrationForPattern(
175 ServiceWorkerRegistration* FindInstallingRegistrationForId(
176 int64 registration_id);
178 // For finding registrations being installed.
179 typedef std::map<int64, scoped_refptr<ServiceWorkerRegistration> >
180 RegistrationRefsById;
181 RegistrationRefsById installing_registrations_;
183 // Lazy disk_cache getter.
184 ServiceWorkerDiskCache* disk_cache();
186 // Origins having registations.
187 std::set<GURL> registered_origins_;
189 // Pending database tasks waiting for initialization.
190 std::vector<base::Closure> pending_tasks_;
192 int64 next_registration_id_;
193 int64 next_version_id_;
194 int64 next_resource_id_;
204 base::FilePath path_;
205 base::WeakPtr<ServiceWorkerContextCore> context_;
207 // Only accessed on |database_task_runner_|.
208 scoped_ptr<ServiceWorkerDatabase> database_;
210 scoped_refptr<base::SequencedTaskRunner> database_task_runner_;
211 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_;
212 scoped_ptr<ServiceWorkerDiskCache> disk_cache_;
214 base::WeakPtrFactory<ServiceWorkerStorage> weak_factory_;
216 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage);
219 } // namespace content
221 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_