color_provider_source_observer.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2021 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 UI_COLOR_COLOR_PROVIDER_SOURCE_OBSERVER_H_
  5. #define UI_COLOR_COLOR_PROVIDER_SOURCE_OBSERVER_H_
  6. #include "base/component_export.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/scoped_observation.h"
  9. #include "ui/color/color_provider_source.h"
  10. namespace ui {
  11. // Implemented by classes wanting access to the ColorProviderSource's current
  12. // ColorProvider instance and receives updates on changes to the instance
  13. // supplied. Can only observe a single ColorProviderSource at a time.
  14. class COMPONENT_EXPORT(COLOR) ColorProviderSourceObserver
  15. : public base::CheckedObserver {
  16. public:
  17. ColorProviderSourceObserver();
  18. ~ColorProviderSourceObserver() override;
  19. // Called when the source's ColorProvider instance has changed.
  20. virtual void OnColorProviderChanged() = 0;
  21. // Called by the ColorProviderSource during destruction. Avoids situations
  22. // where we could be left with a dangling pointer should the observer outlive
  23. // the source.
  24. void OnColorProviderSourceDestroying();
  25. const ui::ColorProviderSource* GetColorProviderSourceForTesting() const;
  26. protected:
  27. // Starts observing the new `source`. Clears the current observation if
  28. // already observing a ColorProviderSource.
  29. void Observe(ColorProviderSource* source);
  30. // Gets the ColorProviderSource currently under observation, if it exists.
  31. const ui::ColorProviderSource* GetColorProviderSource() const;
  32. private:
  33. // The currently observed source.
  34. raw_ptr<const ui::ColorProviderSource> source_ = nullptr;
  35. // Ensure references to the observer are removed from the source should the
  36. // source outlive the observer.
  37. base::ScopedObservation<ColorProviderSource, ColorProviderSourceObserver>
  38. color_provider_source_observation_{this};
  39. };
  40. } // namespace ui
  41. #endif // UI_COLOR_COLOR_PROVIDER_SOURCE_OBSERVER_H_