value_store_frontend.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #ifndef COMPONENTS_VALUE_STORE_VALUE_STORE_FRONTEND_H_
  5. #define COMPONENTS_VALUE_STORE_VALUE_STORE_FRONTEND_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/values.h"
  11. namespace base {
  12. class FilePath;
  13. class SequencedTaskRunner;
  14. } // namespace base
  15. namespace value_store {
  16. class ValueStoreFactory;
  17. // A frontend for a LeveldbValueStore, for use on the UI thread.
  18. class ValueStoreFrontend {
  19. public:
  20. using ReadCallback = base::OnceCallback<void(std::unique_ptr<base::Value>)>;
  21. ValueStoreFrontend(
  22. const scoped_refptr<ValueStoreFactory>& store_factory,
  23. const base::FilePath& directory,
  24. const std::string& uma_client_name,
  25. const scoped_refptr<base::SequencedTaskRunner>& origin_task_runner,
  26. const scoped_refptr<base::SequencedTaskRunner>& file_task_runner);
  27. ~ValueStoreFrontend();
  28. ValueStoreFrontend(const ValueStoreFrontend&) = delete;
  29. ValueStoreFrontend& operator=(const ValueStoreFrontend&) = delete;
  30. // Retrieves a value from the database asynchronously, passing a copy to
  31. // |callback| when ready. NULL is passed if no matching entry is found.
  32. void Get(const std::string& key, ReadCallback callback);
  33. // Sets a value with the given key.
  34. void Set(const std::string& key, std::unique_ptr<base::Value> value);
  35. // Removes the value with the given key.
  36. void Remove(const std::string& key);
  37. private:
  38. class Backend;
  39. // A helper class to manage lifetime of the backing ValueStore, which lives
  40. // on the FILE thread.
  41. scoped_refptr<Backend> backend_;
  42. // The task runner on which to fire callbacks.
  43. scoped_refptr<base::SequencedTaskRunner> origin_task_runner_;
  44. scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
  45. };
  46. } // namespace value_store
  47. #endif // COMPONENTS_VALUE_STORE_VALUE_STORE_FRONTEND_H_