night_light_controller.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2019 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_PUBLIC_CPP_NIGHT_LIGHT_CONTROLLER_H_
  5. #define ASH_PUBLIC_CPP_NIGHT_LIGHT_CONTROLLER_H_
  6. #include "ash/public/cpp/ash_public_export.h"
  7. #include "base/observer_list.h"
  8. namespace ash {
  9. class ASH_PUBLIC_EXPORT NightLightController {
  10. public:
  11. // These values are written to logs. New enum values can be added, but
  12. // existing enums must never be renumbered or deleted and reused.
  13. enum ScheduleType {
  14. // Automatic toggling of NightLight is turned off.
  15. kNone = 0,
  16. // Turned automatically on at the user's local sunset time, and off at the
  17. // user's local sunrise time.
  18. kSunsetToSunrise = 1,
  19. // Toggled automatically based on the custom set start and end times
  20. // selected by the user from the system settings.
  21. kCustom = 2,
  22. // kMaxValue is required for UMA_HISTOGRAM_ENUMERATION.
  23. kMaxValue = kCustom,
  24. };
  25. // Represents a geolocation position fix. It's "simple" because it doesn't
  26. // expose all the parameters of the position interface as defined by the
  27. // Geolocation API Specification:
  28. // https://dev.w3.org/geo/api/spec-source.html#position_interface
  29. // The NightLightController is only interested in valid latitude and
  30. // longitude. It also doesn't require any specific accuracy. The more accurate
  31. // the positions, the more accurate sunset and sunrise times calculations.
  32. // However, an IP-based geoposition is considered good enough.
  33. struct SimpleGeoposition {
  34. bool operator==(const SimpleGeoposition& other) const {
  35. return latitude == other.latitude && longitude == other.longitude;
  36. }
  37. double latitude;
  38. double longitude;
  39. };
  40. class Observer {
  41. public:
  42. // Notifies observers with the new schedule type whenever it changes.
  43. virtual void OnScheduleTypeChanged(ScheduleType new_type) {}
  44. // Emitted when the NightLight status is changed.
  45. virtual void OnNightLightEnabledChanged(bool enabled) {}
  46. protected:
  47. virtual ~Observer() {}
  48. };
  49. static NightLightController* GetInstance();
  50. NightLightController(const NightLightController&) = delete;
  51. NightLightController& operator=(const NightLightController&) = delete;
  52. // Provides the NightLightController with the user's geoposition so that it
  53. // can calculate the sunset and sunrise times. This should only be called when
  54. // the schedule type is set to "Sunset to Sunrise".
  55. virtual void SetCurrentGeoposition(const SimpleGeoposition& position) = 0;
  56. // Whether Night Light is enabled.
  57. virtual bool GetEnabled() const = 0;
  58. void AddObserver(Observer* observer);
  59. void RemoveObserver(Observer* observer);
  60. protected:
  61. NightLightController();
  62. virtual ~NightLightController();
  63. base::ObserverList<Observer>::Unchecked observers_;
  64. };
  65. } // namespace ash
  66. #endif // ASH_PUBLIC_CPP_NIGHT_LIGHT_CONTROLLER_H_