Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / value_store / leveldb_value_store.h
blob91217e3c7a9a799963efd6688778cd11924a0d3c
1 // Copyright (c) 2012 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 CHROME_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_
6 #define CHROME_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 "chrome/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 {
20 public:
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 virtual ~LeveldbValueStore();
32 // ValueStore implementation.
33 virtual size_t GetBytesInUse(const std::string& key) OVERRIDE;
34 virtual size_t GetBytesInUse(const std::vector<std::string>& keys) OVERRIDE;
35 virtual size_t GetBytesInUse() OVERRIDE;
36 virtual ReadResult Get(const std::string& key) OVERRIDE;
37 virtual ReadResult Get(const std::vector<std::string>& keys) OVERRIDE;
38 virtual ReadResult Get() OVERRIDE;
39 virtual WriteResult Set(
40 WriteOptions options,
41 const std::string& key,
42 const base::Value& value) OVERRIDE;
43 virtual WriteResult Set(
44 WriteOptions options, const base::DictionaryValue& values) OVERRIDE;
45 virtual WriteResult Remove(const std::string& key) OVERRIDE;
46 virtual WriteResult Remove(const std::vector<std::string>& keys) OVERRIDE;
47 virtual WriteResult Clear() OVERRIDE;
49 private:
50 // Tries to open the database if it hasn't been opened already.
51 scoped_ptr<ValueStore::Error> EnsureDbIsOpen();
53 // Reads a setting from the database.
54 scoped_ptr<ValueStore::Error> ReadFromDb(
55 leveldb::ReadOptions options,
56 const std::string& key,
57 // Will be reset() with the result, if any.
58 scoped_ptr<base::Value>* setting);
60 // Adds a setting to a WriteBatch, and logs the change in |changes|. For use
61 // with WriteToDb.
62 scoped_ptr<ValueStore::Error> AddToBatch(ValueStore::WriteOptions options,
63 const std::string& key,
64 const base::Value& value,
65 leveldb::WriteBatch* batch,
66 ValueStoreChangeList* changes);
68 // Commits the changes in |batch| to the database.
69 scoped_ptr<ValueStore::Error> WriteToDb(leveldb::WriteBatch* batch);
71 // Converts an error leveldb::Status to a ValueStore::Error. Returns a
72 // scoped_ptr for convenience; the result will always be non-empty.
73 scoped_ptr<ValueStore::Error> ToValueStoreError(
74 const leveldb::Status& status,
75 scoped_ptr<std::string> key);
77 // Removes the on-disk database at |db_path_|. Any file system locks should
78 // be released before calling this method.
79 void DeleteDbFile();
81 // Returns whether the database is empty.
82 bool IsEmpty();
84 // The location of the leveldb backend.
85 const base::FilePath db_path_;
87 // leveldb backend.
88 scoped_ptr<leveldb::DB> db_;
90 DISALLOW_COPY_AND_ASSIGN(LeveldbValueStore);
93 #endif // CHROME_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_