pref_service_factory.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2013 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_PREFS_PREF_SERVICE_FACTORY_H_
  5. #define COMPONENTS_PREFS_PREF_SERVICE_FACTORY_H_
  6. #include "base/callback.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "components/prefs/persistent_pref_store.h"
  9. #include "components/prefs/pref_registry.h"
  10. #include "components/prefs/pref_store.h"
  11. #include "components/prefs/pref_value_store.h"
  12. #include "components/prefs/prefs_export.h"
  13. class PrefService;
  14. namespace base {
  15. class FilePath;
  16. class SequencedTaskRunner;
  17. }
  18. // A class that allows convenient building of PrefService.
  19. class COMPONENTS_PREFS_EXPORT PrefServiceFactory {
  20. public:
  21. PrefServiceFactory();
  22. PrefServiceFactory(const PrefServiceFactory&) = delete;
  23. PrefServiceFactory& operator=(const PrefServiceFactory&) = delete;
  24. virtual ~PrefServiceFactory();
  25. // Functions for setting the various parameters of the PrefService to build.
  26. void set_managed_prefs(scoped_refptr<PrefStore> prefs) {
  27. managed_prefs_.swap(prefs);
  28. }
  29. void set_supervised_user_prefs(scoped_refptr<PrefStore> prefs) {
  30. supervised_user_prefs_.swap(prefs);
  31. }
  32. void set_extension_prefs(scoped_refptr<PrefStore> prefs) {
  33. extension_prefs_.swap(prefs);
  34. }
  35. void set_standalone_browser_prefs(scoped_refptr<PersistentPrefStore> prefs) {
  36. standalone_browser_prefs_.swap(prefs);
  37. }
  38. void set_command_line_prefs(scoped_refptr<PrefStore> prefs) {
  39. command_line_prefs_.swap(prefs);
  40. }
  41. void set_user_prefs(scoped_refptr<PersistentPrefStore> prefs) {
  42. user_prefs_.swap(prefs);
  43. }
  44. void set_recommended_prefs(scoped_refptr<PrefStore> prefs) {
  45. recommended_prefs_.swap(prefs);
  46. }
  47. // Sets up error callback for the PrefService. A do-nothing default is
  48. // provided if this is not called. This callback is always invoked (async or
  49. // not) on the sequence on which Create is invoked.
  50. void set_read_error_callback(
  51. base::RepeatingCallback<void(PersistentPrefStore::PrefReadError)>
  52. read_error_callback) {
  53. read_error_callback_ = std::move(read_error_callback);
  54. }
  55. // Specifies to use an actual file-backed user pref store.
  56. void SetUserPrefsFile(const base::FilePath& prefs_file,
  57. base::SequencedTaskRunner* task_runner);
  58. void set_async(bool async) {
  59. async_ = async;
  60. }
  61. // Creates a PrefService object initialized with the parameters from
  62. // this factory.
  63. std::unique_ptr<PrefService> Create(
  64. scoped_refptr<PrefRegistry> pref_registry,
  65. std::unique_ptr<PrefValueStore::Delegate> delegate = nullptr);
  66. // Add pref stores from this object to the |pref_service|.
  67. void ChangePrefValueStore(
  68. PrefService* pref_service,
  69. std::unique_ptr<PrefValueStore::Delegate> delegate = nullptr);
  70. protected:
  71. scoped_refptr<PrefStore> managed_prefs_;
  72. scoped_refptr<PrefStore> supervised_user_prefs_;
  73. scoped_refptr<PrefStore> extension_prefs_;
  74. scoped_refptr<PersistentPrefStore> standalone_browser_prefs_;
  75. scoped_refptr<PrefStore> command_line_prefs_;
  76. scoped_refptr<PersistentPrefStore> user_prefs_;
  77. scoped_refptr<PrefStore> recommended_prefs_;
  78. base::RepeatingCallback<void(PersistentPrefStore::PrefReadError)>
  79. read_error_callback_;
  80. // Defaults to false.
  81. bool async_;
  82. };
  83. #endif // COMPONENTS_PREFS_PREF_SERVICE_FACTORY_H_