ambient_controller.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_AMBIENT_AMBIENT_CONTROLLER_H_
  5. #define ASH_AMBIENT_AMBIENT_CONTROLLER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "ash/ambient/ambient_access_token_controller.h"
  9. #include "ash/ambient/ambient_photo_controller.h"
  10. #include "ash/ambient/ambient_view_delegate_impl.h"
  11. #include "ash/ambient/model/ambient_backend_model.h"
  12. #include "ash/ambient/model/ambient_backend_model_observer.h"
  13. #include "ash/ambient/ui/ambient_view_delegate.h"
  14. #include "ash/ash_export.h"
  15. #include "ash/assistant/model/assistant_interaction_model_observer.h"
  16. #include "ash/constants/ambient_animation_theme.h"
  17. #include "ash/public/cpp/ambient/ambient_ui_model.h"
  18. #include "ash/public/cpp/session/session_observer.h"
  19. #include "ash/session/session_controller_impl.h"
  20. #include "ash/system/power/power_status.h"
  21. #include "base/gtest_prod_util.h"
  22. #include "base/memory/weak_ptr.h"
  23. #include "base/scoped_observation.h"
  24. #include "base/time/time.h"
  25. #include "base/timer/timer.h"
  26. #include "chromeos/dbus/power/power_manager_client.h"
  27. #include "components/prefs/pref_change_registrar.h"
  28. #include "components/prefs/pref_service.h"
  29. #include "mojo/public/cpp/bindings/pending_remote.h"
  30. #include "mojo/public/cpp/bindings/remote.h"
  31. #include "services/device/public/mojom/fingerprint.mojom.h"
  32. #include "services/device/public/mojom/wake_lock.mojom.h"
  33. #include "third_party/abseil-cpp/absl/types/optional.h"
  34. #include "ui/base/user_activity/user_activity_detector.h"
  35. #include "ui/base/user_activity/user_activity_observer.h"
  36. #include "ui/events/event_handler.h"
  37. #include "ui/views/widget/widget.h"
  38. #include "ui/views/widget/widget_observer.h"
  39. class PrefRegistrySimple;
  40. namespace ash {
  41. class AmbientAnimationProgressTracker;
  42. class AmbientBackendController;
  43. class AmbientContainerView;
  44. class AmbientMultiScreenMetricsRecorder;
  45. class AmbientPhotoController;
  46. class AmbientWeatherController;
  47. // Class to handle all ambient mode functionalities.
  48. class ASH_EXPORT AmbientController
  49. : public AmbientUiModelObserver,
  50. public AmbientBackendModelObserver,
  51. public SessionObserver,
  52. public PowerStatus::Observer,
  53. public chromeos::PowerManagerClient::Observer,
  54. public device::mojom::FingerprintObserver,
  55. public ui::UserActivityObserver,
  56. public ui::EventHandler,
  57. public AssistantInteractionModelObserver {
  58. public:
  59. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  60. explicit AmbientController(
  61. mojo::PendingRemote<device::mojom::Fingerprint> fingerprint);
  62. AmbientController(const AmbientController&) = delete;
  63. AmbientController& operator=(const AmbientController&) = delete;
  64. ~AmbientController() override;
  65. // AmbientUiModelObserver:
  66. void OnAmbientUiVisibilityChanged(AmbientUiVisibility visibility) override;
  67. // SessionObserver:
  68. void OnLockStateChanged(bool locked) override;
  69. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  70. // PowerStatus::Observer:
  71. void OnPowerStatusChanged() override;
  72. // chromeos::PowerManagerClient::Observer:
  73. void ScreenIdleStateChanged(
  74. const power_manager::ScreenIdleState& idle_state) override;
  75. void SuspendImminent(power_manager::SuspendImminent::Reason reason) override;
  76. void SuspendDone(base::TimeDelta sleep_duration) override;
  77. // fingerprint::mojom::FingerprintObserver:
  78. void OnAuthScanDone(
  79. const device::mojom::FingerprintMessagePtr msg,
  80. const base::flat_map<std::string, std::vector<std::string>>& matches)
  81. override;
  82. void OnSessionFailed() override {}
  83. void OnRestarted() override {}
  84. void OnEnrollScanDone(device::mojom::ScanResult scan_result,
  85. bool enroll_session_complete,
  86. int percent_complete) override {}
  87. // ui::UserActivityObserver:
  88. void OnUserActivity(const ui::Event* event) override;
  89. // ui::EventHandler:
  90. void OnKeyEvent(ui::KeyEvent* event) override;
  91. // AssistantInteractionModelObserver:
  92. void OnInteractionStateChanged(InteractionState interaction_state) override;
  93. void ShowUi();
  94. // Ui will be enabled but not shown immediately. If there is no user activity
  95. // Ui will be shown after a short delay.
  96. void ShowHiddenUi();
  97. void CloseUi();
  98. void ToggleInSessionUi();
  99. // Returns true if ambient mode containers are visible or are being
  100. // constructed.
  101. bool IsShown() const;
  102. void RequestAccessToken(
  103. AmbientAccessTokenController::AccessTokenCallback callback,
  104. bool may_refresh_token_on_lock = false);
  105. // Creates a widget and |AmbientContainerView| for the container.
  106. std::unique_ptr<views::Widget> CreateWidget(aura::Window* container);
  107. AmbientBackendModel* GetAmbientBackendModel();
  108. AmbientWeatherModel* GetAmbientWeatherModel();
  109. AmbientBackendController* ambient_backend_controller() {
  110. return ambient_backend_controller_.get();
  111. }
  112. AmbientPhotoController* ambient_photo_controller() {
  113. return ambient_photo_controller_.get();
  114. }
  115. AmbientWeatherController* ambient_weather_controller() {
  116. return ambient_weather_controller_.get();
  117. }
  118. AmbientUiModel* ambient_ui_model() { return &ambient_ui_model_; }
  119. AmbientViewDelegate* ambient_view_delegate() { return &delegate_; }
  120. private:
  121. friend class AmbientAshTestBase;
  122. friend class AmbientControllerTest;
  123. FRIEND_TEST_ALL_PREFIXES(AmbientControllerTest,
  124. BindsObserversWhenAmbientEnabled);
  125. FRIEND_TEST_ALL_PREFIXES(AmbientControllerTest, BindsObserversWhenAmbientOn);
  126. // Hide or close Ambient mode UI.
  127. void DismissUI();
  128. // AmbientBackendModelObserver overrides:
  129. void OnImagesReady() override;
  130. void OnImagesFailed() override;
  131. // Creates and shows a full-screen widget for each root window to show the
  132. // ambient UI.
  133. void CreateAndShowWidgets();
  134. void StartRefreshingImages();
  135. void StopRefreshingImages();
  136. AmbientAnimationTheme GetCurrentTheme() const;
  137. // Invoked when the auto-show timer in |InactivityMonitor| gets fired after
  138. // device being inactive for a specific amount of time.
  139. void OnAutoShowTimeOut();
  140. void set_backend_controller_for_testing(
  141. std::unique_ptr<AmbientBackendController> photo_client);
  142. // Creates (if not created) and acquires |wake_lock_|. Unbalanced call
  143. // without subsequently |ReleaseWakeLock| will have no effect.
  144. void AcquireWakeLock();
  145. // Release |wake_lock_|. Unbalanced release call will have no effect.
  146. void ReleaseWakeLock();
  147. void CloseAllWidgets(bool immediately);
  148. // Invoked when the Ambient mode prefs state changes.
  149. void OnEnabledPrefChanged();
  150. void OnLockScreenInactivityTimeoutPrefChanged();
  151. void OnLockScreenBackgroundTimeoutPrefChanged();
  152. void OnPhotoRefreshIntervalPrefChanged();
  153. void OnAnimationThemePrefChanged();
  154. void OnAnimationPlaybackSpeedChanged();
  155. AmbientAccessTokenController* access_token_controller_for_testing() {
  156. return &access_token_controller_;
  157. }
  158. AmbientViewDelegateImpl delegate_{this};
  159. AmbientUiModel ambient_ui_model_;
  160. AmbientAccessTokenController access_token_controller_;
  161. std::unique_ptr<AmbientBackendController> ambient_backend_controller_;
  162. std::unique_ptr<AmbientPhotoController> ambient_photo_controller_;
  163. std::unique_ptr<AmbientWeatherController> ambient_weather_controller_;
  164. std::unique_ptr<AmbientAnimationProgressTracker>
  165. ambient_animation_progress_tracker_;
  166. // Monitors the device inactivity and controls the auto-show of ambient.
  167. base::OneShotTimer inactivity_timer_;
  168. // Lazily initialized on the first call of |AcquireWakeLock|.
  169. mojo::Remote<device::mojom::WakeLock> wake_lock_;
  170. base::ScopedObservation<AmbientUiModel, AmbientUiModelObserver>
  171. ambient_ui_model_observer_{this};
  172. base::ScopedObservation<AmbientBackendModel, AmbientBackendModelObserver>
  173. ambient_backend_model_observer_{this};
  174. base::ScopedObservation<SessionControllerImpl, SessionObserver>
  175. session_observer_{this};
  176. base::ScopedObservation<PowerStatus, PowerStatus::Observer>
  177. power_status_observer_{this};
  178. base::ScopedObservation<chromeos::PowerManagerClient,
  179. chromeos::PowerManagerClient::Observer>
  180. power_manager_client_observer_{this};
  181. base::ScopedObservation<ui::UserActivityDetector, ui::UserActivityObserver>
  182. user_activity_observer_{this};
  183. // Observes user profile prefs for ambient.
  184. std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
  185. // Used to record Ambient mode engagement metrics.
  186. absl::optional<base::Time> start_time_ = absl::nullopt;
  187. base::OneShotTimer delayed_lock_timer_;
  188. mojo::Remote<device::mojom::Fingerprint> fingerprint_;
  189. mojo::Receiver<device::mojom::FingerprintObserver>
  190. fingerprint_observer_receiver_{this};
  191. // Set when |SuspendImminent| is called and cleared when |SuspendDone| is
  192. // called. Used to prevent Ambient mode from reactivating while device is
  193. // going to suspend.
  194. bool is_suspend_imminent_ = false;
  195. // Set to the off value in |ScreenIdleState| when ScreenIdleState() is
  196. // called. Used to prevent Ambient mode starting after screen is off.
  197. bool is_screen_off_ = false;
  198. // Not set until the AmbientAnimationTheme is initially read from pref
  199. // storage when ambient mode is enabled.
  200. absl::optional<AmbientAnimationTheme> current_theme_from_pref_;
  201. std::unique_ptr<AmbientMultiScreenMetricsRecorder>
  202. multi_screen_metrics_recorder_;
  203. base::WeakPtrFactory<AmbientController> weak_ptr_factory_{this};
  204. };
  205. } // namespace ash
  206. #endif // ASH_AMBIENT_AMBIENT_CONTROLLER_H_