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
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_profile_win.h"
8 #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"
12 #include "device/bluetooth/bluetooth_service_record.h"
13 #include "device/bluetooth/bluetooth_socket_thread_win.h"
14 #include "device/bluetooth/bluetooth_socket_win.h"
18 using device::BluetoothAdapter
;
19 using device::BluetoothDevice
;
20 using device::BluetoothProfileWin
;
21 using device::BluetoothSocket
;
22 using device::BluetoothSocketWin
;
24 const char kNoConnectionCallback
[] = "Connection callback not set";
25 const char kProfileNotFound
[] = "Profile not found";
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
);
37 connection_callback
.Run(device
, socket
);
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
,
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
);
69 BluetoothProfileWin::BluetoothProfileWin(const BluetoothUUID
& uuid
,
70 const std::string
& name
)
71 : BluetoothProfile(), uuid_(uuid
), name_(name
) {
74 BluetoothProfileWin::~BluetoothProfileWin() {
77 void BluetoothProfileWin::Unregister() {
81 void BluetoothProfileWin::SetConnectionCallback(
82 const ConnectionCallback
& callback
) {
83 connection_callback_
= callback
;
86 void BluetoothProfileWin::Connect(
87 const BluetoothDeviceWin
* device
,
88 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
,
89 scoped_refptr
<BluetoothSocketThreadWin
> socket_thread
,
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
);
100 const BluetoothServiceRecord
* record
= device
->GetServiceRecord(uuid_
);
102 error_callback
.Run(kProfileNotFound
);
106 scoped_refptr
<BluetoothSocketWin
> socket(
107 BluetoothSocketWin::CreateBluetoothSocket(
108 *record
, ui_task_runner
, socket_thread
, net_log
, source
));
110 socket
->Connect(base::Bind(&OnConnectSuccessUI
,
113 connection_callback_
,
114 device
->GetAddress(),
119 } // namespace device