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: Decimal string which fits into an int64_t. |
| 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 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db)); | 81 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db)); |
65 if (status != STATUS_OK) | 82 if (status != STATUS_OK) |
66 return status; | 83 return status; |
67 | 84 |
68 state_ = STATE_INITIALIZED; | 85 state_ = STATE_INITIALIZED; |
69 db_.reset(db); | 86 db_.reset(db); |
70 | 87 |
71 return STATUS_OK; | 88 return STATUS_OK; |
72 } | 89 } |
73 | 90 |
| 91 NotificationDatabase::Status NotificationDatabase::GetNextNotificationId( |
| 92 int64_t* notification_id) const { |
| 93 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
| 94 DCHECK_EQ(state_, STATE_INITIALIZED); |
| 95 DCHECK(notification_id); |
| 96 |
| 97 std::string value; |
| 98 Status status = LevelDBStatusToStatus( |
| 99 db_->Get(leveldb::ReadOptions(), kNextNotificationIdKey, &value)); |
| 100 |
| 101 if (status == STATUS_ERROR_NOT_FOUND) { |
| 102 *notification_id = kFirstNotificationId; |
| 103 return STATUS_OK; |
| 104 } |
| 105 |
| 106 if (status != STATUS_OK) |
| 107 return status; |
| 108 |
| 109 int64_t next_notification_id; |
| 110 if (!base::StringToInt64(value, &next_notification_id) || |
| 111 next_notification_id < kFirstNotificationId) { |
| 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::WriteNextNotificationId( |
| 138 leveldb::WriteBatch* batch, |
| 139 int64_t next_notification_id) const { |
| 140 DCHECK_GE(next_notification_id, kFirstNotificationId); |
| 141 DCHECK(batch); |
| 142 |
| 143 batch->Put(kNextNotificationIdKey, base::Int64ToString(next_notification_id)); |
| 144 } |
| 145 |
92 } // namespace content | 146 } // namespace content |
OLD | NEW |