OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
6 | 6 |
| 7 #include <map> |
7 #include <string> | 8 #include <string> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/weak_ptr.h" |
12 #include "base/string16.h" | 14 #include "base/string16.h" |
13 #include "base/string_util.h" | 15 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 17 #include "base/values.h" |
16 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 18 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
17 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" | 19 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" |
| 20 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" |
18 #include "chrome/browser/chromeos/dbus/introspect_util.h" | 21 #include "chrome/browser/chromeos/dbus/introspect_util.h" |
19 #include "chromeos/dbus/bluetooth_adapter_client.h" | 22 #include "chromeos/dbus/bluetooth_adapter_client.h" |
20 #include "chromeos/dbus/bluetooth_agent_service_provider.h" | 23 #include "chromeos/dbus/bluetooth_agent_service_provider.h" |
21 #include "chromeos/dbus/bluetooth_device_client.h" | 24 #include "chromeos/dbus/bluetooth_device_client.h" |
22 #include "chromeos/dbus/bluetooth_input_client.h" | 25 #include "chromeos/dbus/bluetooth_input_client.h" |
23 #include "chromeos/dbus/dbus_thread_manager.h" | 26 #include "chromeos/dbus/dbus_thread_manager.h" |
24 #include "chromeos/dbus/introspectable_client.h" | 27 #include "chromeos/dbus/introspectable_client.h" |
25 #include "dbus/bus.h" | 28 #include "dbus/bus.h" |
26 #include "dbus/object_path.h" | 29 #include "dbus/object_path.h" |
27 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 const dbus::ObjectPath& object_path, | 195 const dbus::ObjectPath& object_path, |
193 const BluetoothDeviceClient::ServiceMap& service_map, | 196 const BluetoothDeviceClient::ServiceMap& service_map, |
194 bool success) { | 197 bool success) { |
195 if (!success) { | 198 if (!success) { |
196 callback.Run(false); | 199 callback.Run(false); |
197 return; | 200 return; |
198 } | 201 } |
199 | 202 |
200 for (BluetoothDeviceClient::ServiceMap::const_iterator i = | 203 for (BluetoothDeviceClient::ServiceMap::const_iterator i = |
201 service_map.begin(); i != service_map.end(); ++i) { | 204 service_map.begin(); i != service_map.end(); ++i) { |
202 BluetoothServiceRecord service_record(i->second); | 205 BluetoothServiceRecord service_record(address(), i->second); |
203 if (service_record.name() == name) { | 206 if (service_record.name() == name) { |
204 callback.Run(true); | 207 callback.Run(true); |
205 return; | 208 return; |
206 } | 209 } |
207 } | 210 } |
208 | 211 |
209 callback.Run(false); | 212 callback.Run(false); |
210 } | 213 } |
211 | 214 |
212 void BluetoothDevice::ProvidesServiceWithName(const std::string& name, | 215 void BluetoothDevice::ProvidesServiceWithName(const std::string& name, |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 | 448 |
446 void BluetoothDevice::Forget(ErrorCallback error_callback) { | 449 void BluetoothDevice::Forget(ErrorCallback error_callback) { |
447 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 450 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
448 RemoveDevice(adapter_->object_path_, | 451 RemoveDevice(adapter_->object_path_, |
449 object_path_, | 452 object_path_, |
450 base::Bind(&BluetoothDevice::ForgetCallback, | 453 base::Bind(&BluetoothDevice::ForgetCallback, |
451 weak_ptr_factory_.GetWeakPtr(), | 454 weak_ptr_factory_.GetWeakPtr(), |
452 error_callback)); | 455 error_callback)); |
453 } | 456 } |
454 | 457 |
| 458 |
| 459 void BluetoothDevice::ConnectToMatchingService( |
| 460 const std::string& service_uuid, |
| 461 SocketCallback callback, |
| 462 const dbus::ObjectPath& object_path, |
| 463 const BluetoothDeviceClient::ServiceMap& service_map, |
| 464 bool success) { |
| 465 if (success) { |
| 466 // If multiple service records are found, use the first one that works. |
| 467 for (BluetoothDeviceClient::ServiceMap::const_iterator i = |
| 468 service_map.begin(); i != service_map.end(); ++i) { |
| 469 BluetoothServiceRecord service_record(address(), i->second); |
| 470 scoped_refptr<BluetoothSocket> socket( |
| 471 BluetoothSocket::CreateBluetoothSocket(service_record)); |
| 472 if (socket.get() != NULL) { |
| 473 callback.Run(socket); |
| 474 break; |
| 475 } |
| 476 } |
| 477 } |
| 478 callback.Run(NULL); |
| 479 } |
| 480 |
| 481 void BluetoothDevice::ConnectToService(const std::string& service_uuid, |
| 482 SocketCallback callback) { |
| 483 // quick sanity check |
| 484 if (!ProvidesServiceWithUUID(service_uuid)) { |
| 485 callback.Run(NULL); |
| 486 return; |
| 487 } |
| 488 |
| 489 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
| 490 DiscoverServices( |
| 491 object_path_, |
| 492 service_uuid, |
| 493 base::Bind(&BluetoothDevice::ConnectToMatchingService, |
| 494 weak_ptr_factory_.GetWeakPtr(), |
| 495 service_uuid, |
| 496 callback)); |
| 497 } |
| 498 |
455 void BluetoothDevice::ForgetCallback(ErrorCallback error_callback, | 499 void BluetoothDevice::ForgetCallback(ErrorCallback error_callback, |
456 const dbus::ObjectPath& adapter_path, | 500 const dbus::ObjectPath& adapter_path, |
457 bool success) { | 501 bool success) { |
458 // It's quite normal that this path never gets called on success; we use a | 502 // It's quite normal that this path never gets called on success; we use a |
459 // weak pointer, and bluetoothd might send the DeviceRemoved signal before | 503 // weak pointer, and bluetoothd might send the DeviceRemoved signal before |
460 // the method reply, in which case this object is deleted and the | 504 // the method reply, in which case this object is deleted and the |
461 // callback never takes place. Therefore don't do anything here for the | 505 // callback never takes place. Therefore don't do anything here for the |
462 // success case. | 506 // success case. |
463 if (!success) { | 507 if (!success) { |
464 LOG(WARNING) << "Forget failed: " << address_; | 508 LOG(WARNING) << "Forget failed: " << address_; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 // static | 627 // static |
584 BluetoothDevice* BluetoothDevice::CreateUnbound( | 628 BluetoothDevice* BluetoothDevice::CreateUnbound( |
585 BluetoothAdapter* adapter, | 629 BluetoothAdapter* adapter, |
586 const BluetoothDeviceClient::Properties* properties) { | 630 const BluetoothDeviceClient::Properties* properties) { |
587 BluetoothDevice* device = new BluetoothDevice(adapter); | 631 BluetoothDevice* device = new BluetoothDevice(adapter); |
588 device->Update(properties, false); | 632 device->Update(properties, false); |
589 return device; | 633 return device; |
590 } | 634 } |
591 | 635 |
592 } // namespace chromeos | 636 } // namespace chromeos |
OLD | NEW |