blob: 82895f3bb984cb2ba9e854aaf3d549fe64437dee [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2018 The Chromium Authors
Ayu Ishii4aa9d9f2020-05-01 19:49:492// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_IDLE_IDLE_MANAGER_IMPL_H_
6#define CONTENT_BROWSER_IDLE_IDLE_MANAGER_IMPL_H_
7
Keishi Hattori0e45c022021-11-27 09:25:528#include "base/memory/raw_ptr.h"
Ayu Ishii4aa9d9f2020-05-01 19:49:499#include "base/memory/weak_ptr.h"
Reilly Grant980a89b2021-07-24 02:52:5910#include "base/scoped_observation.h"
Ayu Ishii4aa9d9f2020-05-01 19:49:4911#include "base/sequence_checker.h"
12#include "base/time/time.h"
Reilly Grant980a89b2021-07-24 02:52:5913#include "content/common/content_export.h"
Ayu Ishii4aa9d9f2020-05-01 19:49:4914#include "mojo/public/cpp/bindings/pending_receiver.h"
15#include "mojo/public/cpp/bindings/pending_remote.h"
16#include "mojo/public/cpp/bindings/receiver_set.h"
Reilly Grant4b6750d2021-11-08 08:42:4617#include "mojo/public/cpp/bindings/remote_set.h"
Ayu Ishii4aa9d9f2020-05-01 19:49:4918#include "third_party/blink/public/mojom/idle/idle_manager.mojom.h"
Nicolas Ouellet-Payeur52feb9df2022-05-12 20:28:3719#include "ui/base/idle/idle_polling_service.h"
Ayu Ishii4aa9d9f2020-05-01 19:49:4920#include "url/origin.h"
21
22namespace content {
23
Reilly Grantf5f49932021-07-20 03:39:3824class RenderFrameHost;
Ayu Ishii4aa9d9f2020-05-01 19:49:4925
Reilly Grant4b6aa442021-07-26 21:00:3026class CONTENT_EXPORT IdleManagerImpl : public blink::mojom::IdleManager,
Nicolas Ouellet-Payeur52feb9df2022-05-12 20:28:3727 public ui::IdlePollingService::Observer {
Ayu Ishii4aa9d9f2020-05-01 19:49:4928 public:
Reilly Grantf5f49932021-07-20 03:39:3829 explicit IdleManagerImpl(RenderFrameHost* render_frame_host);
Ayu Ishii4aa9d9f2020-05-01 19:49:4930 ~IdleManagerImpl() override;
31
32 IdleManagerImpl(const IdleManagerImpl&) = delete;
33 IdleManagerImpl& operator=(const IdleManagerImpl&) = delete;
34
Reilly Grant5ac325b2021-07-27 02:55:5635 void CreateService(mojo::PendingReceiver<blink::mojom::IdleManager> receiver);
Ayu Ishii4aa9d9f2020-05-01 19:49:4936
37 // blink.mojom.IdleManager:
Reilly Grant4b6750d2021-11-08 08:42:4638 void AddMonitor(mojo::PendingRemote<blink::mojom::IdleMonitor> monitor_remote,
Ayu Ishii4aa9d9f2020-05-01 19:49:4939 AddMonitorCallback callback) final;
40
Reilly Grant4b6750d2021-11-08 08:42:4641 void SetIdleOverride(bool is_user_active, bool is_screen_unlocked);
Reilly Grant4b6aa442021-07-26 21:00:3042 void ClearIdleOverride();
Maksim Sadyma30263e2020-07-29 17:09:2743
Ayu Ishii4aa9d9f2020-05-01 19:49:4944 private:
45 // Check permission controller to see if the notification permission is
Reilly Grant5ac325b2021-07-27 02:55:5646 // enabled for this frame.
47 bool HasPermission();
Ayu Ishii4aa9d9f2020-05-01 19:49:4948
Reilly Grant4b6750d2021-11-08 08:42:4649 // When a monitor's pipe closes and it has been removed from |monitors_|.
50 void OnMonitorDisconnected(mojo::RemoteSetElementId id);
Ayu Ishii4aa9d9f2020-05-01 19:49:4951
Reilly Grant980a89b2021-07-24 02:52:5952 // Notifies |monitors_| of the new idle state.
Nicolas Ouellet-Payeur52feb9df2022-05-12 20:28:3753 void OnIdleStateChange(const ui::IdlePollingService::State& state) override;
Ayu Ishii4aa9d9f2020-05-01 19:49:4954
Reilly Grant4b6750d2021-11-08 08:42:4655 blink::mojom::IdleStatePtr CreateIdleState(
Nicolas Ouellet-Payeur52feb9df2022-05-12 20:28:3756 const ui::IdlePollingService::State& state);
Reilly Grant4b6750d2021-11-08 08:42:4657 blink::mojom::IdleStatePtr CheckIdleState();
Ayu Ishii4aa9d9f2020-05-01 19:49:4958
Reilly Grant4b6750d2021-11-08 08:42:4659 // |observer_| and |state_override_| are mutually exclusive as when DevTools
60 // has provided an override we no longer need to poll for the actual state.
Nicolas Ouellet-Payeur52feb9df2022-05-12 20:28:3761 base::ScopedObservation<ui::IdlePollingService,
62 ui::IdlePollingService::Observer>
Reilly Grant980a89b2021-07-24 02:52:5963 observer_{this};
Reilly Grant4b6750d2021-11-08 08:42:4664 bool state_override_ = false;
Maksim Sadyma30263e2020-07-29 17:09:2765
Reilly Grantf5f49932021-07-20 03:39:3866 // Raw pointer is safe because this object is owned by |render_frame_host_|.
Keishi Hattori0e45c022021-11-27 09:25:5267 const raw_ptr<RenderFrameHost> render_frame_host_;
Ayu Ishii4aa9d9f2020-05-01 19:49:4968
69 // Registered clients.
Reilly Grant5ac325b2021-07-27 02:55:5670 mojo::ReceiverSet<blink::mojom::IdleManager> receivers_;
Ayu Ishii4aa9d9f2020-05-01 19:49:4971
Reilly Grant4b6750d2021-11-08 08:42:4672 blink::mojom::IdleStatePtr last_state_;
73
74 // Registered IdleMonitor instances, added when clients call AddMonitor().
75 mojo::RemoteSet<blink::mojom::IdleMonitor> monitors_;
Ayu Ishii4aa9d9f2020-05-01 19:49:4976
77 SEQUENCE_CHECKER(sequence_checker_);
78 base::WeakPtrFactory<IdleManagerImpl> weak_factory_{this};
79};
80
81} // namespace content
82
83#endif // CONTENT_BROWSER_IDLE_IDLE_MANAGER_IMPL_H_