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" |
10 #include "device/bluetooth/bluetooth_socket_win.h" | 13 #include "device/bluetooth/bluetooth_socket_win.h" |
11 | 14 |
| 15 namespace { |
| 16 |
| 17 using device::BluetoothAdapter; |
| 18 using device::BluetoothDevice; |
| 19 using device::BluetoothProfileWin; |
| 20 using device::BluetoothSocket; |
| 21 using device::BluetoothSocketWin; |
| 22 |
| 23 const char kNoConnectionCallback[] = "Connection callback not set"; |
| 24 const char kProfileNotFound[] = "Profile not found"; |
| 25 |
| 26 void OnSocketConnectErrorUI( |
| 27 const BluetoothProfileWin::ErrorCallback& error_callback, |
| 28 const std::string& error) { |
| 29 error_callback.Run(error); |
| 30 } |
| 31 |
| 32 void OnSocketConnectSuccess( |
| 33 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 34 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 35 const base::Closure& success_callback) { |
| 36 DCHECK(file_task_runner->RunsTasksOnCurrentThread()); |
| 37 ui_task_runner->PostTask(FROM_HERE, success_callback); |
| 38 } |
| 39 |
| 40 void OnSocketConnectError( |
| 41 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 42 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 43 const BluetoothProfileWin::ErrorCallback& error_callback, |
| 44 const std::string& error) { |
| 45 DCHECK(file_task_runner->RunsTasksOnCurrentThread()); |
| 46 ui_task_runner->PostTask( |
| 47 FROM_HERE, base::Bind(OnSocketConnectErrorUI, error_callback, error)); |
| 48 } |
| 49 |
| 50 void OnSocketConnect(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 51 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 52 scoped_refptr<BluetoothSocket> socket, |
| 53 const base::Closure& success_callback, |
| 54 const BluetoothProfileWin::ErrorCallback& error_callback) { |
| 55 DCHECK(file_task_runner->RunsTasksOnCurrentThread()); |
| 56 socket->Connect(base::Bind(OnSocketConnectSuccess, |
| 57 ui_task_runner, |
| 58 file_task_runner, |
| 59 success_callback), |
| 60 base::Bind(OnSocketConnectError, |
| 61 ui_task_runner, |
| 62 file_task_runner, |
| 63 error_callback)); |
| 64 } |
| 65 |
| 66 void OnConnectSuccessUIWithAdapter( |
| 67 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 68 const base::Closure& callback, |
| 69 const BluetoothProfileWin::ConnectionCallback& connection_callback, |
| 70 const std::string& device_address, |
| 71 scoped_refptr<BluetoothSocket> socket, |
| 72 scoped_refptr<BluetoothAdapter> adapter) { |
| 73 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
| 74 const BluetoothDevice* device = adapter->GetDevice(device_address); |
| 75 if (device) { |
| 76 connection_callback.Run(device, socket); |
| 77 callback.Run(); |
| 78 } |
| 79 } |
| 80 |
| 81 void OnConnectSuccessUI( |
| 82 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 83 const base::Closure& callback, |
| 84 const BluetoothProfileWin::ConnectionCallback& connection_callback, |
| 85 const std::string& device_address, |
| 86 scoped_refptr<BluetoothSocket> socket) { |
| 87 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
| 88 device::BluetoothAdapterFactory::GetAdapter( |
| 89 base::Bind(&OnConnectSuccessUIWithAdapter, |
| 90 ui_task_runner, |
| 91 callback, |
| 92 connection_callback, |
| 93 device_address, |
| 94 socket)); |
| 95 } |
| 96 |
| 97 void OnConnectErrorUI(scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 98 const BluetoothProfileWin::ErrorCallback& error_callback, |
| 99 const std::string& error) { |
| 100 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
| 101 error_callback.Run(error); |
| 102 } |
| 103 |
| 104 } // namespace |
| 105 |
12 namespace device { | 106 namespace device { |
13 | 107 |
14 BluetoothProfileWin::BluetoothProfileWin(const std::string& uuid, | 108 BluetoothProfileWin::BluetoothProfileWin(const std::string& uuid, |
15 const std::string& name) | 109 const std::string& name) |
16 : BluetoothProfile(), uuid_(uuid), name_(name) { | 110 : BluetoothProfile(), uuid_(uuid), name_(name) { |
17 } | 111 } |
18 | 112 |
19 BluetoothProfileWin::~BluetoothProfileWin() { | 113 BluetoothProfileWin::~BluetoothProfileWin() { |
20 } | 114 } |
21 | 115 |
22 void BluetoothProfileWin::Unregister() { | 116 void BluetoothProfileWin::Unregister() { |
23 delete this; | 117 delete this; |
24 } | 118 } |
25 | 119 |
26 void BluetoothProfileWin::SetConnectionCallback( | 120 void BluetoothProfileWin::SetConnectionCallback( |
27 const ConnectionCallback& callback) { | 121 const ConnectionCallback& callback) { |
28 connection_callback_ = callback; | 122 connection_callback_ = callback; |
29 } | 123 } |
30 | 124 |
31 bool BluetoothProfileWin::Connect(const BluetoothDeviceWin* device) { | 125 void BluetoothProfileWin::Connect( |
32 if (connection_callback_.is_null()) | 126 const BluetoothDeviceWin* device, |
33 return false; | 127 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 128 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 129 net::NetLog* net_log, |
| 130 const net::NetLog::Source& source, |
| 131 const base::Closure& success_callback, |
| 132 const ErrorCallback& error_callback) { |
| 133 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
| 134 if (connection_callback_.is_null()) { |
| 135 error_callback.Run(kNoConnectionCallback); |
| 136 return; |
| 137 } |
34 | 138 |
35 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_); | 139 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_); |
36 if (record) { | 140 if (!record) { |
37 scoped_refptr<BluetoothSocket> socket( | 141 error_callback.Run(kProfileNotFound); |
38 BluetoothSocketWin::CreateBluetoothSocket(*record)); | 142 return; |
39 if (socket.get() != NULL) { | |
40 connection_callback_.Run(device, socket); | |
41 return true; | |
42 } | |
43 } | 143 } |
44 return false; | 144 |
| 145 scoped_refptr<BluetoothSocket> socket( |
| 146 BluetoothSocketWin::CreateBluetoothSocket( |
| 147 *record, file_task_runner, net_log, source)); |
| 148 |
| 149 file_task_runner->PostTask( |
| 150 FROM_HERE, |
| 151 base::Bind(&OnSocketConnect, |
| 152 ui_task_runner, |
| 153 file_task_runner, |
| 154 socket, |
| 155 base::Bind(OnConnectSuccessUI, |
| 156 ui_task_runner, |
| 157 success_callback, |
| 158 connection_callback_, |
| 159 device->GetAddress(), |
| 160 socket), |
| 161 base::Bind(OnConnectErrorUI, ui_task_runner, error_callback))); |
45 } | 162 } |
46 | 163 |
47 } // namespace device | 164 } // namespace device |
OLD | NEW |