#ifndef ASH_QUICK_PAIR_KEYED_SERVICE_QUICK_PAIR_MEDIATOR_H_
#define ASH_QUICK_PAIR_KEYED_SERVICE_QUICK_PAIR_MEDIATOR_H_
#include <memory>
#include "ash/quick_pair/companion_app/companion_app_broker.h"
#include "ash/quick_pair/feature_status_tracker/quick_pair_feature_status_tracker.h"
#include "ash/quick_pair/keyed_service/fast_pair_bluetooth_config_delegate.h"
#include "ash/quick_pair/pairing/pairer_broker.h"
#include "ash/quick_pair/pairing/retroactive_pairing_detector.h"
#include "ash/quick_pair/scanning/scanner_broker.h"
#include "ash/quick_pair/ui/ui_broker.h"
#include "base/memory/scoped_refptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "chromeos/ash/services/bluetooth_config/adapter_state_controller.h"
#include "chromeos/ash/services/bluetooth_config/public/mojom/cros_bluetooth_config.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
class PrefRegistrySimple;
namespace chromeos {
namespace bluetooth_config {
class FastPairDelegate;
}
}
namespace ash {
namespace quick_pair {
class FastPairRepository;
class Device;
class QuickPairProcessManager;
class QuickPairMetricsLogger;
class MessageStreamLookup;
class BatteryUpdateMessageHandler;
class Mediator final
: public FeatureStatusTracker::Observer,
public ScannerBroker::Observer,
public PairerBroker::Observer,
public UIBroker::Observer,
public CompanionAppBroker::Observer,
public RetroactivePairingDetector::Observer,
public FastPairBluetoothConfigDelegate::Delegate,
public bluetooth_config::AdapterStateController::Observer,
public bluetooth_config::mojom::DiscoverySessionStatusObserver {
public:
class Factory {
public:
virtual ~Factory() = default;
virtual std::unique_ptr<Mediator> BuildInstance() = 0;
};
class FactoryImpl : public Factory {
private:
std::unique_ptr<Mediator> BuildInstance() override;
};
Mediator(
std::unique_ptr<FeatureStatusTracker> feature_status_tracker,
std::unique_ptr<ScannerBroker> scanner_broker,
std::unique_ptr<RetroactivePairingDetector> retroactive_pairing_detector,
std::unique_ptr<MessageStreamLookup> message_stream_lookup,
std::unique_ptr<PairerBroker> pairer_broker,
std::unique_ptr<UIBroker> ui_broker,
std::unique_ptr<CompanionAppBroker> companion_app_broker,
std::unique_ptr<FastPairRepository> fast_pair_repository,
std::unique_ptr<QuickPairProcessManager> process_manager);
Mediator(const Mediator&) = delete;
Mediator& operator=(const Mediator&) = delete;
~Mediator() override;
static void RegisterProfilePrefs(PrefRegistrySimple* registry);
static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
bluetooth_config::FastPairDelegate* GetFastPairDelegate();
void OnFastPairEnabledChanged(bool is_enabled) override;
void OnDeviceFound(scoped_refptr<Device> device) override;
void OnDeviceLost(scoped_refptr<Device> device) override;
void OnDevicePaired(scoped_refptr<Device> device) override;
void OnPairFailure(scoped_refptr<Device> device,
PairFailure failure) override;
void OnAccountKeyWrite(scoped_refptr<Device> device,
std::optional<AccountKeyFailure> error) override;
void OnDisplayPasskey(std::u16string device_name, uint32_t passkey) override;
void OnDiscoveryAction(scoped_refptr<Device> device,
DiscoveryAction action) override;
void OnPairingFailureAction(scoped_refptr<Device> device,
PairingFailedAction action) override;
void OnCompanionAppAction(scoped_refptr<Device> device,
CompanionAppAction action) override;
void OnAssociateAccountAction(scoped_refptr<Device> device,
AssociateAccountAction action) override;
void ShowInstallCompanionApp(scoped_refptr<Device> device) override;
void ShowLaunchCompanionApp(scoped_refptr<Device> device) override;
void OnCompanionAppInstalled(scoped_refptr<Device> device) override;
void OnRetroactivePairFound(scoped_refptr<Device> device) override;
void OnAdapterStateControllerChanged(bluetooth_config::AdapterStateController*
adapter_state_controller) override;
void OnAdapterStateChanged() override;
void OnHasAtLeastOneDiscoverySessionChanged(
bool has_at_least_one_discovery_session) override;
private:
enum class DiscoveryNotificationDismissalState {
kDismissed,
kShortBan,
kLongBan,
};
void SetFastPairState(bool is_enabled);
void BindToCrosBluetoothConfig();
void CancelPairing();
bool IsDeviceCurrentlyShowingNotification(scoped_refptr<Device> device);
bool IsDeviceBlockedForDiscoveryNotifications(scoped_refptr<Device> device);
void UpdateDiscoveryBlockList(scoped_refptr<Device> device);
void RemoveFromDiscoveryBlockList(scoped_refptr<Device> device);
bool has_at_least_one_discovery_session_ = false;
scoped_refptr<Device> device_currently_showing_notification_;
base::flat_map<
std::pair<std::string, Protocol>,
std::pair<DiscoveryNotificationDismissalState, std::optional<base::Time>>>
discovery_notification_block_list_;
std::unique_ptr<FeatureStatusTracker> feature_status_tracker_;
std::unique_ptr<ScannerBroker> scanner_broker_;
std::unique_ptr<MessageStreamLookup> message_stream_lookup_;
std::unique_ptr<PairerBroker> pairer_broker_;
std::unique_ptr<RetroactivePairingDetector> retroactive_pairing_detector_;
std::unique_ptr<UIBroker> ui_broker_;
std::unique_ptr<CompanionAppBroker> companion_app_broker_;
std::unique_ptr<FastPairRepository> fast_pair_repository_;
std::unique_ptr<QuickPairProcessManager> process_manager_;
std::unique_ptr<QuickPairMetricsLogger> metrics_logger_;
std::unique_ptr<FastPairBluetoothConfigDelegate>
fast_pair_bluetooth_config_delegate_;
std::unique_ptr<BatteryUpdateMessageHandler> battery_update_message_handler_;
base::ScopedObservation<FeatureStatusTracker, FeatureStatusTracker::Observer>
feature_status_tracker_observation_{this};
base::ScopedObservation<ScannerBroker, ScannerBroker::Observer>
scanner_broker_observation_{this};
base::ScopedObservation<PairerBroker, PairerBroker::Observer>
pairer_broker_observation_{this};
base::ScopedObservation<RetroactivePairingDetector,
RetroactivePairingDetector::Observer>
retroactive_pairing_detector_observation_{this};
base::ScopedObservation<UIBroker, UIBroker::Observer> ui_broker_observation_{
this};
base::ScopedObservation<CompanionAppBroker, CompanionAppBroker::Observer>
companion_app_broker_observation_{this};
base::ScopedObservation<bluetooth_config::AdapterStateController,
bluetooth_config::AdapterStateController::Observer>
adapter_state_controller_observation_{this};
mojo::Remote<bluetooth_config::mojom::CrosBluetoothConfig>
remote_cros_bluetooth_config_;
mojo::Receiver<bluetooth_config::mojom::DiscoverySessionStatusObserver>
cros_discovery_session_observer_receiver_{this};
base::WeakPtrFactory<Mediator> weak_ptr_factory_{this};
};
}
}
#endif