scoped_user_pref_update.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. //
  5. // A helper class that assists preferences in firing notifications when lists
  6. // or dictionaries are changed.
  7. #ifndef COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_
  8. #define COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_
  9. #include <string>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/values.h"
  13. #include "components/prefs/pref_service.h"
  14. #include "components/prefs/prefs_export.h"
  15. class PrefService;
  16. namespace subtle {
  17. // Base class for ScopedUserPrefUpdateTemplate that contains the parts
  18. // that do not depend on ScopedUserPrefUpdateTemplate's template parameter.
  19. //
  20. // We need this base class mostly for making it a friend of PrefService
  21. // and getting access to PrefService::GetMutableUserPref and
  22. // PrefService::ReportUserPrefChanged.
  23. class COMPONENTS_PREFS_EXPORT ScopedUserPrefUpdateBase {
  24. public:
  25. ScopedUserPrefUpdateBase(const ScopedUserPrefUpdateBase&) = delete;
  26. ScopedUserPrefUpdateBase& operator=(const ScopedUserPrefUpdateBase&) = delete;
  27. protected:
  28. ScopedUserPrefUpdateBase(PrefService* service, const std::string& path);
  29. // Calls Notify().
  30. ~ScopedUserPrefUpdateBase();
  31. // Sets |value_| to |service_|->GetMutableUserPref and returns it.
  32. base::Value* GetValueOfType(base::Value::Type type);
  33. private:
  34. // If |value_| is not null, triggers a notification of PrefObservers and
  35. // resets |value_|.
  36. void Notify();
  37. // Weak pointer.
  38. raw_ptr<PrefService> service_;
  39. // Path of the preference being updated.
  40. std::string path_;
  41. // Cache of value from user pref store (set between Get() and Notify() calls).
  42. raw_ptr<base::Value> value_;
  43. SEQUENCE_CHECKER(sequence_checker_);
  44. };
  45. } // namespace subtle
  46. // Class to support modifications to dictionary and list base::Values while
  47. // guaranteeing that PrefObservers are notified of changed values.
  48. //
  49. // This class may only be used on the UI thread as it requires access to the
  50. // PrefService.
  51. template <base::Value::Type type_enum_value>
  52. class ScopedUserPrefUpdate : public subtle::ScopedUserPrefUpdateBase {
  53. public:
  54. ScopedUserPrefUpdate(PrefService* service, const std::string& path)
  55. : ScopedUserPrefUpdateBase(service, path) {}
  56. ScopedUserPrefUpdate(const ScopedUserPrefUpdate&) = delete;
  57. ScopedUserPrefUpdate& operator=(const ScopedUserPrefUpdate&) = delete;
  58. // Triggers an update notification if Get() was called.
  59. virtual ~ScopedUserPrefUpdate() {}
  60. // Returns a mutable |base::Value| instance that
  61. // - is already in the user pref store, or
  62. // - is (silently) created and written to the user pref store if none existed
  63. // before.
  64. //
  65. // Calling Get() implies that an update notification is necessary at
  66. // destruction time.
  67. //
  68. // The ownership of the return value remains with the user pref store.
  69. // Virtual so it can be overriden in subclasses that transform the value
  70. // before returning it (for example to return a subelement of a dictionary).
  71. virtual base::Value* Get() { return GetValueOfType(type_enum_value); }
  72. base::Value& operator*() { return *Get(); }
  73. base::Value* operator->() { return Get(); }
  74. };
  75. typedef ScopedUserPrefUpdate<base::Value::Type::DICTIONARY>
  76. DictionaryPrefUpdate;
  77. typedef ScopedUserPrefUpdate<base::Value::Type::LIST> ListPrefUpdate;
  78. #endif // COMPONENTS_PREFS_SCOPED_USER_PREF_UPDATE_H_