| OLD | NEW |
| 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_profile_win.h" | 5 #include "device/bluetooth/bluetooth_profile_win.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 9 #include "base/sequenced_task_runner.h" | |
| 10 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 11 #include "device/bluetooth/bluetooth_device_win.h" | 8 #include "device/bluetooth/bluetooth_device_win.h" |
| 12 #include "device/bluetooth/bluetooth_service_record.h" | 9 #include "device/bluetooth/bluetooth_service_record.h" |
| 13 #include "device/bluetooth/bluetooth_socket_thread_win.h" | |
| 14 #include "device/bluetooth/bluetooth_socket_win.h" | 10 #include "device/bluetooth/bluetooth_socket_win.h" |
| 15 | 11 |
| 16 namespace { | |
| 17 | |
| 18 using device::BluetoothAdapter; | |
| 19 using device::BluetoothDevice; | |
| 20 using device::BluetoothProfileWin; | |
| 21 using device::BluetoothSocket; | |
| 22 using device::BluetoothSocketWin; | |
| 23 | |
| 24 const char kNoConnectionCallback[] = "Connection callback not set"; | |
| 25 const char kProfileNotFound[] = "Profile not found"; | |
| 26 | |
| 27 void OnConnectSuccessUIWithAdapter( | |
| 28 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
| 29 const base::Closure& callback, | |
| 30 const BluetoothProfileWin::ConnectionCallback& connection_callback, | |
| 31 const std::string& device_address, | |
| 32 scoped_refptr<BluetoothSocketWin> socket, | |
| 33 scoped_refptr<BluetoothAdapter> adapter) { | |
| 34 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | |
| 35 const BluetoothDevice* device = adapter->GetDevice(device_address); | |
| 36 if (device) { | |
| 37 connection_callback.Run(device, socket); | |
| 38 callback.Run(); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 void OnConnectSuccessUI( | |
| 43 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
| 44 const base::Closure& callback, | |
| 45 const BluetoothProfileWin::ConnectionCallback& connection_callback, | |
| 46 const std::string& device_address, | |
| 47 scoped_refptr<BluetoothSocketWin> socket) { | |
| 48 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | |
| 49 device::BluetoothAdapterFactory::GetAdapter( | |
| 50 base::Bind(&OnConnectSuccessUIWithAdapter, | |
| 51 ui_task_runner, | |
| 52 callback, | |
| 53 connection_callback, | |
| 54 device_address, | |
| 55 socket)); | |
| 56 } | |
| 57 | |
| 58 void OnConnectErrorUI(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
| 59 const BluetoothProfileWin::ErrorCallback& error_callback, | |
| 60 const std::string& error) { | |
| 61 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | |
| 62 error_callback.Run(error); | |
| 63 } | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 67 namespace device { | 12 namespace device { |
| 68 | 13 |
| 69 BluetoothProfileWin::BluetoothProfileWin(const BluetoothUUID& uuid, | 14 BluetoothProfileWin::BluetoothProfileWin(const BluetoothUUID& uuid, |
| 70 const std::string& name) | 15 const std::string& name) |
| 71 : BluetoothProfile(), uuid_(uuid), name_(name) { | 16 : BluetoothProfile(), uuid_(uuid), name_(name) { |
| 72 } | 17 } |
| 73 | 18 |
| 74 BluetoothProfileWin::~BluetoothProfileWin() { | 19 BluetoothProfileWin::~BluetoothProfileWin() { |
| 75 } | 20 } |
| 76 | 21 |
| 77 void BluetoothProfileWin::Unregister() { | 22 void BluetoothProfileWin::Unregister() { |
| 78 delete this; | 23 delete this; |
| 79 } | 24 } |
| 80 | 25 |
| 81 void BluetoothProfileWin::SetConnectionCallback( | 26 void BluetoothProfileWin::SetConnectionCallback( |
| 82 const ConnectionCallback& callback) { | 27 const ConnectionCallback& callback) { |
| 83 connection_callback_ = callback; | 28 connection_callback_ = callback; |
| 84 } | 29 } |
| 85 | 30 |
| 86 void BluetoothProfileWin::Connect( | 31 bool BluetoothProfileWin::Connect(const BluetoothDeviceWin* device) { |
| 87 const BluetoothDeviceWin* device, | 32 if (connection_callback_.is_null()) |
| 88 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 33 return false; |
| 89 scoped_refptr<BluetoothSocketThreadWin> socket_thread, | |
| 90 net::NetLog* net_log, | |
| 91 const net::NetLog::Source& source, | |
| 92 const base::Closure& success_callback, | |
| 93 const ErrorCallback& error_callback) { | |
| 94 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | |
| 95 if (connection_callback_.is_null()) { | |
| 96 error_callback.Run(kNoConnectionCallback); | |
| 97 return; | |
| 98 } | |
| 99 | 34 |
| 100 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_); | 35 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_); |
| 101 if (!record) { | 36 if (record) { |
| 102 error_callback.Run(kProfileNotFound); | 37 scoped_refptr<BluetoothSocket> socket( |
| 103 return; | 38 BluetoothSocketWin::CreateBluetoothSocket(*record)); |
| 39 if (socket.get() != NULL) { |
| 40 connection_callback_.Run(device, socket); |
| 41 return true; |
| 42 } |
| 104 } | 43 } |
| 105 | 44 return false; |
| 106 scoped_refptr<BluetoothSocketWin> socket( | |
| 107 BluetoothSocketWin::CreateBluetoothSocket( | |
| 108 *record, ui_task_runner, socket_thread, net_log, source)); | |
| 109 | |
| 110 socket->Connect(base::Bind(&OnConnectSuccessUI, | |
| 111 ui_task_runner, | |
| 112 success_callback, | |
| 113 connection_callback_, | |
| 114 device->GetAddress(), | |
| 115 socket), | |
| 116 error_callback); | |
| 117 } | 45 } |
| 118 | 46 |
| 119 } // namespace device | 47 } // namespace device |
| OLD | NEW |