OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 5 #ifndef CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
6 #define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 6 #define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
7 | 7 |
8 #include <stdint.h> | |
9 | |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
10 #include "base/sequence_checker.h" | 12 #include "base/sequence_checker.h" |
11 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
12 | 14 |
13 namespace leveldb { | 15 namespace leveldb { |
14 class DB; | 16 class DB; |
15 class Env; | 17 class Env; |
18 class WriteBatch; | |
16 } | 19 } |
17 | 20 |
18 namespace content { | 21 namespace content { |
19 | 22 |
20 // Implementation of the persistent notification database. | 23 // Implementation of the persistent notification database. |
21 // | 24 // |
22 // This class can be constructed on any thread, but method calls must only be | 25 // This class can be constructed on any thread, but method calls must only be |
23 // made on a thread or sequenced task runner that allows file I/O. The same | 26 // made on a thread or sequenced task runner that allows file I/O. The same |
24 // thread or task runner must be used for all method calls. | 27 // thread or task runner must be used for all method calls. |
25 class CONTENT_EXPORT NotificationDatabase { | 28 class CONTENT_EXPORT NotificationDatabase { |
(...skipping 16 matching lines...) Expand all Loading... | |
42 | 45 |
43 explicit NotificationDatabase(const base::FilePath& path); | 46 explicit NotificationDatabase(const base::FilePath& path); |
44 ~NotificationDatabase(); | 47 ~NotificationDatabase(); |
45 | 48 |
46 // Opens the database. If |path| is non-empty, it will be created on the given | 49 // Opens the database. If |path| is non-empty, it will be created on the given |
47 // directory on the filesystem. If |path| is empty, the database will be | 50 // directory on the filesystem. If |path| is empty, the database will be |
48 // created in memory instead, and its lifetime will be tied to this instance. | 51 // created in memory instead, and its lifetime will be tied to this instance. |
49 // |create_if_missing| determines whether to create the database if necessary. | 52 // |create_if_missing| determines whether to create the database if necessary. |
50 Status Open(bool create_if_missing); | 53 Status Open(bool create_if_missing); |
51 | 54 |
55 // Returns whether the next available notification id could be read, and | |
56 // stores the id in |notification_id| if the read was successful. | |
57 Status GetNextNotificationId(int64_t* notification_id); | |
58 | |
52 // Completely destroys the contents of this database. | 59 // Completely destroys the contents of this database. |
53 Status Destroy(); | 60 Status Destroy(); |
54 | 61 |
55 private: | 62 private: |
56 friend class NotificationDatabaseTest; | 63 friend class NotificationDatabaseTest; |
57 | 64 |
58 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. | 65 // TODO(peter): Convert to an enum class when DCHECK_EQ supports this. |
59 // See https://crbug.com/463869. | 66 // See https://crbug.com/463869. |
60 enum State { | 67 enum State { |
61 STATE_UNINITIALIZED, | 68 STATE_UNINITIALIZED, |
62 STATE_INITIALIZED, | 69 STATE_INITIALIZED, |
63 STATE_DISABLED, | 70 STATE_DISABLED, |
64 }; | 71 }; |
65 | 72 |
73 // Increments the available notification id to |current_notification_id| + 1. | |
74 // |current_notification_id| must be equal to or higher than the last id | |
75 // returned by GetNextNotificationId(). The value will be written to |batch|. | |
76 void IncrementNextNotificationId(leveldb::WriteBatch* batch, | |
77 int64_t current_notification_id); | |
78 | |
66 // Returns whether the database has been opened. | 79 // Returns whether the database has been opened. |
67 bool IsOpen() const { return db_ != nullptr; } | 80 bool IsOpen() const { return db_ != nullptr; } |
68 | 81 |
69 // Returns whether the database should only exist in memory. | 82 // Returns whether the database should only exist in memory. |
70 bool IsInMemoryDatabase() const { return path_.empty(); } | 83 bool IsInMemoryDatabase() const { return path_.empty(); } |
71 | 84 |
85 // Exposes the LevelDB database used to back this notification database. | |
86 // Should only be used for testing purposes. | |
87 leveldb::DB* db() { return db_.get(); } | |
Bernhard Bauer
2015/03/11 22:02:38
Name this GetDBForTesting(), so the presubmit chec
Peter Beverloo
2015/03/11 22:33:33
Done.
| |
88 | |
72 base::FilePath path_; | 89 base::FilePath path_; |
73 | 90 |
74 // These members depend on the declaration order for destruction. | 91 // These members depend on the declaration order for destruction. |
75 scoped_ptr<leveldb::Env> env_; | 92 scoped_ptr<leveldb::Env> env_; |
76 scoped_ptr<leveldb::DB> db_; | 93 scoped_ptr<leveldb::DB> db_; |
77 | 94 |
95 int64_t next_available_notification_id_; | |
96 | |
78 State state_; | 97 State state_; |
79 | 98 |
80 base::SequenceChecker sequence_checker_; | 99 base::SequenceChecker sequence_checker_; |
81 | 100 |
82 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); | 101 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); |
83 }; | 102 }; |
84 | 103 |
85 } // namespace content | 104 } // namespace content |
86 | 105 |
87 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 106 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
OLD | NEW |