1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_gatt_connection_chromeos.h"
8 #include "base/logging.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "device/bluetooth/bluetooth_adapter.h"
11 #include "device/bluetooth/bluetooth_device.h"
15 BluetoothGattConnectionChromeOS::BluetoothGattConnectionChromeOS(
16 scoped_refptr
<device::BluetoothAdapter
> adapter
,
17 const std::string
& device_address
,
18 const dbus::ObjectPath
& object_path
)
19 : BluetoothGattConnection(adapter
.get(), device_address
),
21 object_path_(object_path
) {
22 DCHECK(adapter_
.get());
23 DCHECK(!device_address_
.empty());
24 DCHECK(object_path_
.IsValid());
26 DBusThreadManager::Get()->GetBluetoothDeviceClient()->AddObserver(this);
29 BluetoothGattConnectionChromeOS::~BluetoothGattConnectionChromeOS() {
30 DBusThreadManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(this);
34 bool BluetoothGattConnectionChromeOS::IsConnected() {
35 // Lazily determine the activity state of the connection. If already
36 // marked as inactive, then return false. Otherwise, explicitly mark
37 // |connected_| as false if the device is removed or disconnected. We do this,
38 // so that if this method is called during a call to DeviceRemoved or
39 // DeviceChanged somewhere else, it returns the correct status.
43 BluetoothDeviceClient::Properties
* properties
=
44 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
45 GetProperties(object_path_
);
46 if (!properties
|| !properties
->connected
.value())
52 void BluetoothGattConnectionChromeOS::Disconnect() {
54 VLOG(1) << "Connection already inactive.";
58 // TODO(armansito): There isn't currently a good way to manage the ownership
59 // of a connection between Chrome and bluetoothd plugins/profiles. Until
60 // a proper reference count is kept by bluetoothd, we might unwittingly kill
61 // a connection that is managed by the daemon (e.g. HoG). For now, just return
62 // success to indicate that this BluetoothGattConnection is no longer active,
63 // even though the underlying connection won't actually be disconnected. This
64 // technically doesn't violate the contract put forth by this API.
68 void BluetoothGattConnectionChromeOS::DeviceRemoved(
69 const dbus::ObjectPath
& object_path
) {
70 if (object_path
!= object_path_
)
76 void BluetoothGattConnectionChromeOS::DevicePropertyChanged(
77 const dbus::ObjectPath
& object_path
,
78 const std::string
& property_name
) {
79 if (object_path
!= object_path_
)
85 BluetoothDeviceClient::Properties
* properties
=
86 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
87 GetProperties(object_path_
);
94 if (property_name
== properties
->connected
.name() &&
95 !properties
->connected
.value())
99 } // namespace chromeos