Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Igor | 900cbc1 | 2021-10-12 17:58:23 | [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 CHROME_BROWSER_LACROS_DEVICE_SETTINGS_LACROS_H_ |
| 6 | #define CHROME_BROWSER_LACROS_DEVICE_SETTINGS_LACROS_H_ |
| 7 | |
| 8 | #include "base/memory/weak_ptr.h" |
Vignesh Shenvi | 3fe3bf4 | 2022-08-17 18:11:24 | [diff] [blame] | 9 | #include "base/observer_list.h" |
| 10 | #include "base/observer_list_types.h" |
| 11 | #include "base/sequence_checker.h" |
| 12 | #include "base/thread_annotations.h" |
Igor | 900cbc1 | 2021-10-12 17:58:23 | [diff] [blame] | 13 | #include "chromeos/crosapi/mojom/device_settings_service.mojom.h" |
| 14 | #include "mojo/public/cpp/bindings/receiver.h" |
| 15 | |
| 16 | // The keeper of device settings needed for Lacros. Initializes with current |
| 17 | // value at startup and receives the updates from ash when the settings are |
| 18 | // changed. Lacros should use the device settings provided by this class when |
| 19 | // needs to use any device settings. |
| 20 | class DeviceSettingsLacros : public crosapi::mojom::DeviceSettingsObserver { |
| 21 | public: |
Vignesh Shenvi | 3fe3bf4 | 2022-08-17 18:11:24 | [diff] [blame] | 22 | // Observer that is notified on certain events like device settings updates in |
| 23 | // Ash. |
| 24 | class Observer : public base::CheckedObserver { |
| 25 | public: |
| 26 | Observer() = default; |
| 27 | Observer(const Observer&) = delete; |
| 28 | Observer& operator=(const Observer&) = delete; |
| 29 | ~Observer() override = default; |
| 30 | |
| 31 | // Triggered when device settings are updated, |
| 32 | virtual void OnDeviceSettingsUpdated() {} |
| 33 | }; |
| 34 | |
Igor | 900cbc1 | 2021-10-12 17:58:23 | [diff] [blame] | 35 | DeviceSettingsLacros(); |
| 36 | DeviceSettingsLacros(const DeviceSettingsLacros&) = delete; |
| 37 | DeviceSettingsLacros& operator=(const DeviceSettingsLacros&) = delete; |
| 38 | ~DeviceSettingsLacros() override; |
| 39 | |
Vignesh Shenvi | 3fe3bf4 | 2022-08-17 18:11:24 | [diff] [blame] | 40 | // Returns device settings that were retrieved from Ash via crosapi. Needs to |
| 41 | // be accessed in a valid sequence for thread safety. |
Igor | 900cbc1 | 2021-10-12 17:58:23 | [diff] [blame] | 42 | crosapi::mojom::DeviceSettings* GetDeviceSettings(); |
| 43 | |
| 44 | // crosapi::mojom::DeviceSettingsObserver: |
Vignesh Shenvi | 3fe3bf4 | 2022-08-17 18:11:24 | [diff] [blame] | 45 | // Updated device settings as they are recorded in Ash. Needs to run in a |
| 46 | // valid sequence for thread safety. |
Igor | 900cbc1 | 2021-10-12 17:58:23 | [diff] [blame] | 47 | void UpdateDeviceSettings( |
| 48 | crosapi::mojom::DeviceSettingsPtr device_settings) override; |
| 49 | |
Vignesh Shenvi | 3fe3bf4 | 2022-08-17 18:11:24 | [diff] [blame] | 50 | void AddObserver(Observer* observer); |
| 51 | void RemoveObserver(Observer* observer); |
| 52 | |
Igor | 900cbc1 | 2021-10-12 17:58:23 | [diff] [blame] | 53 | private: |
| 54 | void Init(); |
| 55 | |
Vignesh Shenvi | 3fe3bf4 | 2022-08-17 18:11:24 | [diff] [blame] | 56 | SEQUENCE_CHECKER(sequence_checker_); |
| 57 | |
| 58 | crosapi::mojom::DeviceSettingsPtr device_settings_ |
| 59 | GUARDED_BY_CONTEXT(sequence_checker_); |
| 60 | base::ObserverList<DeviceSettingsLacros::Observer> observers_; |
| 61 | |
Igor | 900cbc1 | 2021-10-12 17:58:23 | [diff] [blame] | 62 | mojo::Receiver<crosapi::mojom::DeviceSettingsObserver> receiver_{this}; |
| 63 | base::WeakPtrFactory<DeviceSettingsLacros> weak_ptr_factory_{this}; |
| 64 | }; |
| 65 | |
| 66 | #endif // CHROME_BROWSER_LACROS_DEVICE_SETTINGS_LACROS_H_ |