OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/notifications/notification_database.h" | |
6 | |
7 #include "base/files/file_util.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | |
10 #include "third_party/leveldatabase/src/include/leveldb/db.h" | |
11 #include "third_party/leveldatabase/src/include/leveldb/env.h" | |
12 | |
13 namespace content { | |
14 namespace { | |
15 | |
16 // Converts the LevelDB |status| to one of the notification database's values. | |
17 NotificationDatabase::Status LevelDBStatusToStatus( | |
18 const leveldb::Status& status) { | |
19 if (status.ok()) | |
20 return NotificationDatabase::STATUS_OK; | |
cmumford
2015/03/11 17:13:24
You could call status.IsNotFound() and return STAT
Peter Beverloo
2015/03/11 18:01:25
Ah yes, good point, thanks!
I'm ramping up the er
| |
21 | |
22 return NotificationDatabase::STATUS_ERROR_FAILED; | |
23 } | |
24 | |
25 } // namespace | |
26 | |
27 NotificationDatabase::NotificationDatabase(const base::FilePath& path) | |
28 : path_(path), | |
29 state_(STATE_UNINITIALIZED) { | |
30 sequence_checker_.DetachFromSequence(); | |
31 } | |
32 | |
33 NotificationDatabase::~NotificationDatabase() { | |
34 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
35 db_.reset(); | |
Bernhard Bauer
2015/03/11 17:15:22
You don't need to explicitly reset the database he
Peter Beverloo
2015/03/11 18:01:26
I added this to clarify that db_ needs to be reset
Bernhard Bauer
2015/03/11 18:20:01
I'm all for being explicit about it, but I'd rathe
Peter Beverloo
2015/03/11 19:32:25
Ok. Done.
| |
36 } | |
37 | |
38 NotificationDatabase::Status NotificationDatabase::Open( | |
39 bool create_if_missing) { | |
40 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
41 DCHECK_EQ(state_, STATE_UNINITIALIZED); | |
Bernhard Bauer
2015/03/11 17:15:22
Put the expected value first for nicer error messa
Peter Beverloo
2015/03/11 18:01:25
Done.
| |
42 | |
43 if (!create_if_missing) { | |
44 if (IsInMemoryDatabase() || | |
45 !base::PathExists(path_) || | |
46 base::IsDirectoryEmpty(path_)) { | |
47 return NotificationDatabase::STATUS_ERROR_NOT_FOUND; | |
48 } | |
49 } | |
50 | |
51 leveldb::Options options; | |
52 options.create_if_missing = create_if_missing; | |
cmumford
2015/03/11 17:13:24
I recommend paranoid_checks as they are cheap and
Peter Beverloo
2015/03/11 18:01:26
Done, thanks. It won't be a high-frequency databas
| |
53 if (IsInMemoryDatabase()) { | |
54 env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); | |
55 options.env = env_.get(); | |
56 } | |
57 | |
58 leveldb::DB* db; | |
Bernhard Bauer
2015/03/11 17:15:22
Initialize with nullptr?
Peter Beverloo
2015/03/11 18:01:26
Done.
| |
59 Status status = LevelDBStatusToStatus( | |
60 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db)); | |
cmumford
2015/03/11 17:13:24
Will UMA stats arrive in a future CL?
Peter Beverloo
2015/03/11 18:01:25
Yes. I've clarified this in the header and added i
| |
61 if (status != STATUS_OK) | |
62 return status; | |
cmumford
2015/03/11 17:13:24
FYI: leveldb supports the concept of a database re
Peter Beverloo
2015/03/11 18:01:26
That's a really good question actually - are we OK
cmumford
2015/03/11 18:24:33
If leveldb::DB::Open() returns an status.IsCorrupt
Peter Beverloo
2015/03/11 19:32:25
Right, thanks for the explanation!
This is logic
| |
63 | |
64 state_ = STATE_INITIALIZED; | |
65 db_.reset(db); | |
66 | |
67 return STATUS_OK; | |
68 } | |
69 | |
70 NotificationDatabase::Status NotificationDatabase::Destroy() { | |
71 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | |
72 | |
73 leveldb::Options options; | |
74 if (IsInMemoryDatabase()) { | |
75 if (!env_) | |
76 return STATUS_OK; // The database has not been initialized. | |
77 | |
78 options.env = env_.get(); | |
79 } | |
80 | |
81 state_ = STATE_DISABLED; | |
82 db_.reset(); | |
83 | |
84 return LevelDBStatusToStatus( | |
cmumford
2015/03/11 17:13:24
If you destroy an in-memory db then won't you stil
Peter Beverloo
2015/03/11 18:01:25
The |path_| is a reference, so AsUTF8Unsafe() will
| |
85 leveldb::DestroyDB(path_.AsUTF8Unsafe(), options)); | |
cmumford
2015/03/11 17:13:24
FYI: DestroyDB does leave behind non leveldb files
Peter Beverloo
2015/03/11 18:01:25
How does that happen? Reading the code, it looks l
cmumford
2015/03/11 18:24:33
ChromiumEnv::DeleteDir calls base::DeleteFile(...,
Peter Beverloo
2015/03/11 19:32:25
I see. I think this is OK, since non-database file
| |
86 } | |
87 | |
88 } // namespace content | |
OLD | NEW |