pressure_manager_impl.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 SERVICES_DEVICE_COMPUTE_PRESSURE_PRESSURE_MANAGER_IMPL_H_
  5. #define SERVICES_DEVICE_COMPUTE_PRESSURE_PRESSURE_MANAGER_IMPL_H_
  6. #include <memory>
  7. #include "base/sequence_checker.h"
  8. #include "base/thread_annotations.h"
  9. #include "base/time/time.h"
  10. #include "mojo/public/cpp/bindings/pending_receiver.h"
  11. #include "mojo/public/cpp/bindings/pending_remote.h"
  12. #include "mojo/public/cpp/bindings/receiver_set.h"
  13. #include "mojo/public/cpp/bindings/remote_set.h"
  14. #include "services/device/compute_pressure/platform_collector.h"
  15. #include "services/device/compute_pressure/pressure_sample.h"
  16. #include "services/device/public/mojom/pressure_manager.mojom.h"
  17. namespace device {
  18. class CpuProbe;
  19. // Handles the communication between the browser process and services.
  20. //
  21. // This class owns one instance of PlatformCollector. The PlatformCollector
  22. // instance keeps collecting compute pressure information from the
  23. // underlying operating system when `clients_` is not empty and stops
  24. // collecting when `clients_` becomes empty.
  25. //
  26. // DeviceService owns one instance of this class.
  27. //
  28. // Instances are not thread-safe and should be used on the same sequence.
  29. class PressureManagerImpl : public mojom::PressureManager {
  30. public:
  31. // The sampling interval must be smaller or equal to the rate-limit for
  32. // observer updates.
  33. static constexpr base::TimeDelta kDefaultSamplingInterval = base::Seconds(1);
  34. // Factory method for production instances.
  35. static std::unique_ptr<PressureManagerImpl> Create();
  36. // Factory method with dependency injection support for testing.
  37. static std::unique_ptr<PressureManagerImpl> CreateForTesting(
  38. std::unique_ptr<CpuProbe> cpu_probe,
  39. base::TimeDelta sampling_interval);
  40. ~PressureManagerImpl() override;
  41. PressureManagerImpl(const PressureManagerImpl&) = delete;
  42. PressureManagerImpl& operator=(const PressureManagerImpl&) = delete;
  43. void Bind(mojo::PendingReceiver<mojom::PressureManager> receiver);
  44. // device::mojom::PressureManager implementation.
  45. void AddClient(mojo::PendingRemote<mojom::PressureClient> client,
  46. AddClientCallback callback) override;
  47. private:
  48. PressureManagerImpl(std::unique_ptr<CpuProbe> cpu_probe,
  49. base::TimeDelta sampling_interval);
  50. // Called periodically by PlatformCollector.
  51. void UpdateClients(PressureSample sample);
  52. // Stop `collector_` once there is no client.
  53. void OnClientRemoteDisconnected(mojo::RemoteSetElementId /*id*/);
  54. SEQUENCE_CHECKER(sequence_checker_);
  55. PlatformCollector collector_ GUARDED_BY_CONTEXT(sequence_checker_);
  56. mojo::ReceiverSet<mojom::PressureManager> receivers_
  57. GUARDED_BY_CONTEXT(sequence_checker_);
  58. mojo::RemoteSet<mojom::PressureClient> clients_
  59. GUARDED_BY_CONTEXT(sequence_checker_);
  60. };
  61. } // namespace device
  62. #endif // SERVICES_DEVICE_COMPUTE_PRESSURE_PRESSURE_MANAGER_IMPL_H_