Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / device / bluetooth / bluetooth_profile_win.cc
blob79848caf074fc2a931b307695e946dca9f8bde09
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"
7 #include "base/bind.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"
16 namespace {
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);
36 if (device) {
37 connection_callback.Run(device, socket);
38 callback.Run();
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));
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);
65 } // namespace
67 namespace device {
69 BluetoothProfileWin::BluetoothProfileWin(const BluetoothUUID& uuid,
70 const std::string& name)
71 : BluetoothProfile(), uuid_(uuid), name_(name) {
74 BluetoothProfileWin::~BluetoothProfileWin() {
77 void BluetoothProfileWin::Unregister() {
78 delete this;
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,
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;
100 const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_);
101 if (!record) {
102 error_callback.Run(kProfileNotFound);
103 return;
106 scoped_refptr<BluetoothSocketWin> socket(
107 BluetoothSocketWin::CreateBluetoothSocket(
108 *record, ui_task_runner, socket_thread, net_log, source));
110 socket->Connect(base::Bind(&OnConnectSuccessUI,
111 ui_task_runner,
112 success_callback,
113 connection_callback_,
114 device->GetAddress(),
115 socket),
116 error_callback);
119 } // namespace device