1 // Copyright 2015 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_adapter_profile_chromeos.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "dbus/object_path.h"
16 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
17 #include "device/bluetooth/bluetooth_uuid.h"
22 void BluetoothAdapterProfileChromeOS::Register(
23 const device::BluetoothUUID
& uuid
,
24 const BluetoothProfileManagerClient::Options
& options
,
25 const ProfileRegisteredCallback
& success_callback
,
26 const BluetoothProfileManagerClient::ErrorCallback
& error_callback
) {
27 scoped_ptr
<BluetoothAdapterProfileChromeOS
> profile(
28 new BluetoothAdapterProfileChromeOS(uuid
));
30 VLOG(1) << "Registering profile: " << profile
->object_path().value();
31 const dbus::ObjectPath
& object_path
= profile
->object_path();
32 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()->RegisterProfile(
34 uuid
.canonical_value(),
36 base::Bind(success_callback
, base::Passed(&profile
)),
40 BluetoothAdapterProfileChromeOS::BluetoothAdapterProfileChromeOS(
41 const device::BluetoothUUID
& uuid
)
42 : uuid_(uuid
), weak_ptr_factory_(this) {
43 std::string uuid_path
;
44 base::ReplaceChars(uuid
.canonical_value(), ":-", "_", &uuid_path
);
46 dbus::ObjectPath("/org/chromium/bluetooth_profile/" + uuid_path
);
48 dbus::Bus
* system_bus
= DBusThreadManager::Get()->GetSystemBus();
50 BluetoothProfileServiceProvider::Create(system_bus
, object_path_
, this));
51 DCHECK(profile_
.get());
54 BluetoothAdapterProfileChromeOS::~BluetoothAdapterProfileChromeOS() {
57 bool BluetoothAdapterProfileChromeOS::SetDelegate(
58 const dbus::ObjectPath
& device_path
,
59 BluetoothProfileServiceProvider::Delegate
* delegate
) {
61 VLOG(1) << "SetDelegate: " << object_path_
.value() << " dev "
62 << device_path
.value();
64 if (delegates_
.find(device_path
.value()) != delegates_
.end()) {
68 delegates_
[device_path
.value()] = delegate
;
72 void BluetoothAdapterProfileChromeOS::RemoveDelegate(
73 const dbus::ObjectPath
& device_path
,
74 const base::Closure
& unregistered_callback
) {
75 VLOG(1) << object_path_
.value() << " dev " << device_path
.value()
76 << ": RemoveDelegate";
78 if (delegates_
.find(device_path
.value()) == delegates_
.end())
81 delegates_
.erase(device_path
.value());
83 if (delegates_
.size() != 0)
86 VLOG(1) << device_path
.value() << " No delegates left, unregistering.";
88 // No users left, release the profile.
89 DBusThreadManager::Get()
90 ->GetBluetoothProfileManagerClient()
92 object_path_
, unregistered_callback
,
93 base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfileError
,
94 weak_ptr_factory_
.GetWeakPtr(), unregistered_callback
));
97 void BluetoothAdapterProfileChromeOS::OnUnregisterProfileError(
98 const base::Closure
& unregistered_callback
,
99 const std::string
& error_name
,
100 const std::string
& error_message
) {
101 LOG(WARNING
) << this->object_path().value()
102 << ": Failed to unregister profile: " << error_name
<< ": "
105 unregistered_callback
.Run();
108 // BluetoothProfileServiceProvider::Delegate:
109 void BluetoothAdapterProfileChromeOS::Released() {
110 VLOG(1) << object_path_
.value() << ": Release";
113 void BluetoothAdapterProfileChromeOS::NewConnection(
114 const dbus::ObjectPath
& device_path
,
115 scoped_ptr
<dbus::FileDescriptor
> fd
,
116 const BluetoothProfileServiceProvider::Delegate::Options
& options
,
117 const ConfirmationCallback
& callback
) {
118 dbus::ObjectPath delegate_path
= device_path
;
120 if (delegates_
.find(device_path
.value()) == delegates_
.end())
121 delegate_path
= dbus::ObjectPath("");
123 if (delegates_
.find(delegate_path
.value()) == delegates_
.end()) {
124 VLOG(1) << object_path_
.value() << ": New connection for device "
125 << device_path
.value() << " which has no delegates!";
126 callback
.Run(REJECTED
);
130 delegates_
[delegate_path
.value()]->NewConnection(device_path
, fd
.Pass(),
134 void BluetoothAdapterProfileChromeOS::RequestDisconnection(
135 const dbus::ObjectPath
& device_path
,
136 const ConfirmationCallback
& callback
) {
137 dbus::ObjectPath delegate_path
= device_path
;
139 if (delegates_
.find(device_path
.value()) == delegates_
.end())
140 delegate_path
= dbus::ObjectPath("");
142 if (delegates_
.find(delegate_path
.value()) == delegates_
.end()) {
143 VLOG(1) << object_path_
.value() << ": RequestDisconnection for device "
144 << device_path
.value() << " which has no delegates!";
148 delegates_
[delegate_path
.value()]->RequestDisconnection(device_path
,
152 void BluetoothAdapterProfileChromeOS::Cancel() {
153 // Cancel() should only go to a delegate accepting connections.
154 if (delegates_
.find("") == delegates_
.end()) {
155 VLOG(1) << object_path_
.value() << ": Cancel with no delegate!";
159 delegates_
[""]->Cancel();
162 } // namespace chromeos