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 BluetoothAdapterProfileChromeOS
* BluetoothAdapterProfileChromeOS::Register(
23 BluetoothAdapterChromeOS
* adapter
,
24 const device::BluetoothUUID
& uuid
,
25 const BluetoothProfileManagerClient::Options
& options
,
26 const base::Closure
& success_callback
,
27 const BluetoothProfileManagerClient::ErrorCallback
& error_callback
) {
30 BluetoothAdapterProfileChromeOS
* profile
=
31 new BluetoothAdapterProfileChromeOS(adapter
, uuid
);
33 VLOG(1) << "Registering profile: " << profile
->object_path().value();
34 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()->RegisterProfile(
35 profile
->object_path(), uuid
.canonical_value(), options
, success_callback
,
41 BluetoothAdapterProfileChromeOS::BluetoothAdapterProfileChromeOS(
42 BluetoothAdapterChromeOS
* adapter
,
43 const device::BluetoothUUID
& uuid
)
44 : uuid_(uuid
), adapter_(adapter
), weak_ptr_factory_(this) {
45 std::string uuid_path
;
46 base::ReplaceChars(uuid
.canonical_value(), ":-", "_", &uuid_path
);
48 dbus::ObjectPath("/org/chromium/bluetooth_profile/" + uuid_path
);
50 dbus::Bus
* system_bus
= DBusThreadManager::Get()->GetSystemBus();
52 BluetoothProfileServiceProvider::Create(system_bus
, object_path_
, this));
53 DCHECK(profile_
.get());
56 BluetoothAdapterProfileChromeOS::~BluetoothAdapterProfileChromeOS() {
60 bool BluetoothAdapterProfileChromeOS::SetDelegate(
61 const dbus::ObjectPath
& device_path
,
62 BluetoothProfileServiceProvider::Delegate
* delegate
) {
64 VLOG(1) << "SetDelegate: " << object_path_
.value() << " dev "
65 << device_path
.value();
67 if (delegates_
.find(device_path
.value()) != delegates_
.end()) {
71 delegates_
[device_path
.value()] = delegate
;
75 void BluetoothAdapterProfileChromeOS::RemoveDelegate(
76 const dbus::ObjectPath
& device_path
,
77 const base::Closure
& unregistered_callback
) {
78 VLOG(1) << object_path_
.value() << " dev " << device_path
.value()
79 << ": RemoveDelegate";
81 if (delegates_
.find(device_path
.value()) == delegates_
.end())
84 delegates_
.erase(device_path
.value());
86 if (delegates_
.size() != 0)
89 VLOG(1) << device_path
.value() << " No delegates left, unregistering.";
91 // No users left, release the profile.
92 DBusThreadManager::Get()
93 ->GetBluetoothProfileManagerClient()
95 object_path_
, unregistered_callback
,
96 base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfileError
,
97 weak_ptr_factory_
.GetWeakPtr(), unregistered_callback
));
100 void BluetoothAdapterProfileChromeOS::OnUnregisterProfileError(
101 const base::Closure
& unregistered_callback
,
102 const std::string
& error_name
,
103 const std::string
& error_message
) {
104 LOG(WARNING
) << this->object_path().value()
105 << ": Failed to unregister profile: " << error_name
<< ": "
108 unregistered_callback
.Run();
111 // BluetoothProfileServiceProvider::Delegate:
112 void BluetoothAdapterProfileChromeOS::Released() {
113 VLOG(1) << object_path_
.value() << ": Release";
116 void BluetoothAdapterProfileChromeOS::NewConnection(
117 const dbus::ObjectPath
& device_path
,
118 scoped_ptr
<dbus::FileDescriptor
> fd
,
119 const BluetoothProfileServiceProvider::Delegate::Options
& options
,
120 const ConfirmationCallback
& callback
) {
121 dbus::ObjectPath delegate_path
= device_path
;
123 if (delegates_
.find(device_path
.value()) == delegates_
.end())
124 delegate_path
= dbus::ObjectPath("");
126 if (delegates_
.find(delegate_path
.value()) == delegates_
.end()) {
127 VLOG(1) << object_path_
.value() << ": New connection for device "
128 << device_path
.value() << " which has no delegates!";
129 callback
.Run(REJECTED
);
133 delegates_
[delegate_path
.value()]->NewConnection(device_path
, fd
.Pass(),
137 void BluetoothAdapterProfileChromeOS::RequestDisconnection(
138 const dbus::ObjectPath
& device_path
,
139 const ConfirmationCallback
& callback
) {
140 dbus::ObjectPath delegate_path
= device_path
;
142 if (delegates_
.find(device_path
.value()) == delegates_
.end())
143 delegate_path
= dbus::ObjectPath("");
145 if (delegates_
.find(delegate_path
.value()) == delegates_
.end()) {
146 VLOG(1) << object_path_
.value() << ": RequestDisconnection for device "
147 << device_path
.value() << " which has no delegates!";
151 delegates_
[delegate_path
.value()]->RequestDisconnection(device_path
,
155 void BluetoothAdapterProfileChromeOS::Cancel() {
156 // Cancel() should only go to a delegate accepting connections.
157 if (delegates_
.find("") == delegates_
.end()) {
158 VLOG(1) << object_path_
.value() << ": Cancel with no delegate!";
162 delegates_
[""]->Cancel();
165 } // namespace chromeos