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; |
| 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(); |
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 const GURL& origin, |
| 62 ServiceWorkerRegistrationData* registration, |
| 63 std::vector<ServiceWorkerResourceRecord>* resources); |
| 64 bool WriteRegistration( |
| 65 const ServiceWorkerRegistrationData& registration, |
| 66 const std::vector<ServiceWorkerResourceRecord>& resources); |
| 67 |
| 68 bool UpdateVersionToActive(int64 registration_id, |
| 69 const GURL& origin); |
| 70 bool UpdateLastCheckTime(int64 registration_id, |
| 71 const GURL& origin, |
| 72 const base::Time& time); |
| 73 bool DeleteRegistration(int64 registration_id, |
| 74 const GURL& origin); |
65 | 75 |
66 bool is_disabled() const { return is_disabled_; } | 76 bool is_disabled() const { return is_disabled_; } |
67 bool was_corruption_detected() const { return was_corruption_detected_; } | 77 bool was_corruption_detected() const { return was_corruption_detected_; } |
68 | 78 |
69 private: | 79 private: |
70 // Opens the database at the |path_|. This is lazily called when the first | 80 // 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 | 81 // 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 | 82 // false if the opening failed or was not neccessary, that is, the database |
73 // does not exist and |create_if_needed| is false. | 83 // does not exist and |create_if_needed| is false. |
74 bool LazyOpen(bool create_if_needed); | 84 bool LazyOpen(bool create_if_needed); |
75 | 85 |
76 // Populates the database with initial data, namely, database schema version | 86 // Populates the database with initial data, namely, database schema version |
77 // and next available IDs. | 87 // and next available IDs. |
78 bool PopulateInitialData(); | 88 bool PopulateInitialData(); |
79 | 89 |
80 bool ReadNextAvailableId(const char* id_key, | 90 bool ReadNextAvailableId(const char* id_key, |
81 int64* next_avail_id); | 91 int64* next_avail_id); |
| 92 bool ReadRegistrationData(int64 registration_id, |
| 93 const GURL& origin, |
| 94 ServiceWorkerRegistrationData* registration); |
82 bool WriteBatch(leveldb::WriteBatch* batch); | 95 bool WriteBatch(leveldb::WriteBatch* batch); |
83 | 96 |
| 97 // Bumps the next available id specified by |id_key| if |used_id| is greater |
| 98 // than or equal to the stored one. |
| 99 bool BumpNextAvailableIdIfNeeded(const char* id_key, |
| 100 int64 used_id, |
| 101 leveldb::WriteBatch* batch); |
| 102 |
84 bool IsOpen(); | 103 bool IsOpen(); |
85 bool IsEmpty(); | 104 bool IsEmpty(); |
86 | 105 |
87 void HandleError(const tracked_objects::Location& from_here, | 106 void HandleError(const tracked_objects::Location& from_here, |
88 const leveldb::Status& status); | 107 const leveldb::Status& status); |
89 | 108 |
90 base::FilePath path_; | 109 base::FilePath path_; |
91 scoped_ptr<leveldb::Env> env_; | 110 scoped_ptr<leveldb::Env> env_; |
92 scoped_ptr<leveldb::DB> db_; | 111 scoped_ptr<leveldb::DB> db_; |
93 | 112 |
94 // True if a database error has occurred (e.g. cannot read data). | 113 // True if a database error has occurred (e.g. cannot read data). |
95 // If true, all database accesses will fail. | 114 // If true, all database accesses will fail. |
96 bool is_disabled_; | 115 bool is_disabled_; |
97 | 116 |
98 // True if a database corruption was detected. | 117 // True if a database corruption was detected. |
99 bool was_corruption_detected_; | 118 bool was_corruption_detected_; |
100 | 119 |
101 base::SequenceChecker sequence_checker_; | 120 base::SequenceChecker sequence_checker_; |
102 | 121 |
103 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); | 122 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase); |
104 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); | 123 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, OpenDatabase_InMemory); |
105 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); | 124 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerDatabaseTest, GetNextAvailableIds); |
106 | 125 |
107 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); | 126 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDatabase); |
108 }; | 127 }; |
109 | 128 |
110 } // namespace content | 129 } // namespace content |
111 | 130 |
112 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ | 131 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DATABASE_H_ |
OLD | NEW |