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

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: rebase 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->address.name()) { 445 if (property_name == properties->address.name()) {
458 for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) { 446 for (auto iter = devices_.begin(); iter != devices_.end(); ++iter) {
459 if (iter->second->GetAddress() == device_chromeos->GetAddress()) { 447 if (iter->second->GetAddress() == device_bluez->GetAddress()) {
460 std::string old_address = iter->first; 448 std::string old_address = iter->first;
461 VLOG(1) << "Device changed address, old: " << old_address 449 VLOG(1) << "Device changed address, old: " << old_address
462 << " new: " << device_chromeos->GetAddress(); 450 << " new: " << device_bluez->GetAddress();
463 devices_.erase(iter); 451 devices_.erase(iter);
464 452
465 DCHECK(devices_.find(device_chromeos->GetAddress()) == devices_.end()); 453 DCHECK(devices_.find(device_bluez->GetAddress()) == devices_.end());
466 devices_[device_chromeos->GetAddress()] = device_chromeos; 454 devices_[device_bluez->GetAddress()] = device_bluez;
467 NotifyDeviceAddressChanged(device_chromeos, old_address); 455 NotifyDeviceAddressChanged(device_bluez, old_address);
468 break; 456 break;
469 } 457 }
470 } 458 }
471 } 459 }
472 460
473 if (property_name == properties->bluetooth_class.name() || 461 if (property_name == properties->bluetooth_class.name() ||
474 property_name == properties->address.name() || 462 property_name == properties->address.name() ||
475 property_name == properties->alias.name() || 463 property_name == properties->alias.name() ||
476 property_name == properties->paired.name() || 464 property_name == properties->paired.name() ||
477 property_name == properties->trusted.name() || 465 property_name == properties->trusted.name() ||
478 property_name == properties->connected.name() || 466 property_name == properties->connected.name() ||
479 property_name == properties->uuids.name() || 467 property_name == properties->uuids.name() ||
480 property_name == properties->rssi.name() || 468 property_name == properties->rssi.name() ||
481 property_name == properties->tx_power.name()) { 469 property_name == properties->tx_power.name()) {
482 NotifyDeviceChanged(device_chromeos); 470 NotifyDeviceChanged(device_bluez);
483 } 471 }
484 472
485 // When a device becomes paired, mark it as trusted so that the user does 473 // When a device becomes paired, mark it as trusted so that the user does
486 // not need to approve every incoming connection 474 // not need to approve every incoming connection
487 if (property_name == properties->paired.name() && 475 if (property_name == properties->paired.name() &&
488 properties->paired.value() && !properties->trusted.value()) { 476 properties->paired.value() && !properties->trusted.value()) {
489 device_chromeos->SetTrusted(); 477 device_bluez->SetTrusted();
490 } 478 }
491 479
492 // UMA connection counting 480 // UMA connection counting
493 if (property_name == properties->connected.name()) { 481 if (property_name == properties->connected.name()) {
494 // PlayStation joystick tries to reconnect after disconnection from USB. 482 // PlayStation joystick tries to reconnect after disconnection from USB.
495 // If it is still not trusted, set it, so it becomes available on the 483 // If it is still not trusted, set it, so it becomes available on the
496 // list of known devices. 484 // list of known devices.
497 if (properties->connected.value() && device_chromeos->IsTrustable() && 485 if (properties->connected.value() && device_bluez->IsTrustable() &&
498 !properties->trusted.value()) 486 !properties->trusted.value())
499 device_chromeos->SetTrusted(); 487 device_bluez->SetTrusted();
500 488
501 int count = 0; 489 int count = 0;
502 490
503 for (DevicesMap::iterator iter = devices_.begin(); 491 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end();
504 iter != devices_.end(); ++iter) { 492 ++iter) {
505 if (iter->second->IsPaired() && iter->second->IsConnected()) 493 if (iter->second->IsPaired() && iter->second->IsConnected())
506 ++count; 494 ++count;
507 } 495 }
508 496
509 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count); 497 UMA_HISTOGRAM_COUNTS_100("Bluetooth.ConnectedDeviceCount", count);
510 } 498 }
511 } 499 }
512 500
513 void BluetoothAdapterChromeOS::InputPropertyChanged( 501 void BluetoothAdapterBlueZ::InputPropertyChanged(
514 const dbus::ObjectPath& object_path, 502 const dbus::ObjectPath& object_path,
515 const std::string& property_name) { 503 const std::string& property_name) {
516 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); 504 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(object_path);
517 if (!device_chromeos) 505 if (!device_bluez)
518 return; 506 return;
519 507
520 bluez::BluetoothInputClient::Properties* properties = 508 bluez::BluetoothInputClient::Properties* properties =
521 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->GetProperties( 509 bluez::BluezDBusManager::Get()->GetBluetoothInputClient()->GetProperties(
522 object_path); 510 object_path);
523 511
524 // Properties structure can be removed, which triggers a change in the 512 // Properties structure can be removed, which triggers a change in the
525 // BluetoothDevice::IsConnectable() property, as does a change in the 513 // BluetoothDevice::IsConnectable() property, as does a change in the
526 // actual reconnect_mode property. 514 // actual reconnect_mode property.
527 if (!properties || property_name == properties->reconnect_mode.name()) { 515 if (!properties || property_name == properties->reconnect_mode.name()) {
528 NotifyDeviceChanged(device_chromeos); 516 NotifyDeviceChanged(device_bluez);
529 } 517 }
530 } 518 }
531 519
532 void BluetoothAdapterChromeOS::Released() { 520 void BluetoothAdapterBlueZ::Released() {
533 VLOG(1) << "Release"; 521 VLOG(1) << "Release";
534 if (!IsPresent()) 522 if (!IsPresent())
535 return; 523 return;
536 DCHECK(agent_.get()); 524 DCHECK(agent_.get());
537 525
538 // Called after we unregister the pairing agent, e.g. when changing I/O 526 // Called after we unregister the pairing agent, e.g. when changing I/O
539 // capabilities. Nothing much to be done right now. 527 // capabilities. Nothing much to be done right now.
540 } 528 }
541 529
542 void BluetoothAdapterChromeOS::RequestPinCode( 530 void BluetoothAdapterBlueZ::RequestPinCode(const dbus::ObjectPath& device_path,
543 const dbus::ObjectPath& device_path, 531 const PinCodeCallback& callback) {
544 const PinCodeCallback& callback) {
545 DCHECK(IsPresent()); 532 DCHECK(IsPresent());
546 DCHECK(agent_.get()); 533 DCHECK(agent_.get());
547 VLOG(1) << device_path.value() << ": RequestPinCode"; 534 VLOG(1) << device_path.value() << ": RequestPinCode";
548 535
549 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 536 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
550 if (!pairing) { 537 if (!pairing) {
551 callback.Run(REJECTED, ""); 538 callback.Run(REJECTED, "");
552 return; 539 return;
553 } 540 }
554 541
555 pairing->RequestPinCode(callback); 542 pairing->RequestPinCode(callback);
556 } 543 }
557 544
558 void BluetoothAdapterChromeOS::DisplayPinCode( 545 void BluetoothAdapterBlueZ::DisplayPinCode(const dbus::ObjectPath& device_path,
559 const dbus::ObjectPath& device_path, 546 const std::string& pincode) {
560 const std::string& pincode) {
561 DCHECK(IsPresent()); 547 DCHECK(IsPresent());
562 DCHECK(agent_.get()); 548 DCHECK(agent_.get());
563 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode; 549 VLOG(1) << device_path.value() << ": DisplayPinCode: " << pincode;
564 550
565 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 551 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
566 if (!pairing) 552 if (!pairing)
567 return; 553 return;
568 554
569 pairing->DisplayPinCode(pincode); 555 pairing->DisplayPinCode(pincode);
570 } 556 }
571 557
572 void BluetoothAdapterChromeOS::RequestPasskey( 558 void BluetoothAdapterBlueZ::RequestPasskey(const dbus::ObjectPath& device_path,
573 const dbus::ObjectPath& device_path, 559 const PasskeyCallback& callback) {
574 const PasskeyCallback& callback) {
575 DCHECK(IsPresent()); 560 DCHECK(IsPresent());
576 DCHECK(agent_.get()); 561 DCHECK(agent_.get());
577 VLOG(1) << device_path.value() << ": RequestPasskey"; 562 VLOG(1) << device_path.value() << ": RequestPasskey";
578 563
579 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 564 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
580 if (!pairing) { 565 if (!pairing) {
581 callback.Run(REJECTED, 0); 566 callback.Run(REJECTED, 0);
582 return; 567 return;
583 } 568 }
584 569
585 pairing->RequestPasskey(callback); 570 pairing->RequestPasskey(callback);
586 } 571 }
587 572
588 void BluetoothAdapterChromeOS::DisplayPasskey( 573 void BluetoothAdapterBlueZ::DisplayPasskey(const dbus::ObjectPath& device_path,
589 const dbus::ObjectPath& device_path, 574 uint32 passkey,
590 uint32 passkey, 575 uint16 entered) {
591 uint16 entered) {
592 DCHECK(IsPresent()); 576 DCHECK(IsPresent());
593 DCHECK(agent_.get()); 577 DCHECK(agent_.get());
594 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey 578 VLOG(1) << device_path.value() << ": DisplayPasskey: " << passkey << " ("
595 << " (" << entered << " entered)"; 579 << entered << " entered)";
596 580
597 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 581 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
598 if (!pairing) 582 if (!pairing)
599 return; 583 return;
600 584
601 if (entered == 0) 585 if (entered == 0)
602 pairing->DisplayPasskey(passkey); 586 pairing->DisplayPasskey(passkey);
603 587
604 pairing->KeysEntered(entered); 588 pairing->KeysEntered(entered);
605 } 589 }
606 590
607 void BluetoothAdapterChromeOS::RequestConfirmation( 591 void BluetoothAdapterBlueZ::RequestConfirmation(
608 const dbus::ObjectPath& device_path, 592 const dbus::ObjectPath& device_path,
609 uint32 passkey, 593 uint32 passkey,
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() << ": RequestConfirmation: " << passkey; 597 VLOG(1) << device_path.value() << ": RequestConfirmation: " << passkey;
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->RequestConfirmation(passkey, callback); 605 pairing->RequestConfirmation(passkey, callback);
622 } 606 }
623 607
624 void BluetoothAdapterChromeOS::RequestAuthorization( 608 void BluetoothAdapterBlueZ::RequestAuthorization(
625 const dbus::ObjectPath& device_path, 609 const dbus::ObjectPath& device_path,
626 const ConfirmationCallback& callback) { 610 const ConfirmationCallback& callback) {
627 DCHECK(IsPresent()); 611 DCHECK(IsPresent());
628 DCHECK(agent_.get()); 612 DCHECK(agent_.get());
629 VLOG(1) << device_path.value() << ": RequestAuthorization"; 613 VLOG(1) << device_path.value() << ": RequestAuthorization";
630 614
631 BluetoothPairingChromeOS* pairing = GetPairing(device_path); 615 BluetoothPairingBlueZ* pairing = GetPairing(device_path);
632 if (!pairing) { 616 if (!pairing) {
633 callback.Run(REJECTED); 617 callback.Run(REJECTED);
634 return; 618 return;
635 } 619 }
636 620
637 pairing->RequestAuthorization(callback); 621 pairing->RequestAuthorization(callback);
638 } 622 }
639 623
640 void BluetoothAdapterChromeOS::AuthorizeService( 624 void BluetoothAdapterBlueZ::AuthorizeService(
641 const dbus::ObjectPath& device_path, 625 const dbus::ObjectPath& device_path,
642 const std::string& uuid, 626 const std::string& uuid,
643 const ConfirmationCallback& callback) { 627 const ConfirmationCallback& callback) {
644 DCHECK(IsPresent()); 628 DCHECK(IsPresent());
645 DCHECK(agent_.get()); 629 DCHECK(agent_.get());
646 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid; 630 VLOG(1) << device_path.value() << ": AuthorizeService: " << uuid;
647 631
648 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(device_path); 632 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(device_path);
649 if (!device_chromeos) { 633 if (!device_bluez) {
650 callback.Run(CANCELLED); 634 callback.Run(CANCELLED);
651 return; 635 return;
652 } 636 }
653 637
654 // We always set paired devices to Trusted, so the only reason that this 638 // We always set paired devices to Trusted, so the only reason that this
655 // method call would ever be called is in the case of a race condition where 639 // method call would ever be called is in the case of a race condition where
656 // our "Set('Trusted', true)" method call is still pending in the Bluetooth 640 // our "Set('Trusted', true)" method call is still pending in the Bluetooth
657 // daemon because it's busy handling the incoming connection. 641 // daemon because it's busy handling the incoming connection.
658 if (device_chromeos->IsPaired()) { 642 if (device_bluez->IsPaired()) {
659 callback.Run(SUCCESS); 643 callback.Run(SUCCESS);
660 return; 644 return;
661 } 645 }
662 646
663 // TODO(keybuk): reject service authorizations when not paired, determine 647 // TODO(keybuk): reject service authorizations when not paired, determine
664 // whether this is acceptable long-term. 648 // whether this is acceptable long-term.
665 LOG(WARNING) << "Rejecting service connection from unpaired device " 649 LOG(WARNING) << "Rejecting service connection from unpaired device "
666 << device_chromeos->GetAddress() << " for UUID " << uuid; 650 << device_bluez->GetAddress() << " for UUID " << uuid;
667 callback.Run(REJECTED); 651 callback.Run(REJECTED);
668 } 652 }
669 653
670 void BluetoothAdapterChromeOS::Cancel() { 654 void BluetoothAdapterBlueZ::Cancel() {
671 DCHECK(IsPresent()); 655 DCHECK(IsPresent());
672 DCHECK(agent_.get()); 656 DCHECK(agent_.get());
673 VLOG(1) << "Cancel"; 657 VLOG(1) << "Cancel";
674 } 658 }
675 659
676 void BluetoothAdapterChromeOS::OnRegisterAgent() { 660 void BluetoothAdapterBlueZ::OnRegisterAgent() {
677 VLOG(1) << "Pairing agent registered, requesting to be made default"; 661 VLOG(1) << "Pairing agent registered, requesting to be made default";
678 662
679 bluez::BluezDBusManager::Get() 663 bluez::BluezDBusManager::Get()
680 ->GetBluetoothAgentManagerClient() 664 ->GetBluetoothAgentManagerClient()
681 ->RequestDefaultAgent( 665 ->RequestDefaultAgent(
682 dbus::ObjectPath(kAgentPath), 666 dbus::ObjectPath(kAgentPath),
683 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgent, 667 base::Bind(&BluetoothAdapterBlueZ::OnRequestDefaultAgent,
684 weak_ptr_factory_.GetWeakPtr()), 668 weak_ptr_factory_.GetWeakPtr()),
685 base::Bind(&BluetoothAdapterChromeOS::OnRequestDefaultAgentError, 669 base::Bind(&BluetoothAdapterBlueZ::OnRequestDefaultAgentError,
686 weak_ptr_factory_.GetWeakPtr())); 670 weak_ptr_factory_.GetWeakPtr()));
687 } 671 }
688 672
689 void BluetoothAdapterChromeOS::OnRegisterAgentError( 673 void BluetoothAdapterBlueZ::OnRegisterAgentError(
690 const std::string& error_name, 674 const std::string& error_name,
691 const std::string& error_message) { 675 const std::string& error_message) {
692 // Our agent being already registered isn't an error. 676 // Our agent being already registered isn't an error.
693 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists) 677 if (error_name == bluetooth_agent_manager::kErrorAlreadyExists)
694 return; 678 return;
695 679
696 LOG(WARNING) << ": Failed to register pairing agent: " 680 LOG(WARNING) << ": Failed to register pairing agent: " << error_name << ": "
697 << error_name << ": " << error_message; 681 << error_message;
698 } 682 }
699 683
700 void BluetoothAdapterChromeOS::OnRequestDefaultAgent() { 684 void BluetoothAdapterBlueZ::OnRequestDefaultAgent() {
701 VLOG(1) << "Pairing agent now default"; 685 VLOG(1) << "Pairing agent now default";
702 } 686 }
703 687
704 void BluetoothAdapterChromeOS::OnRequestDefaultAgentError( 688 void BluetoothAdapterBlueZ::OnRequestDefaultAgentError(
705 const std::string& error_name, 689 const std::string& error_name,
706 const std::string& error_message) { 690 const std::string& error_message) {
707 LOG(WARNING) << ": Failed to make pairing agent default: " 691 LOG(WARNING) << ": Failed to make pairing agent default: " << error_name
708 << error_name << ": " << error_message; 692 << ": " << error_message;
709 } 693 }
710 694
711 void BluetoothAdapterChromeOS::OnRegisterAudioSink( 695 void BluetoothAdapterBlueZ::OnRegisterAudioSink(
712 const device::BluetoothAdapter::AcquiredCallback& callback, 696 const device::BluetoothAdapter::AcquiredCallback& callback,
713 const device::BluetoothAudioSink::ErrorCallback& error_callback, 697 const device::BluetoothAudioSink::ErrorCallback& error_callback,
714 scoped_refptr<BluetoothAudioSink> audio_sink) { 698 scoped_refptr<BluetoothAudioSink> audio_sink) {
715 if (!IsPresent()) { 699 if (!IsPresent()) {
716 VLOG(1) << "Failed to register audio sink, adapter not present"; 700 VLOG(1) << "Failed to register audio sink, adapter not present";
717 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER); 701 error_callback.Run(BluetoothAudioSink::ERROR_INVALID_ADAPTER);
718 return; 702 return;
719 } 703 }
720 DCHECK(audio_sink.get()); 704 DCHECK(audio_sink.get());
721 callback.Run(audio_sink); 705 callback.Run(audio_sink);
722 } 706 }
723 707
724 BluetoothDeviceChromeOS* 708 BluetoothDeviceBlueZ* BluetoothAdapterBlueZ::GetDeviceWithPath(
725 BluetoothAdapterChromeOS::GetDeviceWithPath(
726 const dbus::ObjectPath& object_path) { 709 const dbus::ObjectPath& object_path) {
727 if (!IsPresent()) 710 if (!IsPresent())
728 return nullptr; 711 return nullptr;
729 712
730 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end(); 713 for (DevicesMap::iterator iter = devices_.begin(); iter != devices_.end();
731 ++iter) { 714 ++iter) {
732 BluetoothDeviceChromeOS* device_chromeos = 715 BluetoothDeviceBlueZ* device_bluez =
733 static_cast<BluetoothDeviceChromeOS*>(iter->second); 716 static_cast<BluetoothDeviceBlueZ*>(iter->second);
734 if (device_chromeos->object_path() == object_path) 717 if (device_bluez->object_path() == object_path)
735 return device_chromeos; 718 return device_bluez;
736 } 719 }
737 720
738 return nullptr; 721 return nullptr;
739 } 722 }
740 723
741 BluetoothPairingChromeOS* BluetoothAdapterChromeOS::GetPairing( 724 BluetoothPairingBlueZ* BluetoothAdapterBlueZ::GetPairing(
742 const dbus::ObjectPath& object_path) { 725 const dbus::ObjectPath& object_path) {
743 DCHECK(IsPresent()); 726 DCHECK(IsPresent());
744 BluetoothDeviceChromeOS* device_chromeos = GetDeviceWithPath(object_path); 727 BluetoothDeviceBlueZ* device_bluez = GetDeviceWithPath(object_path);
745 if (!device_chromeos) { 728 if (!device_bluez) {
746 LOG(WARNING) << "Pairing Agent request for unknown device: " 729 LOG(WARNING) << "Pairing Agent request for unknown device: "
747 << object_path.value(); 730 << object_path.value();
748 return nullptr; 731 return nullptr;
749 } 732 }
750 733
751 BluetoothPairingChromeOS* pairing = device_chromeos->GetPairing(); 734 BluetoothPairingBlueZ* pairing = device_bluez->GetPairing();
752 if (pairing) 735 if (pairing)
753 return pairing; 736 return pairing;
754 737
755 // The device doesn't have its own pairing context, so this is an incoming 738 // The device doesn't have its own pairing context, so this is an incoming
756 // pairing request that should use our best default delegate (if we have one). 739 // pairing request that should use our best default delegate (if we have one).
757 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate(); 740 BluetoothDevice::PairingDelegate* pairing_delegate = DefaultPairingDelegate();
758 if (!pairing_delegate) 741 if (!pairing_delegate)
759 return nullptr; 742 return nullptr;
760 743
761 return device_chromeos->BeginPairing(pairing_delegate); 744 return device_bluez->BeginPairing(pairing_delegate);
762 } 745 }
763 746
764 void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { 747 void BluetoothAdapterBlueZ::SetAdapter(const dbus::ObjectPath& object_path) {
765 DCHECK(!IsPresent()); 748 DCHECK(!IsPresent());
766 DCHECK(!dbus_is_shutdown_); 749 DCHECK(!dbus_is_shutdown_);
767 object_path_ = object_path; 750 object_path_ = object_path;
768 751
769 VLOG(1) << object_path_.value() << ": using adapter."; 752 VLOG(1) << object_path_.value() << ": using adapter.";
770 753
771 VLOG(1) << "Registering pairing agent"; 754 VLOG(1) << "Registering pairing agent";
772 bluez::BluezDBusManager::Get() 755 bluez::BluezDBusManager::Get()
773 ->GetBluetoothAgentManagerClient() 756 ->GetBluetoothAgentManagerClient()
774 ->RegisterAgent( 757 ->RegisterAgent(dbus::ObjectPath(kAgentPath),
775 dbus::ObjectPath(kAgentPath), 758 bluetooth_agent_manager::kKeyboardDisplayCapability,
776 bluetooth_agent_manager::kKeyboardDisplayCapability, 759 base::Bind(&BluetoothAdapterBlueZ::OnRegisterAgent,
777 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgent, 760 weak_ptr_factory_.GetWeakPtr()),
778 weak_ptr_factory_.GetWeakPtr()), 761 base::Bind(&BluetoothAdapterBlueZ::OnRegisterAgentError,
779 base::Bind(&BluetoothAdapterChromeOS::OnRegisterAgentError, 762 weak_ptr_factory_.GetWeakPtr()));
780 weak_ptr_factory_.GetWeakPtr()));
781 763
782 SetDefaultAdapterName(); 764 SetDefaultAdapterName();
783 765
784 bluez::BluetoothAdapterClient::Properties* properties = 766 bluez::BluetoothAdapterClient::Properties* properties =
785 bluez::BluezDBusManager::Get() 767 bluez::BluezDBusManager::Get()
786 ->GetBluetoothAdapterClient() 768 ->GetBluetoothAdapterClient()
787 ->GetProperties(object_path_); 769 ->GetProperties(object_path_);
788 770
789 PresentChanged(true); 771 PresentChanged(true);
790 772
791 if (properties->powered.value()) 773 if (properties->powered.value())
792 PoweredChanged(true); 774 PoweredChanged(true);
793 if (properties->discoverable.value()) 775 if (properties->discoverable.value())
794 DiscoverableChanged(true); 776 DiscoverableChanged(true);
795 if (properties->discovering.value()) 777 if (properties->discovering.value())
796 DiscoveringChanged(true); 778 DiscoveringChanged(true);
797 779
798 std::vector<dbus::ObjectPath> device_paths = 780 std::vector<dbus::ObjectPath> device_paths =
799 bluez::BluezDBusManager::Get() 781 bluez::BluezDBusManager::Get()
800 ->GetBluetoothDeviceClient() 782 ->GetBluetoothDeviceClient()
801 ->GetDevicesForAdapter(object_path_); 783 ->GetDevicesForAdapter(object_path_);
802 784
803 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin(); 785 for (std::vector<dbus::ObjectPath>::iterator iter = device_paths.begin();
804 iter != device_paths.end(); ++iter) { 786 iter != device_paths.end(); ++iter) {
805 DeviceAdded(*iter); 787 DeviceAdded(*iter);
806 } 788 }
807 } 789 }
808 790
809 void BluetoothAdapterChromeOS::SetDefaultAdapterName() { 791 void BluetoothAdapterBlueZ::SetDefaultAdapterName() {
810 DCHECK(IsPresent()); 792 DCHECK(IsPresent());
811 793
812 std::string alias; 794 std::string alias;
795 #if defined(OS_CHROMEOS)
813 switch (chromeos::GetDeviceType()) { 796 switch (chromeos::GetDeviceType()) {
814 case DeviceType::kChromebase: 797 case chromeos::DeviceType::kChromebase:
815 alias = "Chromebase"; 798 alias = "Chromebase";
816 break; 799 break;
817 case DeviceType::kChromebit: 800 case chromeos::DeviceType::kChromebit:
818 alias = "Chromebit"; 801 alias = "Chromebit";
819 break; 802 break;
820 case DeviceType::kChromebook: 803 case chromeos::DeviceType::kChromebook:
821 alias = "Chromebook"; 804 alias = "Chromebook";
822 break; 805 break;
823 case DeviceType::kChromebox: 806 case chromeos::DeviceType::kChromebox:
824 alias = "Chromebox"; 807 alias = "Chromebox";
825 break; 808 break;
826 case DeviceType::kUnknown: 809 case chromeos::DeviceType::kUnknown:
827 alias = "Chromebook"; 810 alias = "Chromebook";
828 break; 811 break;
829 } 812 }
813 #elif defined(OS_LINUX)
814 alias = "ChromeLinux";
815 #endif
830 816
831 SetName(alias, base::Bind(&base::DoNothing), base::Bind(&base::DoNothing)); 817 SetName(alias, base::Bind(&base::DoNothing), base::Bind(&base::DoNothing));
832 } 818 }
833 819
834 void BluetoothAdapterChromeOS::RemoveAdapter() { 820 void BluetoothAdapterBlueZ::RemoveAdapter() {
835 DCHECK(IsPresent()); 821 DCHECK(IsPresent());
836 VLOG(1) << object_path_.value() << ": adapter removed."; 822 VLOG(1) << object_path_.value() << ": adapter removed.";
837 823
838 bluez::BluetoothAdapterClient::Properties* properties = 824 bluez::BluetoothAdapterClient::Properties* properties =
839 bluez::BluezDBusManager::Get() 825 bluez::BluezDBusManager::Get()
840 ->GetBluetoothAdapterClient() 826 ->GetBluetoothAdapterClient()
841 ->GetProperties(object_path_); 827 ->GetProperties(object_path_);
842 828
843 object_path_ = dbus::ObjectPath(""); 829 object_path_ = dbus::ObjectPath("");
844 830
845 if (properties->powered.value()) 831 if (properties->powered.value())
846 PoweredChanged(false); 832 PoweredChanged(false);
847 if (properties->discoverable.value()) 833 if (properties->discoverable.value())
848 DiscoverableChanged(false); 834 DiscoverableChanged(false);
849 if (properties->discovering.value()) 835 if (properties->discovering.value())
850 DiscoveringChanged(false); 836 DiscoveringChanged(false);
851 837
852 // Copy the devices list here and clear the original so that when we 838 // Copy the devices list here and clear the original so that when we
853 // send DeviceRemoved(), GetDevices() returns no devices. 839 // send DeviceRemoved(), GetDevices() returns no devices.
854 DevicesMap devices = devices_; 840 DevicesMap devices = devices_;
855 devices_.clear(); 841 devices_.clear();
856 842
857 for (DevicesMap::iterator iter = devices.begin(); 843 for (DevicesMap::iterator iter = devices.begin(); iter != devices.end();
858 iter != devices.end(); ++iter) { 844 ++iter) {
859 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 845 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
860 DeviceRemoved(this, iter->second)); 846 DeviceRemoved(this, iter->second));
861 delete iter->second; 847 delete iter->second;
862 } 848 }
863 849
864 PresentChanged(false); 850 PresentChanged(false);
865 } 851 }
866 852
867 void BluetoothAdapterChromeOS::PoweredChanged(bool powered) { 853 void BluetoothAdapterBlueZ::PoweredChanged(bool powered) {
868 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 854 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
869 AdapterPoweredChanged(this, powered)); 855 AdapterPoweredChanged(this, powered));
870 } 856 }
871 857
872 void BluetoothAdapterChromeOS::DiscoverableChanged(bool discoverable) { 858 void BluetoothAdapterBlueZ::DiscoverableChanged(bool discoverable) {
873 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 859 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
874 AdapterDiscoverableChanged(this, discoverable)); 860 AdapterDiscoverableChanged(this, discoverable));
875 } 861 }
876 862
877 void BluetoothAdapterChromeOS::DiscoveringChanged( 863 void BluetoothAdapterBlueZ::DiscoveringChanged(bool discovering) {
878 bool discovering) {
879 // If the adapter stopped discovery due to a reason other than a request by 864 // If the adapter stopped discovery due to a reason other than a request by
880 // us, reset the count to 0. 865 // us, reset the count to 0.
881 VLOG(1) << "Discovering changed: " << discovering; 866 VLOG(1) << "Discovering changed: " << discovering;
882 if (!discovering && !discovery_request_pending_ && 867 if (!discovering && !discovery_request_pending_ &&
883 num_discovery_sessions_ > 0) { 868 num_discovery_sessions_ > 0) {
884 VLOG(1) << "Marking sessions as inactive."; 869 VLOG(1) << "Marking sessions as inactive.";
885 num_discovery_sessions_ = 0; 870 num_discovery_sessions_ = 0;
886 MarkDiscoverySessionsAsInactive(); 871 MarkDiscoverySessionsAsInactive();
887 } 872 }
888 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 873 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
889 AdapterDiscoveringChanged(this, discovering)); 874 AdapterDiscoveringChanged(this, discovering));
890 } 875 }
891 876
892 void BluetoothAdapterChromeOS::PresentChanged(bool present) { 877 void BluetoothAdapterBlueZ::PresentChanged(bool present) {
893 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 878 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
894 AdapterPresentChanged(this, present)); 879 AdapterPresentChanged(this, present));
895 } 880 }
896 881
897 void BluetoothAdapterChromeOS::NotifyDeviceChanged( 882 void BluetoothAdapterBlueZ::NotifyDeviceChanged(BluetoothDeviceBlueZ* device) {
898 BluetoothDeviceChromeOS* device) {
899 DCHECK(device); 883 DCHECK(device);
900 DCHECK(device->adapter_ == this); 884 DCHECK(device->adapter_ == this);
901 885
902 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 886 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
903 DeviceChanged(this, device)); 887 DeviceChanged(this, device));
904 } 888 }
905 889
906 void BluetoothAdapterChromeOS::NotifyDeviceAddressChanged( 890 void BluetoothAdapterBlueZ::NotifyDeviceAddressChanged(
907 BluetoothDeviceChromeOS* device, 891 BluetoothDeviceBlueZ* device,
908 const std::string& old_address) { 892 const std::string& old_address) {
909 DCHECK(device->adapter_ == this); 893 DCHECK(device->adapter_ == this);
910 894
911 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, 895 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
912 DeviceAddressChanged(this, device, old_address)); 896 DeviceAddressChanged(this, device, old_address));
913 } 897 }
914 898
915 void BluetoothAdapterChromeOS::NotifyGattServiceAdded( 899 void BluetoothAdapterBlueZ::NotifyGattServiceAdded(
916 BluetoothRemoteGattServiceChromeOS* service) { 900 BluetoothRemoteGattServiceBlueZ* service) {
917 DCHECK_EQ(service->GetAdapter(), this); 901 DCHECK_EQ(service->GetAdapter(), this);
918 DCHECK_EQ( 902 DCHECK_EQ(static_cast<BluetoothDeviceBlueZ*>(service->GetDevice())->adapter_,
919 static_cast<BluetoothDeviceChromeOS*>(service->GetDevice())->adapter_, 903 this);
920 this);
921 904
922 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 905 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
923 observers_,
924 GattServiceAdded(this, service->GetDevice(), service)); 906 GattServiceAdded(this, service->GetDevice(), service));
925 } 907 }
926 908
927 void BluetoothAdapterChromeOS::NotifyGattServiceRemoved( 909 void BluetoothAdapterBlueZ::NotifyGattServiceRemoved(
928 BluetoothRemoteGattServiceChromeOS* service) { 910 BluetoothRemoteGattServiceBlueZ* service) {
929 DCHECK_EQ(service->GetAdapter(), this); 911 DCHECK_EQ(service->GetAdapter(), this);
930 DCHECK_EQ( 912 DCHECK_EQ(static_cast<BluetoothDeviceBlueZ*>(service->GetDevice())->adapter_,
931 static_cast<BluetoothDeviceChromeOS*>(service->GetDevice())->adapter_, 913 this);
932 this);
933 914
934 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 915 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
935 observers_,
936 GattServiceRemoved(this, service->GetDevice(), service)); 916 GattServiceRemoved(this, service->GetDevice(), service));
937 } 917 }
938 918
939 void BluetoothAdapterChromeOS::NotifyGattServiceChanged( 919 void BluetoothAdapterBlueZ::NotifyGattServiceChanged(
940 BluetoothRemoteGattServiceChromeOS* service) { 920 BluetoothRemoteGattServiceBlueZ* service) {
941 DCHECK_EQ(service->GetAdapter(), this); 921 DCHECK_EQ(service->GetAdapter(), this);
942 922
943 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 923 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
944 observers_,
945 GattServiceChanged(this, service)); 924 GattServiceChanged(this, service));
946 } 925 }
947 926
948 void BluetoothAdapterChromeOS::NotifyGattDiscoveryComplete( 927 void BluetoothAdapterBlueZ::NotifyGattDiscoveryComplete(
949 BluetoothRemoteGattServiceChromeOS* service) { 928 BluetoothRemoteGattServiceBlueZ* service) {
950 DCHECK_EQ(service->GetAdapter(), this); 929 DCHECK_EQ(service->GetAdapter(), this);
951 930
952 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 931 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
953 observers_,
954 GattDiscoveryCompleteForService(this, service)); 932 GattDiscoveryCompleteForService(this, service));
955 } 933 }
956 934
957 void BluetoothAdapterChromeOS::NotifyGattCharacteristicAdded( 935 void BluetoothAdapterBlueZ::NotifyGattCharacteristicAdded(
958 BluetoothRemoteGattCharacteristicChromeOS* characteristic) { 936 BluetoothRemoteGattCharacteristicBlueZ* characteristic) {
959 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 937 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
960 characteristic->GetService())->GetAdapter(), 938 characteristic->GetService())
939 ->GetAdapter(),
961 this); 940 this);
962 941
963 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 942 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
964 observers_,
965 GattCharacteristicAdded(this, characteristic)); 943 GattCharacteristicAdded(this, characteristic));
966 } 944 }
967 945
968 void BluetoothAdapterChromeOS::NotifyGattCharacteristicRemoved( 946 void BluetoothAdapterBlueZ::NotifyGattCharacteristicRemoved(
969 BluetoothRemoteGattCharacteristicChromeOS* characteristic) { 947 BluetoothRemoteGattCharacteristicBlueZ* characteristic) {
970 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 948 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
971 characteristic->GetService())->GetAdapter(), 949 characteristic->GetService())
950 ->GetAdapter(),
972 this); 951 this);
973 952
974 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 953 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
975 observers_,
976 GattCharacteristicRemoved(this, characteristic)); 954 GattCharacteristicRemoved(this, characteristic));
977 } 955 }
978 956
979 void BluetoothAdapterChromeOS::NotifyGattDescriptorAdded( 957 void BluetoothAdapterBlueZ::NotifyGattDescriptorAdded(
980 BluetoothRemoteGattDescriptorChromeOS* descriptor) { 958 BluetoothRemoteGattDescriptorBlueZ* descriptor) {
981 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 959 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
982 descriptor->GetCharacteristic()->GetService())->GetAdapter(), 960 descriptor->GetCharacteristic()->GetService())
961 ->GetAdapter(),
983 this); 962 this);
984 963
985 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 964 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
986 observers_,
987 GattDescriptorAdded(this, descriptor)); 965 GattDescriptorAdded(this, descriptor));
988 } 966 }
989 967
990 void BluetoothAdapterChromeOS::NotifyGattDescriptorRemoved( 968 void BluetoothAdapterBlueZ::NotifyGattDescriptorRemoved(
991 BluetoothRemoteGattDescriptorChromeOS* descriptor) { 969 BluetoothRemoteGattDescriptorBlueZ* descriptor) {
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 GattDescriptorRemoved(this, descriptor)); 976 GattDescriptorRemoved(this, descriptor));
999 } 977 }
1000 978
1001 void BluetoothAdapterChromeOS::NotifyGattCharacteristicValueChanged( 979 void BluetoothAdapterBlueZ::NotifyGattCharacteristicValueChanged(
1002 BluetoothRemoteGattCharacteristicChromeOS* characteristic, 980 BluetoothRemoteGattCharacteristicBlueZ* characteristic,
1003 const std::vector<uint8>& value) { 981 const std::vector<uint8>& value) {
1004 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 982 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
1005 characteristic->GetService())->GetAdapter(), 983 characteristic->GetService())
984 ->GetAdapter(),
1006 this); 985 this);
1007 986
1008 FOR_EACH_OBSERVER( 987 FOR_EACH_OBSERVER(
1009 BluetoothAdapter::Observer, 988 BluetoothAdapter::Observer, observers_,
1010 observers_,
1011 GattCharacteristicValueChanged(this, characteristic, value)); 989 GattCharacteristicValueChanged(this, characteristic, value));
1012 } 990 }
1013 991
1014 void BluetoothAdapterChromeOS::NotifyGattDescriptorValueChanged( 992 void BluetoothAdapterBlueZ::NotifyGattDescriptorValueChanged(
1015 BluetoothRemoteGattDescriptorChromeOS* descriptor, 993 BluetoothRemoteGattDescriptorBlueZ* descriptor,
1016 const std::vector<uint8>& value) { 994 const std::vector<uint8>& value) {
1017 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceChromeOS*>( 995 DCHECK_EQ(static_cast<BluetoothRemoteGattServiceBlueZ*>(
1018 descriptor->GetCharacteristic()->GetService())->GetAdapter(), 996 descriptor->GetCharacteristic()->GetService())
997 ->GetAdapter(),
1019 this); 998 this);
1020 999
1021 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, 1000 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
1022 observers_,
1023 GattDescriptorValueChanged(this, descriptor, value)); 1001 GattDescriptorValueChanged(this, descriptor, value));
1024 } 1002 }
1025 1003
1026 void BluetoothAdapterChromeOS::UseProfile( 1004 void BluetoothAdapterBlueZ::UseProfile(
1027 const BluetoothUUID& uuid, 1005 const BluetoothUUID& uuid,
1028 const dbus::ObjectPath& device_path, 1006 const dbus::ObjectPath& device_path,
1029 const bluez::BluetoothProfileManagerClient::Options& options, 1007 const bluez::BluetoothProfileManagerClient::Options& options,
1030 bluez::BluetoothProfileServiceProvider::Delegate* delegate, 1008 bluez::BluetoothProfileServiceProvider::Delegate* delegate,
1031 const ProfileRegisteredCallback& success_callback, 1009 const ProfileRegisteredCallback& success_callback,
1032 const ErrorCompletionCallback& error_callback) { 1010 const ErrorCompletionCallback& error_callback) {
1033 DCHECK(delegate); 1011 DCHECK(delegate);
1034 1012
1035 if (!IsPresent()) { 1013 if (!IsPresent()) {
1036 VLOG(2) << "Adapter not present, erroring out"; 1014 VLOG(2) << "Adapter not present, erroring out";
1037 error_callback.Run("Adapter not present"); 1015 error_callback.Run("Adapter not present");
1038 return; 1016 return;
1039 } 1017 }
1040 1018
1041 if (profiles_.find(uuid) != profiles_.end()) { 1019 if (profiles_.find(uuid) != profiles_.end()) {
1042 // TODO(jamuraa) check that the options are the same and error when they are 1020 // TODO(jamuraa) check that the options are the same and error when they are
1043 // not. 1021 // not.
1044 SetProfileDelegate(uuid, device_path, delegate, success_callback, 1022 SetProfileDelegate(uuid, device_path, delegate, success_callback,
1045 error_callback); 1023 error_callback);
1046 return; 1024 return;
1047 } 1025 }
1048 1026
1049 if (profile_queues_.find(uuid) == profile_queues_.end()) { 1027 if (profile_queues_.find(uuid) == profile_queues_.end()) {
1050 BluetoothAdapterProfileChromeOS::Register( 1028 BluetoothAdapterProfileBlueZ::Register(
1051 uuid, options, 1029 uuid, options,
1052 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfile, this, uuid), 1030 base::Bind(&BluetoothAdapterBlueZ::OnRegisterProfile, this, uuid),
1053 base::Bind(&BluetoothAdapterChromeOS::OnRegisterProfileError, this, 1031 base::Bind(&BluetoothAdapterBlueZ::OnRegisterProfileError, this, uuid));
1054 uuid));
1055 1032
1056 profile_queues_[uuid] = new std::vector<RegisterProfileCompletionPair>(); 1033 profile_queues_[uuid] = new std::vector<RegisterProfileCompletionPair>();
1057 } 1034 }
1058 1035
1059 profile_queues_[uuid]->push_back(std::make_pair( 1036 profile_queues_[uuid]->push_back(std::make_pair(
1060 base::Bind(&BluetoothAdapterChromeOS::SetProfileDelegate, this, uuid, 1037 base::Bind(&BluetoothAdapterBlueZ::SetProfileDelegate, this, uuid,
1061 device_path, delegate, success_callback, error_callback), 1038 device_path, delegate, success_callback, error_callback),
1062 error_callback)); 1039 error_callback));
1063 } 1040 }
1064 1041
1065 void BluetoothAdapterChromeOS::ReleaseProfile( 1042 void BluetoothAdapterBlueZ::ReleaseProfile(
1066 const dbus::ObjectPath& device_path, 1043 const dbus::ObjectPath& device_path,
1067 BluetoothAdapterProfileChromeOS* profile) { 1044 BluetoothAdapterProfileBlueZ* profile) {
1068 VLOG(2) << "Releasing Profile: " << profile->uuid().canonical_value() 1045 VLOG(2) << "Releasing Profile: " << profile->uuid().canonical_value()
1069 << " from " << device_path.value(); 1046 << " from " << device_path.value();
1070 profile->RemoveDelegate( 1047 profile->RemoveDelegate(
1071 device_path, base::Bind(&BluetoothAdapterChromeOS::RemoveProfile, 1048 device_path, base::Bind(&BluetoothAdapterBlueZ::RemoveProfile,
1072 weak_ptr_factory_.GetWeakPtr(), profile->uuid())); 1049 weak_ptr_factory_.GetWeakPtr(), profile->uuid()));
1073 } 1050 }
1074 1051
1075 void BluetoothAdapterChromeOS::RemoveProfile(const BluetoothUUID& uuid) { 1052 void BluetoothAdapterBlueZ::RemoveProfile(const BluetoothUUID& uuid) {
1076 VLOG(2) << "Remove Profile: " << uuid.canonical_value(); 1053 VLOG(2) << "Remove Profile: " << uuid.canonical_value();
1077 1054
1078 if (profiles_.find(uuid) != profiles_.end()) { 1055 if (profiles_.find(uuid) != profiles_.end()) {
1079 delete profiles_[uuid]; 1056 delete profiles_[uuid];
1080 profiles_.erase(uuid); 1057 profiles_.erase(uuid);
1081 } 1058 }
1082 } 1059 }
1083 1060
1084 void BluetoothAdapterChromeOS::OnRegisterProfile( 1061 void BluetoothAdapterBlueZ::OnRegisterProfile(
1085 const BluetoothUUID& uuid, 1062 const BluetoothUUID& uuid,
1086 scoped_ptr<BluetoothAdapterProfileChromeOS> profile) { 1063 scoped_ptr<BluetoothAdapterProfileBlueZ> profile) {
1087 profiles_[uuid] = profile.release(); 1064 profiles_[uuid] = profile.release();
1088 1065
1089 if (profile_queues_.find(uuid) == profile_queues_.end()) 1066 if (profile_queues_.find(uuid) == profile_queues_.end())
1090 return; 1067 return;
1091 1068
1092 for (auto& it : *profile_queues_[uuid]) 1069 for (auto& it : *profile_queues_[uuid])
1093 it.first.Run(); 1070 it.first.Run();
1094 delete profile_queues_[uuid]; 1071 delete profile_queues_[uuid];
1095 profile_queues_.erase(uuid); 1072 profile_queues_.erase(uuid);
1096 } 1073 }
1097 1074
1098 void BluetoothAdapterChromeOS::SetProfileDelegate( 1075 void BluetoothAdapterBlueZ::SetProfileDelegate(
1099 const BluetoothUUID& uuid, 1076 const BluetoothUUID& uuid,
1100 const dbus::ObjectPath& device_path, 1077 const dbus::ObjectPath& device_path,
1101 bluez::BluetoothProfileServiceProvider::Delegate* delegate, 1078 bluez::BluetoothProfileServiceProvider::Delegate* delegate,
1102 const ProfileRegisteredCallback& success_callback, 1079 const ProfileRegisteredCallback& success_callback,
1103 const ErrorCompletionCallback& error_callback) { 1080 const ErrorCompletionCallback& error_callback) {
1104 if (profiles_.find(uuid) == profiles_.end()) { 1081 if (profiles_.find(uuid) == profiles_.end()) {
1105 error_callback.Run("Cannot find profile!"); 1082 error_callback.Run("Cannot find profile!");
1106 return; 1083 return;
1107 } 1084 }
1108 1085
1109 if (profiles_[uuid]->SetDelegate(device_path, delegate)) { 1086 if (profiles_[uuid]->SetDelegate(device_path, delegate)) {
1110 success_callback.Run(profiles_[uuid]); 1087 success_callback.Run(profiles_[uuid]);
1111 return; 1088 return;
1112 } 1089 }
1113 // Already set 1090 // Already set
1114 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists); 1091 error_callback.Run(bluetooth_agent_manager::kErrorAlreadyExists);
1115 } 1092 }
1116 1093
1117 void BluetoothAdapterChromeOS::OnRegisterProfileError( 1094 void BluetoothAdapterBlueZ::OnRegisterProfileError(
1118 const BluetoothUUID& uuid, 1095 const BluetoothUUID& uuid,
1119 const std::string& error_name, 1096 const std::string& error_name,
1120 const std::string& error_message) { 1097 const std::string& error_message) {
1121 VLOG(2) << object_path_.value() << ": Failed to register profile: " 1098 VLOG(2) << object_path_.value()
1122 << error_name << ": " << error_message; 1099 << ": Failed to register profile: " << error_name << ": "
1100 << error_message;
1123 if (profile_queues_.find(uuid) == profile_queues_.end()) 1101 if (profile_queues_.find(uuid) == profile_queues_.end())
1124 return; 1102 return;
1125 1103
1126 for (auto& it : *profile_queues_[uuid]) 1104 for (auto& it : *profile_queues_[uuid])
1127 it.second.Run(error_message); 1105 it.second.Run(error_message);
1128 1106
1129 delete profile_queues_[uuid]; 1107 delete profile_queues_[uuid];
1130 profile_queues_.erase(uuid); 1108 profile_queues_.erase(uuid);
1131 } 1109 }
1132 1110
1133 void BluetoothAdapterChromeOS::OnSetDiscoverable( 1111 void BluetoothAdapterBlueZ::OnSetDiscoverable(
1134 const base::Closure& callback, 1112 const base::Closure& callback,
1135 const ErrorCallback& error_callback, 1113 const ErrorCallback& error_callback,
1136 bool success) { 1114 bool success) {
1137 if (!IsPresent()) { 1115 if (!IsPresent()) {
1138 error_callback.Run(); 1116 error_callback.Run();
1139 return; 1117 return;
1140 } 1118 }
1141 1119
1142 // Set the discoverable_timeout property to zero so the adapter remains 1120 // Set the discoverable_timeout property to zero so the adapter remains
1143 // discoverable forever. 1121 // discoverable forever.
1144 bluez::BluezDBusManager::Get() 1122 bluez::BluezDBusManager::Get()
1145 ->GetBluetoothAdapterClient() 1123 ->GetBluetoothAdapterClient()
1146 ->GetProperties(object_path_) 1124 ->GetProperties(object_path_)
1147 ->discoverable_timeout.Set( 1125 ->discoverable_timeout.Set(
1148 0, 1126 0,
1149 base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, 1127 base::Bind(&BluetoothAdapterBlueZ::OnPropertyChangeCompleted,
1150 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1128 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1151 } 1129 }
1152 1130
1153 void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( 1131 void BluetoothAdapterBlueZ::OnPropertyChangeCompleted(
1154 const base::Closure& callback, 1132 const base::Closure& callback,
1155 const ErrorCallback& error_callback, 1133 const ErrorCallback& error_callback,
1156 bool success) { 1134 bool success) {
1157 if (IsPresent() && success) { 1135 if (IsPresent() && success) {
1158 callback.Run(); 1136 callback.Run();
1159 } else { 1137 } else {
1160 error_callback.Run(); 1138 error_callback.Run();
1161 } 1139 }
1162 } 1140 }
1163 1141
1164 void BluetoothAdapterChromeOS::AddDiscoverySession( 1142 void BluetoothAdapterBlueZ::AddDiscoverySession(
1165 BluetoothDiscoveryFilter* discovery_filter, 1143 BluetoothDiscoveryFilter* discovery_filter,
1166 const base::Closure& callback, 1144 const base::Closure& callback,
1167 const DiscoverySessionErrorCallback& error_callback) { 1145 const DiscoverySessionErrorCallback& error_callback) {
1168 if (!IsPresent()) { 1146 if (!IsPresent()) {
1169 error_callback.Run( 1147 error_callback.Run(
1170 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT); 1148 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT);
1171 return; 1149 return;
1172 } 1150 }
1173 VLOG(1) << __func__; 1151 VLOG(1) << __func__;
1174 if (discovery_request_pending_) { 1152 if (discovery_request_pending_) {
(...skipping 22 matching lines...) Expand all
1197 DCHECK_EQ(num_discovery_sessions_, 0); 1175 DCHECK_EQ(num_discovery_sessions_, 0);
1198 1176
1199 if (discovery_filter) { 1177 if (discovery_filter) {
1200 discovery_request_pending_ = true; 1178 discovery_request_pending_ = true;
1201 1179
1202 scoped_ptr<BluetoothDiscoveryFilter> df(new BluetoothDiscoveryFilter( 1180 scoped_ptr<BluetoothDiscoveryFilter> df(new BluetoothDiscoveryFilter(
1203 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL)); 1181 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL));
1204 df->CopyFrom(*discovery_filter); 1182 df->CopyFrom(*discovery_filter);
1205 SetDiscoveryFilter( 1183 SetDiscoveryFilter(
1206 df.Pass(), 1184 df.Pass(),
1207 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter, 1185 base::Bind(&BluetoothAdapterBlueZ::OnPreSetDiscoveryFilter,
1208 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), 1186 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
1209 base::Bind(&BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError, 1187 base::Bind(&BluetoothAdapterBlueZ::OnPreSetDiscoveryFilterError,
1210 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1188 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1211 return; 1189 return;
1212 } else { 1190 } else {
1213 current_filter_.reset(); 1191 current_filter_.reset();
1214 } 1192 }
1215 1193
1216 // This is the first request to start device discovery. 1194 // This is the first request to start device discovery.
1217 discovery_request_pending_ = true; 1195 discovery_request_pending_ = true;
1218 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery( 1196 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery(
1219 object_path_, 1197 object_path_,
1220 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, 1198 base::Bind(&BluetoothAdapterBlueZ::OnStartDiscovery,
1221 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), 1199 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
1222 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, 1200 base::Bind(&BluetoothAdapterBlueZ::OnStartDiscoveryError,
1223 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1201 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1224 } 1202 }
1225 1203
1226 void BluetoothAdapterChromeOS::RemoveDiscoverySession( 1204 void BluetoothAdapterBlueZ::RemoveDiscoverySession(
1227 BluetoothDiscoveryFilter* discovery_filter, 1205 BluetoothDiscoveryFilter* discovery_filter,
1228 const base::Closure& callback, 1206 const base::Closure& callback,
1229 const DiscoverySessionErrorCallback& error_callback) { 1207 const DiscoverySessionErrorCallback& error_callback) {
1230 if (!IsPresent()) { 1208 if (!IsPresent()) {
1231 error_callback.Run( 1209 error_callback.Run(
1232 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT); 1210 UMABluetoothDiscoverySessionOutcome::ADAPTER_NOT_PRESENT);
1233 return; 1211 return;
1234 } 1212 }
1235 1213
1236 VLOG(1) << __func__; 1214 VLOG(1) << __func__;
(...skipping 26 matching lines...) Expand all
1263 error_callback.Run( 1241 error_callback.Run(
1264 UMABluetoothDiscoverySessionOutcome::ACTIVE_SESSION_NOT_IN_ADAPTER); 1242 UMABluetoothDiscoverySessionOutcome::ACTIVE_SESSION_NOT_IN_ADAPTER);
1265 return; 1243 return;
1266 } 1244 }
1267 1245
1268 // There is exactly one active discovery session. Request BlueZ to stop 1246 // There is exactly one active discovery session. Request BlueZ to stop
1269 // discovery. 1247 // discovery.
1270 DCHECK_EQ(num_discovery_sessions_, 1); 1248 DCHECK_EQ(num_discovery_sessions_, 1);
1271 discovery_request_pending_ = true; 1249 discovery_request_pending_ = true;
1272 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StopDiscovery( 1250 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StopDiscovery(
1273 object_path_, base::Bind(&BluetoothAdapterChromeOS::OnStopDiscovery, 1251 object_path_, base::Bind(&BluetoothAdapterBlueZ::OnStopDiscovery,
1274 weak_ptr_factory_.GetWeakPtr(), callback), 1252 weak_ptr_factory_.GetWeakPtr(), callback),
1275 base::Bind(&BluetoothAdapterChromeOS::OnStopDiscoveryError, 1253 base::Bind(&BluetoothAdapterBlueZ::OnStopDiscoveryError,
1276 weak_ptr_factory_.GetWeakPtr(), error_callback)); 1254 weak_ptr_factory_.GetWeakPtr(), error_callback));
1277 } 1255 }
1278 1256
1279 void BluetoothAdapterChromeOS::SetDiscoveryFilter( 1257 void BluetoothAdapterBlueZ::SetDiscoveryFilter(
1280 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter, 1258 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
1281 const base::Closure& callback, 1259 const base::Closure& callback,
1282 const DiscoverySessionErrorCallback& error_callback) { 1260 const DiscoverySessionErrorCallback& error_callback) {
1283 if (!IsPresent()) { 1261 if (!IsPresent()) {
1284 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); 1262 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED);
1285 return; 1263 return;
1286 } 1264 }
1287 1265
1288 // If old and new filter are equal (null) then don't make request, just call 1266 // If old and new filter are equal (null) then don't make request, just call
1289 // succes callback 1267 // succes callback
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1334 1312
1335 for (const auto& it : uuids) 1313 for (const auto& it : uuids)
1336 dbus_discovery_filter.uuids.get()->push_back(it.value()); 1314 dbus_discovery_filter.uuids.get()->push_back(it.value());
1337 } 1315 }
1338 } 1316 }
1339 1317
1340 bluez::BluezDBusManager::Get() 1318 bluez::BluezDBusManager::Get()
1341 ->GetBluetoothAdapterClient() 1319 ->GetBluetoothAdapterClient()
1342 ->SetDiscoveryFilter( 1320 ->SetDiscoveryFilter(
1343 object_path_, dbus_discovery_filter, 1321 object_path_, dbus_discovery_filter,
1344 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilter, 1322 base::Bind(&BluetoothAdapterBlueZ::OnSetDiscoveryFilter,
1345 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), 1323 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
1346 base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoveryFilterError, 1324 base::Bind(&BluetoothAdapterBlueZ::OnSetDiscoveryFilterError,
1347 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1325 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1348 } 1326 }
1349 1327
1350 void BluetoothAdapterChromeOS::OnStartDiscovery( 1328 void BluetoothAdapterBlueZ::OnStartDiscovery(
1351 const base::Closure& callback, 1329 const base::Closure& callback,
1352 const DiscoverySessionErrorCallback& error_callback) { 1330 const DiscoverySessionErrorCallback& error_callback) {
1353 // Report success on the original request and increment the count. 1331 // Report success on the original request and increment the count.
1354 VLOG(1) << __func__; 1332 VLOG(1) << __func__;
1355 DCHECK(discovery_request_pending_); 1333 DCHECK(discovery_request_pending_);
1356 DCHECK_EQ(num_discovery_sessions_, 0); 1334 DCHECK_EQ(num_discovery_sessions_, 0);
1357 discovery_request_pending_ = false; 1335 discovery_request_pending_ = false;
1358 num_discovery_sessions_++; 1336 num_discovery_sessions_++;
1359 if (IsPresent()) { 1337 if (IsPresent()) {
1360 callback.Run(); 1338 callback.Run();
1361 } else { 1339 } else {
1362 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); 1340 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED);
1363 } 1341 }
1364 1342
1365 // Try to add a new discovery session for each queued request. 1343 // Try to add a new discovery session for each queued request.
1366 ProcessQueuedDiscoveryRequests(); 1344 ProcessQueuedDiscoveryRequests();
1367 } 1345 }
1368 1346
1369 void BluetoothAdapterChromeOS::OnStartDiscoveryError( 1347 void BluetoothAdapterBlueZ::OnStartDiscoveryError(
1370 const base::Closure& callback, 1348 const base::Closure& callback,
1371 const DiscoverySessionErrorCallback& error_callback, 1349 const DiscoverySessionErrorCallback& error_callback,
1372 const std::string& error_name, 1350 const std::string& error_name,
1373 const std::string& error_message) { 1351 const std::string& error_message) {
1374 LOG(WARNING) << object_path_.value() << ": Failed to start discovery: " 1352 LOG(WARNING) << object_path_.value()
1375 << error_name << ": " << error_message; 1353 << ": Failed to start discovery: " << error_name << ": "
1354 << error_message;
1376 1355
1377 // Failed to start discovery. This can only happen if the count is at 0. 1356 // Failed to start discovery. This can only happen if the count is at 0.
1378 DCHECK_EQ(num_discovery_sessions_, 0); 1357 DCHECK_EQ(num_discovery_sessions_, 0);
1379 DCHECK(discovery_request_pending_); 1358 DCHECK(discovery_request_pending_);
1380 discovery_request_pending_ = false; 1359 discovery_request_pending_ = false;
1381 1360
1382 // Discovery request may fail if discovery was previously initiated by Chrome, 1361 // Discovery request may fail if discovery was previously initiated by Chrome,
1383 // but the session were invalidated due to the discovery state unexpectedly 1362 // but the session were invalidated due to the discovery state unexpectedly
1384 // changing to false and then back to true. In this case, report success. 1363 // changing to false and then back to true. In this case, report success.
1385 if (IsPresent() && error_name == bluetooth_device::kErrorInProgress && 1364 if (IsPresent() && error_name == bluetooth_device::kErrorInProgress &&
1386 IsDiscovering()) { 1365 IsDiscovering()) {
1387 VLOG(1) << "Discovery previously initiated. Reporting success."; 1366 VLOG(1) << "Discovery previously initiated. Reporting success.";
1388 num_discovery_sessions_++; 1367 num_discovery_sessions_++;
1389 callback.Run(); 1368 callback.Run();
1390 } else { 1369 } else {
1391 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name)); 1370 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name));
1392 } 1371 }
1393 1372
1394 // Try to add a new discovery session for each queued request. 1373 // Try to add a new discovery session for each queued request.
1395 ProcessQueuedDiscoveryRequests(); 1374 ProcessQueuedDiscoveryRequests();
1396 } 1375 }
1397 1376
1398 void BluetoothAdapterChromeOS::OnStopDiscovery(const base::Closure& callback) { 1377 void BluetoothAdapterBlueZ::OnStopDiscovery(const base::Closure& callback) {
1399 // Report success on the original request and decrement the count. 1378 // Report success on the original request and decrement the count.
1400 VLOG(1) << __func__; 1379 VLOG(1) << __func__;
1401 DCHECK(discovery_request_pending_); 1380 DCHECK(discovery_request_pending_);
1402 DCHECK_EQ(num_discovery_sessions_, 1); 1381 DCHECK_EQ(num_discovery_sessions_, 1);
1403 discovery_request_pending_ = false; 1382 discovery_request_pending_ = false;
1404 num_discovery_sessions_--; 1383 num_discovery_sessions_--;
1405 callback.Run(); 1384 callback.Run();
1406 1385
1407 current_filter_.reset(); 1386 current_filter_.reset();
1408 1387
1409 // Try to add a new discovery session for each queued request. 1388 // Try to add a new discovery session for each queued request.
1410 ProcessQueuedDiscoveryRequests(); 1389 ProcessQueuedDiscoveryRequests();
1411 } 1390 }
1412 1391
1413 void BluetoothAdapterChromeOS::OnStopDiscoveryError( 1392 void BluetoothAdapterBlueZ::OnStopDiscoveryError(
1414 const DiscoverySessionErrorCallback& error_callback, 1393 const DiscoverySessionErrorCallback& error_callback,
1415 const std::string& error_name, 1394 const std::string& error_name,
1416 const std::string& error_message) { 1395 const std::string& error_message) {
1417 LOG(WARNING) << object_path_.value() << ": Failed to stop discovery: " 1396 LOG(WARNING) << object_path_.value()
1418 << error_name << ": " << error_message; 1397 << ": Failed to stop discovery: " << error_name << ": "
1398 << error_message;
1419 1399
1420 // Failed to stop discovery. This can only happen if the count is at 1. 1400 // Failed to stop discovery. This can only happen if the count is at 1.
1421 DCHECK(discovery_request_pending_); 1401 DCHECK(discovery_request_pending_);
1422 DCHECK_EQ(num_discovery_sessions_, 1); 1402 DCHECK_EQ(num_discovery_sessions_, 1);
1423 discovery_request_pending_ = false; 1403 discovery_request_pending_ = false;
1424 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name)); 1404 error_callback.Run(TranslateDiscoveryErrorToUMA(error_name));
1425 1405
1426 // Try to add a new discovery session for each queued request. 1406 // Try to add a new discovery session for each queued request.
1427 ProcessQueuedDiscoveryRequests(); 1407 ProcessQueuedDiscoveryRequests();
1428 } 1408 }
1429 1409
1430 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilter( 1410 void BluetoothAdapterBlueZ::OnPreSetDiscoveryFilter(
1431 const base::Closure& callback, 1411 const base::Closure& callback,
1432 const DiscoverySessionErrorCallback& error_callback) { 1412 const DiscoverySessionErrorCallback& error_callback) {
1433 // This is the first request to start device discovery. 1413 // This is the first request to start device discovery.
1434 DCHECK(discovery_request_pending_); 1414 DCHECK(discovery_request_pending_);
1435 DCHECK_EQ(num_discovery_sessions_, 0); 1415 DCHECK_EQ(num_discovery_sessions_, 0);
1436 1416
1437 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery( 1417 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()->StartDiscovery(
1438 object_path_, 1418 object_path_,
1439 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscovery, 1419 base::Bind(&BluetoothAdapterBlueZ::OnStartDiscovery,
1440 weak_ptr_factory_.GetWeakPtr(), callback, error_callback), 1420 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
1441 base::Bind(&BluetoothAdapterChromeOS::OnStartDiscoveryError, 1421 base::Bind(&BluetoothAdapterBlueZ::OnStartDiscoveryError,
1442 weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); 1422 weak_ptr_factory_.GetWeakPtr(), callback, error_callback));
1443 } 1423 }
1444 1424
1445 void BluetoothAdapterChromeOS::OnPreSetDiscoveryFilterError( 1425 void BluetoothAdapterBlueZ::OnPreSetDiscoveryFilterError(
1446 const base::Closure& callback, 1426 const base::Closure& callback,
1447 const DiscoverySessionErrorCallback& error_callback, 1427 const DiscoverySessionErrorCallback& error_callback,
1448 UMABluetoothDiscoverySessionOutcome outcome) { 1428 UMABluetoothDiscoverySessionOutcome outcome) {
1449 LOG(WARNING) << object_path_.value() 1429 LOG(WARNING) << object_path_.value()
1450 << ": Failed to pre set discovery filter."; 1430 << ": Failed to pre set discovery filter.";
1451 1431
1452 // Failed to start discovery. This can only happen if the count is at 0. 1432 // Failed to start discovery. This can only happen if the count is at 0.
1453 DCHECK_EQ(num_discovery_sessions_, 0); 1433 DCHECK_EQ(num_discovery_sessions_, 0);
1454 DCHECK(discovery_request_pending_); 1434 DCHECK(discovery_request_pending_);
1455 discovery_request_pending_ = false; 1435 discovery_request_pending_ = false;
1456 1436
1457 error_callback.Run(outcome); 1437 error_callback.Run(outcome);
1458 1438
1459 // Try to add a new discovery session for each queued request. 1439 // Try to add a new discovery session for each queued request.
1460 ProcessQueuedDiscoveryRequests(); 1440 ProcessQueuedDiscoveryRequests();
1461 } 1441 }
1462 1442
1463 void BluetoothAdapterChromeOS::OnSetDiscoveryFilter( 1443 void BluetoothAdapterBlueZ::OnSetDiscoveryFilter(
1464 const base::Closure& callback, 1444 const base::Closure& callback,
1465 const DiscoverySessionErrorCallback& error_callback) { 1445 const DiscoverySessionErrorCallback& error_callback) {
1466 // Report success on the original request and increment the count. 1446 // Report success on the original request and increment the count.
1467 VLOG(1) << __func__; 1447 VLOG(1) << __func__;
1468 if (IsPresent()) { 1448 if (IsPresent()) {
1469 callback.Run(); 1449 callback.Run();
1470 } else { 1450 } else {
1471 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED); 1451 error_callback.Run(UMABluetoothDiscoverySessionOutcome::ADAPTER_REMOVED);
1472 } 1452 }
1473 } 1453 }
1474 1454
1475 void BluetoothAdapterChromeOS::OnSetDiscoveryFilterError( 1455 void BluetoothAdapterBlueZ::OnSetDiscoveryFilterError(
1476 const base::Closure& callback, 1456 const base::Closure& callback,
1477 const DiscoverySessionErrorCallback& error_callback, 1457 const DiscoverySessionErrorCallback& error_callback,
1478 const std::string& error_name, 1458 const std::string& error_name,
1479 const std::string& error_message) { 1459 const std::string& error_message) {
1480 LOG(WARNING) << object_path_.value() 1460 LOG(WARNING) << object_path_.value()
1481 << ": Failed to set discovery filter: " << error_name << ": " 1461 << ": Failed to set discovery filter: " << error_name << ": "
1482 << error_message; 1462 << error_message;
1483 1463
1484 UMABluetoothDiscoverySessionOutcome outcome = 1464 UMABluetoothDiscoverySessionOutcome outcome =
1485 TranslateDiscoveryErrorToUMA(error_name); 1465 TranslateDiscoveryErrorToUMA(error_name);
1486 if (outcome == UMABluetoothDiscoverySessionOutcome::FAILED) { 1466 if (outcome == UMABluetoothDiscoverySessionOutcome::FAILED) {
1487 // bluez/doc/adapter-api.txt says "Failed" is returned from 1467 // bluez/doc/adapter-api.txt says "Failed" is returned from
1488 // SetDiscoveryFilter when the controller doesn't support the requested 1468 // SetDiscoveryFilter when the controller doesn't support the requested
1489 // transport. 1469 // transport.
1490 outcome = UMABluetoothDiscoverySessionOutcome:: 1470 outcome = UMABluetoothDiscoverySessionOutcome::
1491 CHROMEOS_DBUS_FAILED_MAYBE_UNSUPPORTED_TRANSPORT; 1471 BLUEZ_DBUS_FAILED_MAYBE_UNSUPPORTED_TRANSPORT;
1492 } 1472 }
1493 error_callback.Run(outcome); 1473 error_callback.Run(outcome);
1494 1474
1495 // Try to add a new discovery session for each queued request. 1475 // Try to add a new discovery session for each queued request.
1496 ProcessQueuedDiscoveryRequests(); 1476 ProcessQueuedDiscoveryRequests();
1497 } 1477 }
1498 1478
1499 void BluetoothAdapterChromeOS::ProcessQueuedDiscoveryRequests() { 1479 void BluetoothAdapterBlueZ::ProcessQueuedDiscoveryRequests() {
1500 while (!discovery_request_queue_.empty()) { 1480 while (!discovery_request_queue_.empty()) {
1501 VLOG(1) << "Process queued discovery request."; 1481 VLOG(1) << "Process queued discovery request.";
1502 DiscoveryParamTuple params = discovery_request_queue_.front(); 1482 DiscoveryParamTuple params = discovery_request_queue_.front();
1503 discovery_request_queue_.pop(); 1483 discovery_request_queue_.pop();
1504 AddDiscoverySession(std::get<0>(params), std::get<1>(params), 1484 AddDiscoverySession(std::get<0>(params), std::get<1>(params),
1505 std::get<2>(params)); 1485 std::get<2>(params));
1506 1486
1507 // If the queued request resulted in a pending call, then let it 1487 // If the queued request resulted in a pending call, then let it
1508 // asynchonously process the remaining queued requests once the pending 1488 // asynchonously process the remaining queued requests once the pending
1509 // call returns. 1489 // call returns.
1510 if (discovery_request_pending_) 1490 if (discovery_request_pending_)
1511 return; 1491 return;
1512 } 1492 }
1513 } 1493 }
1514 1494
1515 } // namespace chromeos 1495 } // namespace bluez
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_bluez.h ('k') | device/bluetooth/bluetooth_adapter_chromeos.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698