assistant_alarm_timer_controller_impl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_ASSISTANT_ASSISTANT_ALARM_TIMER_CONTROLLER_IMPL_H_
  5. #define ASH_ASSISTANT_ASSISTANT_ALARM_TIMER_CONTROLLER_IMPL_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "ash/assistant/model/assistant_alarm_timer_model.h"
  10. #include "ash/assistant/model/assistant_alarm_timer_model_observer.h"
  11. #include "ash/public/cpp/assistant/assistant_state.h"
  12. #include "ash/public/cpp/assistant/controller/assistant_alarm_timer_controller.h"
  13. #include "ash/public/cpp/assistant/controller/assistant_controller.h"
  14. #include "ash/public/cpp/assistant/controller/assistant_controller_observer.h"
  15. #include "base/scoped_observation.h"
  16. #include "base/timer/timer.h"
  17. #include "chromeos/ash/services/assistant/public/cpp/assistant_service.h"
  18. namespace ash {
  19. namespace assistant {
  20. namespace util {
  21. enum class AlarmTimerAction;
  22. } // namespace util
  23. } // namespace assistant
  24. class AssistantControllerImpl;
  25. // The AssistantAlarmTimerControllerImpl is a sub-controller of
  26. // AssistantController tasked with tracking alarm/timer state and providing
  27. // alarm/timer APIs.
  28. class AssistantAlarmTimerControllerImpl
  29. : public AssistantAlarmTimerController,
  30. public AssistantControllerObserver,
  31. public AssistantStateObserver,
  32. public AssistantAlarmTimerModelObserver {
  33. public:
  34. explicit AssistantAlarmTimerControllerImpl(
  35. AssistantControllerImpl* assistant_controller);
  36. AssistantAlarmTimerControllerImpl(const AssistantAlarmTimerControllerImpl&) =
  37. delete;
  38. AssistantAlarmTimerControllerImpl& operator=(
  39. const AssistantAlarmTimerControllerImpl&) = delete;
  40. ~AssistantAlarmTimerControllerImpl() override;
  41. // Provides a pointer to the |assistant| owned by AssistantService.
  42. void SetAssistant(assistant::Assistant* assistant);
  43. // AssistantAlarmTimerController:
  44. const AssistantAlarmTimerModel* GetModel() const override;
  45. void OnTimerStateChanged(
  46. const std::vector<chromeos::assistant::AssistantTimer>& timers) override;
  47. // AssistantControllerObserver:
  48. void OnAssistantControllerConstructed() override;
  49. void OnAssistantControllerDestroying() override;
  50. void OnDeepLinkReceived(
  51. assistant::util::DeepLinkType type,
  52. const std::map<std::string, std::string>& params) override;
  53. // AssistantStateObserver:
  54. void OnAssistantStatusChanged(assistant::AssistantStatus status) override;
  55. // AssistantAlarmTimerModelObserver:
  56. void OnTimerAdded(const chromeos::assistant::AssistantTimer& timer) override;
  57. void OnTimerUpdated(
  58. const chromeos::assistant::AssistantTimer& timer) override;
  59. void OnTimerRemoved(
  60. const chromeos::assistant::AssistantTimer& timer) override;
  61. private:
  62. void PerformAlarmTimerAction(const assistant::util::AlarmTimerAction& action,
  63. const std::string& alarm_timer_id,
  64. const absl::optional<base::TimeDelta>& duration);
  65. void ScheduleNextTick(const chromeos::assistant::AssistantTimer& timer);
  66. void Tick(const std::string& timer_id);
  67. AssistantControllerImpl* const assistant_controller_; // Owned by Shell.
  68. AssistantAlarmTimerModel model_;
  69. // We independently tick timers in our |model_| to update their respective
  70. // remaining times. This map contains these tickers, keyed by timer id.
  71. std::map<std::string, base::OneShotTimer> tickers_;
  72. // Owned by AssistantService.
  73. assistant::Assistant* assistant_;
  74. base::ScopedObservation<AssistantController, AssistantControllerObserver>
  75. assistant_controller_observation_{this};
  76. };
  77. } // namespace ash
  78. #endif // ASH_ASSISTANT_ASSISTANT_ALARM_TIMER_CONTROLLER_IMPL_H_