Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 2 | // 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 Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 8 | #include "base/memory/raw_ptr.h" |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 9 | #include "base/memory/weak_ptr.h" |
Reilly Grant | 980a89b | 2021-07-24 02:52:59 | [diff] [blame] | 10 | #include "base/scoped_observation.h" |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 11 | #include "base/sequence_checker.h" |
| 12 | #include "base/time/time.h" |
Reilly Grant | 980a89b | 2021-07-24 02:52:59 | [diff] [blame] | 13 | #include "content/common/content_export.h" |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 14 | #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 Grant | 4b6750d | 2021-11-08 08:42:46 | [diff] [blame] | 17 | #include "mojo/public/cpp/bindings/remote_set.h" |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 18 | #include "third_party/blink/public/mojom/idle/idle_manager.mojom.h" |
Nicolas Ouellet-Payeur | 52feb9df | 2022-05-12 20:28:37 | [diff] [blame] | 19 | #include "ui/base/idle/idle_polling_service.h" |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 20 | #include "url/origin.h" |
| 21 | |
| 22 | namespace content { |
| 23 | |
Reilly Grant | f5f4993 | 2021-07-20 03:39:38 | [diff] [blame] | 24 | class RenderFrameHost; |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 25 | |
Reilly Grant | 4b6aa44 | 2021-07-26 21:00:30 | [diff] [blame] | 26 | class CONTENT_EXPORT IdleManagerImpl : public blink::mojom::IdleManager, |
Nicolas Ouellet-Payeur | 52feb9df | 2022-05-12 20:28:37 | [diff] [blame] | 27 | public ui::IdlePollingService::Observer { |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 28 | public: |
Reilly Grant | f5f4993 | 2021-07-20 03:39:38 | [diff] [blame] | 29 | explicit IdleManagerImpl(RenderFrameHost* render_frame_host); |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 30 | ~IdleManagerImpl() override; |
| 31 | |
| 32 | IdleManagerImpl(const IdleManagerImpl&) = delete; |
| 33 | IdleManagerImpl& operator=(const IdleManagerImpl&) = delete; |
| 34 | |
Reilly Grant | 5ac325b | 2021-07-27 02:55:56 | [diff] [blame] | 35 | void CreateService(mojo::PendingReceiver<blink::mojom::IdleManager> receiver); |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 36 | |
| 37 | // blink.mojom.IdleManager: |
Reilly Grant | 4b6750d | 2021-11-08 08:42:46 | [diff] [blame] | 38 | void AddMonitor(mojo::PendingRemote<blink::mojom::IdleMonitor> monitor_remote, |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 39 | AddMonitorCallback callback) final; |
| 40 | |
Reilly Grant | 4b6750d | 2021-11-08 08:42:46 | [diff] [blame] | 41 | void SetIdleOverride(bool is_user_active, bool is_screen_unlocked); |
Reilly Grant | 4b6aa44 | 2021-07-26 21:00:30 | [diff] [blame] | 42 | void ClearIdleOverride(); |
Maksim Sadym | a30263e | 2020-07-29 17:09:27 | [diff] [blame] | 43 | |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 44 | private: |
| 45 | // Check permission controller to see if the notification permission is |
Reilly Grant | 5ac325b | 2021-07-27 02:55:56 | [diff] [blame] | 46 | // enabled for this frame. |
| 47 | bool HasPermission(); |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 48 | |
Reilly Grant | 4b6750d | 2021-11-08 08:42:46 | [diff] [blame] | 49 | // When a monitor's pipe closes and it has been removed from |monitors_|. |
| 50 | void OnMonitorDisconnected(mojo::RemoteSetElementId id); |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 51 | |
Reilly Grant | 980a89b | 2021-07-24 02:52:59 | [diff] [blame] | 52 | // Notifies |monitors_| of the new idle state. |
Nicolas Ouellet-Payeur | 52feb9df | 2022-05-12 20:28:37 | [diff] [blame] | 53 | void OnIdleStateChange(const ui::IdlePollingService::State& state) override; |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 54 | |
Reilly Grant | 4b6750d | 2021-11-08 08:42:46 | [diff] [blame] | 55 | blink::mojom::IdleStatePtr CreateIdleState( |
Nicolas Ouellet-Payeur | 52feb9df | 2022-05-12 20:28:37 | [diff] [blame] | 56 | const ui::IdlePollingService::State& state); |
Reilly Grant | 4b6750d | 2021-11-08 08:42:46 | [diff] [blame] | 57 | blink::mojom::IdleStatePtr CheckIdleState(); |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 58 | |
Reilly Grant | 4b6750d | 2021-11-08 08:42:46 | [diff] [blame] | 59 | // |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-Payeur | 52feb9df | 2022-05-12 20:28:37 | [diff] [blame] | 61 | base::ScopedObservation<ui::IdlePollingService, |
| 62 | ui::IdlePollingService::Observer> |
Reilly Grant | 980a89b | 2021-07-24 02:52:59 | [diff] [blame] | 63 | observer_{this}; |
Reilly Grant | 4b6750d | 2021-11-08 08:42:46 | [diff] [blame] | 64 | bool state_override_ = false; |
Maksim Sadym | a30263e | 2020-07-29 17:09:27 | [diff] [blame] | 65 | |
Reilly Grant | f5f4993 | 2021-07-20 03:39:38 | [diff] [blame] | 66 | // Raw pointer is safe because this object is owned by |render_frame_host_|. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 67 | const raw_ptr<RenderFrameHost> render_frame_host_; |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 68 | |
| 69 | // Registered clients. |
Reilly Grant | 5ac325b | 2021-07-27 02:55:56 | [diff] [blame] | 70 | mojo::ReceiverSet<blink::mojom::IdleManager> receivers_; |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 71 | |
Reilly Grant | 4b6750d | 2021-11-08 08:42:46 | [diff] [blame] | 72 | blink::mojom::IdleStatePtr last_state_; |
| 73 | |
| 74 | // Registered IdleMonitor instances, added when clients call AddMonitor(). |
| 75 | mojo::RemoteSet<blink::mojom::IdleMonitor> monitors_; |
Ayu Ishii | 4aa9d9f | 2020-05-01 19:49:49 | [diff] [blame] | 76 | |
| 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_ |