Commit 1ffc3026 authored by puthik's avatar puthik Committed by Commit bot

arc: bluetooth: Add/Remove BT observer only when ARC is ready

The current implementation make arc_bluetooth_bridge to be
a bluetooth observer all the time. This lead to unnessary
work when ARC is not ready.

This patch changes that by register arc_bluetooth_bridge
to be a bluetooth observer when arc is ready and unregister
when arc is gone

BUG=635578
TEST=No log spam when there is no arc support

Review-Url: https://codereview.chromium.org/2223203002
Cr-Commit-Position: refs/heads/master@{#410828}
parent e2311a2b
...@@ -180,13 +180,12 @@ namespace arc { ...@@ -180,13 +180,12 @@ namespace arc {
ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service)
: ArcService(bridge_service), binding_(this), weak_factory_(this) { : ArcService(bridge_service), binding_(this), weak_factory_(this) {
if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
VLOG(1) << "registering bluetooth adapter"; VLOG(1) << "Registering bluetooth adapter.";
BluetoothAdapterFactory::GetAdapter(base::Bind( BluetoothAdapterFactory::GetAdapter(base::Bind(
&ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr()));
} else { } else {
VLOG(1) << "no bluetooth adapter available"; VLOG(1) << "No bluetooth adapter available.";
} }
arc_bridge_service()->bluetooth()->AddObserver(this); arc_bridge_service()->bluetooth()->AddObserver(this);
} }
...@@ -207,7 +206,14 @@ void ArcBluetoothBridge::OnAdapterInitialized( ...@@ -207,7 +206,14 @@ void ArcBluetoothBridge::OnAdapterInitialized(
// so our adapter uses BlueZ. // so our adapter uses BlueZ.
bluetooth_adapter_ = bluetooth_adapter_ =
static_cast<bluez::BluetoothAdapterBlueZ*>(adapter.get()); static_cast<bluez::BluetoothAdapterBlueZ*>(adapter.get());
bluetooth_adapter_->AddObserver(this);
// The ARC instance was ready before the Bluetooth adapter, hence we didn't
// register ourselves as an observer with it then. Since our adapter is
// ready, we should register it now.
if (!bluetooth_adapter_->HasObserver(this) &&
arc_bridge_service()->bluetooth()->instance()) {
bluetooth_adapter_->AddObserver(this);
}
} }
void ArcBluetoothBridge::OnInstanceReady() { void ArcBluetoothBridge::OnInstanceReady() {
...@@ -218,7 +224,19 @@ void ArcBluetoothBridge::OnInstanceReady() { ...@@ -218,7 +224,19 @@ void ArcBluetoothBridge::OnInstanceReady() {
<< "but no bluetooth instance found"; << "but no bluetooth instance found";
return; return;
} }
bluetooth_instance->Init(binding_.CreateInterfacePtrAndBind()); bluetooth_instance->Init(binding_.CreateInterfacePtrAndBind());
// The Bluetooth adapter was ready before the ARC instance, hence we didn't
// register ourselves as an observer with it then. Since our instance is
// ready, we should register it now.
if (bluetooth_adapter_ && !bluetooth_adapter_->HasObserver(this))
bluetooth_adapter_->AddObserver(this);
}
void ArcBluetoothBridge::OnInstanceClosed() {
if (bluetooth_adapter_)
bluetooth_adapter_->RemoveObserver(this);
} }
void ArcBluetoothBridge::AdapterPoweredChanged(BluetoothAdapter* adapter, void ArcBluetoothBridge::AdapterPoweredChanged(BluetoothAdapter* adapter,
......
...@@ -45,6 +45,7 @@ class ArcBluetoothBridge ...@@ -45,6 +45,7 @@ class ArcBluetoothBridge
// Overridden from InstanceHolder<mojom::BluetoothInstance>::Observer: // Overridden from InstanceHolder<mojom::BluetoothInstance>::Observer:
void OnInstanceReady() override; void OnInstanceReady() override;
void OnInstanceClosed() override;
void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter);
......
...@@ -55,6 +55,11 @@ void BluetoothAdapter::RemoveObserver(BluetoothAdapter::Observer* observer) { ...@@ -55,6 +55,11 @@ void BluetoothAdapter::RemoveObserver(BluetoothAdapter::Observer* observer) {
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
} }
bool BluetoothAdapter::HasObserver(BluetoothAdapter::Observer* observer) {
DCHECK(observer);
return observers_.HasObserver(observer);
}
void BluetoothAdapter::StartDiscoverySession( void BluetoothAdapter::StartDiscoverySession(
const DiscoverySessionCallback& callback, const DiscoverySessionCallback& callback,
const ErrorCallback& error_callback) { const ErrorCallback& error_callback) {
......
...@@ -272,6 +272,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter ...@@ -272,6 +272,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter
// methods to determine which adapter is issuing the event. // methods to determine which adapter is issuing the event.
virtual void AddObserver(BluetoothAdapter::Observer* observer); virtual void AddObserver(BluetoothAdapter::Observer* observer);
virtual void RemoveObserver(BluetoothAdapter::Observer* observer); virtual void RemoveObserver(BluetoothAdapter::Observer* observer);
virtual bool HasObserver(BluetoothAdapter::Observer* observer);
// The address of this adapter. The address format is "XX:XX:XX:XX:XX:XX", // The address of this adapter. The address format is "XX:XX:XX:XX:XX:XX",
// where each XX is a hexadecimal number. // where each XX is a hexadecimal number.
......
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