pref_hash_filter.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 SERVICES_PREFERENCES_TRACKED_PREF_HASH_FILTER_H_
  5. #define SERVICES_PREFERENCES_TRACKED_PREF_HASH_FILTER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <unordered_map>
  11. #include <vector>
  12. #include "base/callback.h"
  13. #include "base/compiler_specific.h"
  14. #include "base/memory/scoped_refptr.h"
  15. #include "mojo/public/cpp/bindings/pending_remote.h"
  16. #include "mojo/public/cpp/bindings/remote.h"
  17. #include "services/preferences/public/mojom/preferences.mojom.h"
  18. #include "services/preferences/tracked/hash_store_contents.h"
  19. #include "services/preferences/tracked/interceptable_pref_filter.h"
  20. #include "services/preferences/tracked/pref_hash_store.h"
  21. #include "services/preferences/tracked/tracked_preference.h"
  22. #include "third_party/abseil-cpp/absl/types/optional.h"
  23. class PrefService;
  24. namespace base {
  25. class DictionaryValue;
  26. class Time;
  27. } // namespace base
  28. namespace prefs {
  29. namespace mojom {
  30. class TrackedPreferenceValidationDelegate;
  31. }
  32. }
  33. namespace user_prefs {
  34. class PrefRegistrySyncable;
  35. } // namespace user_prefs
  36. // Intercepts preference values as they are loaded from disk and verifies them
  37. // using a PrefHashStore. Keeps the PrefHashStore contents up to date as values
  38. // are changed.
  39. class PrefHashFilter : public InterceptablePrefFilter {
  40. public:
  41. using StoreContentsPair = std::pair<std::unique_ptr<PrefHashStore>,
  42. std::unique_ptr<HashStoreContents>>;
  43. // Constructs a PrefHashFilter tracking the specified |tracked_preferences|
  44. // using |pref_hash_store| to check/store hashes. An optional |delegate| is
  45. // notified of the status of each preference as it is checked.
  46. // If |reset_on_load_observer| is provided, it will be notified if a reset
  47. // occurs in FilterOnLoad.
  48. // |reporting_ids_count| is the count of all possible IDs (possibly greater
  49. // than |tracked_preferences.size()|).
  50. // |external_validation_hash_store_pair_| will be used (if non-null) to
  51. // perform extra validations without triggering resets.
  52. PrefHashFilter(
  53. std::unique_ptr<PrefHashStore> pref_hash_store,
  54. StoreContentsPair external_validation_hash_store_pair_,
  55. const std::vector<prefs::mojom::TrackedPreferenceMetadataPtr>&
  56. tracked_preferences,
  57. mojo::PendingRemote<prefs::mojom::ResetOnLoadObserver>
  58. reset_on_load_observer,
  59. scoped_refptr<base::RefCountedData<
  60. mojo::Remote<prefs::mojom::TrackedPreferenceValidationDelegate>>>
  61. delegate,
  62. size_t reporting_ids_count);
  63. PrefHashFilter(const PrefHashFilter&) = delete;
  64. PrefHashFilter& operator=(const PrefHashFilter&) = delete;
  65. ~PrefHashFilter() override;
  66. // Registers required user preferences.
  67. static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
  68. // Retrieves the time of the last reset event, if any, for the provided user
  69. // preferences. If no reset has occurred, Returns a null |Time|.
  70. static base::Time GetResetTime(PrefService* user_prefs);
  71. // Clears the time of the last reset event, if any, for the provided user
  72. // preferences.
  73. static void ClearResetTime(PrefService* user_prefs);
  74. // Initializes the PrefHashStore with hashes of the tracked preferences in
  75. // |pref_store_contents|. |pref_store_contents| will be the |storage| passed
  76. // to PrefHashStore::BeginTransaction().
  77. void Initialize(base::DictionaryValue* pref_store_contents);
  78. // PrefFilter remaining implementation.
  79. void FilterUpdate(const std::string& path) override;
  80. OnWriteCallbackPair FilterSerializeData(
  81. base::DictionaryValue* pref_store_contents) override;
  82. void OnStoreDeletionFromDisk() override;
  83. private:
  84. // InterceptablePrefFilter implementation.
  85. void FinalizeFilterOnLoad(
  86. PostFilterOnLoadCallback post_filter_on_load_callback,
  87. std::unique_ptr<base::DictionaryValue> pref_store_contents,
  88. bool prefs_altered) override;
  89. // Helper function to generate FilterSerializeData()'s pre-write and
  90. // post-write callbacks. The returned callbacks are thread-safe.
  91. OnWriteCallbackPair GetOnWriteSynchronousCallbacks(
  92. base::DictionaryValue* pref_store_contents);
  93. // Clears the MACs contained in |external_validation_hash_store_contents|
  94. // which are present in |paths_to_clear|.
  95. static void ClearFromExternalStore(
  96. HashStoreContents* external_validation_hash_store_contents,
  97. const base::DictionaryValue* changed_paths_and_macs);
  98. // Flushes the MACs contained in |changed_paths_and_mac| to
  99. // external_hash_store_contents if |write_success|, otherwise discards the
  100. // changes.
  101. static void FlushToExternalStore(
  102. std::unique_ptr<HashStoreContents> external_hash_store_contents,
  103. std::unique_ptr<base::DictionaryValue> changed_paths_and_macs,
  104. bool write_success);
  105. // Callback to be invoked only once (and subsequently reset) on the next
  106. // FilterOnLoad event. It will be allowed to modify the |prefs| handed to
  107. // FilterOnLoad before handing them back to this PrefHashFilter.
  108. FilterOnLoadInterceptor filter_on_load_interceptor_;
  109. // A map of paths to TrackedPreferences; this map owns this individual
  110. // TrackedPreference objects.
  111. using TrackedPreferencesMap =
  112. std::unordered_map<std::string, std::unique_ptr<TrackedPreference>>;
  113. // A map from changed paths to their corresponding TrackedPreferences (which
  114. // aren't owned by this map).
  115. using ChangedPathsMap = std::map<std::string, const TrackedPreference*>;
  116. std::unique_ptr<PrefHashStore> pref_hash_store_;
  117. // A store and contents on which to perform extra validations without
  118. // triggering resets.
  119. // Will be null if the platform does not support external validation.
  120. absl::optional<StoreContentsPair> external_validation_hash_store_pair_;
  121. // Notified if a reset occurs in a call to FilterOnLoad.
  122. mojo::Remote<prefs::mojom::ResetOnLoadObserver> reset_on_load_observer_;
  123. scoped_refptr<base::RefCountedData<
  124. mojo::Remote<prefs::mojom::TrackedPreferenceValidationDelegate>>>
  125. delegate_;
  126. TrackedPreferencesMap tracked_paths_;
  127. // The set of all paths whose value has changed since the last call to
  128. // FilterSerializeData.
  129. ChangedPathsMap changed_paths_;
  130. };
  131. #endif // SERVICES_PREFERENCES_TRACKED_PREF_HASH_FILTER_H_