OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/logging.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "dbus/bus.h" | |
13 #include "dbus/object_path.h" | |
14 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | |
15 #include "device/bluetooth/bluetooth_uuid.h" | |
16 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h" | |
17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
18 | |
19 namespace chromeos { | |
20 | |
21 // static | |
22 void BluetoothAdapterProfileChromeOS::Register( | |
23 const device::BluetoothUUID& uuid, | |
24 const bluez::BluetoothProfileManagerClient::Options& options, | |
25 const ProfileRegisteredCallback& success_callback, | |
26 const bluez::BluetoothProfileManagerClient::ErrorCallback& error_callback) { | |
27 scoped_ptr<BluetoothAdapterProfileChromeOS> profile( | |
28 new BluetoothAdapterProfileChromeOS(uuid)); | |
29 | |
30 VLOG(1) << "Registering profile: " << profile->object_path().value(); | |
31 const dbus::ObjectPath& object_path = profile->object_path(); | |
32 bluez::BluezDBusManager::Get() | |
33 ->GetBluetoothProfileManagerClient() | |
34 ->RegisterProfile(object_path, uuid.canonical_value(), options, | |
35 base::Bind(success_callback, base::Passed(&profile)), | |
36 error_callback); | |
37 } | |
38 | |
39 BluetoothAdapterProfileChromeOS::BluetoothAdapterProfileChromeOS( | |
40 const device::BluetoothUUID& uuid) | |
41 : uuid_(uuid), weak_ptr_factory_(this) { | |
42 std::string uuid_path; | |
43 base::ReplaceChars(uuid.canonical_value(), ":-", "_", &uuid_path); | |
44 object_path_ = | |
45 dbus::ObjectPath("/org/chromium/bluetooth_profile/" + uuid_path); | |
46 | |
47 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus(); | |
48 profile_.reset(bluez::BluetoothProfileServiceProvider::Create( | |
49 system_bus, object_path_, this)); | |
50 DCHECK(profile_.get()); | |
51 } | |
52 | |
53 BluetoothAdapterProfileChromeOS::~BluetoothAdapterProfileChromeOS() { | |
54 } | |
55 | |
56 bool BluetoothAdapterProfileChromeOS::SetDelegate( | |
57 const dbus::ObjectPath& device_path, | |
58 bluez::BluetoothProfileServiceProvider::Delegate* delegate) { | |
59 DCHECK(delegate); | |
60 VLOG(1) << "SetDelegate: " << object_path_.value() << " dev " | |
61 << device_path.value(); | |
62 | |
63 if (delegates_.find(device_path.value()) != delegates_.end()) { | |
64 return false; | |
65 } | |
66 | |
67 delegates_[device_path.value()] = delegate; | |
68 return true; | |
69 } | |
70 | |
71 void BluetoothAdapterProfileChromeOS::RemoveDelegate( | |
72 const dbus::ObjectPath& device_path, | |
73 const base::Closure& unregistered_callback) { | |
74 VLOG(1) << object_path_.value() << " dev " << device_path.value() | |
75 << ": RemoveDelegate"; | |
76 | |
77 if (delegates_.find(device_path.value()) == delegates_.end()) | |
78 return; | |
79 | |
80 delegates_.erase(device_path.value()); | |
81 | |
82 if (delegates_.size() != 0) | |
83 return; | |
84 | |
85 VLOG(1) << device_path.value() << " No delegates left, unregistering."; | |
86 | |
87 // No users left, release the profile. | |
88 bluez::BluezDBusManager::Get() | |
89 ->GetBluetoothProfileManagerClient() | |
90 ->UnregisterProfile( | |
91 object_path_, unregistered_callback, | |
92 base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfileError, | |
93 weak_ptr_factory_.GetWeakPtr(), unregistered_callback)); | |
94 } | |
95 | |
96 void BluetoothAdapterProfileChromeOS::OnUnregisterProfileError( | |
97 const base::Closure& unregistered_callback, | |
98 const std::string& error_name, | |
99 const std::string& error_message) { | |
100 LOG(WARNING) << this->object_path().value() | |
101 << ": Failed to unregister profile: " << error_name << ": " | |
102 << error_message; | |
103 | |
104 unregistered_callback.Run(); | |
105 } | |
106 | |
107 // bluez::BluetoothProfileServiceProvider::Delegate: | |
108 void BluetoothAdapterProfileChromeOS::Released() { | |
109 VLOG(1) << object_path_.value() << ": Release"; | |
110 } | |
111 | |
112 void BluetoothAdapterProfileChromeOS::NewConnection( | |
113 const dbus::ObjectPath& device_path, | |
114 scoped_ptr<dbus::FileDescriptor> fd, | |
115 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, | |
116 const ConfirmationCallback& callback) { | |
117 dbus::ObjectPath delegate_path = device_path; | |
118 | |
119 if (delegates_.find(device_path.value()) == delegates_.end()) | |
120 delegate_path = dbus::ObjectPath(""); | |
121 | |
122 if (delegates_.find(delegate_path.value()) == delegates_.end()) { | |
123 VLOG(1) << object_path_.value() << ": New connection for device " | |
124 << device_path.value() << " which has no delegates!"; | |
125 callback.Run(REJECTED); | |
126 return; | |
127 } | |
128 | |
129 delegates_[delegate_path.value()]->NewConnection(device_path, fd.Pass(), | |
130 options, callback); | |
131 } | |
132 | |
133 void BluetoothAdapterProfileChromeOS::RequestDisconnection( | |
134 const dbus::ObjectPath& device_path, | |
135 const ConfirmationCallback& callback) { | |
136 dbus::ObjectPath delegate_path = device_path; | |
137 | |
138 if (delegates_.find(device_path.value()) == delegates_.end()) | |
139 delegate_path = dbus::ObjectPath(""); | |
140 | |
141 if (delegates_.find(delegate_path.value()) == delegates_.end()) { | |
142 VLOG(1) << object_path_.value() << ": RequestDisconnection for device " | |
143 << device_path.value() << " which has no delegates!"; | |
144 return; | |
145 } | |
146 | |
147 delegates_[delegate_path.value()]->RequestDisconnection(device_path, | |
148 callback); | |
149 } | |
150 | |
151 void BluetoothAdapterProfileChromeOS::Cancel() { | |
152 // Cancel() should only go to a delegate accepting connections. | |
153 if (delegates_.find("") == delegates_.end()) { | |
154 VLOG(1) << object_path_.value() << ": Cancel with no delegate!"; | |
155 return; | |
156 } | |
157 | |
158 delegates_[""]->Cancel(); | |
159 } | |
160 | |
161 } // namespace chromeos | |
OLD | NEW |