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 Status; | 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 registration data in a database. | 29 // Class to persist serviceworker registration data in a database. |
27 // Should NOT be used on the IO thread since this does blocking | 30 // Should NOT be used on the IO thread since this does blocking |
28 // file io. The ServiceWorkerStorage class owns this class and | 31 // file io. The ServiceWorkerStorage class owns this class and |
29 // is responsible for only calling it serially on background | 32 // is responsible for only calling it serially on background |
30 // non-IO threads (ala SequencedWorkerPool). | 33 // non-IO threads (ala SequencedWorkerPool). |
31 class CONTENT_EXPORT ServiceWorkerDatabase { | 34 class CONTENT_EXPORT ServiceWorkerDatabase { |
32 public: | 35 public: |
33 // We do leveldb stuff in |path| or in memory if |path| is empty. | 36 // We do leveldb stuff in |path| or in memory if |path| is empty. |
34 explicit ServiceWorkerDatabase(const base::FilePath& path); | 37 explicit ServiceWorkerDatabase(const base::FilePath& path); |
35 ~ServiceWorkerDatabase(); | 38 ~ServiceWorkerDatabase(); |
36 | 39 |
37 struct RegistrationData { | 40 struct CONTENT_EXPORT RegistrationData { |
38 // These values are immutable for the life of a registration. | 41 // These values are immutable for the life of a registration. |
39 int64 registration_id; | 42 int64 registration_id; |
40 GURL scope; | 43 GURL scope; |
41 GURL script; | 44 GURL script; |
42 | 45 |
43 // Versions are first stored once they successfully install and become | 46 // Versions are first stored once they successfully install and become |
44 // the waiting version. Then transition to the active version. The stored | 47 // the waiting version. Then transition to the active version. The stored |
45 // version may be in the ACTIVE state or in the INSTALLED state. | 48 // version may be in the ACTIVE state or in the INSTALLED state. |
46 int64 version_id; | 49 int64 version_id; |
47 bool is_active; | 50 bool is_active; |
48 bool has_fetch_handler; | 51 bool has_fetch_handler; |
49 base::Time last_update_check; | 52 base::Time last_update_check; |
50 | 53 |
51 ServiceWorkerVersion::Status GetVersionStatus() const { | 54 ServiceWorkerVersion::Status GetVersionStatus() const { |
52 if (is_active) | 55 if (is_active) |
53 return ServiceWorkerVersion::ACTIVE; | 56 return ServiceWorkerVersion::ACTIVE; |
54 return ServiceWorkerVersion::INSTALLED; | 57 return ServiceWorkerVersion::INSTALLED; |
55 } | 58 } |
56 | 59 |
57 RegistrationData(); | 60 RegistrationData(); |
58 ~RegistrationData(); | 61 ~RegistrationData(); |
59 }; | 62 }; |
60 | 63 |
| 64 struct ResourceRecord { |
| 65 int64 resource_id; |
| 66 GURL url; |
| 67 }; |
| 68 |
61 // For use during initialization. | 69 // For use during initialization. |
62 bool GetNextAvailableIds(int64* next_avail_registration_id, | 70 bool GetNextAvailableIds(int64* next_avail_registration_id, |
63 int64* next_avail_version_id, | 71 int64* next_avail_version_id, |
64 int64* next_avail_resource_id); | 72 int64* next_avail_resource_id); |
| 73 bool GetOriginsWithRegistrations(std::set<GURL>* origins); |
| 74 |
| 75 // For use when first handling a request in an origin with registrations. |
| 76 bool GetRegistrationsForOrigin(const GURL& origin, |
| 77 std::vector<RegistrationData>* registrations); |
| 78 |
| 79 // Saving, retrieving, and updating registration data. |
| 80 // (will bump next_avail_xxxx_ids as needed) |
| 81 // (resource ids will be added/removed from the uncommitted/purgeable |
| 82 // lists as needed) |
| 83 |
| 84 bool ReadRegistration(int64 registration_id, |
| 85 const GURL& origin, |
| 86 RegistrationData* registration, |
| 87 std::vector<ResourceRecord>* resources); |
| 88 bool WriteRegistration(const RegistrationData& registration, |
| 89 const std::vector<ResourceRecord>& resources); |
| 90 |
| 91 bool UpdateVersionToActive(int64 registration_id, |
| 92 const GURL& origin); |
| 93 bool UpdateLastCheckTime(int64 registration_id, |
| 94 const GURL& origin, |
| 95 const base::Time& time); |
| 96 bool DeleteRegistration(int64 registration_id, |
| 97 const GURL& origin); |
65 | 98 |
66 bool is_disabled() const { return is_disabled_; } | 99 bool is_disabled() const { return is_disabled_; } |
67 bool was_corruption_detected() const { return was_corruption_detected_; } | 100 bool was_corruption_detected() const { return was_corruption_detected_; } |
68 | 101 |
69 private: | 102 private: |
70 // Opens the database at the |path_|. This is lazily called when the first | 103 // 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 | 104 // 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 | 105 // false if the opening failed or was not neccessary, that is, the database |
73 // does not exist and |create_if_needed| is false. | 106 // does not exist and |create_if_needed| is false. |
74 bool LazyOpen(bool create_if_needed); | 107 bool LazyOpen(bool create_if_needed); |
75 | 108 |
76 // Populates the database with initial data, namely, database schema version | |
77 // and next available IDs. | |
78 bool PopulateInitialData(); | |
79 | |
80 bool ReadNextAvailableId(const char* id_key, | 109 bool ReadNextAvailableId(const char* id_key, |
81 int64* next_avail_id); | 110 int64* next_avail_id); |
| 111 bool ReadRegistrationData(int64 registration_id, |
| 112 const GURL& origin, |
| 113 RegistrationData* registration); |
| 114 bool ReadDatabaseVersion(int64* db_version); |
| 115 |
| 116 // Write a batch into the database. |
| 117 // NOTE: You must call this when you want to put something into the database |
| 118 // because this initializes the database if needed. |
82 bool WriteBatch(leveldb::WriteBatch* batch); | 119 bool WriteBatch(leveldb::WriteBatch* batch); |
83 | 120 |
| 121 // Bumps the next available id if |used_id| is greater than or equal to the |
| 122 // cached one. |
| 123 void BumpNextRegistrationIdIfNeeded(int64 used_id, |
| 124 leveldb::WriteBatch* batch); |
| 125 void BumpNextVersionIdIfNeeded(int64 used_id, |
| 126 leveldb::WriteBatch* batch); |
| 127 |
84 bool IsOpen(); | 128 bool IsOpen(); |
85 bool IsEmpty(); | |
86 | 129 |
87 void HandleError(const tracked_objects::Location& from_here, | 130 void HandleError(const tracked_objects::Location& from_here, |
88 const leveldb::Status& status); | 131 const leveldb::Status& status); |
89 | 132 |
90 base::FilePath path_; | 133 base::FilePath path_; |
91 scoped_ptr<leveldb::Env> env_; | 134 scoped_ptr<leveldb::Env> env_; |
92 scoped_ptr<leveldb::DB> db_; | 135 scoped_ptr<leveldb::DB> db_; |
93 | 136 |
| 137 int64 next_avail_registration_id_; |
| 138 int64 next_avail_resource_id_; |
| 139 int64 next_avail_version_id_; |
| 140 |
94 // True if a database error has occurred (e.g. cannot read data). | 141 // True if a database error has occurred (e.g. cannot read data). |
95 // If true, all database accesses will fail. | 142 // If true, all database accesses will fail. |
96 bool is_disabled_; | 143 bool is_disabled_; |
97 | 144 |
98 // True if a database corruption was detected. | 145 // True if a database corruption was detected. |
99 bool was_corruption_detected_; | 146 bool was_corruption_detected_; |
100 | 147 |
| 148 // True if a database was initialized, that is, the schema version was written |
| 149 // in the database. |
| 150 bool is_initialized_; |
| 151 |
101 base::SequenceChecker sequence_checker_; | 152 base::SequenceChecker sequence_checker_; |
102 | 153 |
103 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); | 154 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); |
104 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); | 155 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); |
105 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); | 156 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); |
106 | 157 |
107 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 158 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
108 }; | 159 }; |
109 | 160 |
110 } // namespace content | 161 } // namespace content |
111 | 162 |
112 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 163 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
OLD | NEW |