feature_state_manager.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2018 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 ASH_SERVICES_MULTIDEVICE_SETUP_FEATURE_STATE_MANAGER_H_
  5. #define ASH_SERVICES_MULTIDEVICE_SETUP_FEATURE_STATE_MANAGER_H_
  6. #include <ostream>
  7. #include "ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
  8. #include "base/containers/flat_map.h"
  9. #include "base/observer_list.h"
  10. namespace ash {
  11. namespace multidevice_setup {
  12. // Tracks the state of the multi-device features, providing a getter/observer
  13. // interface as well as a way to toggle a feature on/off when appropriate.
  14. class FeatureStateManager {
  15. public:
  16. using FeatureStatesMap = base::flat_map<mojom::Feature, mojom::FeatureState>;
  17. class Observer {
  18. public:
  19. virtual ~Observer() = default;
  20. // Invoked when one or more features have changed state.
  21. virtual void OnFeatureStatesChange(
  22. const FeatureStatesMap& feature_states_map) = 0;
  23. };
  24. FeatureStateManager(const FeatureStateManager&) = delete;
  25. FeatureStateManager& operator=(const FeatureStateManager&) = delete;
  26. virtual ~FeatureStateManager();
  27. virtual FeatureStatesMap GetFeatureStates() = 0;
  28. // Attempts to enable or disable the feature; returns whether this operation
  29. // succeeded. A feature can only be changed via this function if the current
  30. // state is mojom::FeatureState::kEnabledByUser,
  31. // mojom::FeatureState::kFurtherSetupRequired or
  32. // mojom::FeatureState::kDisabledByUser.
  33. bool SetFeatureEnabledState(mojom::Feature feature, bool enabled);
  34. void AddObserver(Observer* observer);
  35. void RemoveObserver(Observer* observer);
  36. protected:
  37. FeatureStateManager();
  38. // Enables or disables the feature; by the time this function has been called,
  39. // it has already been confirmed that the state is indeed able to be changed.
  40. virtual void PerformSetFeatureEnabledState(mojom::Feature feature,
  41. bool enabled) = 0;
  42. void NotifyFeatureStatesChange(const FeatureStatesMap& feature_states_map);
  43. private:
  44. base::ObserverList<Observer>::Unchecked observer_list_;
  45. };
  46. std::ostream& operator<<(std::ostream& stream,
  47. const FeatureStateManager::FeatureStatesMap& map);
  48. } // namespace multidevice_setup
  49. } // namespace ash
  50. #endif // ASH_SERVICES_MULTIDEVICE_SETUP_FEATURE_STATE_MANAGER_H_