Commit 6a96ae29 authored by Qiyu Hu's avatar Qiyu Hu Committed by Commit Bot

arc: bluetooth: Observe connections in a direct manner

It's a bit redundant to have a helper class to simply observe
the app and intent helper connections.

BUG=b:111367421
TEST=Bluetooth works

Change-Id: Ice714e43331c7590f8c08fd8ddaa59fbc31ec3e0
Reviewed-on: https://chromium-review.googlesource.com/c/1285087Reviewed-by: default avatarHidehiko Abe <hidehiko@chromium.org>
Reviewed-by: default avatarYury Khmel <khmel@chromium.org>
Commit-Queue: Qiyu Hu <qiyuh@google.com>
Cr-Commit-Position: refs/heads/master@{#600420}
parent 5804c136
...@@ -432,59 +432,11 @@ ArcBluetoothBridge* ArcBluetoothBridge::GetForBrowserContext( ...@@ -432,59 +432,11 @@ ArcBluetoothBridge* ArcBluetoothBridge::GetForBrowserContext(
return ArcBluetoothBridgeFactory::GetForBrowserContext(context); return ArcBluetoothBridgeFactory::GetForBrowserContext(context);
} }
template <typename InstanceType, typename HostType>
class ArcBluetoothBridge::ConnectionObserverImpl
: public ConnectionObserver<InstanceType> {
public:
ConnectionObserverImpl(ArcBluetoothBridge* owner,
ArcBridgeService* arc_bridge_service)
: owner_(owner), arc_bridge_service_(arc_bridge_service) {
GetHolder()->AddObserver(this);
}
~ConnectionObserverImpl() override { GetHolder()->RemoveObserver(this); }
protected:
ConnectionHolder<InstanceType, HostType>* GetHolder();
ArcBridgeService* arc_bridge_service() { return arc_bridge_service_; }
private:
// ConnectionObserver<T>:
void OnConnectionReady() override { owner_->MaybeSendInitialPowerChange(); }
// Unowned pointer
ArcBluetoothBridge* const owner_;
ArcBridgeService* const arc_bridge_service_;
DISALLOW_COPY_AND_ASSIGN(ConnectionObserverImpl);
};
template <>
ConnectionHolder<mojom::AppInstance, mojom::AppHost>*
ArcBluetoothBridge::ConnectionObserverImpl<mojom::AppInstance,
mojom::AppHost>::GetHolder() {
return arc_bridge_service()->app();
}
template <>
ConnectionHolder<mojom::IntentHelperInstance, mojom::IntentHelperHost>*
ArcBluetoothBridge::ConnectionObserverImpl<
mojom::IntentHelperInstance,
mojom::IntentHelperHost>::GetHolder() {
return arc_bridge_service()->intent_helper();
}
ArcBluetoothBridge::ArcBluetoothBridge(content::BrowserContext* context, ArcBluetoothBridge::ArcBluetoothBridge(content::BrowserContext* context,
ArcBridgeService* bridge_service) ArcBridgeService* bridge_service)
: arc_bridge_service_(bridge_service), weak_factory_(this) { : arc_bridge_service_(bridge_service), weak_factory_(this) {
app_observer_ = std::make_unique< arc_bridge_service_->app()->AddObserver(this);
ConnectionObserverImpl<mojom::AppInstance, mojom::AppHost>>( arc_bridge_service_->intent_helper()->AddObserver(this);
this, arc_bridge_service_);
intent_helper_observer_ =
std::make_unique<ConnectionObserverImpl<mojom::IntentHelperInstance,
mojom::IntentHelperHost>>(
this, arc_bridge_service_);
if (BluetoothAdapterFactory::IsBluetoothSupported()) { if (BluetoothAdapterFactory::IsBluetoothSupported()) {
VLOG(1) << "Registering bluetooth adapter."; VLOG(1) << "Registering bluetooth adapter.";
...@@ -501,6 +453,8 @@ ArcBluetoothBridge::~ArcBluetoothBridge() { ...@@ -501,6 +453,8 @@ ArcBluetoothBridge::~ArcBluetoothBridge() {
if (bluetooth_adapter_) if (bluetooth_adapter_)
bluetooth_adapter_->RemoveObserver(this); bluetooth_adapter_->RemoveObserver(this);
arc_bridge_service_->app()->RemoveObserver(this);
arc_bridge_service_->intent_helper()->RemoveObserver(this);
arc_bridge_service_->bluetooth()->SetHost(nullptr); arc_bridge_service_->bluetooth()->SetHost(nullptr);
} }
...@@ -2473,7 +2427,7 @@ bool ArcBluetoothBridge::IsPowerChangeInitiatedByLocal( ...@@ -2473,7 +2427,7 @@ bool ArcBluetoothBridge::IsPowerChangeInitiatedByLocal(
local_power_changes_.front() == powered; local_power_changes_.front() == powered;
} }
void ArcBluetoothBridge::MaybeSendInitialPowerChange() { void ArcBluetoothBridge::OnConnectionReady() {
if (!bluetooth_adapter_ || !bluetooth_adapter_->IsPowered()) { if (!bluetooth_adapter_ || !bluetooth_adapter_->IsPowered()) {
// The default power state of Bluetooth on Android is off, so there is no // The default power state of Bluetooth on Android is off, so there is no
// need to send an intent to turn off Bluetooth if the initial power state // need to send an intent to turn off Bluetooth if the initial power state
......
...@@ -41,9 +41,7 @@ class BrowserContext; ...@@ -41,9 +41,7 @@ class BrowserContext;
namespace arc { namespace arc {
namespace mojom { namespace mojom {
class AppHost;
class AppInstance; class AppInstance;
class IntentHelperHost;
class IntentHelperInstance; class IntentHelperInstance;
} // namespace mojom } // namespace mojom
...@@ -54,6 +52,8 @@ class ArcBluetoothBridge ...@@ -54,6 +52,8 @@ class ArcBluetoothBridge
public device::BluetoothAdapter::Observer, public device::BluetoothAdapter::Observer,
public device::BluetoothAdapterFactory::AdapterCallback, public device::BluetoothAdapterFactory::AdapterCallback,
public device::BluetoothLocalGattService::Delegate, public device::BluetoothLocalGattService::Delegate,
public ConnectionObserver<mojom::AppInstance>,
public ConnectionObserver<mojom::IntentHelperInstance>,
public mojom::BluetoothHost { public mojom::BluetoothHost {
public: public:
using GattStatusCallback = using GattStatusCallback =
...@@ -341,9 +341,6 @@ class ArcBluetoothBridge ...@@ -341,9 +341,6 @@ class ArcBluetoothBridge
void StartDiscoveryImpl(bool le_scan); void StartDiscoveryImpl(bool le_scan);
void CancelDiscoveryImpl(); void CancelDiscoveryImpl();
template <typename InstanceType, typename HostType>
class ConnectionObserverImpl;
// Power state change on Bluetooth adapter. // Power state change on Bluetooth adapter.
enum class AdapterPowerState { TURN_OFF, TURN_ON }; enum class AdapterPowerState { TURN_OFF, TURN_ON };
...@@ -393,8 +390,9 @@ class ArcBluetoothBridge ...@@ -393,8 +390,9 @@ class ArcBluetoothBridge
bool IsPowerChangeInitiatedByLocal( bool IsPowerChangeInitiatedByLocal(
ArcBluetoothBridge::AdapterPowerState powered) const; ArcBluetoothBridge::AdapterPowerState powered) const;
// Sends initial power state when all preconditions are met. // ConnectionObserver<mojom::AppInstance>:
void MaybeSendInitialPowerChange(); // ConnectionObserver<mojom::IntentHelperInstance>:
void OnConnectionReady() override;
// Manages the powered change intents sent to Android. // Manages the powered change intents sent to Android.
void EnqueueLocalPowerChange(AdapterPowerState powered); void EnqueueLocalPowerChange(AdapterPowerState powered);
...@@ -574,12 +572,6 @@ class ArcBluetoothBridge ...@@ -574,12 +572,6 @@ class ArcBluetoothBridge
// Timer to turn adapter discoverable off. // Timer to turn adapter discoverable off.
base::OneShotTimer discoverable_off_timer_; base::OneShotTimer discoverable_off_timer_;
// Observers to listen the start-up of App and Intent Helper.
std::unique_ptr<ConnectionObserverImpl<mojom::AppInstance, mojom::AppHost>>
app_observer_;
std::unique_ptr<ConnectionObserverImpl<mojom::IntentHelperInstance,
mojom::IntentHelperHost>>
intent_helper_observer_;
// Queue to track the powered state changes initiated by Android. // Queue to track the powered state changes initiated by Android.
base::queue<AdapterPowerState> remote_power_changes_; base::queue<AdapterPowerState> remote_power_changes_;
// Queue to track the powered state changes initiated by Chrome. // Queue to track the powered state changes initiated by Chrome.
......
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