1 // Copyright 2016 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_SCOPED_DATABASE_H_ 6 #define EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_SCOPED_DATABASE_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "base/macros.h" 13 #include "base/memory/ref_counted.h" 14 #include "extensions/browser/value_store/lazy_leveldb.h" 15 #include "extensions/browser/value_store/value_store.h" 16 17 // This database is used to persist values with their keys scoped within a 18 // specified namespace - AKA |scope|. Values will be written as follows: 19 // 20 // <scope><delimiter><scoped-key> -> <value> 21 // 22 // Note: |scope| must not contain the delimiter, but the |key| may. 23 // 24 class LeveldbScopedDatabase 25 : public LazyLevelDb, 26 public base::RefCountedThreadSafe<LeveldbScopedDatabase> { 27 public: 28 // Splits the full key into the scope and inner (scoped) key. 29 // Returns true if successfully split, and false if not and leaves |scope| and 30 // |key| unchanged. 31 static bool SplitKey(const std::string& full_key, 32 std::string* scope, 33 std::string* key); 34 35 // Creates a fully scoped key. |scope| cannot be an empty key and cannot 36 // contain the delimiter. |scoped_key| will be set to: 37 // 38 // <scope><delimiter><key> 39 // 40 // Will return true when successful, false if not. 41 static bool CreateKey(const std::string& scope, 42 const std::string& key, 43 std::string* scoped_key); 44 45 LeveldbScopedDatabase(const std::string& uma_client_name, 46 const base::FilePath& path); 47 48 // Reads a single |value| from the database for the specified |key|. 49 ValueStore::Status Read(const std::string& scope, 50 const std::string& key, 51 base::Optional<base::Value>* value); 52 53 // Reads all |values| from the database stored within the specified |scope|. 54 ValueStore::Status Read(const std::string& scope, 55 base::DictionaryValue* values); 56 57 // Writes a single |key| => |value| to the database. 58 ValueStore::Status Write(const std::string& scope, 59 const std::string& key, 60 const base::Value& value); 61 62 // Writes all |values| to the database with the keys scoped with |scope|. 63 ValueStore::Status Write(const std::string& scope, 64 const base::DictionaryValue& values); 65 66 // Deletes all |keys| from the databases withing the specified |scope|. 67 ValueStore::Status DeleteValues(const std::string& scope, 68 const std::vector<std::string>& keys); 69 70 protected: 71 friend class base::RefCountedThreadSafe<LeveldbScopedDatabase>; 72 virtual ~LeveldbScopedDatabase(); 73 74 static ValueStore::Status AddToWriteBatch(leveldb::WriteBatch* batch, 75 const std::string& scope, 76 const std::string& key, 77 const base::Value& value); 78 79 private: 80 DISALLOW_COPY_AND_ASSIGN(LeveldbScopedDatabase); 81 }; 82 83 #endif // EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_SCOPED_DATABASE_H_ 84