interceptable_pref_filter.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 SERVICES_PREFERENCES_TRACKED_INTERCEPTABLE_PREF_FILTER_H_
  5. #define SERVICES_PREFERENCES_TRACKED_INTERCEPTABLE_PREF_FILTER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/values.h"
  10. #include "components/prefs/pref_filter.h"
  11. // A partial implementation of a PrefFilter whose FilterOnLoad call may be
  12. // intercepted by a FilterOnLoadInterceptor. Implementations of
  13. // InterceptablePrefFilter are expected to override FinalizeFilterOnLoad rather
  14. // than re-overriding FilterOnLoad.
  15. class InterceptablePrefFilter
  16. : public PrefFilter,
  17. public base::SupportsWeakPtr<InterceptablePrefFilter> {
  18. public:
  19. // A callback to be invoked by a FilterOnLoadInterceptor when its ready to
  20. // hand back the |prefs| it was handed for early filtering. |prefs_altered|
  21. // indicates whether the |prefs| were actually altered by the
  22. // FilterOnLoadInterceptor before being handed back.
  23. using FinalizeFilterOnLoadCallback =
  24. base::OnceCallback<void(std::unique_ptr<base::DictionaryValue> prefs,
  25. bool prefs_altered)>;
  26. // A callback to be invoked from FilterOnLoad. It takes ownership of prefs
  27. // and may modify them before handing them back to this
  28. // InterceptablePrefFilter via |finalize_filter_on_load|.
  29. using FilterOnLoadInterceptor = base::OnceCallback<void(
  30. FinalizeFilterOnLoadCallback finalize_filter_on_load,
  31. std::unique_ptr<base::DictionaryValue> prefs)>;
  32. InterceptablePrefFilter();
  33. ~InterceptablePrefFilter() override;
  34. // PrefFilter partial implementation.
  35. void FilterOnLoad(
  36. PostFilterOnLoadCallback post_filter_on_load_callback,
  37. std::unique_ptr<base::DictionaryValue> pref_store_contents) override;
  38. // Registers |filter_on_load_interceptor| to intercept the next FilterOnLoad
  39. // event. At most one FilterOnLoadInterceptor should be registered per
  40. // PrefFilter.
  41. void InterceptNextFilterOnLoad(
  42. FilterOnLoadInterceptor filter_on_load_interceptor);
  43. void OnStoreDeletionFromDisk() override;
  44. private:
  45. // Does any extra filtering required by the implementation of this
  46. // InterceptablePrefFilter and hands back the |pref_store_contents| to the
  47. // initial caller of FilterOnLoad.
  48. virtual void FinalizeFilterOnLoad(
  49. PostFilterOnLoadCallback post_filter_on_load_callback,
  50. std::unique_ptr<base::DictionaryValue> pref_store_contents,
  51. bool prefs_altered) = 0;
  52. // Callback to be invoked only once (and subsequently reset) on the next
  53. // FilterOnLoad event. It will be allowed to modify the |prefs| handed to
  54. // FilterOnLoad before handing them back to this PrefHashFilter.
  55. FilterOnLoadInterceptor filter_on_load_interceptor_;
  56. };
  57. #endif // SERVICES_PREFERENCES_TRACKED_INTERCEPTABLE_PREF_FILTER_H_