system_idle_cache.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2021 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 CHROMEOS_LACROS_SYSTEM_IDLE_CACHE_H_
  5. #define CHROMEOS_LACROS_SYSTEM_IDLE_CACHE_H_
  6. #include "base/time/time.h"
  7. #include "chromeos/crosapi/mojom/idle_service.mojom.h"
  8. #include "mojo/public/cpp/bindings/receiver.h"
  9. namespace chromeos {
  10. // Provider of synchronous API to read idle info, used to compute idle state of
  11. // the system. Depending on IsIdleServiceAvailable(), this operates under one of
  12. // the following modes:
  13. // * false => Fallback mode: The getters produce fallback values to placate the
  14. // caller.
  15. // * true => Streaming mode: The instance connects to ash-chrome, listens to
  16. // system idle info stream, and caches the info for later synchronous reads
  17. // using getters.
  18. class COMPONENT_EXPORT(CHROMEOS_LACROS) SystemIdleCache
  19. : public crosapi::mojom::IdleInfoObserver {
  20. public:
  21. // TODO(huangs): Remove when ash-chrome reaches M91.
  22. // Instantiates under Fallback mode.
  23. SystemIdleCache();
  24. // Instantiates under Streaming mode, caching |info| iniitally.
  25. explicit SystemIdleCache(const crosapi::mojom::IdleInfo& info);
  26. SystemIdleCache(const SystemIdleCache&) = delete;
  27. SystemIdleCache& operator=(const SystemIdleCache&) = delete;
  28. ~SystemIdleCache() override;
  29. // Streaming mode only: Start observing idle info changes in ash-chrome.
  30. // This is a post-construction step to decouple from LacrosService.
  31. void Start();
  32. // Getters: These can be used even before Start() gets called.
  33. // Idle time for auto lock to kick in, with 0 meaning auto lock is disabled.
  34. // Fallback: Empty time (becomes 0) to pretend that auto lock is disabled.
  35. base::TimeDelta auto_lock_delay() const;
  36. // Most recent time that user is active. Fallback: Current time to pretend
  37. // that the user is always active.
  38. base::TimeTicks last_activity_time() const;
  39. // Whether the system is locked. Fallback: false to pretend that the user is
  40. // always logged in.
  41. bool is_locked() const;
  42. private:
  43. // crosapi::mojom::IdleInfoObserver:
  44. void OnIdleInfoChanged(crosapi::mojom::IdleInfoPtr info) override;
  45. // True for Fallback mode, false for Streaming mode.
  46. const bool is_fallback_;
  47. // Cached idle info.
  48. crosapi::mojom::IdleInfoPtr info_;
  49. // Receives mojo messages from ash-chromem (under Streaming mode).
  50. mojo::Receiver<crosapi::mojom::IdleInfoObserver> receiver_{this};
  51. };
  52. } // namespace chromeos
  53. #endif // CHROMEOS_LACROS_SYSTEM_IDLE_CACHE_H_