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 "chromeos/dbus/bluetooth_profile_service_provider.h" | |
13 #include "chromeos/dbus/dbus_thread_manager.h" | |
14 #include "dbus/bus.h" | |
15 #include "dbus/object_path.h" | |
16 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | |
17 #include "device/bluetooth/bluetooth_uuid.h" | |
18 | |
19 namespace chromeos { | |
20 | |
21 // static | |
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) { | |
28 DCHECK(adapter); | |
29 | |
30 BluetoothAdapterProfileChromeOS* profile = | |
31 new BluetoothAdapterProfileChromeOS(adapter, uuid); | |
32 | |
33 VLOG(1) << "Registering profile: " << profile->object_path().value(); | |
34 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()->RegisterProfile( | |
35 profile->object_path(), uuid.canonical_value(), options, success_callback, | |
36 error_callback); | |
37 | |
38 return profile; | |
39 } | |
40 | |
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); | |
47 object_path_ = | |
48 dbus::ObjectPath("/org/chromium/bluetooth_profile/" + uuid_path); | |
49 | |
50 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | |
51 profile_.reset( | |
52 BluetoothProfileServiceProvider::Create(system_bus, object_path_, this)); | |
53 DCHECK(profile_.get()); | |
54 } | |
55 | |
56 BluetoothAdapterProfileChromeOS::~BluetoothAdapterProfileChromeOS() { | |
57 profile_.reset(); | |
58 } | |
59 | |
60 bool BluetoothAdapterProfileChromeOS::SetDelegate( | |
61 const dbus::ObjectPath& device_path, | |
62 BluetoothProfileServiceProvider::Delegate* delegate) { | |
63 DCHECK(delegate); | |
64 VLOG(1) << "SetDelegate: " << object_path_.value() << " dev " | |
65 << device_path.value(); | |
66 | |
67 if (delegates_.find(device_path.value()) != delegates_.end()) { | |
68 return false; | |
69 } | |
70 | |
71 delegates_[device_path.value()] = delegate; | |
72 return true; | |
73 } | |
74 | |
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"; | |
80 | |
81 if (delegates_.find(device_path.value()) == delegates_.end()) | |
82 return; | |
83 | |
84 VLOG(1) << device_path.value() << " Delegate found"; | |
85 | |
86 delegates_.erase(device_path.value()); | |
87 | |
88 if (delegates_.size() != 0) | |
89 return; | |
90 | |
91 // No users left, release the profile. | |
92 DBusThreadManager::Get() | |
93 ->GetBluetoothProfileManagerClient() | |
94 ->UnregisterProfile( | |
95 object_path_, unregistered_callback, | |
96 base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfileError, | |
97 weak_ptr_factory_.GetWeakPtr(), unregistered_callback)); | |
98 } | |
99 | |
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 << ": " | |
106 << error_message; | |
107 | |
108 unregistered_callback.Run(); | |
109 } | |
110 | |
111 // BluetoothProfileServiceProvider::Delegate: | |
112 void BluetoothAdapterProfileChromeOS::Released() { | |
113 VLOG(1) << object_path_.value() << ": Release"; | |
114 } | |
115 | |
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; | |
122 | |
123 if (delegates_.find(device_path.value()) == delegates_.end()) | |
124 delegate_path = dbus::ObjectPath(""); | |
125 | |
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); | |
130 return; | |
131 } | |
132 | |
133 delegates_[delegate_path.value()]->NewConnection(device_path, fd.Pass(), | |
134 options, callback); | |
135 } | |
136 | |
137 void BluetoothAdapterProfileChromeOS::RequestDisconnection( | |
138 const dbus::ObjectPath& device_path, | |
139 const ConfirmationCallback& callback) { | |
140 dbus::ObjectPath delegate_path = device_path; | |
141 | |
142 if (delegates_.find(device_path.value()) == delegates_.end()) | |
143 delegate_path = dbus::ObjectPath(""); | |
144 | |
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!"; | |
148 return; | |
149 } | |
150 | |
151 delegates_[delegate_path.value()]->RequestDisconnection(device_path, | |
152 callback); | |
153 } | |
154 | |
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!"; | |
159 return; | |
160 } | |
161 | |
162 delegates_[""]->Cancel(); | |
163 } | |
164 | |
165 } // namespace chromeos | |
OLD | NEW |