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> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "content/browser/notifications/notification_database_data.h" | 12 #include "content/browser/notifications/notification_database_data_conversions.h
" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "storage/common/database/database_identifier.h" | 14 #include "storage/common/database/database_identifier.h" |
15 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | 15 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" |
16 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 16 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
17 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 17 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
20 | 20 |
21 // Notification LevelDB database schema (in alphabetized order) | 21 // Notification LevelDB database schema (in alphabetized order) |
22 // ======================= | 22 // ======================= |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 DCHECK(notification_database_data); | 118 DCHECK(notification_database_data); |
119 | 119 |
120 std::string key = CreateDataKey(notification_id, origin); | 120 std::string key = CreateDataKey(notification_id, origin); |
121 std::string serialized_data; | 121 std::string serialized_data; |
122 | 122 |
123 Status status = LevelDBStatusToStatus( | 123 Status status = LevelDBStatusToStatus( |
124 db_->Get(leveldb::ReadOptions(), key, &serialized_data)); | 124 db_->Get(leveldb::ReadOptions(), key, &serialized_data)); |
125 if (status != STATUS_OK) | 125 if (status != STATUS_OK) |
126 return status; | 126 return status; |
127 | 127 |
128 if (notification_database_data->ParseFromString(serialized_data)) | 128 if (DeserializeNotificationDatabaseData(serialized_data, |
| 129 notification_database_data)) { |
129 return STATUS_OK; | 130 return STATUS_OK; |
| 131 } |
130 | 132 |
131 DLOG(ERROR) << "Unable to deserialize data for notification " | 133 DLOG(ERROR) << "Unable to deserialize data for notification " |
132 << notification_id << " belonging to " << origin << "."; | 134 << notification_id << " belonging to " << origin << "."; |
133 return STATUS_ERROR_CORRUPTED; | 135 return STATUS_ERROR_CORRUPTED; |
134 } | 136 } |
135 | 137 |
136 NotificationDatabase::Status NotificationDatabase::WriteNotificationData( | 138 NotificationDatabase::Status NotificationDatabase::WriteNotificationData( |
137 const GURL& origin, | 139 const GURL& origin, |
138 const NotificationDatabaseData& notification_database_data, | 140 const NotificationDatabaseData& notification_database_data, |
139 int64_t* notification_id) { | 141 int64_t* notification_id) { |
140 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 142 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); |
141 DCHECK_EQ(STATE_INITIALIZED, state_); | 143 DCHECK_EQ(STATE_INITIALIZED, state_); |
142 DCHECK(notification_id); | 144 DCHECK(notification_id); |
143 DCHECK(origin.is_valid()); | 145 DCHECK(origin.is_valid()); |
144 | 146 |
145 std::string serialized_data; | 147 std::string serialized_data; |
146 if (!notification_database_data.SerializeToString(&serialized_data)) { | 148 if (!SerializeNotificationDatabaseData(notification_database_data, |
| 149 &serialized_data)) { |
147 DLOG(ERROR) << "Unable to serialize data for a notification belonging " | 150 DLOG(ERROR) << "Unable to serialize data for a notification belonging " |
148 << "to: " << origin; | 151 << "to: " << origin; |
149 return STATUS_ERROR_FAILED; | 152 return STATUS_ERROR_FAILED; |
150 } | 153 } |
151 | 154 |
152 DCHECK_GE(next_notification_id_, kFirstNotificationId); | 155 DCHECK_GE(next_notification_id_, kFirstNotificationId); |
153 | 156 |
154 leveldb::WriteBatch batch; | 157 leveldb::WriteBatch batch; |
155 batch.Put(CreateDataKey(next_notification_id_, origin), serialized_data); | 158 batch.Put(CreateDataKey(next_notification_id_, origin), serialized_data); |
156 batch.Put(kNextNotificationIdKey, | 159 batch.Put(kNextNotificationIdKey, |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 | 213 |
211 if (!base::StringToInt64(value, &next_notification_id_) || | 214 if (!base::StringToInt64(value, &next_notification_id_) || |
212 next_notification_id_ < kFirstNotificationId) { | 215 next_notification_id_ < kFirstNotificationId) { |
213 return STATUS_ERROR_CORRUPTED; | 216 return STATUS_ERROR_CORRUPTED; |
214 } | 217 } |
215 | 218 |
216 return STATUS_OK; | 219 return STATUS_OK; |
217 } | 220 } |
218 | 221 |
219 } // namespace content | 222 } // namespace content |
OLD | NEW |