Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(487)

Side by Side Diff: device/bluetooth/bluetooth_adapter_bluez.cc

Issue 1367663002: Add Linux support for the Bluetooth API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor_dbus
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/bluetooth/bluetooth_adapter_chromeos.h" 5 #include "device/bluetooth/bluetooth_adapter_bluez.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/sequenced_task_runner.h" 13 #include "base/sequenced_task_runner.h"
14 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/thread_task_runner_handle.h" 15 #include "base/thread_task_runner_handle.h"
16 #include "chromeos/system/devicetype.h" 16 #include "device/bluetooth/bluetooth_adapter_profile_bluez.h"
17 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" 17 #include "device/bluetooth/bluetooth_advertisement_bluez.h"
18 #include "device/bluetooth/bluetooth_advertisement_chromeos.h" 18 #include "device/bluetooth/bluetooth_audio_sink_bluez.h"
19 #include "device/bluetooth/bluetooth_audio_sink_chromeos.h"
20 #include "device/bluetooth/bluetooth_device.h" 19 #include "device/bluetooth/bluetooth_device.h"
21 #include "device/bluetooth/bluetooth_device_chromeos.h" 20 #include "device/bluetooth/bluetooth_device_bluez.h"
22 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" 21 #include "device/bluetooth/bluetooth_discovery_session_outcome.h"
23 #include "device/bluetooth/bluetooth_pairing_chromeos.h" 22 #include "device/bluetooth/bluetooth_pairing_bluez.h"
24 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" 23 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_bluez.h"
25 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_chromeos.h" 24 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_bluez.h"
26 #include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" 25 #include "device/bluetooth/bluetooth_remote_gatt_service_bluez.h"
27 #include "device/bluetooth/bluetooth_socket_chromeos.h" 26 #include "device/bluetooth/bluetooth_socket_bluez.h"
28 #include "device/bluetooth/bluetooth_socket_thread.h" 27 #include "device/bluetooth/bluetooth_socket_thread.h"
29 #include "device/bluetooth/bluetooth_uuid.h" 28 #include "device/bluetooth/bluetooth_uuid.h"
30 #include "device/bluetooth/dbus/bluetooth_adapter_client.h" 29 #include "device/bluetooth/dbus/bluetooth_adapter_client.h"
31 #include "device/bluetooth/dbus/bluetooth_agent_manager_client.h" 30 #include "device/bluetooth/dbus/bluetooth_agent_manager_client.h"
32 #include "device/bluetooth/dbus/bluetooth_agent_service_provider.h" 31 #include "device/bluetooth/dbus/bluetooth_agent_service_provider.h"
33 #include "device/bluetooth/dbus/bluetooth_device_client.h" 32 #include "device/bluetooth/dbus/bluetooth_device_client.h"
34 #include "device/bluetooth/dbus/bluetooth_input_client.h" 33 #include "device/bluetooth/dbus/bluetooth_input_client.h"
35 #include "device/bluetooth/dbus/bluez_dbus_manager.h" 34 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
36 #include "third_party/cros_system_api/dbus/service_constants.h" 35 #include "third_party/cros_system_api/dbus/service_constants.h"
37 36
37 #if defined(OS_CHROMEOS)
38 #include "chromeos/system/devicetype.h"
39 #endif
40
38 using device::BluetoothAdapter; 41 using device::BluetoothAdapter;
39 using device::BluetoothAudioSink; 42 using device::BluetoothAudioSink;
40 using device::BluetoothDevice; 43 using device::BluetoothDevice;
41 using device::BluetoothDiscoveryFilter; 44 using device::BluetoothDiscoveryFilter;
42 using device::BluetoothSocket; 45 using device::BluetoothSocket;
43 using device::BluetoothUUID; 46 using device::BluetoothUUID;
44 using device::UMABluetoothDiscoverySessionOutcome; 47 using device::UMABluetoothDiscoverySessionOutcome;
45 48
46 namespace { 49 namespace {
47 50
48 // The agent path is relatively meaningless since BlueZ only permits one to 51 // The agent path is relatively meaningless since BlueZ only permits one to
49 // exist per D-Bus connection, it just has to be unique within Chromium. 52 // exist per D-Bus connection, it just has to be unique within Chromium.
50 const char kAgentPath[] = "/org/chromium/bluetooth_agent"; 53 const char kAgentPath[] = "/org/chromium/bluetooth_agent";
51 54
52 void OnUnregisterAgentError(const std::string& error_name, 55 void OnUnregisterAgentError(const std::string& error_name,
53 const std::string& error_message) { 56 const std::string& error_message) {
54 // It's okay if the agent didn't exist, it means we never saw an adapter. 57 // It's okay if the agent didn't exist, it means we never saw an adapter.
55 if (error_name == bluetooth_agent_manager::kErrorDoesNotExist) 58 if (error_name == bluetooth_agent_manager::kErrorDoesNotExist)
56 return; 59 return;
57 60
58 LOG(WARNING) << "Failed to unregister pairing agent: " 61 LOG(WARNING) << "Failed to unregister pairing agent: " << error_name << ": "
59 << error_name << ": " << error_message; 62 << error_message;
60 } 63 }
61 64
62 UMABluetoothDiscoverySessionOutcome TranslateDiscoveryErrorToUMA( 65 UMABluetoothDiscoverySessionOutcome TranslateDiscoveryErrorToUMA(
63 const std::string& error_name) { 66 const std::string& error_name) {
64 if (error_name == bluez::BluetoothAdapterClient::kUnknownAdapterError) { 67 if (error_name == bluez::BluetoothAdapterClient::kUnknownAdapterError) {
65 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_UNKNOWN_ADAPTER; 68 return UMABluetoothDiscoverySessionOutcome::BLUEZ_DBUS_UNKNOWN_ADAPTER;
66 } else if (error_name == bluez::BluetoothAdapterClient::kNoResponseError) { 69 } else if (error_name == bluez::BluetoothAdapterClient::kNoResponseError) {
67 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_NO_RESPONSE; 70 return UMABluetoothDiscoverySessionOutcome::BLUEZ_DBUS_NO_RESPONSE;
68 } else if (error_name == bluetooth_device::kErrorInProgress) { 71 } else if (error_name == bluetooth_device::kErrorInProgress) {
69 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_IN_PROGRESS; 72 return UMABluetoothDiscoverySessionOutcome::BLUEZ_DBUS_IN_PROGRESS;
70 } else if (error_name == bluetooth_device::kErrorNotReady) { 73 } else if (error_name == bluetooth_device::kErrorNotReady) {
71 return UMABluetoothDiscoverySessionOutcome::CHROMEOS_DBUS_NOT_READY; 74 return UMABluetoothDiscoverySessionOutcome::BLUEZ_DBUS_NOT_READY;
72 } else if (error_name == bluetooth_device::kErrorFailed) { 75 } else if (error_name == bluetooth_device::kErrorFailed) {
73 return UMABluetoothDiscoverySessionOutcome::FAILED; 76 return UMABluetoothDiscoverySessionOutcome::FAILED;
74 } else { 77 } else {
75 LOG(WARNING) << "Can't histogram DBus error " << error_name; 78 LOG(WARNING) << "Can't histogram DBus error " << error_name;
76 return UMABluetoothDiscoverySessionOutcome::UNKNOWN; 79 return UMABluetoothDiscoverySessionOutcome::UNKNOWN;
77 } 80 }
78 } 81 }
79 82
80 } // namespace 83 } // namespace
81 84
82 namespace device { 85 namespace device {
83 86
84 // static 87 // static
85 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter( 88 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
86 const InitCallback& init_callback) { 89 const InitCallback& init_callback) {
87 return chromeos::BluetoothAdapterChromeOS::CreateAdapter(); 90 return bluez::BluetoothAdapterBlueZ::CreateAdapter();
88 } 91 }
89 92
90 } // namespace device 93 } // namespace device
91 94
92 namespace chromeos { 95 namespace bluez {
93 96
94 // static 97 // static
95 base::WeakPtr<BluetoothAdapter> BluetoothAdapterChromeOS::CreateAdapter() { 98 base::WeakPtr<BluetoothAdapter> BluetoothAdapterBlueZ::CreateAdapter() {
96 BluetoothAdapterChromeOS* adapter = new BluetoothAdapterChromeOS(); 99 BluetoothAdapterBlueZ* adapter = new BluetoothAdapterBlueZ();
97 return adapter->weak_ptr_factory_.GetWeakPtr(); 100 return adapter->weak_ptr_factory_.GetWeakPtr();
98 } 101 }
99 102
100 void BluetoothAdapterChromeOS::Shutdown() { 103 void BluetoothAdapterBlueZ::Shutdown() {
101 if (dbus_is_shutdown_) 104 if (dbus_is_shutdown_)
102 return; 105 return;
103 DCHECK(bluez::BluezDBusManager::IsInitialized()) 106 DCHECK(bluez::BluezDBusManager::IsInitialized())
104 << "Call BluetoothAdapterFactory::Shutdown() before " 107 << "Call BluetoothAdapterFactory::Shutdown() before "
105 "BluezDBusManager::Shutdown()."; 108 "BluezDBusManager::Shutdown().";
106 109
107 if (IsPresent()) 110 if (IsPresent())
108 RemoveAdapter(); // Also deletes devices_. 111 RemoveAdapter(); // Also deletes devices_.
109 DCHECK(devices_.empty()); 112 DCHECK(devices_.empty());
110 // profiles_ is empty because all BluetoothSockets have been notified 113 // profiles_ is empty because all BluetoothSockets have been notified
(...skipping 15 matching lines...) Expand all
126 bluez::BluezDBusManager::Get() 129 bluez::BluezDBusManager::Get()
127 ->GetBluetoothAgentManagerClient() 130 ->GetBluetoothAgentManagerClient()
128 ->UnregisterAgent(dbus::ObjectPath(kAgentPath), 131 ->UnregisterAgent(dbus::ObjectPath(kAgentPath),
129 base::Bind(&base::DoNothing), 132 base::Bind(&base::DoNothing),
130 base::Bind(&OnUnregisterAgentError)); 133 base::Bind(&OnUnregisterAgentError));
131 134
132 agent_.reset(); 135 agent_.reset();
133 dbus_is_shutdown_ = true; 136 dbus_is_shutdown_ = true;
134 } 137 }
135 138
136 BluetoothAdapterChromeOS::BluetoothAdapterChromeOS() 139 BluetoothAdapterBlueZ::BluetoothAdapterBlueZ()
137 : dbus_is_shutdown_(false), 140 : dbus_is_shutdown_(false),
138 num_discovery_sessions_(0), 141 num_discovery_sessions_(0),
139 discovery_request_pending_(false), 142 discovery_request_pending_(false),
140 weak_ptr_factory_(this) { 143 weak_ptr_factory_(this) {
141 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 144 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
142 socket_thread_ = device::BluetoothSocketThread::Get(); 145 socket_thread_ = device::BluetoothSocketThread::Get();
143 146
144 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->AddObserver( 147 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->AddObserver(
145 this); 148 this);
146 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this); 149 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this);
147 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->AddObserver(this); 150 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->AddObserver(this);
148 151
149 // Register the pairing agent. 152 // Register the pairing agent.
150 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus(); 153 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus();
151 agent_.reset(bluez::BluetoothAgentServiceProvider::Create( 154 agent_.reset(bluez::BluetoothAgentServiceProvider::Create(
152 system_bus, dbus::ObjectPath(kAgentPath), this)); 155 system_bus, dbus::ObjectPath(kAgentPath), this));
153 DCHECK(agent_.get()); 156 DCHECK(agent_.get());
154 157
155 std::vector<dbus::ObjectPath> object_paths = bluez::BluezDBusManager::Get() 158 std::vector<dbus::ObjectPath> object_paths = bluez::BluezDBusManager::Get()
156 ->GetBluetoothAdapterClient() 159 ->GetBluetoothAdapterClient()
157 ->GetAdapters(); 160 ->GetAdapters();
158 161
159 if (!object_paths.empty()) { 162 if (!object_paths.empty()) {
160 VLOG(1) << object_paths.size() << " Bluetooth adapter(s) available."; 163 VLOG(1) << object_paths.size() << " Bluetooth adapter(s) available.";
161 SetAdapter(object_paths[0]); 164 SetAdapter(object_paths[0]);
162 } 165 }
163 } 166 }
164 167
165 BluetoothAdapterChromeOS::~BluetoothAdapterChromeOS() { 168 BluetoothAdapterBlueZ::~BluetoothAdapterBlueZ() {
166 Shutdown(); 169 Shutdown();
167 } 170 }
168 171
169 std::string BluetoothAdapterChromeOS::GetAddress() const { 172 std::string BluetoothAdapterBlueZ::GetAddress() const {
170 if (!IsPresent()) 173 if (!IsPresent())
171 return std::string(); 174 return std::string();
172 175
173 bluez::BluetoothAdapterClient::Properties* properties = 176 bluez::BluetoothAdapterClient::Properties* properties =
174 bluez::BluezDBusManager::Get() 177 bluez::BluezDBusManager::Get()
175 ->GetBluetoothAdapterClient() 178 ->GetBluetoothAdapterClient()
176 ->GetProperties(object_path_); 179 ->GetProperties(object_path_);
177 DCHECK(properties); 180 DCHECK(properties);
178 181
179 return BluetoothDevice::CanonicalizeAddress(properties->address.value()); 182 return BluetoothDevice::CanonicalizeAddress(properties->address.value());
180 } 183 }
181 184
182 std::string BluetoothAdapterChromeOS::GetName() const { 185 std::string BluetoothAdapterBlueZ::GetName() const {
183 if (!IsPresent()) 186 if (!IsPresent())
184 return std::string(); 187 return std::string();
185 188
186 bluez::BluetoothAdapterClient::Properties* properties = 189 bluez::BluetoothAdapterClient::Properties* properties =
187 bluez::BluezDBusManager::Get() 190 bluez::BluezDBusManager::Get()
188 ->GetBluetoothAdapterClient() 191 ->GetBluetoothAdapterClient()
189 ->GetProperties(object_path_); 192 ->GetProperties(object_path_);
190 DCHECK(properties); 193 DCHECK(properties);
191 194
192 return properties->alias.value(); 195 return properties->alias.value();
193 } 196 }
194 197
195 void BluetoothAdapterChromeOS::SetName(const std::string& name, 198 void BluetoothAdapterBlueZ::SetName(const std::string& name,
196 const base::Closure& callback, 199 const base::Closure& callback,
197 const ErrorCallback& error_callback) { 200 const ErrorCallback& error_callback) {
198 if (!IsPresent()) { 201 if (!IsPresent()) {
199 error_callback.Run(); 202 error_callback.Run();
200 return; 203 return;
201 } 204 }
202 205
203 bluez::BluezDBusManager::Get() 206 bluez::BluezDBusManager::Get()
204 ->GetBluetoothAdapterClient() 207 ->GetBluetoothAdapterClient()
205 ->GetProperties(object_path_) 208 ->GetProperties(object_path_)
206 ->alias.Set( 209 ->alias.Set(
207 name, 210 name,
208 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, 211 base::Bind(&BluetoothAdapterBlueZ::OnPropertyChangeCompleted,
209 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 212 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
210 } 213 }
211 214
212 bool BluetoothAdapterChromeOS::IsInitialized() const { 215 bool BluetoothAdapterBlueZ::IsInitialized() const {
213 return true; 216 return true;
214 } 217 }
215 218
216 bool BluetoothAdapterChromeOS::IsPresent() const { 219 bool BluetoothAdapterBlueZ::IsPresent() const {
217 return !dbus_is_shutdown_ && !object_path_.value().empty(); 220 return !dbus_is_shutdown_ && !object_path_.value().empty();
218 } 221 }
219 222
220 bool BluetoothAdapterChromeOS::IsPowered() const { 223 bool BluetoothAdapterBlueZ::IsPowered() const {
221 if (!IsPresent()) 224 if (!IsPresent())
222 return false; 225 return false;
223 226
224 bluez::BluetoothAdapterClient::Properties* properties = 227 bluez::BluetoothAdapterClient::Properties* properties =
225 bluez::BluezDBusManager::Get() 228 bluez::BluezDBusManager::Get()
226 ->GetBluetoothAdapterClient() 229 ->GetBluetoothAdapterClient()
227 ->GetProperties(object_path_); 230 ->GetProperties(object_path_);
228 231
229 return properties->powered.value(); 232 return properties->powered.value();
230 } 233 }
231 234
232 void BluetoothAdapterChromeOS::SetPowered( 235 void BluetoothAdapterBlueZ::SetPowered(bool powered,
233 bool powered, 236 const base::Closure& callback,
234 const base::Closure& callback, 237 const ErrorCallback& error_callback) {
235 const ErrorCallback& error_callback) {
236 if (!IsPresent()) { 238 if (!IsPresent()) {
237 error_callback.Run(); 239 error_callback.Run();
238 return; 240 return;
239 } 241 }
240 242
241 bluez::BluezDBusManager::Get() 243 bluez::BluezDBusManager::Get()
242 ->GetBluetoothAdapterClient() 244 ->GetBluetoothAdapterClient()
243 ->GetProperties(object_path_) 245 ->GetProperties(object_path_)
244 ->powered.Set( 246 ->powered.Set(
245 powered, 247 powered,
246 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, 248 base::Bind(&BluetoothAdapterBlueZ::OnPropertyChangeCompleted,
247 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 249 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
248 } 250 }
249 251
250 bool BluetoothAdapterChromeOS::IsDiscoverable() const { 252 bool BluetoothAdapterBlueZ::IsDiscoverable() const {
251 if (!IsPresent()) 253 if (!IsPresent())
252 return false; 254 return false;
253 255
254 bluez::BluetoothAdapterClient::Properties* properties = 256 bluez::BluetoothAdapterClient::Properties* properties =
255 bluez::BluezDBusManager::Get() 257 bluez::BluezDBusManager::Get()
256 ->GetBluetoothAdapterClient() 258 ->GetBluetoothAdapterClient()
257 ->GetProperties(object_path_); 259 ->GetProperties(object_path_);
258 260
259 return properties->discoverable.value(); 261 return properties->discoverable.value();
260 } 262 }
261 263
262 void BluetoothAdapterChromeOS::SetDiscoverable( 264 void BluetoothAdapterBlueZ::SetDiscoverable(
263 bool discoverable, 265 bool discoverable,
264 const base::Closure& callback, 266 const base::Closure& callback,
265 const ErrorCallback& error_callback) { 267 const ErrorCallback& error_callback) {
266 if (!IsPresent()) { 268 if (!IsPresent()) {
267 error_callback.Run(); 269 error_callback.Run();
268 return; 270 return;
269 } 271 }
270 272
271 bluez::BluezDBusManager::Get() 273 bluez::BluezDBusManager::Get()
272 ->GetBluetoothAdapterClient() 274 ->GetBluetoothAdapterClient()
273 ->GetProperties(object_path_) 275 ->GetProperties(object_path_)
274 ->discoverable.Set( 276 ->discoverable.Set(
275 discoverable, 277 discoverable,
276 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoverable, 278 base::Bind(&BluetoothAdapterBlueZ::OnSetDiscoverable,
277 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 279 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
278 } 280 }
279 281
280 bool BluetoothAdapterChromeOS::IsDiscovering() const { 282 bool BluetoothAdapterBlueZ::IsDiscovering() const {
281 if (!IsPresent()) 283 if (!IsPresent())
282 return false; 284 return false;
283 285
284 bluez::BluetoothAdapterClient::Properties* properties = 286 bluez::BluetoothAdapterClient::Properties* properties =
285 bluez::BluezDBusManager::Get() 287 bluez::BluezDBusManager::Get()
286 ->GetBluetoothAdapterClient() 288 ->GetBluetoothAdapterClient()
287 ->GetProperties(object_path_); 289 ->GetProperties(object_path_);
288 290
289 return properties->discovering.value(); 291 return properties->discovering.value();
290 } 292 }
291 293
292 void BluetoothAdapterChromeOS::CreateRfcommService( 294 void BluetoothAdapterBlueZ::CreateRfcommService(
293 const BluetoothUUID& uuid, 295 const BluetoothUUID& uuid,
294 const ServiceOptions& options, 296 const ServiceOptions& options,
295 const CreateServiceCallback& callback, 297 const CreateServiceCallback& callback,
296 const CreateServiceErrorCallback& error_callback) { 298 const CreateServiceErrorCallback& error_callback) {
297 DCHECK(!dbus_is_shutdown_); 299 DCHECK(!dbus_is_shutdown_);
298 VLOG(1) << object_path_.value() << ": Creating RFCOMM service: " 300 VLOG(1) << object_path_.value()
299 << uuid.canonical_value(); 301 << ": Creating RFCOMM service: " << uuid.canonical_value();
300 scoped_refptr<BluetoothSocketChromeOS> socket = 302 scoped_refptr<BluetoothSocketBlueZ> socket =
301 BluetoothSocketChromeOS::CreateBluetoothSocket( 303 BluetoothSocketBlueZ::CreateBluetoothSocket(ui_task_runner_,
302 ui_task_runner_, socket_thread_); 304 socket_thread_);
303 socket->Listen(this, 305 socket->Listen(this, BluetoothSocketBlueZ::kRfcomm, uuid, options,
304 BluetoothSocketChromeOS::kRfcomm, 306 base::Bind(callback, socket), error_callback);
305 uuid,
306 options,
307 base::Bind(callback, socket),
308 error_callback);
309 } 307 }
310 308
311 void BluetoothAdapterChromeOS::CreateL2capService( 309 void BluetoothAdapterBlueZ::CreateL2capService(
312 const BluetoothUUID& uuid, 310 const BluetoothUUID& uuid,
313 const ServiceOptions& options, 311 const ServiceOptions& options,
314 const CreateServiceCallback& callback, 312 const CreateServiceCallback& callback,
315 const CreateServiceErrorCallback& error_callback) { 313 const CreateServiceErrorCallback& error_callback) {
316 DCHECK(!dbus_is_shutdown_); 314 DCHECK(!dbus_is_shutdown_);
317 VLOG(1) << object_path_.value() << ": Creating L2CAP service: " 315 VLOG(1) << object_path_.value()
318 << uuid.canonical_value(); 316 << ": Creating L2CAP service: " << uuid.canonical_value();
319 scoped_refptr<BluetoothSocketChromeOS> socket = 317 scoped_refptr<BluetoothSocketBlueZ> socket =
320 BluetoothSocketChromeOS::CreateBluetoothSocket( 318 BluetoothSocketBlueZ::CreateBluetoothSocket(ui_task_runner_,
321 ui_task_runner_, socket_thread_); 319 socket_thread_);
322 socket->Listen(this, 320 socket->Listen(this, BluetoothSocketBlueZ::kL2cap, uuid, options,
323 BluetoothSocketChromeOS::kL2cap, 321 base::Bind(callback, socket), error_callback);
324 uuid,
325 options,
326 base::Bind(callback, socket),
327 error_callback);
328 } 322 }
329 323
330 void BluetoothAdapterChromeOS::RegisterAudioSink( 324 void BluetoothAdapterBlueZ::RegisterAudioSink(
331 const BluetoothAudioSink::Options& options, 325 const BluetoothAudioSink::Options& options,
332 const device::BluetoothAdapter::AcquiredCallback& callback, 326 const device::BluetoothAdapter::AcquiredCallback& callback,
333 const BluetoothAudioSink::ErrorCallback& error_callback) { 327 const BluetoothAudioSink::ErrorCallback& error_callback) {
334 VLOG(1) << "Registering audio sink"; 328 VLOG(1) << "Registering audio sink";
335 if (!this->IsPresent()) { 329 if (!this->IsPresent()) {
336 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER); 330 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER);
337 return; 331 return;
338 } 332 }
339 scoped_refptr<BluetoothAudioSinkChromeOS> audio_sink( 333 scoped_refptr<BluetoothAudioSinkBlueZ> audio_sink(
340 new BluetoothAudioSinkChromeOS(this)); 334 new BluetoothAudioSinkBlueZ(this));
341 audio_sink->Register( 335 audio_sink->Register(options,
342 options, base::Bind(&BluetoothAdapterChromeOS::OnRegisterAudioSink, 336 base::Bind(&BluetoothAdapterBlueZ::OnRegisterAudioSink,
343 weak_ptr_factory_.GetWeakPtr(), callback, 337 weak_ptr_factory_.GetWeakPtr(), callback,
344 error_callback, audio_sink), 338 error_callback, audio_sink),
345 error_callback); 339 error_callback);
346 } 340 }
347 341
348 void BluetoothAdapterChromeOS::RegisterAdvertisement( 342 void BluetoothAdapterBlueZ::RegisterAdvertisement(
349 scoped_ptr<device::BluetoothAdvertisement::Data> advertisement_data, 343 scoped_ptr<device::BluetoothAdvertisement::Data> advertisement_data,
350 const CreateAdvertisementCallback& callback, 344 const CreateAdvertisementCallback& callback,
351 const CreateAdvertisementErrorCallback& error_callback) { 345 const CreateAdvertisementErrorCallback& error_callback) {
352 scoped_refptr<BluetoothAdvertisementChromeOS> advertisement( 346 scoped_refptr<BluetoothAdvertisementBlueZ> advertisement(
353 new BluetoothAdvertisementChromeOS(advertisement_data.Pass(), this)); 347 new BluetoothAdvertisementBlueZ(advertisement_data.Pass(), this));
354 advertisement->Register(base::Bind(callback, advertisement), error_callback); 348 advertisement->Register(base::Bind(callback, advertisement), error_callback);
355 } 349 }
356 350
357 void BluetoothAdapterChromeOS::RemovePairingDelegateInternal( 351 void BluetoothAdapterBlueZ::RemovePairingDelegateInternal(
358 BluetoothDevice::PairingDelegate* pairing_delegate) { 352 BluetoothDevice::PairingDelegate* pairing_delegate) {
359 // Check if any device is using the pairing delegate. 353 // Check if any device is using the pairing delegate.
360 // If so, clear the pairing context which will make any responses no-ops. 354 // If so, clear the pairing context which will make any responses no-ops.
361 for (DevicesMap::iterator iter = devices_.begin(); 355 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end();
362 iter != devices_.end(); ++iter) { 356 ++iter) {
363 BluetoothDeviceChromeOS* device_chromeos = 357 BluetoothDeviceBlueZ* device_bluez =
364 static_cast<BluetoothDeviceChromeOS*>(iter->second); 358 static_cast<BluetoothDeviceBlueZ*>(iter->second);
365 359
366 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); 360 BluetoothPairingBlueZ* pairing = device_bluez->GetPairing();
367 if (pairing && pairing->GetPairingDelegate() == pairing_delegate) 361 if (pairing && pairing->GetPairingDelegate() == pairing_delegate)
368 device_chromeos->EndPairing(); 362 device_bluez->EndPairing();
369 } 363 }
370 } 364 }
371 365
372 void BluetoothAdapterChromeOS::AdapterAdded( 366 void BluetoothAdapterBlueZ::AdapterAdded(const dbus::ObjectPath& object_path) {
373 const dbus::ObjectPath& object_path) {
374 // Set the adapter to the newly added adapter only if no adapter is present. 367 // Set the adapter to the newly added adapter only if no adapter is present.
375 if (!IsPresent()) 368 if (!IsPresent())
376 SetAdapter(object_path); 369 SetAdapter(object_path);
377 } 370 }
378 371
379 void BluetoothAdapterChromeOS::AdapterRemoved( 372 void BluetoothAdapterBlueZ::AdapterRemoved(
380 const dbus::ObjectPath& object_path) { 373 const dbus::ObjectPath& object_path) {
381 if (object_path == object_path_) 374 if (object_path == object_path_)
382 RemoveAdapter(); 375 RemoveAdapter();
383 } 376 }
384 377
385 void BluetoothAdapterChromeOS::AdapterPropertyChanged( 378 void BluetoothAdapterBlueZ::AdapterPropertyChanged(
386 const dbus::ObjectPath& object_path, 379 const dbus::ObjectPath& object_path,
387 const std::string& property_name) { 380 const std::string& property_name) {
388 if (object_path != object_path_) 381 if (object_path != object_path_)
389 return; 382 return;
390 DCHECK(IsPresent()); 383 DCHECK(IsPresent());
391 384
392 bluez::BluetoothAdapterClient::Properties* properties = 385 bluez::BluetoothAdapterClient::Properties* properties =
393 bluez::BluezDBusManager::Get() 386 bluez::BluezDBusManager::Get()
394 ->GetBluetoothAdapterClient() 387 ->GetBluetoothAdapterClient()
395 ->GetProperties(object_path_); 388 ->GetProperties(object_path_);
396 389
397 if (property_name == properties->powered.name()) { 390 if (property_name == properties->powered.name()) {
398 PoweredChanged(properties->powered.value()); 391 PoweredChanged(properties->powered.value());
399 } else if (property_name == properties->discoverable.name()) { 392 } else if (property_name == properties->discoverable.name()) {
400 DiscoverableChanged(properties->discoverable.value()); 393 DiscoverableChanged(properties->discoverable.value());
401 } else if (property_name == properties->discovering.name()) { 394 } else if (property_name == properties->discovering.name()) {
402 DiscoveringChanged(properties->discovering.value()); 395 DiscoveringChanged(properties->discovering.value());
403 } 396 }
404 } 397 }
405 398
406 void BluetoothAdapterChromeOS::DeviceAdded( 399 void BluetoothAdapterBlueZ::DeviceAdded(const dbus::ObjectPath& object_path) {
407 const dbus::ObjectPath& object_path) {
408 DCHECK(bluez::BluezDBusManager::Get()); 400 DCHECK(bluez::BluezDBusManager::Get());
409 bluez::BluetoothDeviceClient::Properties* properties = 401 bluez::BluetoothDeviceClient::Properties* properties =
410 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 402 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
411 object_path); 403 object_path);
412 if (!properties || properties->adapter.value() != object_path_) 404 if (!properties || properties->adapter.value() != object_path_)
413 return; 405 return;
414 DCHECK(IsPresent()); 406 DCHECK(IsPresent());
415 407
416 BluetoothDeviceChromeOS* device_chromeos = 408 BluetoothDeviceBlueZ* device_bluez = new BluetoothDeviceBlueZ(
417 new BluetoothDeviceChromeOS(this, 409 this, object_path, ui_task_runner_, socket_thread_);
418 object_path, 410 DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end());
419 ui_task_runner_,
420 socket_thread_);
421 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end());
422 411
423 devices_[device_chromeos->GetAddress()] = device_chromeos; 412 devices_[device_bluez->GetAddress()] = device_bluez;
424 413
425 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 414 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
426 DeviceAdded(this, device_chromeos)); 415 DeviceAdded(this, device_bluez));
427 } 416 }
428 417
429 void BluetoothAdapterChromeOS::DeviceRemoved( 418 void BluetoothAdapterBlueZ::DeviceRemoved(const dbus::ObjectPath& object_path) {
430 const dbus::ObjectPath& object_path) { 419 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end();
431 for (DevicesMap::iterator iter = devices_.begin(); 420 ++iter) {
432 iter != devices_.end(); ++iter) { 421 BluetoothDeviceBlueZ* device_bluez =
433 BluetoothDeviceChromeOS* device_chromeos = 422 static_cast<BluetoothDeviceBlueZ*>(iter->second);
434 static_cast<BluetoothDeviceChromeOS*>(iter->second); 423 if (device_bluez->object_path() == object_path) {
435 if (device_chromeos->object_path() == object_path) {
436 devices_.erase(iter); 424 devices_.erase(iter);
437 425
438 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 426 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
439 DeviceRemoved(this, device_chromeos)); 427 DeviceRemoved(this, device_bluez));
440 delete device_chromeos; 428 delete device_bluez;
441 return; 429 return;
442 } 430 }
443 } 431 }
444 } 432 }
445 433
446 void BluetoothAdapterChromeOS::DevicePropertyChanged( 434 void BluetoothAdapterBlueZ::DevicePropertyChanged(
447 const dbus::ObjectPath& object_path, 435 const dbus::ObjectPath& object_path,
448 const std::string& property_name) { 436 const std::string& property_name) {
449 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); 437 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(object_path);
450 if (!device_chromeos) 438 if (!device_bluez)
451 return; 439 return;
452 440
453 bluez::BluetoothDeviceClient::Properties* properties = 441 bluez::BluetoothDeviceClient::Properties* properties =
454 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties( 442 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
455 object_path); 443 object_path);
456 444
457 if (property_name == properties->bluetooth_class.name() || 445 if (property_name == properties->bluetooth_class.name() ||
458 property_name == properties->address.name() || 446 property_name == properties->address.name() ||
459 property_name == properties->alias.name() || 447 property_name == properties->alias.name() ||
460 property_name == properties->paired.name() || 448 property_name == properties->paired.name() ||
461 property_name == properties->trusted.name() || 449 property_name == properties->trusted.name() ||
462 property_name == properties->connected.name() || 450 property_name == properties->connected.name() ||
463 property_name == properties->uuids.name() || 451 property_name == properties->uuids.name() ||
464 property_name == properties->rssi.name() || 452 property_name == properties->rssi.name() ||
465 property_name == properties->tx_power.name()) { 453 property_name == properties->tx_power.name()) {
466 NotifyDeviceChanged(device_chromeos); 454 NotifyDeviceChanged(device_bluez);
467 } 455 }
468 456
469 // When a device becomes paired, mark it as trusted so that the user does 457 // When a device becomes paired, mark it as trusted so that the user does
470 // not need to approve every incoming connection 458 // not need to approve every incoming connection
471 if (property_name == properties->paired.name() && 459 if (property_name == properties->paired.name() &&
472 properties->paired.value() && !properties->trusted.value()) { 460 properties->paired.value() && !properties->trusted.value()) {
473 device_chromeos->SetTrusted(); 461 device_bluez->SetTrusted();
474 } 462 }
475 463
476 // UMA connection counting 464 // UMA connection counting
477 if (property_name == properties->connected.name()) { 465 if (property_name == properties->connected.name()) {
478 // PlayStation joystick tries to reconnect after disconnection from USB. 466 // PlayStation joystick tries to reconnect after disconnection from USB.
479 // If it is still not trusted, set it, so it becomes available on the 467 // If it is still not trusted, set it, so it becomes available on the
480 // list of known devices. 468 // list of known devices.
481 if (properties->connected.value() && device_chromeos->IsTrustable() && 469 if (properties->connected.value() && device_bluez->IsTrustable() &&
482 !properties->trusted.value()) 470 !properties->trusted.value())
483 device_chromeos->SetTrusted(); 471 device_bluez->SetTrusted();
484 472
485 int count = 0; 473 int count = 0;
486 474
487 for (DevicesMap::iterator iter = devices_.begin(); 475 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end();
488 iter != devices_.end(); ++iter) { 476 ++iter) {
489 if (iter->second->IsPaired() && iter->second->IsConnected()) 477 if (iter->second->IsPaired() && iter->second->IsConnected())
490 ++count; 478 ++count;
491 } 479 }
492 480
493 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); 481 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count);
494 } 482 }
495 } 483 }
496 484
497 void BluetoothAdapterChromeOS::InputPropertyChanged( 485 void BluetoothAdapterBlueZ::InputPropertyChanged(
498 const dbus::ObjectPath& object_path, 486 const dbus::ObjectPath& object_path,
499 const std::string& property_name) { 487 const std::string& property_name) {
500 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); 488 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(object_path);
501 if (!device_chromeos) 489 if (!device_bluez)
502 return; 490 return;
503 491
504 bluez::BluetoothInputClient::Properties* properties = 492 bluez::BluetoothInputClient::Properties* properties =
505 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->GetProperties( 493 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->GetProperties(
506 object_path); 494 object_path);
507 495
508 // Properties structure can be removed, which triggers a change in the 496 // Properties structure can be removed, which triggers a change in the
509 // BluetoothDevice::IsConnectable() property, as does a change in the 497 // BluetoothDevice::IsConnectable() property, as does a change in the
510 // actual reconnect_mode property. 498 // actual reconnect_mode property.
511 if (!properties || property_name == properties->reconnect_mode.name()) { 499 if (!properties || property_name == properties->reconnect_mode.name()) {
512 NotifyDeviceChanged(device_chromeos); 500 NotifyDeviceChanged(device_bluez);
513 } 501 }
514 } 502 }
515 503
516 void BluetoothAdapterChromeOS::Released() { 504 void BluetoothAdapterBlueZ::Released() {
517 VLOG(1) << "Release"; 505 VLOG(1) << "Release";
518 if (!IsPresent()) 506 if (!IsPresent())
519 return; 507 return;
520 DCHECK(agent_.get()); 508 DCHECK(agent_.get());
521 509
522 // Called after we unregister the pairing agent, e.g. when changing I/O 510 // Called after we unregister the pairing agent, e.g. when changing I/O
523 // capabilities. Nothing much to be done right now. 511 // capabilities. Nothing much to be done right now.
524 } 512 }
525 513
526 void BluetoothAdapterChromeOS::RequestPinCode( 514 void BluetoothAdapterBlueZ::RequestPinCode(const dbus::ObjectPath& device_path,
527 const dbus::ObjectPath& device_path, 515 const PinCodeCallback& callback) {
528 const PinCodeCallback& callback) {
529 DCHECK(IsPresent()); 516 DCHECK(IsPresent());
530 DCHECK(agent_.get()); 517 DCHECK(agent_.get());
531 VLOG(1) << device_path.value() << ": RequestPinCode"; 518 VLOG(1) << device_path.value() << ": RequestPinCode";
532 519
533 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 520 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
534 if (!pairing) { 521 if (!pairing) {
535 callback.Run(REJECTED, ""); 522 callback.Run(REJECTED, "");
536 return; 523 return;
537 } 524 }
538 525
539 pairing->RequestPinCode(callback); 526 pairing->RequestPinCode(callback);
540 } 527 }
541 528
542 void BluetoothAdapterChromeOS::DisplayPinCode( 529 void BluetoothAdapterBlueZ::DisplayPinCode(const dbus::ObjectPath& device_path,
543 const dbus::ObjectPath& device_path, 530 const std::string& pincode) {
544 const std::string& pincode) {
545 DCHECK(IsPresent()); 531 DCHECK(IsPresent());
546 DCHECK(agent_.get()); 532 DCHECK(agent_.get());
547 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode; 533 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode;
548 534
549 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 535 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
550 if (!pairing) 536 if (!pairing)
551 return; 537 return;
552 538
553 pairing->DisplayPinCode(pincode); 539 pairing->DisplayPinCode(pincode);
554 } 540 }
555 541
556 void BluetoothAdapterChromeOS::RequestPasskey( 542 void BluetoothAdapterBlueZ::RequestPasskey(const dbus::ObjectPath& device_path,
557 const dbus::ObjectPath& device_path, 543 const PasskeyCallback& callback) {
558 const PasskeyCallback& callback) {
559 DCHECK(IsPresent()); 544 DCHECK(IsPresent());
560 DCHECK(agent_.get()); 545 DCHECK(agent_.get());
561 VLOG(1) << device_path.value() << ": RequestPasskey"; 546 VLOG(1) << device_path.value() << ": RequestPasskey";
562 547
563 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 548 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
564 if (!pairing) { 549 if (!pairing) {
565 callback.Run(REJECTED, 0); 550 callback.Run(REJECTED, 0);
566 return; 551 return;
567 } 552 }
568 553
569 pairing->RequestPasskey(callback); 554 pairing->RequestPasskey(callback);
570 } 555 }
571 556
572 void BluetoothAdapterChromeOS::DisplayPasskey( 557 void BluetoothAdapterBlueZ::DisplayPasskey(const dbus::ObjectPath& device_path,
573 const dbus::ObjectPath& device_path, 558 uint32 passkey,
574 uint32 passkey, 559 uint16 entered) {
575 uint16 entered) {
576 DCHECK(IsPresent()); 560 DCHECK(IsPresent());
577 DCHECK(agent_.get()); 561 DCHECK(agent_.get());
578 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey 562 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey << " ("
579 << " (" << entered << " entered)"; 563 << entered << " entered)";
580 564
581 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 565 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
582 if (!pairing) 566 if (!pairing)
583 return; 567 return;
584 568
585 if (entered == 0) 569 if (entered == 0)
586 pairing->DisplayPasskey(passkey); 570 pairing->DisplayPasskey(passkey);
587 571
588 pairing->KeysEntered(entered); 572 pairing->KeysEntered(entered);
589 } 573 }
590 574
591 void BluetoothAdapterChromeOS::RequestConfirmation( 575 void BluetoothAdapterBlueZ::RequestConfirmation(
592 const dbus::ObjectPath& device_path, 576 const dbus::ObjectPath& device_path,
593 uint32 passkey, 577 uint32 passkey,
594 const ConfirmationCallback& callback) { 578 const ConfirmationCallback& callback) {
595 DCHECK(IsPresent()); 579 DCHECK(IsPresent());
596 DCHECK(agent_.get()); 580 DCHECK(agent_.get());
597 VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey; 581 VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey;
598 582
599 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 583 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
600 if (!pairing) { 584 if (!pairing) {
601 callback.Run(REJECTED); 585 callback.Run(REJECTED);
602 return; 586 return;
603 } 587 }
604 588
605 pairing->RequestConfirmation(passkey, callback); 589 pairing->RequestConfirmation(passkey, callback);
606 } 590 }
607 591
608 void BluetoothAdapterChromeOS::RequestAuthorization( 592 void BluetoothAdapterBlueZ::RequestAuthorization(
609 const dbus::ObjectPath& device_path, 593 const dbus::ObjectPath& device_path,
610 const ConfirmationCallback& callback) { 594 const ConfirmationCallback& callback) {
611 DCHECK(IsPresent()); 595 DCHECK(IsPresent());
612 DCHECK(agent_.get()); 596 DCHECK(agent_.get());
613 VLOG(1) << device_path.value() << ": RequestAuthorization"; 597 VLOG(1) << device_path.value() << ": RequestAuthorization";
614 598
615 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 599 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
616 if (!pairing) { 600 if (!pairing) {
617 callback.Run(REJECTED); 601 callback.Run(REJECTED);
618 return; 602 return;
619 } 603 }
620 604
621 pairing->RequestAuthorization(callback); 605 pairing->RequestAuthorization(callback);
622 } 606 }
623 607
624 void BluetoothAdapterChromeOS::AuthorizeService( 608 void BluetoothAdapterBlueZ::AuthorizeService(
625 const dbus::ObjectPath& device_path, 609 const dbus::ObjectPath& device_path,
626 const std::string& uuid, 610 const std::string& uuid,
627 const ConfirmationCallback& callback) { 611 const ConfirmationCallback& callback) {
628 DCHECK(IsPresent()); 612 DCHECK(IsPresent());
629 DCHECK(agent_.get()); 613 DCHECK(agent_.get());
630 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid; 614 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid;
631 615
632 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(device_path); 616 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(device_path);
633 if (!device_chromeos) { 617 if (!device_bluez) {
634 callback.Run(CANCELLED); 618 callback.Run(CANCELLED);
635 return; 619 return;
636 } 620 }
637 621
638 // We always set paired devices to Trusted, so the only reason that this 622 // We always set paired devices to Trusted, so the only reason that this
639 // method call would ever be called is in the case of a race condition where 623 // method call would ever be called is in the case of a race condition where
640 // our "Set('Trusted', true)" method call is still pending in the Bluetooth 624 // our "Set('Trusted', true)" method call is still pending in the Bluetooth
641 // daemon because it's busy handling the incoming connection. 625 // daemon because it's busy handling the incoming connection.
642 if (device_chromeos->IsPaired()) { 626 if (device_bluez->IsPaired()) {
643 callback.Run(SUCCESS); 627 callback.Run(SUCCESS);
644 return; 628 return;
645 } 629 }
646 630
647 // TODO(keybuk): reject service authorizations when not paired, determine 631 // TODO(keybuk): reject service authorizations when not paired, determine
648 // whether this is acceptable long-term. 632 // whether this is acceptable long-term.
649 LOG(WARNING) << "Rejecting service connection from unpaired device " 633 LOG(WARNING) << "Rejecting service connection from unpaired device "
650 << device_chromeos->GetAddress() << " for UUID " << uuid; 634 << device_bluez->GetAddress() << " for UUID " << uuid;
651 callback.Run(REJECTED); 635 callback.Run(REJECTED);
652 } 636 }
653 637
654 void BluetoothAdapterChromeOS::Cancel() { 638 void BluetoothAdapterBlueZ::Cancel() {
655 DCHECK(IsPresent()); 639 DCHECK(IsPresent());
656 DCHECK(agent_.get()); 640 DCHECK(agent_.get());
657 VLOG(1) << "Cancel"; 641 VLOG(1) << "Cancel";
658 } 642 }
659 643
660 void BluetoothAdapterChromeOS::OnRegisterAgent() { 644 void BluetoothAdapterBlueZ::OnRegisterAgent() {
661 VLOG(1) << "Pairing agent registered, requesting to be made default"; 645 VLOG(1) << "Pairing agent registered, requesting to be made default";
662 646
663 bluez::BluezDBusManager::Get() 647 bluez::BluezDBusManager::Get()
664 ->GetBluetoothAgentManagerClient() 648 ->GetBluetoothAgentManagerClient()
665 ->RequestDefaultAgent( 649 ->RequestDefaultAgent(
666 dbus::ObjectPath(kAgentPath), 650 dbus::ObjectPath(kAgentPath),
667 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent, 651 base::Bind(&BluetoothAdapterBlueZ::OnRequestDefaultAgent,
668 weak_ptr_factory_.GetWeakPtr()), 652 weak_ptr_factory_.GetWeakPtr()),
669 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError, 653 base::Bind(&BluetoothAdapterBlueZ::OnRequestDefaultAgentError,
670 weak_ptr_factory_.GetWeakPtr())); 654 weak_ptr_factory_.GetWeakPtr()));
671 } 655 }
672 656
673 void BluetoothAdapterChromeOS::OnRegisterAgentError( 657 void BluetoothAdapterBlueZ::OnRegisterAgentError(
674 const std::string& error_name, 658 const std::string& error_name,
675 const std::string& error_message) { 659 const std::string& error_message) {
676 // Our agent being already registered isn't an error. 660 // Our agent being already registered isn't an error.
677 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) 661 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists)
678 return; 662 return;
679 663
680 LOG(WARNING) << ": Failed to register pairing agent: " 664 LOG(WARNING) << ": Failed to register pairing agent: " << error_name << ": "
681 << error_name << ": " << error_message; 665 << error_message;
682 } 666 }
683 667
684 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { 668 void BluetoothAdapterBlueZ::OnRequestDefaultAgent() {
685 VLOG(1) << "Pairing agent now default"; 669 VLOG(1) << "Pairing agent now default";
686 } 670 }
687 671
688 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( 672 void BluetoothAdapterBlueZ::OnRequestDefaultAgentError(
689 const std::string& error_name, 673 const std::string& error_name,
690 const std::string& error_message) { 674 const std::string& error_message) {
691 LOG(WARNING) << ": Failed to make pairing agent default: " 675 LOG(WARNING) << ": Failed to make pairing agent default: " << error_name
692 << error_name << ": " << error_message; 676 << ": " << error_message;
693 } 677 }
694 678
695 void BluetoothAdapterChromeOS::OnRegisterAudioSink( 679 void BluetoothAdapterBlueZ::OnRegisterAudioSink(
696 const device::BluetoothAdapter::AcquiredCallback& callback, 680 const device::BluetoothAdapter::AcquiredCallback& callback,
697 const device::BluetoothAudioSink::ErrorCallback& error_callback, 681 const device::BluetoothAudioSink::ErrorCallback& error_callback,
698 scoped_refptr<BluetoothAudioSink> audio_sink) { 682 scoped_refptr<BluetoothAudioSink> audio_sink) {
699 if (!IsPresent()) { 683 if (!IsPresent()) {
700 VLOG(1) << "Failed to register audio sink, adapter not present"; 684 VLOG(1) << "Failed to register audio sink, adapter not present";
701 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER); 685 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER);
702 return; 686 return;
703 } 687 }
704 DCHECK(audio_sink.get()); 688 DCHECK(audio_sink.get());
705 callback.Run(audio_sink); 689 callback.Run(audio_sink);
706 } 690 }
707 691
708 BluetoothDeviceChromeOS* 692 BluetoothDeviceBlueZ* BluetoothAdapterBlueZ::GetDeviceWithPath(
709 BluetoothAdapterChromeOS::GetDeviceWithPath(
710 const dbus::ObjectPath& object_path) { 693 const dbus::ObjectPath& object_path) {
711 if (!IsPresent()) 694 if (!IsPresent())
712 return nullptr; 695 return nullptr;
713 696
714 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); 697 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end();
715 ++iter) { 698 ++iter) {
716 BluetoothDeviceChromeOS* device_chromeos = 699 BluetoothDeviceBlueZ* device_bluez =
717 static_cast<BluetoothDeviceChromeOS*>(iter->second); 700 static_cast<BluetoothDeviceBlueZ*>(iter->second);
718 if (device_chromeos->object_path() == object_path) 701 if (device_bluez->object_path() == object_path)
719 return device_chromeos; 702 return device_bluez;
720 } 703 }
721 704
722 return nullptr; 705 return nullptr;
723 } 706 }
724 707
725 BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( 708 BluetoothPairingBlueZ* BluetoothAdapterBlueZ::GetPairing(
726 const dbus::ObjectPath& object_path) { 709 const dbus::ObjectPath& object_path) {
727 DCHECK(IsPresent()); 710 DCHECK(IsPresent());
728 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); 711 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(object_path);
729 if (!device_chromeos) { 712 if (!device_bluez) {
730 LOG(WARNING) << "Pairing Agent request for unknown device: " 713 LOG(WARNING) << "Pairing Agent request for unknown device: "
731 << object_path.value(); 714 << object_path.value();
732 return nullptr; 715 return nullptr;
733 } 716 }
734 717
735 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); 718 BluetoothPairingBlueZ* pairing = device_bluez->GetPairing();
736 if (pairing) 719 if (pairing)
737 return pairing; 720 return pairing;
738 721
739 // The device doesn't have its own pairing context, so this is an incoming 722 // The device doesn't have its own pairing context, so this is an incoming
740 // pairing request that should use our best default delegate (if we have one). 723 // pairing request that should use our best default delegate (if we have one).
741 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate(); 724 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate();
742 if (!pairing_delegate) 725 if (!pairing_delegate)
743 return nullptr; 726 return nullptr;
744 727
745 return device_chromeos->BeginPairing(pairing_delegate); 728 return device_bluez->BeginPairing(pairing_delegate);
746 } 729 }
747 730
748 void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { 731 void BluetoothAdapterBlueZ::SetAdapter(const dbus::ObjectPath& object_path) {
749 DCHECK(!IsPresent()); 732 DCHECK(!IsPresent());
750 DCHECK(!dbus_is_shutdown_); 733 DCHECK(!dbus_is_shutdown_);
751 object_path_ = object_path; 734 object_path_ = object_path;
752 735
753 VLOG(1) << object_path_.value() << ": using adapter."; 736 VLOG(1) << object_path_.value() << ": using adapter.";
754 737
755 VLOG(1) << "Registering pairing agent"; 738 VLOG(1) << "Registering pairing agent";
756 bluez::BluezDBusManager::Get() 739 bluez::BluezDBusManager::Get()
757 ->GetBluetoothAgentManagerClient() 740 ->GetBluetoothAgentManagerClient()
758 ->RegisterAgent( 741 ->RegisterAgent(dbus::ObjectPath(kAgentPath),
759 dbus::ObjectPath(kAgentPath), 742 bluetooth_agent_manager::kKeyboardDisplayCapability,
760 bluetooth_agent_manager::kKeyboardDisplayCapability, 743 base::Bind(&BluetoothAdapterBlueZ::OnRegisterAgent,
761 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgent, 744 weak_ptr_factory_.GetWeakPtr()),
762 weak_ptr_factory_.GetWeakPtr()), 745 base::Bind(&BluetoothAdapterBlueZ::OnRegisterAgentError,
763 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgentError, 746 weak_ptr_factory_.GetWeakPtr()));
764 weak_ptr_factory_.GetWeakPtr()));
765 747
766 SetDefaultAdapterName(); 748 SetDefaultAdapterName();
767 749
768 bluez::BluetoothAdapterClient::Properties* properties = 750 bluez::BluetoothAdapterClient::Properties* properties =
769 bluez::BluezDBusManager::Get() 751 bluez::BluezDBusManager::Get()
770 ->GetBluetoothAdapterClient() 752 ->GetBluetoothAdapterClient()
771 ->GetProperties(object_path_); 753 ->GetProperties(object_path_);
772 754
773 PresentChanged(true); 755 PresentChanged(true);
774 756
775 if (properties->powered.value()) 757 if (properties->powered.value())
776 PoweredChanged(true); 758 PoweredChanged(true);
777 if (properties->discoverable.value()) 759 if (properties->discoverable.value())
778 DiscoverableChanged(true); 760 DiscoverableChanged(true);
779 if (properties->discovering.value()) 761 if (properties->discovering.value())
780 DiscoveringChanged(true); 762 DiscoveringChanged(true);
781 763
782 std::vector<dbus::ObjectPath> device_paths = 764 std::vector<dbus::ObjectPath> device_paths =
783 bluez::BluezDBusManager::Get() 765 bluez::BluezDBusManager::Get()
784 ->GetBluetoothDeviceClient() 766 ->GetBluetoothDeviceClient()
785 ->GetDevicesForAdapter(object_path_); 767 ->GetDevicesForAdapter(object_path_);
786 768
787 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin(); 769 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin();
788 iter != device_paths.end(); ++iter) { 770 iter != device_paths.end(); ++iter) {
789 DeviceAdded(*iter); 771 DeviceAdded(*iter);
790 } 772 }
791 } 773 }
792 774
793 void BluetoothAdapterChromeOS::SetDefaultAdapterName() { 775 void BluetoothAdapterBlueZ::SetDefaultAdapterName() {
794 DCHECK(IsPresent()); 776 DCHECK(IsPresent());
795 777
796 std::string alias; 778 std::string alias;
779 #if defined(OS_CHROMEOS)
797 switch (chromeos::GetDeviceType()) { 780 switch (chromeos::GetDeviceType()) {
798 case DeviceType::kChromebase: 781 case chromeos::DeviceType::kChromebase:
799 alias = "Chromebase"; 782 alias = "Chromebase";
800 break; 783 break;
801 case DeviceType::kChromebit: 784 case chromeos::DeviceType::kChromebit:
802 alias = "Chromebit"; 785 alias = "Chromebit";
803 break; 786 break;
804 case DeviceType::kChromebook: 787 case chromeos::DeviceType::kChromebook:
805 alias = "Chromebook"; 788 alias = "Chromebook";
806 break; 789 break;
807 case DeviceType::kChromebox: 790 case chromeos::DeviceType::kChromebox:
808 alias = "Chromebox"; 791 alias = "Chromebox";
809 break; 792 break;
810 case DeviceType::kUnknown: 793 case chromeos::DeviceType::kUnknown:
811 alias = "Chromebook"; 794 alias = "Chromebook";
812 break; 795 break;
813 } 796 }
797 #elif defined(OS_LINUX)
798 alias = "ChromeLinux";
799 #endif
814 800
815 SetName(alias, base::Bind(&base::DoNothing), base::Bind(&base::DoNothing)); 801 SetName(alias, base::Bind(&base::DoNothing), base::Bind(&base::DoNothing));
816 } 802 }
817 803
818 void BluetoothAdapterChromeOS::RemoveAdapter() { 804 void BluetoothAdapterBlueZ::RemoveAdapter() {
819 DCHECK(IsPresent()); 805 DCHECK(IsPresent());
820 VLOG(1) << object_path_.value() << ": adapter removed."; 806 VLOG(1) << object_path_.value() << ": adapter removed.";
821 807
822 bluez::BluetoothAdapterClient::Properties* properties = 808 bluez::BluetoothAdapterClient::Properties* properties =
823 bluez::BluezDBusManager::Get() 809 bluez::BluezDBusManager::Get()
824 ->GetBluetoothAdapterClient() 810 ->GetBluetoothAdapterClient()
825 ->GetProperties(object_path_); 811 ->GetProperties(object_path_);
826 812
827 object_path_ = dbus::ObjectPath(""); 813 object_path_ = dbus::ObjectPath("");
828 814
829 if (properties->powered.value()) 815 if (properties->powered.value())
830 PoweredChanged(false); 816 PoweredChanged(false);
831 if (properties->discoverable.value()) 817 if (properties->discoverable.value())
832 DiscoverableChanged(false); 818 DiscoverableChanged(false);
833 if (properties->discovering.value()) 819 if (properties->discovering.value())
834 DiscoveringChanged(false); 820 DiscoveringChanged(false);
835 821
836 // Copy the devices list here and clear the original so that when we 822 // Copy the devices list here and clear the original so that when we
837 // send DeviceRemoved(), GetDevices() returns no devices. 823 // send DeviceRemoved(), GetDevices() returns no devices.
838 DevicesMap devices = devices_; 824 DevicesMap devices = devices_;
839 devices_.clear(); 825 devices_.clear();
840 826
841 for (DevicesMap::iterator iter = devices.begin(); 827 for (DevicesMap::iterator iter = devices.begin(); iter != devices.end();
842 iter != devices.end(); ++iter) { 828 ++iter) {
843 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 829 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
844 DeviceRemoved(this, iter->second)); 830 DeviceRemoved(this, iter->second));
845 delete iter->second; 831 delete iter->second;
846 } 832 }
847 833
848 PresentChanged(false); 834 PresentChanged(false);
849 } 835 }
850 836
851 void BluetoothAdapterChromeOS::PoweredChanged(bool powered) { 837 void BluetoothAdapterBlueZ::PoweredChanged(bool powered) {
852 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 838 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
853 AdapterPoweredChanged(this, powered)); 839 AdapterPoweredChanged(this, powered));
854 } 840 }
855 841
856 void BluetoothAdapterChromeOS::DiscoverableChanged(bool discoverable) { 842 void BluetoothAdapterBlueZ::DiscoverableChanged(bool discoverable) {
857 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 843 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
858 AdapterDiscoverableChanged(this, discoverable)); 844 AdapterDiscoverableChanged(this, discoverable));
859 } 845 }
860 846
861 void BluetoothAdapterChromeOS::DiscoveringChanged( 847 void BluetoothAdapterBlueZ::DiscoveringChanged(bool discovering) {
862 bool discovering) {
863 // If the adapter stopped discovery due to a reason other than a request by 848 // If the adapter stopped discovery due to a reason other than a request by
864 // us, reset the count to 0. 849 // us, reset the count to 0.
865 VLOG(1) << "Discovering changed: " << discovering; 850 VLOG(1) << "Discovering changed: " << discovering;
866 if (!discovering && !discovery_request_pending_ && 851 if (!discovering && !discovery_request_pending_ &&
867 num_discovery_sessions_ > 0) { 852 num_discovery_sessions_ > 0) {
868 VLOG(1) << "Marking sessions as inactive."; 853 VLOG(1) << "Marking sessions as inactive.";
869 num_discovery_sessions_ = 0; 854 num_discovery_sessions_ = 0;
870 MarkDiscoverySessionsAsInactive(); 855 MarkDiscoverySessionsAsInactive();
871 } 856 }
872 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 857 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
873 AdapterDiscoveringChanged(this, discovering)); 858 AdapterDiscoveringChanged(this, discovering));
874 } 859 }
875 860
876 void BluetoothAdapterChromeOS::PresentChanged(bool present) { 861 void BluetoothAdapterBlueZ::PresentChanged(bool present) {
877 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 862 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
878 AdapterPresentChanged(this, present)); 863 AdapterPresentChanged(this, present));
879 } 864 }
880 865
881 void BluetoothAdapterChromeOS::NotifyDeviceChanged( 866 void BluetoothAdapterBlueZ::NotifyDeviceChanged(BluetoothDeviceBlueZ* device) {
882 BluetoothDeviceChromeOS* device) {
883 DCHECK(device); 867 DCHECK(device);
884 DCHECK(device->adapter_ == this); 868 DCHECK(device->adapter_ == this);
885 869
886 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 870 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
887 DeviceChanged(this, device)); 871 DeviceChanged(this, device));
888 } 872 }
889 873
890 void BluetoothAdapterChromeOS::NotifyGattServiceAdded( 874 void BluetoothAdapterBlueZ::NotifyGattServiceAdded(
891 BluetoothRemoteGattServiceChromeOS* service) { 875 BluetoothRemoteGattServiceBlueZ* service) {
892 DCHECK_EQ(service->GetAdapter(), this); 876 DCHECK_EQ(service->GetAdapter(), this);
893 DCHECK_EQ( 877 DCHECK_EQ(static_cast<BluetoothDeviceBlueZ*>(service->GetDevice())->adapter_,
894 static_cast<BluetoothDeviceChromeOS*>(service->GetDevice())->adapter_, 878 this);
895 this);
896 879
897 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 880 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
898 observers_,
899 GattServiceAdded(this, service->GetDevice(), service)); 881 GattServiceAdded(this, service->GetDevice(), service));
900 } 882 }
901 883
902 void BluetoothAdapterChromeOS::NotifyGattServiceRemoved( 884 void BluetoothAdapterBlueZ::NotifyGattServiceRemoved(
903 BluetoothRemoteGattServiceChromeOS* service) { 885 BluetoothRemoteGattServiceBlueZ* service) {
904 DCHECK_EQ(service->GetAdapter(), this); 886 DCHECK_EQ(service->GetAdapter(), this);
905 DCHECK_EQ( 887 DCHECK_EQ(static_cast<BluetoothDeviceBlueZ*>(service->GetDevice())->adapter_,
906 static_cast<BluetoothDeviceChromeOS*>(service->GetDevice())->adapter_, 888 this);
907 this);
908 889
909 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 890 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
910 observers_,
911 GattServiceRemoved(this, service->GetDevice(), service)); 891 GattServiceRemoved(this, service->GetDevice(), service));
912 } 892 }
913 893
914 void BluetoothAdapterChromeOS::NotifyGattServiceChanged( 894 void BluetoothAdapterBlueZ::NotifyGattServiceChanged(
915 BluetoothRemoteGattServiceChromeOS* service) { 895 BluetoothRemoteGattServiceBlueZ* service) {
916 DCHECK_EQ(service->GetAdapter(), this); 896 DCHECK_EQ(service->GetAdapter(), this);
917 897
918 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 898 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
919 observers_,
920 GattServiceChanged(this, service)); 899 GattServiceChanged(this, service));
921 } 900 }
922 901
923 void BluetoothAdapterChromeOS::NotifyGattDiscoveryComplete( 902 void BluetoothAdapterBlueZ::NotifyGattDiscoveryComplete(
924 BluetoothRemoteGattServiceChromeOS* service) { 903 BluetoothRemoteGattServiceBlueZ* service) {
925 DCHECK_EQ(service->GetAdapter(), this); 904 DCHECK_EQ(service->GetAdapter(), this);
926 905
927 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 906 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
928 observers_,
929 GattDiscoveryCompleteForService(this, service)); 907 GattDiscoveryCompleteForService(this, service));
930 } 908 }
931 909
932 void BluetoothAdapterChromeOS::NotifyGattCharacteristicAdded( 910 void BluetoothAdapterBlueZ::NotifyGattCharacteristicAdded(
933 BluetoothRemoteGattCharacteristicChromeOS* characteristic) { 911 BluetoothRemoteGattCharacteristicBlueZ* characteristic) {
934 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 912 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
935 characteristic->GetService())->GetAdapter(), 913 characteristic->GetService())
914 ->GetAdapter(),
936 this); 915 this);
937 916
938 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 917 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
939 observers_,
940 GattCharacteristicAdded(this, characteristic)); 918 GattCharacteristicAdded(this, characteristic));
941 } 919 }
942 920
943 void BluetoothAdapterChromeOS::NotifyGattCharacteristicRemoved( 921 void BluetoothAdapterBlueZ::NotifyGattCharacteristicRemoved(
944 BluetoothRemoteGattCharacteristicChromeOS* characteristic) { 922 BluetoothRemoteGattCharacteristicBlueZ* characteristic) {
945 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 923 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
946 characteristic->GetService())->GetAdapter(), 924 characteristic->GetService())
925 ->GetAdapter(),
947 this); 926 this);
948 927
949 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 928 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
950 observers_,
951 GattCharacteristicRemoved(this, characteristic)); 929 GattCharacteristicRemoved(this, characteristic));
952 } 930 }
953 931
954 void BluetoothAdapterChromeOS::NotifyGattDescriptorAdded( 932 void BluetoothAdapterBlueZ::NotifyGattDescriptorAdded(
955 BluetoothRemoteGattDescriptorChromeOS* descriptor) { 933 BluetoothRemoteGattDescriptorBlueZ* descriptor) {
956 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 934 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
957 descriptor->GetCharacteristic()->GetService())->GetAdapter(), 935 descriptor->GetCharacteristic()->GetService())
936 ->GetAdapter(),
958 this); 937 this);
959 938
960 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 939 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
961 observers_,
962 GattDescriptorAdded(this, descriptor)); 940 GattDescriptorAdded(this, descriptor));
963 } 941 }
964 942
965 void BluetoothAdapterChromeOS::NotifyGattDescriptorRemoved( 943 void BluetoothAdapterBlueZ::NotifyGattDescriptorRemoved(
966 BluetoothRemoteGattDescriptorChromeOS* descriptor) { 944 BluetoothRemoteGattDescriptorBlueZ* descriptor) {
967 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 945 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
968 descriptor->GetCharacteristic()->GetService())->GetAdapter(), 946 descriptor->GetCharacteristic()->GetService())
947 ->GetAdapter(),
969 this); 948 this);
970 949
971 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 950 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
972 observers_,
973 GattDescriptorRemoved(this, descriptor)); 951 GattDescriptorRemoved(this, descriptor));
974 } 952 }
975 953
976 void BluetoothAdapterChromeOS::NotifyGattCharacteristicValueChanged( 954 void BluetoothAdapterBlueZ::NotifyGattCharacteristicValueChanged(
977 BluetoothRemoteGattCharacteristicChromeOS* characteristic, 955 BluetoothRemoteGattCharacteristicBlueZ* characteristic,
978 const std::vector<uint8>& value) { 956 const std::vector<uint8>& value) {
979 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 957 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
980 characteristic->GetService())->GetAdapter(), 958 characteristic->GetService())
959 ->GetAdapter(),
981 this); 960 this);
982 961
983 FOR_EACH_OBSERVER( 962 FOR_EACH_OBSERVER(
984 BluetoothAdapter::Observer, 963 BluetoothAdapter::Observer, observers_,
985 observers_,
986 GattCharacteristicValueChanged(this, characteristic, value)); 964 GattCharacteristicValueChanged(this, characteristic, value));
987 } 965 }
988 966
989 void BluetoothAdapterChromeOS::NotifyGattDescriptorValueChanged( 967 void BluetoothAdapterBlueZ::NotifyGattDescriptorValueChanged(
990 BluetoothRemoteGattDescriptorChromeOS* descriptor, 968 BluetoothRemoteGattDescriptorBlueZ* descriptor,
991 const std::vector<uint8>& value) { 969 const std::vector<uint8>& value) {
992 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 970 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
993 descriptor->GetCharacteristic()->GetService())->GetAdapter(), 971 descriptor->GetCharacteristic()->GetService())
972 ->GetAdapter(),
994 this); 973 this);
995 974
996 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 975 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
997 observers_,
998 GattDescriptorValueChanged(this, descriptor, value)); 976 GattDescriptorValueChanged(this, descriptor, value));
999 } 977 }
1000 978
1001 void BluetoothAdapterChromeOS::UseProfile( 979 void BluetoothAdapterBlueZ::UseProfile(
1002 const BluetoothUUID& uuid, 980 const BluetoothUUID& uuid,
1003 const dbus::ObjectPath& device_path, 981 const dbus::ObjectPath& device_path,
1004 const bluez::BluetoothProfileManagerClient::Options& options, 982 const bluez::BluetoothProfileManagerClient::Options& options,
1005 bluez::BluetoothProfileServiceProvider::Delegate* delegate, 983 bluez::BluetoothProfileServiceProvider::Delegate* delegate,
1006 const ProfileRegisteredCallback& success_callback, 984 const ProfileRegisteredCallback& success_callback,
1007 const ErrorCompletionCallback& error_callback) { 985 const ErrorCompletionCallback& error_callback) {
1008 DCHECK(delegate); 986 DCHECK(delegate);
1009 987
1010 if (!IsPresent()) { 988 if (!IsPresent()) {
1011 VLOG(2) << "Adapter not present, erroring out"; 989 VLOG(2) << "Adapter not present, erroring out";
1012 error_callback.Run("Adapter not present"); 990 error_callback.Run("Adapter not present");
1013 return; 991 return;
1014 } 992 }
1015 993
1016 if (profiles_.find(uuid) != profiles_.end()) { 994 if (profiles_.find(uuid) != profiles_.end()) {
1017 // TODO(jamuraa) check that the options are the same and error when they are 995 // TODO(jamuraa) check that the options are the same and error when they are
1018 // not. 996 // not.
1019 SetProfileDelegate(uuid, device_path, delegate, success_callback, 997 SetProfileDelegate(uuid, device_path, delegate, success_callback,
1020 error_callback); 998 error_callback);
1021 return; 999 return;
1022 } 1000 }
1023 1001
1024 if (profile_queues_.find(uuid) == profile_queues_.end()) { 1002 if (profile_queues_.find(uuid) == profile_queues_.end()) {
1025 BluetoothAdapterProfileChromeOS::Register( 1003 BluetoothAdapterProfileBlueZ::Register(
1026 uuid, options, 1004 uuid, options,
1027 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfile, this, uuid), 1005 base::Bind(&BluetoothAdapterBlueZ::OnRegisterProfile, this, uuid),
1028 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfileError, this, 1006 base::Bind(&BluetoothAdapterBlueZ::OnRegisterProfileError, this, uuid));
1029 uuid));
1030 1007
1031 profile_queues_[uuid] = new std::vector<RegisterProfileCompletionPair>(); 1008 profile_queues_[uuid] = new std::vector<RegisterProfileCompletionPair>();
1032 } 1009 }
1033 1010
1034 profile_queues_[uuid]->push_back(std::make_pair( 1011 profile_queues_[uuid]->push_back(std::make_pair(
1035 base::Bind(&BluetoothAdapterChromeOS::SetProfileDelegate, this, uuid, 1012 base::Bind(&BluetoothAdapterBlueZ::SetProfileDelegate, this, uuid,
1036 device_path, delegate, success_callback, error_callback), 1013 device_path, delegate, success_callback, error_callback),
1037 error_callback)); 1014 error_callback));
1038 } 1015 }
1039 1016
1040 void BluetoothAdapterChromeOS::ReleaseProfile( 1017 void BluetoothAdapterBlueZ::ReleaseProfile(
1041 const dbus::ObjectPath& device_path, 1018 const dbus::ObjectPath& device_path,
1042 BluetoothAdapterProfileChromeOS* profile) { 1019 BluetoothAdapterProfileBlueZ* profile) {
1043 VLOG(2) << "Releasing Profile: " << profile->uuid().canonical_value() 1020 VLOG(2) << "Releasing Profile: " << profile->uuid().canonical_value()
1044 << " from " << device_path.value(); 1021 << " from " << device_path.value();
1045 profile->RemoveDelegate( 1022 profile->RemoveDelegate(
1046 device_path, base::Bind(&BluetoothAdapterChromeOS::RemoveProfile, 1023 device_path, base::Bind(&BluetoothAdapterBlueZ::RemoveProfile,
1047 weak_ptr_factory_.GetWeakPtr(), profile->uuid())); 1024 weak_ptr_factory_.GetWeakPtr(), profile->uuid()));
1048 } 1025 }
1049 1026
1050 void BluetoothAdapterChromeOS::RemoveProfile(const BluetoothUUID& uuid) { 1027 void BluetoothAdapterBlueZ::RemoveProfile(const BluetoothUUID& uuid) {
1051 VLOG(2) << "Remove Profile: " << uuid.canonical_value(); 1028 VLOG(2) << "Remove Profile: " << uuid.canonical_value();
1052 1029
1053 if (profiles_.find(uuid) != profiles_.end()) { 1030 if (profiles_.find(uuid) != profiles_.end()) {
1054 delete profiles_[uuid]; 1031 delete profiles_[uuid];
1055 profiles_.erase(uuid); 1032 profiles_.erase(uuid);
1056 } 1033 }
1057 } 1034 }
1058 1035
1059 void BluetoothAdapterChromeOS::OnRegisterProfile( 1036 void BluetoothAdapterBlueZ::OnRegisterProfile(
1060 const BluetoothUUID& uuid, 1037 const BluetoothUUID& uuid,
1061 scoped_ptr<BluetoothAdapterProfileChromeOS> profile) { 1038 scoped_ptr<BluetoothAdapterProfileBlueZ> profile) {
1062 profiles_[uuid] = profile.release(); 1039 profiles_[uuid] = profile.release();
1063 1040
1064 if (profile_queues_.find(uuid) == profile_queues_.end()) 1041 if (profile_queues_.find(uuid) == profile_queues_.end())
1065 return; 1042 return;
1066 1043
1067 for (auto& it : *profile_queues_[uuid]) 1044 for (auto& it : *profile_queues_[uuid])
1068 it.first.Run(); 1045 it.first.Run();
1069 delete profile_queues_[uuid]; 1046 delete profile_queues_[uuid];
1070 profile_queues_.erase(uuid); 1047 profile_queues_.erase(uuid);
1071 } 1048 }
1072 1049
1073 void BluetoothAdapterChromeOS::SetProfileDelegate( 1050 void BluetoothAdapterBlueZ::SetProfileDelegate(
1074 const BluetoothUUID& uuid, 1051 const BluetoothUUID& uuid,
1075 const dbus::ObjectPath& device_path, 1052 const dbus::ObjectPath& device_path,
1076 bluez::BluetoothProfileServiceProvider::Delegate* delegate, 1053 bluez::BluetoothProfileServiceProvider::Delegate* delegate,
1077 const ProfileRegisteredCallback& success_callback, 1054 const ProfileRegisteredCallback& success_callback,
1078 const ErrorCompletionCallback& error_callback) { 1055 const ErrorCompletionCallback& error_callback) {
1079 if (profiles_.find(uuid) == profiles_.end()) { 1056 if (profiles_.find(uuid) == profiles_.end()) {
1080 error_callback.Run("Cannot find profile!"); 1057 error_callback.Run("Cannot find profile!");
1081 return; 1058 return;
1082 } 1059 }
1083 1060
1084 if (profiles_[uuid]->SetDelegate(device_path, delegate)) { 1061 if (profiles_[uuid]->SetDelegate(device_path, delegate)) {
1085 success_callback.Run(profiles_[uuid]); 1062 success_callback.Run(profiles_[uuid]);
1086 return; 1063 return;
1087 } 1064 }
1088 // Already set 1065 // Already set
1089 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists); 1066 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists);
1090 } 1067 }
1091 1068
1092 void BluetoothAdapterChromeOS::OnRegisterProfileError( 1069 void BluetoothAdapterBlueZ::OnRegisterProfileError(
1093 const BluetoothUUID& uuid, 1070 const BluetoothUUID& uuid,
1094 const std::string& error_name, 1071 const std::string& error_name,
1095 const std::string& error_message) { 1072 const std::string& error_message) {
1096 VLOG(2) << object_path_.value() << ": Failed to register profile: " 1073 VLOG(2) << object_path_.value()
1097 << error_name << ": " << error_message; 1074 << ": Failed to register profile: " << error_name << ": "
1075 << error_message;
1098 if (profile_queues_.find(uuid) == profile_queues_.end()) 1076 if (profile_queues_.find(uuid) == profile_queues_.end())
1099 return; 1077 return;
1100 1078
1101 for (auto& it : *profile_queues_[uuid]) 1079 for (auto& it : *profile_queues_[uuid])
1102 it.second.Run(error_message); 1080 it.second.Run(error_message);
1103 1081
1104 delete profile_queues_[uuid]; 1082 delete profile_queues_[uuid];
1105 profile_queues_.erase(uuid); 1083 profile_queues_.erase(uuid);
1106 } 1084 }
1107 1085
1108 void BluetoothAdapterChromeOS::OnSetDiscoverable( 1086 void BluetoothAdapterBlueZ::OnSetDiscoverable(
1109 const base::Closure& callback, 1087 const base::Closure& callback,
1110 const ErrorCallback& error_callback, 1088 const ErrorCallback& error_callback,
1111 bool success) { 1089 bool success) {
1112 if (!IsPresent()) { 1090 if (!IsPresent()) {
1113 error_callback.Run(); 1091 error_callback.Run();
1114 return; 1092 return;
1115 } 1093 }
1116 1094
1117 // Set the discoverable_timeout property to zero so the adapter remains 1095 // Set the discoverable_timeout property to zero so the adapter remains
1118 // discoverable forever. 1096 // discoverable forever.
1119 bluez::BluezDBusManager::Get() 1097 bluez::BluezDBusManager::Get()
1120 ->GetBluetoothAdapterClient() 1098 ->GetBluetoothAdapterClient()
1121 ->GetProperties(object_path_) 1099 ->GetProperties(object_path_)
1122 ->discoverable_timeout.Set( 1100 ->discoverable_timeout.Set(
1123 0, 1101 0,
1124 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, 1102 base::Bind(&BluetoothAdapterBlueZ::OnPropertyChangeCompleted,
1125 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1103 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1126 } 1104 }
1127 1105
1128 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( 1106 void BluetoothAdapterBlueZ::OnPropertyChangeCompleted(
1129 const base::Closure& callback, 1107 const base::Closure& callback,
1130 const ErrorCallback& error_callback, 1108 const ErrorCallback& error_callback,
1131 bool success) { 1109 bool success) {
1132 if (IsPresent() && success) { 1110 if (IsPresent() && success) {
1133 callback.Run(); 1111 callback.Run();
1134 } else { 1112 } else {
1135 error_callback.Run(); 1113 error_callback.Run();
1136 } 1114 }
1137 } 1115 }
1138 1116
1139 void BluetoothAdapterChromeOS::AddDiscoverySession( 1117 void BluetoothAdapterBlueZ::AddDiscoverySession(
1140 BluetoothDiscoveryFilter* discovery_filter, 1118 BluetoothDiscoveryFilter* discovery_filter,
1141 const base::Closure& callback, 1119 const base::Closure& callback,
1142 const DiscoverySessionErrorCallback& error_callback) { 1120 const DiscoverySessionErrorCallback& error_callback) {
1143 if (!IsPresent()) { 1121 if (!IsPresent()) {
1144 error_callback.Run( 1122 error_callback.Run(
1145 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT); 1123 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT);
1146 return; 1124 return;
1147 } 1125 }
1148 VLOG(1) << __func__; 1126 VLOG(1) << __func__;
1149 if (discovery_request_pending_) { 1127 if (discovery_request_pending_) {
(...skipping 22 matching lines...) Expand all
1172 DCHECK_EQ(num_discovery_sessions_, 0); 1150 DCHECK_EQ(num_discovery_sessions_, 0);
1173 1151
1174 if (discovery_filter) { 1152 if (discovery_filter) {
1175 discovery_request_pending_ = true; 1153 discovery_request_pending_ = true;
1176 1154
1177 scoped_ptr<BluetoothDiscoveryFilter> df(new BluetoothDiscoveryFilter( 1155 scoped_ptr<BluetoothDiscoveryFilter> df(new BluetoothDiscoveryFilter(
1178 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL)); 1156 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL));
1179 df->CopyFrom(*discovery_filter); 1157 df->CopyFrom(*discovery_filter);
1180 SetDiscoveryFilter( 1158 SetDiscoveryFilter(
1181 df.Pass(), 1159 df.Pass(),
1182 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter, 1160 base::Bind(&BluetoothAdapterBlueZ::OnPreSetDiscoveryFilter,
1183 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), 1161 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
1184 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError, 1162 base::Bind(&BluetoothAdapterBlueZ::OnPreSetDiscoveryFilterError,
1185 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1163 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1186 return; 1164 return;
1187 } else { 1165 } else {
1188 current_filter_.reset(); 1166 current_filter_.reset();
1189 } 1167 }
1190 1168
1191 // This is the first request to start device discovery. 1169 // This is the first request to start device discovery.
1192 discovery_request_pending_ = true; 1170 discovery_request_pending_ = true;
1193 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery( 1171 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery(
1194 object_path_, 1172 object_path_,
1195 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, 1173 base::Bind(&BluetoothAdapterBlueZ::OnStartDiscovery,
1196 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), 1174 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
1197 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, 1175 base::Bind(&BluetoothAdapterBlueZ::OnStartDiscoveryError,
1198 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1176 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1199 } 1177 }
1200 1178
1201 void BluetoothAdapterChromeOS::RemoveDiscoverySession( 1179 void BluetoothAdapterBlueZ::RemoveDiscoverySession(
1202 BluetoothDiscoveryFilter* discovery_filter, 1180 BluetoothDiscoveryFilter* discovery_filter,
1203 const base::Closure& callback, 1181 const base::Closure& callback,
1204 const DiscoverySessionErrorCallback& error_callback) { 1182 const DiscoverySessionErrorCallback& error_callback) {
1205 if (!IsPresent()) { 1183 if (!IsPresent()) {
1206 error_callback.Run( 1184 error_callback.Run(
1207 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT); 1185 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT);
1208 return; 1186 return;
1209 } 1187 }
1210 1188
1211 VLOG(1) << __func__; 1189 VLOG(1) << __func__;
(...skipping 26 matching lines...) Expand all
1238 error_callback.Run( 1216 error_callback.Run(
1239 UMABluetoothDiscoverySessionOutcome::ACTIVE_SESSION_NOT_IN_ADAPTER); 1217 UMABluetoothDiscoverySessionOutcome::ACTIVE_SESSION_NOT_IN_ADAPTER);
1240 return; 1218 return;
1241 } 1219 }
1242 1220
1243 // There is exactly one active discovery session. Request BlueZ to stop 1221 // There is exactly one active discovery session. Request BlueZ to stop
1244 // discovery. 1222 // discovery.
1245 DCHECK_EQ(num_discovery_sessions_, 1); 1223 DCHECK_EQ(num_discovery_sessions_, 1);
1246 discovery_request_pending_ = true; 1224 discovery_request_pending_ = true;
1247 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StopDiscovery( 1225 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StopDiscovery(
1248 object_path_, base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, 1226 object_path_, base::Bind(&BluetoothAdapterBlueZ::OnStopDiscovery,
1249 weak_ptr_factory_.GetWeakPtr(), callback), 1227 weak_ptr_factory_.GetWeakPtr(), callback),
1250 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, 1228 base::Bind(&BluetoothAdapterBlueZ::OnStopDiscoveryError,
1251 weak_ptr_factory_.GetWeakPtr(), error_callback)); 1229 weak_ptr_factory_.GetWeakPtr(), error_callback));
1252 } 1230 }
1253 1231
1254 void BluetoothAdapterChromeOS::SetDiscoveryFilter( 1232 void BluetoothAdapterBlueZ::SetDiscoveryFilter(
1255 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, 1233 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
1256 const base::Closure& callback, 1234 const base::Closure& callback,
1257 const DiscoverySessionErrorCallback& error_callback) { 1235 const DiscoverySessionErrorCallback& error_callback) {
1258 if (!IsPresent()) { 1236 if (!IsPresent()) {
1259 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); 1237 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED);
1260 return; 1238 return;
1261 } 1239 }
1262 1240
1263 // If old and new filter are equal (null) then don't make request, just call 1241 // If old and new filter are equal (null) then don't make request, just call
1264 // succes callback 1242 // succes callback
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 1287
1310 for (const auto& it : uuids) 1288 for (const auto& it : uuids)
1311 dbus_discovery_filter.uuids.get()->push_back(it.value()); 1289 dbus_discovery_filter.uuids.get()->push_back(it.value());
1312 } 1290 }
1313 } 1291 }
1314 1292
1315 bluez::BluezDBusManager::Get() 1293 bluez::BluezDBusManager::Get()
1316 ->GetBluetoothAdapterClient() 1294 ->GetBluetoothAdapterClient()
1317 ->SetDiscoveryFilter( 1295 ->SetDiscoveryFilter(
1318 object_path_, dbus_discovery_filter, 1296 object_path_, dbus_discovery_filter,
1319 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilter, 1297 base::Bind(&BluetoothAdapterBlueZ::OnSetDiscoveryFilter,
1320 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), 1298 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
1321 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilterError, 1299 base::Bind(&BluetoothAdapterBlueZ::OnSetDiscoveryFilterError,
1322 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1300 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1323 } 1301 }
1324 1302
1325 void BluetoothAdapterChromeOS::OnStartDiscovery( 1303 void BluetoothAdapterBlueZ::OnStartDiscovery(
1326 const base::Closure& callback, 1304 const base::Closure& callback,
1327 const DiscoverySessionErrorCallback& error_callback) { 1305 const DiscoverySessionErrorCallback& error_callback) {
1328 // Report success on the original request and increment the count. 1306 // Report success on the original request and increment the count.
1329 VLOG(1) << __func__; 1307 VLOG(1) << __func__;
1330 DCHECK(discovery_request_pending_); 1308 DCHECK(discovery_request_pending_);
1331 DCHECK_EQ(num_discovery_sessions_, 0); 1309 DCHECK_EQ(num_discovery_sessions_, 0);
1332 discovery_request_pending_ = false; 1310 discovery_request_pending_ = false;
1333 num_discovery_sessions_++; 1311 num_discovery_sessions_++;
1334 if (IsPresent()) { 1312 if (IsPresent()) {
1335 callback.Run(); 1313 callback.Run();
1336 } else { 1314 } else {
1337 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); 1315 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED);
1338 } 1316 }
1339 1317
1340 // Try to add a new discovery session for each queued request. 1318 // Try to add a new discovery session for each queued request.
1341 ProcessQueuedDiscoveryRequests(); 1319 ProcessQueuedDiscoveryRequests();
1342 } 1320 }
1343 1321
1344 void BluetoothAdapterChromeOS::OnStartDiscoveryError( 1322 void BluetoothAdapterBlueZ::OnStartDiscoveryError(
1345 const base::Closure& callback, 1323 const base::Closure& callback,
1346 const DiscoverySessionErrorCallback& error_callback, 1324 const DiscoverySessionErrorCallback& error_callback,
1347 const std::string& error_name, 1325 const std::string& error_name,
1348 const std::string& error_message) { 1326 const std::string& error_message) {
1349 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " 1327 LOG(WARNING) << object_path_.value()
1350 << error_name << ": " << error_message; 1328 << ": Failed to start discovery: " << error_name << ": "
1329 << error_message;
1351 1330
1352 // Failed to start discovery. This can only happen if the count is at 0. 1331 // Failed to start discovery. This can only happen if the count is at 0.
1353 DCHECK_EQ(num_discovery_sessions_, 0); 1332 DCHECK_EQ(num_discovery_sessions_, 0);
1354 DCHECK(discovery_request_pending_); 1333 DCHECK(discovery_request_pending_);
1355 discovery_request_pending_ = false; 1334 discovery_request_pending_ = false;
1356 1335
1357 // Discovery request may fail if discovery was previously initiated by Chrome, 1336 // Discovery request may fail if discovery was previously initiated by Chrome,
1358 // but the session were invalidated due to the discovery state unexpectedly 1337 // but the session were invalidated due to the discovery state unexpectedly
1359 // changing to false and then back to true. In this case, report success. 1338 // changing to false and then back to true. In this case, report success.
1360 if (IsPresent() && error_name == bluetooth_device::kErrorInProgress && 1339 if (IsPresent() && error_name == bluetooth_device::kErrorInProgress &&
1361 IsDiscovering()) { 1340 IsDiscovering()) {
1362 VLOG(1) << "Discovery previously initiated. Reporting success."; 1341 VLOG(1) << "Discovery previously initiated. Reporting success.";
1363 num_discovery_sessions_++; 1342 num_discovery_sessions_++;
1364 callback.Run(); 1343 callback.Run();
1365 } else { 1344 } else {
1366 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name)); 1345 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name));
1367 } 1346 }
1368 1347
1369 // Try to add a new discovery session for each queued request. 1348 // Try to add a new discovery session for each queued request.
1370 ProcessQueuedDiscoveryRequests(); 1349 ProcessQueuedDiscoveryRequests();
1371 } 1350 }
1372 1351
1373 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { 1352 void BluetoothAdapterBlueZ::OnStopDiscovery(const base::Closure& callback) {
1374 // Report success on the original request and decrement the count. 1353 // Report success on the original request and decrement the count.
1375 VLOG(1) << __func__; 1354 VLOG(1) << __func__;
1376 DCHECK(discovery_request_pending_); 1355 DCHECK(discovery_request_pending_);
1377 DCHECK_EQ(num_discovery_sessions_, 1); 1356 DCHECK_EQ(num_discovery_sessions_, 1);
1378 discovery_request_pending_ = false; 1357 discovery_request_pending_ = false;
1379 num_discovery_sessions_--; 1358 num_discovery_sessions_--;
1380 callback.Run(); 1359 callback.Run();
1381 1360
1382 current_filter_.reset(); 1361 current_filter_.reset();
1383 1362
1384 // Try to add a new discovery session for each queued request. 1363 // Try to add a new discovery session for each queued request.
1385 ProcessQueuedDiscoveryRequests(); 1364 ProcessQueuedDiscoveryRequests();
1386 } 1365 }
1387 1366
1388 void BluetoothAdapterChromeOS::OnStopDiscoveryError( 1367 void BluetoothAdapterBlueZ::OnStopDiscoveryError(
1389 const DiscoverySessionErrorCallback& error_callback, 1368 const DiscoverySessionErrorCallback& error_callback,
1390 const std::string& error_name, 1369 const std::string& error_name,
1391 const std::string& error_message) { 1370 const std::string& error_message) {
1392 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " 1371 LOG(WARNING) << object_path_.value()
1393 << error_name << ": " << error_message; 1372 << ": Failed to stop discovery: " << error_name << ": "
1373 << error_message;
1394 1374
1395 // Failed to stop discovery. This can only happen if the count is at 1. 1375 // Failed to stop discovery. This can only happen if the count is at 1.
1396 DCHECK(discovery_request_pending_); 1376 DCHECK(discovery_request_pending_);
1397 DCHECK_EQ(num_discovery_sessions_, 1); 1377 DCHECK_EQ(num_discovery_sessions_, 1);
1398 discovery_request_pending_ = false; 1378 discovery_request_pending_ = false;
1399 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name)); 1379 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name));
1400 1380
1401 // Try to add a new discovery session for each queued request. 1381 // Try to add a new discovery session for each queued request.
1402 ProcessQueuedDiscoveryRequests(); 1382 ProcessQueuedDiscoveryRequests();
1403 } 1383 }
1404 1384
1405 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter( 1385 void BluetoothAdapterBlueZ::OnPreSetDiscoveryFilter(
1406 const base::Closure& callback, 1386 const base::Closure& callback,
1407 const DiscoverySessionErrorCallback& error_callback) { 1387 const DiscoverySessionErrorCallback& error_callback) {
1408 // This is the first request to start device discovery. 1388 // This is the first request to start device discovery.
1409 DCHECK(discovery_request_pending_); 1389 DCHECK(discovery_request_pending_);
1410 DCHECK_EQ(num_discovery_sessions_, 0); 1390 DCHECK_EQ(num_discovery_sessions_, 0);
1411 1391
1412 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery( 1392 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery(
1413 object_path_, 1393 object_path_,
1414 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, 1394 base::Bind(&BluetoothAdapterBlueZ::OnStartDiscovery,
1415 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), 1395 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
1416 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, 1396 base::Bind(&BluetoothAdapterBlueZ::OnStartDiscoveryError,
1417 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1397 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1418 } 1398 }
1419 1399
1420 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError( 1400 void BluetoothAdapterBlueZ::OnPreSetDiscoveryFilterError(
1421 const base::Closure& callback, 1401 const base::Closure& callback,
1422 const DiscoverySessionErrorCallback& error_callback, 1402 const DiscoverySessionErrorCallback& error_callback,
1423 UMABluetoothDiscoverySessionOutcome outcome) { 1403 UMABluetoothDiscoverySessionOutcome outcome) {
1424 LOG(WARNING) << object_path_.value() 1404 LOG(WARNING) << object_path_.value()
1425 << ": Failed to pre set discovery filter."; 1405 << ": Failed to pre set discovery filter.";
1426 1406
1427 // Failed to start discovery. This can only happen if the count is at 0. 1407 // Failed to start discovery. This can only happen if the count is at 0.
1428 DCHECK_EQ(num_discovery_sessions_, 0); 1408 DCHECK_EQ(num_discovery_sessions_, 0);
1429 DCHECK(discovery_request_pending_); 1409 DCHECK(discovery_request_pending_);
1430 discovery_request_pending_ = false; 1410 discovery_request_pending_ = false;
1431 1411
1432 error_callback.Run(outcome); 1412 error_callback.Run(outcome);
1433 1413
1434 // Try to add a new discovery session for each queued request. 1414 // Try to add a new discovery session for each queued request.
1435 ProcessQueuedDiscoveryRequests(); 1415 ProcessQueuedDiscoveryRequests();
1436 } 1416 }
1437 1417
1438 void BluetoothAdapterChromeOS::OnSetDiscoveryFilter( 1418 void BluetoothAdapterBlueZ::OnSetDiscoveryFilter(
1439 const base::Closure& callback, 1419 const base::Closure& callback,
1440 const DiscoverySessionErrorCallback& error_callback) { 1420 const DiscoverySessionErrorCallback& error_callback) {
1441 // Report success on the original request and increment the count. 1421 // Report success on the original request and increment the count.
1442 VLOG(1) << __func__; 1422 VLOG(1) << __func__;
1443 if (IsPresent()) { 1423 if (IsPresent()) {
1444 callback.Run(); 1424 callback.Run();
1445 } else { 1425 } else {
1446 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); 1426 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED);
1447 } 1427 }
1448 } 1428 }
1449 1429
1450 void BluetoothAdapterChromeOS::OnSetDiscoveryFilterError( 1430 void BluetoothAdapterBlueZ::OnSetDiscoveryFilterError(
1451 const base::Closure& callback, 1431 const base::Closure& callback,
1452 const DiscoverySessionErrorCallback& error_callback, 1432 const DiscoverySessionErrorCallback& error_callback,
1453 const std::string& error_name, 1433 const std::string& error_name,
1454 const std::string& error_message) { 1434 const std::string& error_message) {
1455 LOG(WARNING) << object_path_.value() 1435 LOG(WARNING) << object_path_.value()
1456 << ": Failed to set discovery filter: " << error_name << ": " 1436 << ": Failed to set discovery filter: " << error_name << ": "
1457 << error_message; 1437 << error_message;
1458 1438
1459 UMABluetoothDiscoverySessionOutcome outcome = 1439 UMABluetoothDiscoverySessionOutcome outcome =
1460 TranslateDiscoveryErrorToUMA(error_name); 1440 TranslateDiscoveryErrorToUMA(error_name);
1461 if (outcome == UMABluetoothDiscoverySessionOutcome::FAILED) { 1441 if (outcome == UMABluetoothDiscoverySessionOutcome::FAILED) {
1462 // bluez/doc/adapter-api.txt says "Failed" is returned from 1442 // bluez/doc/adapter-api.txt says "Failed" is returned from
1463 // SetDiscoveryFilter when the controller doesn't support the requested 1443 // SetDiscoveryFilter when the controller doesn't support the requested
1464 // transport. 1444 // transport.
1465 outcome = UMABluetoothDiscoverySessionOutcome:: 1445 outcome = UMABluetoothDiscoverySessionOutcome::
1466 CHROMEOS_DBUS_FAILED_MAYBE_UNSUPPORTED_TRANSPORT; 1446 BLUEZ_DBUS_FAILED_MAYBE_UNSUPPORTED_TRANSPORT;
1467 } 1447 }
1468 error_callback.Run(outcome); 1448 error_callback.Run(outcome);
1469 1449
1470 // Try to add a new discovery session for each queued request. 1450 // Try to add a new discovery session for each queued request.
1471 ProcessQueuedDiscoveryRequests(); 1451 ProcessQueuedDiscoveryRequests();
1472 } 1452 }
1473 1453
1474 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { 1454 void BluetoothAdapterBlueZ::ProcessQueuedDiscoveryRequests() {
1475 while (!discovery_request_queue_.empty()) { 1455 while (!discovery_request_queue_.empty()) {
1476 VLOG(1) << "Process queued discovery request."; 1456 VLOG(1) << "Process queued discovery request.";
1477 DiscoveryParamTuple params = discovery_request_queue_.front(); 1457 DiscoveryParamTuple params = discovery_request_queue_.front();
1478 discovery_request_queue_.pop(); 1458 discovery_request_queue_.pop();
1479 AddDiscoverySession(std::get<0>(params), std::get<1>(params), 1459 AddDiscoverySession(std::get<0>(params), std::get<1>(params),
1480 std::get<2>(params)); 1460 std::get<2>(params));
1481 1461
1482 // If the queued request resulted in a pending call, then let it 1462 // If the queued request resulted in a pending call, then let it
1483 // asynchonously process the remaining queued requests once the pending 1463 // asynchonously process the remaining queued requests once the pending
1484 // call returns. 1464 // call returns.
1485 if (discovery_request_pending_) 1465 if (discovery_request_pending_)
1486 return; 1466 return;
1487 } 1467 }
1488 } 1468 }
1489 1469
1490 } // namespace chromeos 1470 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698