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 |
29 class ServiceWorkerRegistrationData; | |
30 class ServiceWorkerResourceRecord; | |
michaeln
2014/04/26 00:29:02
looks like these aren't needed in the .h file anym
nhiroki
2014/04/28 04:15:59
Done.
| |
31 | |
26 // Class to persist serviceworker registration data in a database. | 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 // non-IO 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(); |
(...skipping 15 matching lines...) Expand all Loading... | |
51 ServiceWorkerVersion::Status GetVersionStatus() const { | 57 ServiceWorkerVersion::Status GetVersionStatus() const { |
52 if (is_active) | 58 if (is_active) |
53 return ServiceWorkerVersion::ACTIVE; | 59 return ServiceWorkerVersion::ACTIVE; |
54 return ServiceWorkerVersion::INSTALLED; | 60 return ServiceWorkerVersion::INSTALLED; |
55 } | 61 } |
56 | 62 |
57 RegistrationData(); | 63 RegistrationData(); |
58 ~RegistrationData(); | 64 ~RegistrationData(); |
59 }; | 65 }; |
60 | 66 |
67 struct ResourceRecord { | |
68 int64 resource_id; | |
69 GURL url; | |
70 }; | |
71 | |
61 // For use during initialization. | 72 // For use during initialization. |
62 bool GetNextAvailableIds(int64* next_avail_registration_id, | 73 bool GetNextAvailableIds(int64* next_avail_registration_id, |
63 int64* next_avail_version_id, | 74 int64* next_avail_version_id, |
64 int64* next_avail_resource_id); | 75 int64* next_avail_resource_id); |
76 bool GetOriginsWithRegistrations(std::set<GURL>* origins); | |
77 | |
78 // For use when first handling a request in an origin with registrations. | |
79 bool GetRegistrationsForOrigin(const GURL& origin, | |
80 std::vector<RegistrationData>* registrations); | |
81 | |
82 // Saving, retrieving, and updating registration data. | |
83 // (will bump next_avail_xxxx_ids as needed) | |
84 // (resource ids will be added/removed from the uncommitted/purgeable | |
85 // lists as needed) | |
86 | |
87 bool ReadRegistration(int64 registration_id, | |
88 const GURL& origin, | |
89 RegistrationData* registration, | |
90 std::vector<ResourceRecord>* resources); | |
91 bool WriteRegistration(const RegistrationData& registration, | |
92 const std::vector<ResourceRecord>& resources); | |
93 | |
94 bool UpdateVersionToActive(int64 registration_id, | |
95 const GURL& origin); | |
96 bool UpdateLastCheckTime(int64 registration_id, | |
97 const GURL& origin, | |
98 const base::Time& time); | |
99 bool DeleteRegistration(int64 registration_id, | |
100 const GURL& origin); | |
65 | 101 |
66 bool is_disabled() const { return is_disabled_; } | 102 bool is_disabled() const { return is_disabled_; } |
67 bool was_corruption_detected() const { return was_corruption_detected_; } | 103 bool was_corruption_detected() const { return was_corruption_detected_; } |
68 | 104 |
69 private: | 105 private: |
70 // Opens the database at the |path_|. This is lazily called when the first | 106 // 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 | 107 // 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 | 108 // false if the opening failed or was not neccessary, that is, the database |
73 // does not exist and |create_if_needed| is false. | 109 // does not exist and |create_if_needed| is false. |
74 bool LazyOpen(bool create_if_needed); | 110 bool LazyOpen(bool create_if_needed); |
75 | 111 |
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, | 112 bool ReadNextAvailableId(const char* id_key, |
81 int64* next_avail_id); | 113 int64* next_avail_id); |
114 bool ReadRegistrationData(int64 registration_id, | |
115 const GURL& origin, | |
116 RegistrationData* registration); | |
117 bool ReadDatabaseVersion(int64* db_version); | |
118 | |
119 // Write a batch into the database. | |
120 // NOTE: You must call this when you want to put something into the database | |
121 // because this initializes the database if needed. | |
82 bool WriteBatch(leveldb::WriteBatch* batch); | 122 bool WriteBatch(leveldb::WriteBatch* batch); |
83 | 123 |
124 // Bumps the next available id if |used_id| is greater than or equal to the | |
125 // cached one. | |
126 void BumpNextRegistrationIdIfNeeded(int64 used_id, | |
127 leveldb::WriteBatch* batch); | |
128 void BumpNextVersionIdIfNeeded(int64 used_id, | |
129 leveldb::WriteBatch* batch); | |
130 | |
84 bool IsOpen(); | 131 bool IsOpen(); |
85 bool IsEmpty(); | 132 bool IsEmpty(); |
michaeln
2014/04/26 00:29:02
IsEmpty() can be deleted here
nhiroki
2014/04/28 04:15:59
Done.
| |
86 | 133 |
87 void HandleError(const tracked_objects::Location& from_here, | 134 void HandleError(const tracked_objects::Location& from_here, |
88 const leveldb::Status& status); | 135 const leveldb::Status& status); |
89 | 136 |
90 base::FilePath path_; | 137 base::FilePath path_; |
91 scoped_ptr<leveldb::Env> env_; | 138 scoped_ptr<leveldb::Env> env_; |
92 scoped_ptr<leveldb::DB> db_; | 139 scoped_ptr<leveldb::DB> db_; |
93 | 140 |
141 int64 next_avail_registration_id_; | |
142 int64 next_avail_resource_id_; | |
143 int64 next_avail_version_id_; | |
144 | |
94 // 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). |
95 // If true, all database accesses will fail. | 146 // If true, all database accesses will fail. |
96 bool is_disabled_; | 147 bool is_disabled_; |
97 | 148 |
98 // True if a database corruption was detected. | 149 // True if a database corruption was detected. |
99 bool was_corruption_detected_; | 150 bool was_corruption_detected_; |
100 | 151 |
152 // True if a database was initialized, that is, the schema version was written | |
153 // in the database. | |
154 bool is_initialized_; | |
155 | |
101 base::SequenceChecker sequence_checker_; | 156 base::SequenceChecker sequence_checker_; |
102 | 157 |
103 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); | 158 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); |
104 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); | 159 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); |
105 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); | 160 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); |
106 | 161 |
107 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 162 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
108 }; | 163 }; |
109 | 164 |
110 } // namespace content | 165 } // namespace content |
111 | 166 |
112 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 167 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
OLD | NEW |