OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
7 | 7 |
| 8 #include <set> |
| 9 #include <vector> |
| 10 |
8 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
9 #include "base/macros.h" | 12 #include "base/macros.h" |
10 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
11 #include "base/sequence_checker.h" | 14 #include "base/sequence_checker.h" |
12 #include "base/time/time.h" | 15 #include "base/time/time.h" |
13 #include "content/browser/service_worker/service_worker_version.h" | 16 #include "content/browser/service_worker/service_worker_version.h" |
14 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
15 #include "url/gurl.h" | 18 #include "url/gurl.h" |
16 | 19 |
17 namespace leveldb { | 20 namespace leveldb { |
18 class DB; | 21 class DB; |
19 class Env; | 22 class Env; |
20 class Slice; | 23 class Status; |
21 class WriteBatch; | 24 class WriteBatch; |
22 } | 25 } |
23 | 26 |
24 namespace content { | 27 namespace content { |
25 | 28 |
26 // Class to persist serviceworker regitration data in a database. | 29 class ServiceWorkerRegistrationData; |
| 30 class ServiceWorkerResourceRecord; |
| 31 |
| 32 // Class to persist serviceworker registration data in a database. |
27 // Should NOT be used on the IO thread since this does blocking | 33 // Should NOT be used on the IO thread since this does blocking |
28 // file io. The ServiceWorkerStorage class owns this class and | 34 // file io. The ServiceWorkerStorage class owns this class and |
29 // is responsible for only calling it serially on background | 35 // is responsible for only calling it serially on background |
30 // nonIO threads (ala SequencedWorkerPool). | 36 // non-IO threads (ala SequencedWorkerPool). |
31 class CONTENT_EXPORT ServiceWorkerDatabase { | 37 class CONTENT_EXPORT ServiceWorkerDatabase { |
32 public: | 38 public: |
33 // We do leveldb stuff in |path| or in memory if |path| is empty. | 39 // We do leveldb stuff in |path| or in memory if |path| is empty. |
34 explicit ServiceWorkerDatabase(const base::FilePath& path); | 40 explicit ServiceWorkerDatabase(const base::FilePath& path); |
35 ~ServiceWorkerDatabase(); | 41 ~ServiceWorkerDatabase(); |
36 | 42 |
37 struct RegistrationData { | |
38 // These values are immutable for the life of a registration. | |
39 int64 registration_id; | |
40 GURL scope; | |
41 GURL script; | |
42 | |
43 // Versions are first stored once they successfully install and become | |
44 // the waiting version. Then transition to the active version. The stored | |
45 // version may be in the ACTIVE state or in the INSTALLED state. | |
46 int64 version_id; | |
47 bool is_active; | |
48 bool has_fetch_handler; | |
49 base::Time last_update_check; | |
50 | |
51 ServiceWorkerVersion::Status GetVersionStatus() const { | |
52 if (is_active) | |
53 return ServiceWorkerVersion::ACTIVE; | |
54 return ServiceWorkerVersion::INSTALLED; | |
55 } | |
56 | |
57 RegistrationData(); | |
58 ~RegistrationData(); | |
59 }; | |
60 | |
61 // For use during initialization. | 43 // For use during initialization. |
62 bool GetNextAvailableIds(int64* next_avail_registration_id, | 44 bool GetNextAvailableIds(int64* next_avail_registration_id, |
63 int64* next_avail_version_id, | 45 int64* next_avail_version_id, |
64 int64* next_avail_resource_id); | 46 int64* next_avail_resource_id); |
| 47 bool GetOriginsWithRegistrations(std::set<GURL>* origins); |
| 48 |
| 49 // For use when first handling a request in an origin with registrations. |
| 50 bool GetRegistrationsForOrigin( |
| 51 const GURL& origin, |
| 52 std::vector<ServiceWorkerRegistrationData>* registrations); |
| 53 |
| 54 // Saving, retrieving, and updating registration data. |
| 55 // (will bump next_avail_xxxx_ids as needed) |
| 56 // (resource ids will be added/removed from the uncommitted/purgeable |
| 57 // lists as needed) |
| 58 |
| 59 bool ReadRegistration( |
| 60 int64 registration_id, |
| 61 ServiceWorkerRegistrationData* registration, |
| 62 std::vector<ServiceWorkerResourceRecord>* resources); |
| 63 bool WriteRegistration( |
| 64 const ServiceWorkerRegistrationData& registration, |
| 65 const std::vector<ServiceWorkerResourceRecord>& resources); |
| 66 |
| 67 bool UpdateVersionToActive(int64 registration_id); |
| 68 bool UpdateLastCheckTime(int64 registration_id, |
| 69 const base::Time& time); |
| 70 bool DeleteRegistration(int64 registration_id); |
| 71 |
| 72 // As new resources are put into the diskcache, they go into an uncommitted |
| 73 // list. When a registration is saved that refers to those ids, they're |
| 74 // removed from that list. When a resource no longer has any registrations or |
| 75 // caches referring to it, it's added to the unclaimed list. Periodically, |
| 76 // the unclaimed list can be purged from the diskcache. At system startup, all |
| 77 // uncommitted ids are moved to the purgeable list. |
| 78 |
| 79 // Reads uncommitted resource ids from the database. Returns true on success. |
| 80 // Otherwise clears |ids| and returns false. |
| 81 bool GetUncommittedResourceIds(std::set<int64>* ids); |
| 82 |
| 83 // Writes |ids| into the database as uncommitted resources. Returns true on |
| 84 // success. Otherwise writes nothing and returns false. |
| 85 bool WriteUncommittedResourceIds(const std::set<int64>& ids); |
| 86 |
| 87 // Deletes uncommitted resource ids specified by |ids| from the database. |
| 88 // Returns true on success. Otherwise deletes nothing and returns false. |
| 89 bool ClearUncommittedResourceIds(const std::set<int64>& ids); |
| 90 |
| 91 // Reads purgeable resource ids from the database. Returns true on success. |
| 92 // Otherwise clears |ids and returns false. |
| 93 bool GetPurgeableResourceIds(std::set<int64>* ids); |
| 94 |
| 95 // Writes |ids| into the database as purgeable resources. Returns true on |
| 96 // success. Otherwise writes nothing and returns false. |
| 97 bool WritePurgeableResourceIds(const std::set<int64>& ids); |
| 98 |
| 99 // Deletes purgeable resource ids specified by |ids| from the database. |
| 100 // Returns true on success. Otherwise deletes nothing and returns false. |
| 101 bool ClearPurgeableResourceIds(const std::set<int64>& ids); |
| 102 |
| 103 bool DeleteAllDataForOrigin(const GURL& origin); |
| 104 bool DeleteAllData(); |
65 | 105 |
66 bool is_disabled() const { return is_disabled_; } | 106 bool is_disabled() const { return is_disabled_; } |
67 bool was_corruption_detected() const { return was_corruption_detected_; } | 107 bool was_corruption_detected() const { return was_corruption_detected_; } |
68 | 108 |
69 private: | 109 private: |
70 // Opens the database at the |path_|. This is lazily called when the first | 110 // Opens the database at the |path_|. This is lazily called when the first |
71 // database API is called. Returns true if the database was opened. Returns | 111 // database API is called. Returns true if the database was opened. Returns |
72 // false if the opening failed or was not neccessary, that is, the database | 112 // false if the opening failed or was not neccessary, that is, the database |
73 // does not exist and |create_if_needed| is false. | 113 // does not exist and |create_if_needed| is false. |
74 bool LazyOpen(bool create_if_needed); | 114 bool LazyOpen(bool create_if_needed); |
75 | 115 |
76 // Populates the database with initial data, namely, database schema version | 116 // Populates the database with initial data, namely, database schema version |
77 // and next available IDs. | 117 // and next available IDs. |
78 bool PopulateInitialData(); | 118 bool PopulateInitialData(); |
79 | 119 |
80 bool ReadInt64(const leveldb::Slice& key, int64* value_out); | 120 bool ReadNextAvailableId(const char* id_key, int64* next_avail_id); |
81 bool WriteBatch(scoped_ptr<leveldb::WriteBatch> batch); | 121 bool ReadRegistrationData( |
| 122 int64 registration_id, |
| 123 ServiceWorkerRegistrationData* registration); |
| 124 bool ReadResourceRecords( |
| 125 int64 version_id, |
| 126 std::vector<ServiceWorkerResourceRecord>* resources); |
| 127 bool WriteBatch(leveldb::WriteBatch* batch); |
| 128 |
| 129 // Bumps the next available id specified by |id_key| if |used_id| is greater |
| 130 // than or equal to the stored one. |
| 131 bool BumpNextAvailableIdIfNeeded(const char* id_key, |
| 132 int64 used_id, |
| 133 leveldb::WriteBatch* batch); |
82 | 134 |
83 bool IsOpen(); | 135 bool IsOpen(); |
84 bool IsEmpty(); | 136 bool IsEmpty(); |
85 | 137 |
| 138 void HandleError(const tracked_objects::Location& from_here, |
| 139 const leveldb::Status& status); |
| 140 |
86 base::FilePath path_; | 141 base::FilePath path_; |
87 scoped_ptr<leveldb::Env> env_; | 142 scoped_ptr<leveldb::Env> env_; |
88 scoped_ptr<leveldb::DB> db_; | 143 scoped_ptr<leveldb::DB> db_; |
89 | 144 |
90 // True if a database error has occurred (e.g. cannot read data). | 145 // True if a database error has occurred (e.g. cannot read data). |
91 // If true, all database accesses will fail. | 146 // If true, all database accesses will fail. |
92 bool is_disabled_; | 147 bool is_disabled_; |
93 | 148 |
94 // True if a database corruption was detected. | 149 // True if a database corruption was detected. |
95 bool was_corruption_detected_; | 150 bool was_corruption_detected_; |
96 | 151 |
97 base::SequenceChecker sequence_checker_; | 152 base::SequenceChecker sequence_checker_; |
98 | 153 |
99 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); | 154 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); |
100 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); | 155 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); |
101 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); | 156 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); |
102 | 157 |
103 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 158 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
104 }; | 159 }; |
105 | 160 |
106 } // namespace content | 161 } // namespace content |
107 | 162 |
108 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 163 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
OLD | NEW |