Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_gatt_connection_chromeos.cc
blob93b9747adcebc1cc8328e05be635f1f647de5158
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"
7 #include "base/bind.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"
13 namespace chromeos {
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),
20 connected_(true),
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);
31 Disconnect();
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.
40 if (!connected_)
41 return false;
43 BluetoothDeviceClient::Properties* properties =
44 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
45 GetProperties(object_path_);
46 if (!properties || !properties->connected.value())
47 connected_ = false;
49 return connected_;
52 void BluetoothGattConnectionChromeOS::Disconnect() {
53 if (!connected_) {
54 VLOG(1) << "Connection already inactive.";
55 return;
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.
65 connected_ = false;
68 void BluetoothGattConnectionChromeOS::DeviceRemoved(
69 const dbus::ObjectPath& object_path) {
70 if (object_path != object_path_)
71 return;
73 connected_ = false;
76 void BluetoothGattConnectionChromeOS::DevicePropertyChanged(
77 const dbus::ObjectPath& object_path,
78 const std::string& property_name) {
79 if (object_path != object_path_)
80 return;
82 if (!connected_)
83 return;
85 BluetoothDeviceClient::Properties* properties =
86 DBusThreadManager::Get()->GetBluetoothDeviceClient()->
87 GetProperties(object_path_);
89 if (!properties) {
90 connected_ = false;
91 return;
94 if (property_name == properties->connected.name() &&
95 !properties->connected.value())
96 connected_ = false;
99 } // namespace chromeos