1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_ASH_COMPONENTS_HID_DETECTION_HID_DETECTION_MANAGER_IMPL_H_
#define CHROMEOS_ASH_COMPONENTS_HID_DETECTION_HID_DETECTION_MANAGER_IMPL_H_
#include "base/memory/raw_ptr.h"
#include "chromeos/ash/components/hid_detection/hid_detection_manager.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/ash/components/hid_detection/bluetooth_hid_detector.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/device_service.mojom.h"
#include "services/device/public/mojom/input_service.mojom.h"
namespace ash::hid_detection {
// Concrete HidDetectionManager implementation that uses InputDeviceManager and
// BluetoothHidDetectorImpl to detect and connect with devices.
class HidDetectionManagerImpl : public HidDetectionManager,
public device::mojom::InputDeviceManagerClient,
public BluetoothHidDetector::Delegate {
public:
using InputDeviceManagerBinder = base::RepeatingCallback<void(
mojo::PendingReceiver<device::mojom::InputDeviceManager>)>;
// Allows tests to override how this class binds InputDeviceManager receivers.
static void SetInputDeviceManagerBinderForTest(
InputDeviceManagerBinder binder);
explicit HidDetectionManagerImpl(
device::mojom::DeviceService* device_service);
~HidDetectionManagerImpl() override;
private:
friend class HidDetectionManagerImplTest;
// HidDetectionManager:
void GetIsHidDetectionRequired(
base::OnceCallback<void(bool)> callback) override;
void PerformStartHidDetection() override;
void PerformStopHidDetection() override;
HidDetectionManager::HidDetectionStatus ComputeHidDetectionStatus()
const override;
// device::mojom::InputDeviceManagerClient:
void InputDeviceAdded(device::mojom::InputDeviceInfoPtr info) override;
void InputDeviceRemoved(const std::string& id) override;
// BluetoothHidDetector::Delegate:
void OnBluetoothHidStatusChanged() override;
void BindToInputDeviceManagerIfNeeded();
// Processes the list of input devices fetched by GetIsHidDetectionRequired().
// Invokes |callback| with whether HID detection is required or not. The
// returned device list is not saved.
void OnGetDevicesForIsRequired(
base::OnceCallback<void(bool)> callback,
std::vector<device::mojom::InputDeviceInfoPtr> devices);
// Populates |device_id_to_device_map_| with the initial input devices
// connected when HID starts and sets the connected HIDs.
void OnGetDevicesAndSetClient(
std::vector<device::mojom::InputDeviceInfoPtr> devices);
// Iterates through |device_id_to_device_map_| and attempts to set each
// entry as a connected HID. Returns true if any of the entries were set as
// connected and false otherwise.
bool SetConnectedHids();
// Sets |device| as a connected HID if device is an applicable HID and no
// device of the same type is already connected. Returns true if the device
// was set as connected and false otherwise.
bool AttemptSetDeviceAsConnectedHid(
const device::mojom::InputDeviceInfo& device);
// Computes an InputMetadata based on if an input device is connected or if a
// Bluetooth device of the same type as |input_type| is pairing. A null
// |connected_device_id| means no HID for that input is connected. If
// |connected_device_id| is not null, it must be a key for an entry in
// |device_id_to_device_map_|. A null |current_pairing_device| means no
// Bluetooth device is pairing.
InputMetadata GetInputMetadata(
const std::optional<std::string>& connected_device_id,
BluetoothHidDetector::BluetoothHidType input_type,
const std::optional<BluetoothHidDetector::BluetoothHidMetadata>&
current_pairing_device) const;
// Informs |bluetooth_hid_detector_| what devices are missing.
void SetInputDevicesStatus();
// Allows tests to override the BluetoothHidDetector implementation used.
void SetBluetoothHidDetectorForTest(
std::unique_ptr<BluetoothHidDetector> bluetooth_hid_detector);
std::map<std::string, device::mojom::InputDeviceInfoPtr>
device_id_to_device_map_;
std::optional<std::string> connected_touchscreen_id_;
std::optional<std::string> connected_pointer_id_;
std::optional<std::string> connected_keyboard_id_;
raw_ptr<device::mojom::DeviceService> device_service_ = nullptr;
std::unique_ptr<BluetoothHidDetector> bluetooth_hid_detector_;
mojo::Remote<device::mojom::InputDeviceManager> input_device_manager_;
mojo::AssociatedReceiver<device::mojom::InputDeviceManagerClient>
input_device_manager_receiver_{this};
base::WeakPtrFactory<HidDetectionManagerImpl> weak_ptr_factory_{this};
};
} // namespace ash::hid_detection
#endif // CHROMEOS_ASH_COMPONENTS_HID_DETECTION_HID_DETECTION_MANAGER_IMPL_H_
|