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); | |
cmumford
2015/03/12 02:09:37
Should we be using int64 instead of int64_t?
Peter Beverloo
2015/03/12 02:13:56
Using just int64 is deprecated, see basictypes.h.
| |
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 next available notification id, and writes a put operation | |
74 // to |batch| so that this can be written to the database. | |
75 void IncrementNextNotificationId(leveldb::WriteBatch* batch); | |
76 | |
66 // Returns whether the database has been opened. | 77 // Returns whether the database has been opened. |
67 bool IsOpen() const { return db_ != nullptr; } | 78 bool IsOpen() const { return db_ != nullptr; } |
68 | 79 |
69 // Returns whether the database should only exist in memory. | 80 // Returns whether the database should only exist in memory. |
70 bool IsInMemoryDatabase() const { return path_.empty(); } | 81 bool IsInMemoryDatabase() const { return path_.empty(); } |
71 | 82 |
83 // Exposes the LevelDB database used to back this notification database. | |
84 // Should only be used for testing purposes. | |
85 leveldb::DB* GetDBForTesting() { return db_.get(); } | |
cmumford
2015/03/12 02:09:37
const method?
Peter Beverloo
2015/03/12 02:13:56
I'll apply this in the morning.
Peter Beverloo
2015/03/12 14:28:59
Done.
| |
86 | |
72 base::FilePath path_; | 87 base::FilePath path_; |
73 | 88 |
74 // The declaration order for these members matters, as |db_| depends on |env_| | 89 // The declaration order for these members matters, as |db_| depends on |env_| |
75 // and thus has to be destructed first. | 90 // and thus has to be destructed first. |
76 scoped_ptr<leveldb::Env> env_; | 91 scoped_ptr<leveldb::Env> env_; |
77 scoped_ptr<leveldb::DB> db_; | 92 scoped_ptr<leveldb::DB> db_; |
78 | 93 |
94 int64_t next_notification_id_; | |
95 | |
79 State state_; | 96 State state_; |
80 | 97 |
81 base::SequenceChecker sequence_checker_; | 98 base::SequenceChecker sequence_checker_; |
82 | 99 |
83 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); | 100 DISALLOW_COPY_AND_ASSIGN(NotificationDatabase); |
84 }; | 101 }; |
85 | 102 |
86 } // namespace content | 103 } // namespace content |
87 | 104 |
88 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ | 105 #endif // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_ |
OLD | NEW |