test_value_store_factory.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef COMPONENTS_VALUE_STORE_TEST_VALUE_STORE_FACTORY_H_
  5. #define COMPONENTS_VALUE_STORE_TEST_VALUE_STORE_FACTORY_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/files/file_path.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "components/value_store/value_store_factory.h"
  11. namespace value_store {
  12. class ValueStore;
  13. // Used for tests when a new test ValueStore is required. Will either open a
  14. // database on disk (if path provided) returning a |LeveldbValueStore|.
  15. // Otherwise a new |TestingValueStore| instance will be returned.
  16. class TestValueStoreFactory : public ValueStoreFactory {
  17. public:
  18. TestValueStoreFactory();
  19. explicit TestValueStoreFactory(const base::FilePath& db_path);
  20. TestValueStoreFactory(const TestValueStoreFactory&) = delete;
  21. TestValueStoreFactory& operator=(const TestValueStoreFactory&) = delete;
  22. // ValueStoreFactory
  23. std::unique_ptr<ValueStore> CreateValueStore(
  24. const base::FilePath& directory,
  25. const std::string& uma_client_name) override;
  26. void DeleteValueStore(const base::FilePath& directory) override;
  27. bool HasValueStore(const base::FilePath& directory) override;
  28. // Return the last created |ValueStore|. Use with caution as this may return
  29. // a dangling pointer since the creator now owns the ValueStore which can be
  30. // deleted at any time.
  31. ValueStore* LastCreatedStore() const;
  32. // Return the previously created |ValueStore| in the given directory.
  33. ValueStore* GetExisting(const base::FilePath& directory) const;
  34. // Reset this class (as if just created).
  35. void Reset();
  36. private:
  37. ~TestValueStoreFactory() override;
  38. std::unique_ptr<ValueStore> CreateStore();
  39. base::FilePath db_path_;
  40. raw_ptr<ValueStore> last_created_store_ = nullptr;
  41. // A mapping from directories to their ValueStore. None of these value
  42. // stores are owned by this factory, so care must be taken when calling
  43. // GetExisting.
  44. std::map<base::FilePath, ValueStore*> value_store_map_;
  45. };
  46. } // namespace value_store
  47. #endif // COMPONENTS_VALUE_STORE_TEST_VALUE_STORE_FACTORY_H_