OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/proximity_auth/bluetooth_connection.h" | 5 #include "components/proximity_auth/bluetooth_connection.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
10 #include "components/proximity_auth/bluetooth_util.h" | |
11 #include "components/proximity_auth/remote_device.h" | 10 #include "components/proximity_auth/remote_device.h" |
12 #include "components/proximity_auth/wire_message.h" | 11 #include "components/proximity_auth/wire_message.h" |
13 #include "device/bluetooth/bluetooth_adapter_factory.h" | 12 #include "device/bluetooth/bluetooth_adapter_factory.h" |
14 #include "device/bluetooth/bluetooth_device.h" | 13 #include "device/bluetooth/bluetooth_device.h" |
15 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
16 | 15 |
17 namespace proximity_auth { | 16 namespace proximity_auth { |
18 namespace { | 17 namespace { |
19 const int kReceiveBufferSizeBytes = 1024; | 18 const int kReceiveBufferSizeBytes = 1024; |
20 } | 19 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 device::BluetoothDevice* device) { | 88 device::BluetoothDevice* device) { |
90 DCHECK_EQ(adapter, adapter_.get()); | 89 DCHECK_EQ(adapter, adapter_.get()); |
91 if (device->GetAddress() != remote_device().bluetooth_address) | 90 if (device->GetAddress() != remote_device().bluetooth_address) |
92 return; | 91 return; |
93 | 92 |
94 DCHECK_NE(status(), DISCONNECTED); | 93 DCHECK_NE(status(), DISCONNECTED); |
95 VLOG(1) << "[BC] Device disconnected..."; | 94 VLOG(1) << "[BC] Device disconnected..."; |
96 Disconnect(); | 95 Disconnect(); |
97 } | 96 } |
98 | 97 |
99 void BluetoothConnection::ConnectToService( | |
100 device::BluetoothDevice* device, | |
101 const device::BluetoothUUID& uuid, | |
102 const device::BluetoothDevice::ConnectToServiceCallback& callback, | |
103 const device::BluetoothDevice::ConnectToServiceErrorCallback& | |
104 error_callback) { | |
105 bluetooth_util::ConnectToServiceInsecurely( | |
106 device, uuid, callback, error_callback); | |
107 } | |
108 | |
109 void BluetoothConnection::StartReceive() { | 98 void BluetoothConnection::StartReceive() { |
110 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); | 99 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); |
111 socket_->Receive(kReceiveBufferSizeBytes, | 100 socket_->Receive(kReceiveBufferSizeBytes, |
112 base::Bind(&BluetoothConnection::OnReceive, weak_this), | 101 base::Bind(&BluetoothConnection::OnReceive, weak_this), |
113 base::Bind(&BluetoothConnection::OnReceiveError, weak_this)); | 102 base::Bind(&BluetoothConnection::OnReceiveError, weak_this)); |
114 } | 103 } |
115 | 104 |
116 void BluetoothConnection::OnAdapterInitialized( | 105 void BluetoothConnection::OnAdapterInitialized( |
117 scoped_refptr<device::BluetoothAdapter> adapter) { | 106 scoped_refptr<device::BluetoothAdapter> adapter) { |
118 const std::string address = remote_device().bluetooth_address; | 107 const std::string address = remote_device().bluetooth_address; |
119 device::BluetoothDevice* bluetooth_device = adapter->GetDevice(address); | 108 device::BluetoothDevice* bluetooth_device = adapter->GetDevice(address); |
120 if (!bluetooth_device) { | 109 if (!bluetooth_device) { |
121 VLOG(1) << "[BC] Device with address " << address | 110 VLOG(1) << "[BC] Device with address " << address |
122 << " is not known to the system Bluetooth daemon."; | 111 << " is not known to the system Bluetooth daemon."; |
123 // TOOD(isherman): Optimistically attempt to seek the device and connect | 112 // TOOD(isherman): Optimistically attempt to seek the device and connect |
124 // anyway, as was previously implemented in BluetoothConnectionFinder. | 113 // anyway, as was previously implemented in BluetoothConnectionFinder. |
125 Disconnect(); | 114 Disconnect(); |
126 return; | 115 return; |
127 } | 116 } |
128 | 117 |
129 adapter_ = adapter; | 118 adapter_ = adapter; |
130 adapter_->AddObserver(this); | 119 adapter_->AddObserver(this); |
131 | 120 |
132 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); | 121 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); |
133 ConnectToService( | 122 bluetooth_device->ConnectToServiceInsecurely( |
134 bluetooth_device, | |
135 uuid_, | 123 uuid_, |
136 base::Bind(&BluetoothConnection::OnConnected, weak_this), | 124 base::Bind(&BluetoothConnection::OnConnected, weak_this), |
137 base::Bind(&BluetoothConnection::OnConnectionError, weak_this)); | 125 base::Bind(&BluetoothConnection::OnConnectionError, weak_this)); |
138 } | 126 } |
139 | 127 |
140 void BluetoothConnection::OnConnected( | 128 void BluetoothConnection::OnConnected( |
141 scoped_refptr<device::BluetoothSocket> socket) { | 129 scoped_refptr<device::BluetoothSocket> socket) { |
142 if (status() != IN_PROGRESS) { | 130 if (status() != IN_PROGRESS) { |
143 // This case is reachable if the client of |this| connection called | 131 // This case is reachable if the client of |this| connection called |
144 // |Disconnect()| while the backing Bluetooth connection was pending. | 132 // |Disconnect()| while the backing Bluetooth connection was pending. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 182 |
195 // Post a task to delay the read until the socket is available, as | 183 // Post a task to delay the read until the socket is available, as |
196 // calling StartReceive at this point would error with ERR_IO_PENDING. | 184 // calling StartReceive at this point would error with ERR_IO_PENDING. |
197 base::MessageLoop::current()->PostTask( | 185 base::MessageLoop::current()->PostTask( |
198 FROM_HERE, | 186 FROM_HERE, |
199 base::Bind(&BluetoothConnection::StartReceive, | 187 base::Bind(&BluetoothConnection::StartReceive, |
200 weak_ptr_factory_.GetWeakPtr())); | 188 weak_ptr_factory_.GetWeakPtr())); |
201 } | 189 } |
202 | 190 |
203 } // namespace proximity_auth | 191 } // namespace proximity_auth |
OLD | NEW |