Home | History | Annotate | Download | only in value_store
      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.
      4 
      5 #ifndef EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_
      6 #define EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     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"
     16 
     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.
     25   //
     26   // Must be created on the FILE thread.
     27   explicit LeveldbValueStore(const base::FilePath& path);
     28 
     29   // Must be deleted on the FILE thread.
     30   virtual ~LeveldbValueStore();
     31 
     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;
     48   virtual bool Restore() OVERRIDE;
     49   virtual bool RestoreKey(const std::string& key) OVERRIDE;
     50 
     51   // Write directly to the backing levelDB. Only used for testing to cause
     52   // corruption in the database.
     53   bool WriteToDbForTest(leveldb::WriteBatch* batch);
     54 
     55  private:
     56   // Tries to open the database if it hasn't been opened already.
     57   scoped_ptr<ValueStore::Error> EnsureDbIsOpen();
     58 
     59   // Reads a setting from the database.
     60   scoped_ptr<ValueStore::Error> ReadFromDb(
     61       leveldb::ReadOptions options,
     62       const std::string& key,
     63       // Will be reset() with the result, if any.
     64       scoped_ptr<base::Value>* setting);
     65 
     66   // Adds a setting to a WriteBatch, and logs the change in |changes|. For use
     67   // with WriteToDb.
     68   scoped_ptr<ValueStore::Error> AddToBatch(ValueStore::WriteOptions options,
     69                                            const std::string& key,
     70                                            const base::Value& value,
     71                                            leveldb::WriteBatch* batch,
     72                                            ValueStoreChangeList* changes);
     73 
     74   // Commits the changes in |batch| to the database.
     75   scoped_ptr<ValueStore::Error> WriteToDb(leveldb::WriteBatch* batch);
     76 
     77   // Converts an error leveldb::Status to a ValueStore::Error. Returns a
     78   // scoped_ptr for convenience; the result will always be non-empty.
     79   scoped_ptr<ValueStore::Error> ToValueStoreError(
     80       const leveldb::Status& status,
     81       scoped_ptr<std::string> key);
     82 
     83   // Removes the on-disk database at |db_path_|. Any file system locks should
     84   // be released before calling this method.
     85   void DeleteDbFile();
     86 
     87   // Returns whether the database is empty.
     88   bool IsEmpty();
     89 
     90   // The location of the leveldb backend.
     91   const base::FilePath db_path_;
     92 
     93   // leveldb backend.
     94   scoped_ptr<leveldb::DB> db_;
     95 
     96   DISALLOW_COPY_AND_ASSIGN(LeveldbValueStore);
     97 };
     98 
     99 #endif  // EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_VALUE_STORE_H_
    100