Commit 2bc4e486 authored by Jesse Schettler's avatar Jesse Schettler Committed by Commit Bot

ServiceConnection: Add Bluetooth events

Add support for cros_healthd's new Bluetooth events to
ServiceConnection.

Bug: chromium:1067641
Change-Id: Icd578de268f3627f61c98ed735dfa792331d86bb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2182403Reviewed-by: default avatarJorge Lucangeli Obes <jorgelo@chromium.org>
Reviewed-by: default avatarPaul Moy <pmoy@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Commit-Queue: Jesse Schettler <jschettler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#766531}
parent f487305e
......@@ -69,5 +69,12 @@ void FakeCrosHealthdClient::EmitAcInsertedEventForTesting() {
fake_service_.EmitAcInsertedEventForTesting();
}
void FakeCrosHealthdClient::EmitAdapterAddedEventForTesting() {
// Flush the receiver, so any pending observers are registered before the
// event is emitted.
receiver_.FlushForTesting();
fake_service_.EmitAdapterAddedEventForTesting();
}
} // namespace cros_healthd
} // namespace chromeos
......@@ -57,6 +57,10 @@ class COMPONENT_EXPORT(CROS_HEALTHD) FakeCrosHealthdClient
// Calls the power event OnAcInserted on all registered power observers.
void EmitAcInsertedEventForTesting();
// Calls the Bluetooth event OnAdapterAdded on all registered Bluetooth
// observers.
void EmitAdapterAddedEventForTesting();
private:
FakeCrosHealthdService fake_service_;
mojo::Receiver<mojom::CrosHealthdServiceFactory> receiver_{&fake_service_};
......
......@@ -127,6 +127,11 @@ void FakeCrosHealthdService::RunBatteryDischargeRoutine(
std::move(callback).Run(run_routine_response_.Clone());
}
void FakeCrosHealthdService::AddBluetoothObserver(
mojom::CrosHealthdBluetoothObserverPtr observer) {
bluetooth_observers_.Add(observer.PassInterface());
}
void FakeCrosHealthdService::AddPowerObserver(
mojom::CrosHealthdPowerObserverPtr observer) {
power_observers_.Add(observer.PassInterface());
......@@ -163,5 +168,10 @@ void FakeCrosHealthdService::EmitAcInsertedEventForTesting() {
observer->OnAcInserted();
}
void FakeCrosHealthdService::EmitAdapterAddedEventForTesting() {
for (auto& observer : bluetooth_observers_)
observer->OnAdapterAdded();
}
} // namespace cros_healthd
} // namespace chromeos
......@@ -83,6 +83,8 @@ class FakeCrosHealthdService final
RunBatteryDischargeRoutineCallback callback) override;
// CrosHealthdEventService overrides:
void AddBluetoothObserver(
mojom::CrosHealthdBluetoothObserverPtr observer) override;
void AddPowerObserver(mojom::CrosHealthdPowerObserverPtr observer) override;
// CrosHealthdProbeService overrides:
......@@ -111,6 +113,10 @@ class FakeCrosHealthdService final
// Calls the power event OnAcInserted for all registered power observers.
void EmitAcInsertedEventForTesting();
// Calls the Bluetooth event OnAdapterAdded for all registered Bluetooth
// observers.
void EmitAdapterAddedEventForTesting();
private:
// Used as the response to any GetAvailableRoutines IPCs received.
std::vector<mojom::DiagnosticRoutineEnum> available_routines_;
......@@ -129,6 +135,8 @@ class FakeCrosHealthdService final
diagnostics_receiver_set_;
mojo::ReceiverSet<mojom::CrosHealthdEventService> event_receiver_set_;
// Collection of registered Bluetooth observers.
mojo::RemoteSet<mojom::CrosHealthdBluetoothObserver> bluetooth_observers_;
// Collection of registered power observers.
mojo::RemoteSet<mojom::CrosHealthdPowerObserver> power_observers_;
......
......@@ -94,6 +94,9 @@ class ServiceConnectionImpl : public ServiceConnection {
uint32_t maximum_discharge_percent_allowed,
mojom::CrosHealthdDiagnosticsService::RunBatteryDischargeRoutineCallback
callback) override;
void AddBluetoothObserver(
mojo::PendingRemote<mojom::CrosHealthdBluetoothObserver> pending_observer)
override;
void AddPowerObserver(mojo::PendingRemote<mojom::CrosHealthdPowerObserver>
pending_observer) override;
void ProbeTelemetryInfo(
......@@ -292,6 +295,14 @@ void ServiceConnectionImpl::RunBatteryDischargeRoutine(
std::move(callback));
}
void ServiceConnectionImpl::AddBluetoothObserver(
mojo::PendingRemote<mojom::CrosHealthdBluetoothObserver> pending_observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
BindCrosHealthdEventServiceIfNeeded();
mojom::CrosHealthdBluetoothObserverPtr ptr{std::move(pending_observer)};
cros_healthd_event_service_->AddBluetoothObserver(std::move(ptr));
}
void ServiceConnectionImpl::AddPowerObserver(
mojo::PendingRemote<mojom::CrosHealthdPowerObserver> pending_observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
......
......@@ -151,6 +151,13 @@ class ServiceConnection {
mojom::CrosHealthdDiagnosticsService::RunBatteryDischargeRoutineCallback
callback) = 0;
// Subscribes to cros_healthd's Bluetooth-related events. See
// src/chromeos/services/cros_healthd/public/mojom/cros_healthd.mojom for
// details.
virtual void AddBluetoothObserver(
mojo::PendingRemote<mojom::CrosHealthdBluetoothObserver>
pending_observer) = 0;
// Subscribes to cros_healthd's power-related events. See
// src/chromeos/services/cros_healthd/public/mojom/cros_healthd.mojom for
// details.
......
......@@ -157,6 +157,30 @@ mojom::TelemetryInfoPtr MakeTelemetryInfo() {
);
}
class MockCrosHealthdBluetoothObserver
: public mojom::CrosHealthdBluetoothObserver {
public:
MockCrosHealthdBluetoothObserver() : receiver_{this} {}
MockCrosHealthdBluetoothObserver(const MockCrosHealthdBluetoothObserver&) =
delete;
MockCrosHealthdBluetoothObserver& operator=(
const MockCrosHealthdBluetoothObserver&) = delete;
MOCK_METHOD(void, OnAdapterAdded, (), (override));
MOCK_METHOD(void, OnAdapterRemoved, (), (override));
MOCK_METHOD(void, OnAdapterPropertyChanged, (), (override));
MOCK_METHOD(void, OnDeviceAdded, (), (override));
MOCK_METHOD(void, OnDeviceRemoved, (), (override));
MOCK_METHOD(void, OnDevicePropertyChanged, (), (override));
mojo::PendingRemote<mojom::CrosHealthdBluetoothObserver> pending_remote() {
return receiver_.BindNewPipeAndPassRemote();
}
private:
mojo::Receiver<mojom::CrosHealthdBluetoothObserver> receiver_;
};
class MockCrosHealthdPowerObserver : public mojom::CrosHealthdPowerObserver {
public:
MockCrosHealthdPowerObserver() : receiver_{this} {}
......@@ -443,6 +467,22 @@ TEST_F(CrosHealthdServiceConnectionTest, RunBatteryDischargeRoutine) {
run_loop.Run();
}
// Test that we can add a Bluetooth observer.
TEST_F(CrosHealthdServiceConnectionTest, AddBluetoothObserver) {
MockCrosHealthdBluetoothObserver observer;
ServiceConnection::GetInstance()->AddBluetoothObserver(
observer.pending_remote());
// Send out an event to verify the observer is connected.
base::RunLoop run_loop;
EXPECT_CALL(observer, OnAdapterAdded()).WillOnce(Invoke([&]() {
run_loop.Quit();
}));
FakeCrosHealthdClient::Get()->EmitAdapterAddedEventForTesting();
run_loop.Run();
}
// Test that we can add a power observer.
TEST_F(CrosHealthdServiceConnectionTest, AddPowerObserver) {
MockCrosHealthdPowerObserver observer;
......
......@@ -271,6 +271,13 @@ interface CrosHealthdDiagnosticsService {
// Event interface exposed by the cros_healthd daemon.
interface CrosHealthdEventService {
// Adds an observer to be notified on Bluetooth events. The caller can remove
// the observer created by this call by closing their end of the message pipe.
//
// The request:
// * |observer| - Bluetooth observer to be added to cros_healthd.
AddBluetoothObserver(CrosHealthdBluetoothObserver observer);
// Adds an observer to be notified on power events. The caller can remove the
// observer created by this call by closing their end of the message pipe.
//
......
......@@ -10,6 +10,22 @@
module chromeos.cros_healthd.mojom;
// Implemented by clients who desire Bluetooth notifications.
interface CrosHealthdBluetoothObserver {
// Fired when a Bluetooth adapter is added.
OnAdapterAdded();
// Fired when a Bluetooth adapter is removed.
OnAdapterRemoved();
// Fired when a property of a Bluetooth adapter is changed.
OnAdapterPropertyChanged();
// Fired when a Bluetooth device is added.
OnDeviceAdded();
// Fired when a Bluetooth device is removed.
OnDeviceRemoved();
// Fired when a property of a Bluetooth device is changed.
OnDevicePropertyChanged();
};
// Implemented by clients who desire power notifications.
interface CrosHealthdPowerObserver {
// Fired when the device begins consuming from an external power source.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment