optimization_hints_component_update_listener.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2017 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_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_HINTS_COMPONENT_UPDATE_LISTENER_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_HINTS_COMPONENT_UPDATE_LISTENER_H_
  6. #include "base/no_destructor.h"
  7. #include "base/observer_list.h"
  8. #include "base/sequence_checker.h"
  9. #include "components/optimization_guide/core/hints_component_info.h"
  10. #include "components/optimization_guide/core/optimization_hints_component_observer.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. class OptimizationGuideServiceTest;
  13. namespace optimization_guide {
  14. // Tracks the info for the current Optimization Hints component and notifies
  15. // observers of newly available hints components downloaded from the Component
  16. // Updater.
  17. class OptimizationHintsComponentUpdateListener {
  18. public:
  19. static OptimizationHintsComponentUpdateListener* GetInstance();
  20. OptimizationHintsComponentUpdateListener(
  21. const OptimizationHintsComponentUpdateListener&) = delete;
  22. OptimizationHintsComponentUpdateListener& operator=(
  23. const OptimizationHintsComponentUpdateListener&) = delete;
  24. // Adds the observer and synchronously dispatches the current
  25. // HintsComponentInfo to it if one is already available.
  26. //
  27. // Virtual so it can be mocked out in tests.
  28. virtual void AddObserver(OptimizationHintsComponentObserver* observer);
  29. // Virtual so it can be mocked out in tests.
  30. virtual void RemoveObserver(OptimizationHintsComponentObserver* observer);
  31. // If the hints component version in |info| is greater than that in
  32. // |hints_component_info_|, updates |hints_component_info_| and dispatches it
  33. // to all observers. In the case where the version is not greater, it does
  34. // nothing.
  35. void MaybeUpdateHintsComponent(const HintsComponentInfo& info);
  36. // Currently received HintsComponentInfo.
  37. absl::optional<HintsComponentInfo> hints_component_info() {
  38. return hints_component_info_;
  39. }
  40. private:
  41. OptimizationHintsComponentUpdateListener();
  42. ~OptimizationHintsComponentUpdateListener();
  43. friend class base::NoDestructor<OptimizationHintsComponentUpdateListener>;
  44. friend class OptimizationHintsComponentUpdateListenerTest;
  45. friend class ::OptimizationGuideServiceTest;
  46. void ResetStateForTesting();
  47. // Runner for indexing tasks.
  48. SEQUENCE_CHECKER(sequence_checker_);
  49. // Observers receiving notifications on hints being processed.
  50. base::ObserverList<OptimizationHintsComponentObserver>::Unchecked observers_;
  51. // The current HintsComponentInfo available to observers. This is unset until
  52. // the first time MaybeUpdateHintsComponent() is called.
  53. absl::optional<HintsComponentInfo> hints_component_info_;
  54. };
  55. } // namespace optimization_guide
  56. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_HINTS_COMPONENT_UPDATE_LISTENER_H_