Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / value_store / leveldb_value_store.h
blob29016605595accddfb3d90f111a8fb4f3d9f95ad
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_
8 #include <string>
9 #include <vector>
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 namespace base {
18 class HistogramBase;
19 } // namespace base
21 // Value store area, backed by a leveldb database.
22 // All methods must be run on the FILE thread.
23 class LeveldbValueStore : public ValueStore {
24 public:
25 // Creates a database bound to |path|. The underlying database won't be
26 // opened (i.e. may not be created) until one of the get/set/etc methods are
27 // called - this is because opening the database may fail, and extensions
28 // need to be notified of that, but we don't want to permanently give up.
30 // Must be created on the FILE thread.
31 LeveldbValueStore(const std::string& uma_client_name,
32 const base::FilePath& path);
34 // Must be deleted on the FILE thread.
35 ~LeveldbValueStore() override;
37 // ValueStore implementation.
38 size_t GetBytesInUse(const std::string& key) override;
39 size_t GetBytesInUse(const std::vector<std::string>& keys) override;
40 size_t GetBytesInUse() override;
41 ReadResult Get(const std::string& key) override;
42 ReadResult Get(const std::vector<std::string>& keys) override;
43 ReadResult Get() override;
44 WriteResult Set(WriteOptions options,
45 const std::string& key,
46 const base::Value& value) override;
47 WriteResult Set(WriteOptions options,
48 const base::DictionaryValue& values) override;
49 WriteResult Remove(const std::string& key) override;
50 WriteResult Remove(const std::vector<std::string>& keys) override;
51 WriteResult Clear() override;
52 bool Restore() override;
53 bool RestoreKey(const std::string& key) override;
55 // Write directly to the backing levelDB. Only used for testing to cause
56 // corruption in the database.
57 bool WriteToDbForTest(leveldb::WriteBatch* batch);
59 private:
60 // Tries to open the database if it hasn't been opened already.
61 scoped_ptr<ValueStore::Error> EnsureDbIsOpen();
63 // Reads a setting from the database.
64 scoped_ptr<ValueStore::Error> ReadFromDb(
65 leveldb::ReadOptions options,
66 const std::string& key,
67 // Will be reset() with the result, if any.
68 scoped_ptr<base::Value>* setting);
70 // Adds a setting to a WriteBatch, and logs the change in |changes|. For use
71 // with WriteToDb.
72 scoped_ptr<ValueStore::Error> AddToBatch(ValueStore::WriteOptions options,
73 const std::string& key,
74 const base::Value& value,
75 leveldb::WriteBatch* batch,
76 ValueStoreChangeList* changes);
78 // Commits the changes in |batch| to the database.
79 scoped_ptr<ValueStore::Error> WriteToDb(leveldb::WriteBatch* batch);
81 // Converts an error leveldb::Status to a ValueStore::Error. Returns a
82 // scoped_ptr for convenience; the result will always be non-empty.
83 scoped_ptr<ValueStore::Error> ToValueStoreError(
84 const leveldb::Status& status,
85 scoped_ptr<std::string> key);
87 // Removes the on-disk database at |db_path_|. Any file system locks should
88 // be released before calling this method.
89 void DeleteDbFile();
91 // Returns whether the database is empty.
92 bool IsEmpty();
94 // The location of the leveldb backend.
95 const base::FilePath db_path_;
97 // leveldb backend.
98 scoped_ptr<leveldb::DB> db_;
99 base::HistogramBase* open_histogram_;
101 DISALLOW_COPY_AND_ASSIGN(LeveldbValueStore);
104 #endif // EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_