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" |
7 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/sequenced_task_runner.h" |
| 10 #include "device/bluetooth/bluetooth_adapter_factory.h" |
8 #include "device/bluetooth/bluetooth_device_win.h" | 11 #include "device/bluetooth/bluetooth_device_win.h" |
9 #include "device/bluetooth/bluetooth_service_record.h" | 12 #include "device/bluetooth/bluetooth_service_record.h" |
| 13 #include "device/bluetooth/bluetooth_socket_thread_win.h" |
10 #include "device/bluetooth/bluetooth_socket_win.h" | 14 #include "device/bluetooth/bluetooth_socket_win.h" |
11 | 15 |
| 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 |
12 namespace device { | 67 namespace device { |
13 | 68 |
14 BluetoothProfileWin::BluetoothProfileWin(const BluetoothUUID& uuid, | 69 BluetoothProfileWin::BluetoothProfileWin(const BluetoothUUID& uuid, |
15 const std::string& name) | 70 const std::string& name) |
16 : BluetoothProfile(), uuid_(uuid), name_(name) { | 71 : BluetoothProfile(), uuid_(uuid), name_(name) { |
17 } | 72 } |
18 | 73 |
19 BluetoothProfileWin::~BluetoothProfileWin() { | 74 BluetoothProfileWin::~BluetoothProfileWin() { |
20 } | 75 } |
21 | 76 |
22 void BluetoothProfileWin::Unregister() { | 77 void BluetoothProfileWin::Unregister() { |
23 delete this; | 78 delete this; |
24 } | 79 } |
25 | 80 |
26 void BluetoothProfileWin::SetConnectionCallback( | 81 void BluetoothProfileWin::SetConnectionCallback( |
27 const ConnectionCallback& callback) { | 82 const ConnectionCallback& callback) { |
28 connection_callback_ = callback; | 83 connection_callback_ = callback; |
29 } | 84 } |
30 | 85 |
31 bool BluetoothProfileWin::Connect(const BluetoothDeviceWin* device) { | 86 void BluetoothProfileWin::Connect( |
32 if (connection_callback_.is_null()) | 87 const BluetoothDeviceWin* device, |
33 return false; | 88 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 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 } |
34 | 99 |
35 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_); | 100 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_); |
36 if (record) { | 101 if (!record) { |
37 scoped_refptr<BluetoothSocket> socket( | 102 error_callback.Run(kProfileNotFound); |
38 BluetoothSocketWin::CreateBluetoothSocket(*record)); | 103 return; |
39 if (socket.get() != NULL) { | |
40 connection_callback_.Run(device, socket); | |
41 return true; | |
42 } | |
43 } | 104 } |
44 return false; | 105 |
| 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); |
45 } | 117 } |
46 | 118 |
47 } // namespace device | 119 } // namespace device |
OLD | NEW |