1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_
6 #define EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_
11 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "extensions/browser/value_store/value_store.h"
15 #include "third_party/leveldatabase/src/include/leveldb/db.h"
17 // Value store area, backed by a leveldb database.
18 // All methods must be run on the FILE thread.
19 class LeveldbValueStore
: public ValueStore
{
21 // Creates a database bound to |path|. The underlying database won't be
22 // opened (i.e. may not be created) until one of the get/set/etc methods are
23 // called - this is because opening the database may fail, and extensions
24 // need to be notified of that, but we don't want to permanently give up.
26 // Must be created on the FILE thread.
27 explicit LeveldbValueStore(const base::FilePath
& path
);
29 // Must be deleted on the FILE thread.
30 ~LeveldbValueStore() override
;
32 // ValueStore implementation.
33 size_t GetBytesInUse(const std::string
& key
) override
;
34 size_t GetBytesInUse(const std::vector
<std::string
>& keys
) override
;
35 size_t GetBytesInUse() override
;
36 ReadResult
Get(const std::string
& key
) override
;
37 ReadResult
Get(const std::vector
<std::string
>& keys
) override
;
38 ReadResult
Get() override
;
39 WriteResult
Set(WriteOptions options
,
40 const std::string
& key
,
41 const base::Value
& value
) override
;
42 WriteResult
Set(WriteOptions options
,
43 const base::DictionaryValue
& values
) override
;
44 WriteResult
Remove(const std::string
& key
) override
;
45 WriteResult
Remove(const std::vector
<std::string
>& keys
) override
;
46 WriteResult
Clear() override
;
47 bool Restore() override
;
48 bool RestoreKey(const std::string
& key
) override
;
50 // Write directly to the backing levelDB. Only used for testing to cause
51 // corruption in the database.
52 bool WriteToDbForTest(leveldb::WriteBatch
* batch
);
55 // Tries to open the database if it hasn't been opened already.
56 scoped_ptr
<ValueStore::Error
> EnsureDbIsOpen();
58 // Reads a setting from the database.
59 scoped_ptr
<ValueStore::Error
> ReadFromDb(
60 leveldb::ReadOptions options
,
61 const std::string
& key
,
62 // Will be reset() with the result, if any.
63 scoped_ptr
<base::Value
>* setting
);
65 // Adds a setting to a WriteBatch, and logs the change in |changes|. For use
67 scoped_ptr
<ValueStore::Error
> AddToBatch(ValueStore::WriteOptions options
,
68 const std::string
& key
,
69 const base::Value
& value
,
70 leveldb::WriteBatch
* batch
,
71 ValueStoreChangeList
* changes
);
73 // Commits the changes in |batch| to the database.
74 scoped_ptr
<ValueStore::Error
> WriteToDb(leveldb::WriteBatch
* batch
);
76 // Converts an error leveldb::Status to a ValueStore::Error. Returns a
77 // scoped_ptr for convenience; the result will always be non-empty.
78 scoped_ptr
<ValueStore::Error
> ToValueStoreError(
79 const leveldb::Status
& status
,
80 scoped_ptr
<std::string
> key
);
82 // Removes the on-disk database at |db_path_|. Any file system locks should
83 // be released before calling this method.
86 // Returns whether the database is empty.
89 // The location of the leveldb backend.
90 const base::FilePath db_path_
;
93 scoped_ptr
<leveldb::DB
> db_
;
95 DISALLOW_COPY_AND_ASSIGN(LeveldbValueStore
);
98 #endif // EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_