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 #include "content/browser/notifications/notification_database.h" | 5 #include "content/browser/notifications/notification_database.h" |
6 | 6 |
7 #include <string> | |
8 | |
7 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/strings/string_number_conversions.h" | |
8 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
9 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 12 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
10 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 13 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
11 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 14 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
15 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | |
16 | |
17 // Notification LevelDB database schema (in alphabetized order) | |
18 // ======================= | |
19 // | |
20 // key: "NEXT_NOTIFICATION_ID" | |
21 // value: <int64 'next_notification_id'> | |
22 // | |
12 | 23 |
13 namespace content { | 24 namespace content { |
14 namespace { | 25 namespace { |
15 | 26 |
27 // Keys of the fields defined in the database. | |
28 const char kNextNotificationIdKey[] = "NEXT_NOTIFICATION_ID"; | |
29 | |
30 // The first notification id which to be handed out by the database. | |
31 const int64_t kFirstNotificationId = 1; | |
32 | |
16 // Converts the LevelDB |status| to one of the notification database's values. | 33 // Converts the LevelDB |status| to one of the notification database's values. |
17 NotificationDatabase::Status LevelDBStatusToStatus( | 34 NotificationDatabase::Status LevelDBStatusToStatus( |
18 const leveldb::Status& status) { | 35 const leveldb::Status& status) { |
19 if (status.ok()) | 36 if (status.ok()) |
20 return NotificationDatabase::STATUS_OK; | 37 return NotificationDatabase::STATUS_OK; |
21 else if (status.IsNotFound()) | 38 else if (status.IsNotFound()) |
22 return NotificationDatabase::STATUS_ERROR_NOT_FOUND; | 39 return NotificationDatabase::STATUS_ERROR_NOT_FOUND; |
23 else if (status.IsCorruption()) | 40 else if (status.IsCorruption()) |
24 return NotificationDatabase::STATUS_ERROR_CORRUPTED; | 41 return NotificationDatabase::STATUS_ERROR_CORRUPTED; |
25 | 42 |
26 return NotificationDatabase::STATUS_ERROR_FAILED; | 43 return NotificationDatabase::STATUS_ERROR_FAILED; |
27 } | 44 } |
28 | 45 |
29 } // namespace | 46 } // namespace |
30 | 47 |
31 NotificationDatabase::NotificationDatabase(const base::FilePath& path) | 48 NotificationDatabase::NotificationDatabase(const base::FilePath& path) |
32 : path_(path), | 49 : path_(path), |
50 next_notification_id_(kFirstNotificationId), | |
33 state_(STATE_UNINITIALIZED) { | 51 state_(STATE_UNINITIALIZED) { |
34 sequence_checker_.DetachFromSequence(); | 52 sequence_checker_.DetachFromSequence(); |
35 } | 53 } |
36 | 54 |
37 NotificationDatabase::~NotificationDatabase() { | 55 NotificationDatabase::~NotificationDatabase() { |
38 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 56 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
39 } | 57 } |
40 | 58 |
41 NotificationDatabase::Status NotificationDatabase::Open( | 59 NotificationDatabase::Status NotificationDatabase::Open( |
42 bool create_if_missing) { | 60 bool create_if_missing) { |
(...skipping 21 matching lines...) Expand all Loading... | |
64 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db)); | 82 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db)); |
65 if (status != STATUS_OK) | 83 if (status != STATUS_OK) |
66 return status; | 84 return status; |
67 | 85 |
68 state_ = STATE_INITIALIZED; | 86 state_ = STATE_INITIALIZED; |
69 db_.reset(db); | 87 db_.reset(db); |
70 | 88 |
71 return STATUS_OK; | 89 return STATUS_OK; |
72 } | 90 } |
73 | 91 |
92 NotificationDatabase::Status NotificationDatabase::GetNextNotificationId( | |
cmumford
2015/03/12 02:09:37
This doesn't modify the class so could be a const
Peter Beverloo
2015/03/12 02:13:56
It writes to |next_notification_id_| on line 110.
| |
93 int64_t* notification_id) { | |
94 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
95 DCHECK_EQ(state_, STATE_INITIALIZED); | |
96 DCHECK(notification_id); | |
97 | |
98 std::string value; | |
99 Status status = LevelDBStatusToStatus( | |
100 db_->Get(leveldb::ReadOptions(), kNextNotificationIdKey, &value)); | |
101 | |
102 if (status == STATUS_ERROR_NOT_FOUND) { | |
103 *notification_id = kFirstNotificationId; | |
104 return STATUS_OK; | |
105 } | |
106 | |
107 if (status != STATUS_OK) | |
108 return status; | |
109 | |
110 if (!base::StringToInt64(value, &next_notification_id_) || | |
111 next_notification_id_ <= 0) { | |
112 return STATUS_ERROR_CORRUPTED; | |
113 } | |
114 | |
115 *notification_id = next_notification_id_; | |
116 return STATUS_OK; | |
117 } | |
118 | |
74 NotificationDatabase::Status NotificationDatabase::Destroy() { | 119 NotificationDatabase::Status NotificationDatabase::Destroy() { |
75 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 120 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
76 | 121 |
77 leveldb::Options options; | 122 leveldb::Options options; |
78 if (IsInMemoryDatabase()) { | 123 if (IsInMemoryDatabase()) { |
79 if (!env_) | 124 if (!env_) |
80 return STATUS_OK; // The database has not been initialized. | 125 return STATUS_OK; // The database has not been initialized. |
81 | 126 |
82 options.env = env_.get(); | 127 options.env = env_.get(); |
83 } | 128 } |
84 | 129 |
85 state_ = STATE_DISABLED; | 130 state_ = STATE_DISABLED; |
86 db_.reset(); | 131 db_.reset(); |
87 | 132 |
88 return LevelDBStatusToStatus( | 133 return LevelDBStatusToStatus( |
89 leveldb::DestroyDB(path_.AsUTF8Unsafe(), options)); | 134 leveldb::DestroyDB(path_.AsUTF8Unsafe(), options)); |
90 } | 135 } |
91 | 136 |
137 void NotificationDatabase::IncrementNextNotificationId( | |
138 leveldb::WriteBatch* batch) { | |
139 DCHECK(batch); | |
140 | |
141 next_notification_id_++; | |
142 batch->Put(kNextNotificationIdKey, | |
143 base::Int64ToString(next_notification_id_)); | |
144 } | |
145 | |
92 } // namespace content | 146 } // namespace content |
OLD | NEW |