| 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 Disconnect(); | 112 Disconnect(); |
| 124 return; | 113 return; |
| 125 } | 114 } |
| 126 | 115 |
| 127 adapter_ = adapter; | 116 adapter_ = adapter; |
| 128 adapter_->AddObserver(this); | 117 adapter_->AddObserver(this); |
| 129 | 118 |
| 130 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); | 119 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); |
| 131 ConnectToService( | 120 bluetooth_device->ConnectToServiceInsecurely( |
| 132 bluetooth_device, | |
| 133 uuid_, | 121 uuid_, |
| 134 base::Bind(&BluetoothConnection::OnConnected, weak_this), | 122 base::Bind(&BluetoothConnection::OnConnected, weak_this), |
| 135 base::Bind(&BluetoothConnection::OnConnectionError, weak_this)); | 123 base::Bind(&BluetoothConnection::OnConnectionError, weak_this)); |
| 136 } | 124 } |
| 137 | 125 |
| 138 void BluetoothConnection::OnConnected( | 126 void BluetoothConnection::OnConnected( |
| 139 scoped_refptr<device::BluetoothSocket> socket) { | 127 scoped_refptr<device::BluetoothSocket> socket) { |
| 140 if (status() != IN_PROGRESS) { | 128 if (status() != IN_PROGRESS) { |
| 141 // This case is reachable if the client of |this| connection called | 129 // This case is reachable if the client of |this| connection called |
| 142 // |Disconnect()| while the backing Bluetooth connection was pending. | 130 // |Disconnect()| while the backing Bluetooth connection was pending. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 180 |
| 193 // Post a task to delay the read until the socket is available, as | 181 // Post a task to delay the read until the socket is available, as |
| 194 // calling StartReceive at this point would error with ERR_IO_PENDING. | 182 // calling StartReceive at this point would error with ERR_IO_PENDING. |
| 195 base::MessageLoop::current()->PostTask( | 183 base::MessageLoop::current()->PostTask( |
| 196 FROM_HERE, | 184 FROM_HERE, |
| 197 base::Bind(&BluetoothConnection::StartReceive, | 185 base::Bind(&BluetoothConnection::StartReceive, |
| 198 weak_ptr_factory_.GetWeakPtr())); | 186 weak_ptr_factory_.GetWeakPtr())); |
| 199 } | 187 } |
| 200 | 188 |
| 201 } // namespace proximity_auth | 189 } // namespace proximity_auth |
| OLD | NEW |