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/location.h" | 8 #include "base/location.h" |
9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "components/proximity_auth/logging/logging.h" |
12 #include "components/proximity_auth/remote_device.h" | 13 #include "components/proximity_auth/remote_device.h" |
13 #include "components/proximity_auth/wire_message.h" | 14 #include "components/proximity_auth/wire_message.h" |
14 #include "device/bluetooth/bluetooth_adapter_factory.h" | 15 #include "device/bluetooth/bluetooth_adapter_factory.h" |
15 #include "device/bluetooth/bluetooth_device.h" | 16 #include "device/bluetooth/bluetooth_device.h" |
16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
17 | 18 |
18 namespace proximity_auth { | 19 namespace proximity_auth { |
19 namespace { | 20 namespace { |
20 const int kReceiveBufferSizeBytes = 1024; | 21 const int kReceiveBufferSizeBytes = 1024; |
21 } | 22 } |
22 | 23 |
23 BluetoothConnection::BluetoothConnection(const RemoteDevice& remote_device, | 24 BluetoothConnection::BluetoothConnection(const RemoteDevice& remote_device, |
24 const device::BluetoothUUID& uuid) | 25 const device::BluetoothUUID& uuid) |
25 : Connection(remote_device), uuid_(uuid), weak_ptr_factory_(this) { | 26 : Connection(remote_device), uuid_(uuid), weak_ptr_factory_(this) { |
26 } | 27 } |
27 | 28 |
28 BluetoothConnection::~BluetoothConnection() { | 29 BluetoothConnection::~BluetoothConnection() { |
29 Disconnect(); | 30 if (status() != DISCONNECTED) |
| 31 Disconnect(); |
30 } | 32 } |
31 | 33 |
32 void BluetoothConnection::Connect() { | 34 void BluetoothConnection::Connect() { |
33 if (status() != DISCONNECTED) { | 35 if (status() != DISCONNECTED) { |
34 VLOG(1) | 36 PA_LOG(WARNING) |
35 << "[BC] Ignoring attempt to connect a non-disconnected connection."; | 37 << "Ignoring attempt to connect a non-disconnected connection."; |
36 return; | 38 return; |
37 } | 39 } |
38 | 40 |
39 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 41 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
40 VLOG(1) | 42 PA_LOG(WARNING) |
41 << "[BC] Connection failed: Bluetooth is unsupported on this platform."; | 43 << "Connection failed: Bluetooth is unsupported on this platform."; |
42 return; | 44 return; |
43 } | 45 } |
44 | 46 |
45 SetStatus(IN_PROGRESS); | 47 SetStatus(IN_PROGRESS); |
46 device::BluetoothAdapterFactory::GetAdapter( | 48 device::BluetoothAdapterFactory::GetAdapter( |
47 base::Bind(&BluetoothConnection::OnAdapterInitialized, | 49 base::Bind(&BluetoothConnection::OnAdapterInitialized, |
48 weak_ptr_factory_.GetWeakPtr())); | 50 weak_ptr_factory_.GetWeakPtr())); |
49 } | 51 } |
50 | 52 |
51 void BluetoothConnection::Disconnect() { | 53 void BluetoothConnection::Disconnect() { |
52 if (status() == DISCONNECTED) { | 54 if (status() == DISCONNECTED) { |
53 VLOG(1) | 55 PA_LOG(WARNING) |
54 << "[BC] Ignoring attempt to disconnect a non-connected connection."; | 56 << "Ignoring attempt to disconnect a non-connected connection."; |
55 return; | 57 return; |
56 } | 58 } |
57 | 59 |
58 // Set status as disconnected now, rather than after the socket closes, so | 60 // Set status as disconnected now, rather than after the socket closes, so |
59 // this connection is not reused. | 61 // this connection is not reused. |
60 SetStatus(DISCONNECTED); | 62 SetStatus(DISCONNECTED); |
61 if (socket_.get()) { | 63 if (socket_.get()) { |
62 socket_->Disconnect(base::Bind(&base::DoNothing)); | 64 socket_->Disconnect(base::Bind(&base::DoNothing)); |
63 socket_ = NULL; | 65 socket_ = NULL; |
64 } | 66 } |
(...skipping 21 matching lines...) Expand all Loading... |
86 base::Bind(&BluetoothConnection::OnSendError, weak_this)); | 88 base::Bind(&BluetoothConnection::OnSendError, weak_this)); |
87 } | 89 } |
88 | 90 |
89 void BluetoothConnection::DeviceRemoved(device::BluetoothAdapter* adapter, | 91 void BluetoothConnection::DeviceRemoved(device::BluetoothAdapter* adapter, |
90 device::BluetoothDevice* device) { | 92 device::BluetoothDevice* device) { |
91 DCHECK_EQ(adapter, adapter_.get()); | 93 DCHECK_EQ(adapter, adapter_.get()); |
92 if (device->GetAddress() != remote_device().bluetooth_address) | 94 if (device->GetAddress() != remote_device().bluetooth_address) |
93 return; | 95 return; |
94 | 96 |
95 DCHECK_NE(status(), DISCONNECTED); | 97 DCHECK_NE(status(), DISCONNECTED); |
96 VLOG(1) << "[BC] Device disconnected..."; | 98 PA_LOG(INFO) << "Device disconnected..."; |
97 Disconnect(); | 99 Disconnect(); |
98 } | 100 } |
99 | 101 |
100 void BluetoothConnection::StartReceive() { | 102 void BluetoothConnection::StartReceive() { |
101 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); | 103 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); |
102 socket_->Receive(kReceiveBufferSizeBytes, | 104 socket_->Receive(kReceiveBufferSizeBytes, |
103 base::Bind(&BluetoothConnection::OnReceive, weak_this), | 105 base::Bind(&BluetoothConnection::OnReceive, weak_this), |
104 base::Bind(&BluetoothConnection::OnReceiveError, weak_this)); | 106 base::Bind(&BluetoothConnection::OnReceiveError, weak_this)); |
105 } | 107 } |
106 | 108 |
107 void BluetoothConnection::OnAdapterInitialized( | 109 void BluetoothConnection::OnAdapterInitialized( |
108 scoped_refptr<device::BluetoothAdapter> adapter) { | 110 scoped_refptr<device::BluetoothAdapter> adapter) { |
109 const std::string address = remote_device().bluetooth_address; | 111 const std::string address = remote_device().bluetooth_address; |
110 device::BluetoothDevice* bluetooth_device = adapter->GetDevice(address); | 112 device::BluetoothDevice* bluetooth_device = adapter->GetDevice(address); |
111 if (!bluetooth_device) { | 113 if (!bluetooth_device) { |
112 VLOG(1) << "[BC] Device with address " << address | 114 PA_LOG(WARNING) << "Device with address " << address |
113 << " is not known to the system Bluetooth daemon."; | 115 << " is not known to the system Bluetooth daemon."; |
114 // TOOD(isherman): Optimistically attempt to seek the device and connect | 116 // TOOD(isherman): Optimistically attempt to seek the device and connect |
115 // anyway, as was previously implemented in BluetoothConnectionFinder. | 117 // anyway, as was previously implemented in BluetoothConnectionFinder. |
116 Disconnect(); | 118 Disconnect(); |
117 return; | 119 return; |
118 } | 120 } |
119 | 121 |
120 adapter_ = adapter; | 122 adapter_ = adapter; |
121 adapter_->AddObserver(this); | 123 adapter_->AddObserver(this); |
122 | 124 |
123 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); | 125 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); |
124 bluetooth_device->ConnectToServiceInsecurely( | 126 bluetooth_device->ConnectToServiceInsecurely( |
125 uuid_, | 127 uuid_, |
126 base::Bind(&BluetoothConnection::OnConnected, weak_this), | 128 base::Bind(&BluetoothConnection::OnConnected, weak_this), |
127 base::Bind(&BluetoothConnection::OnConnectionError, weak_this)); | 129 base::Bind(&BluetoothConnection::OnConnectionError, weak_this)); |
128 } | 130 } |
129 | 131 |
130 void BluetoothConnection::OnConnected( | 132 void BluetoothConnection::OnConnected( |
131 scoped_refptr<device::BluetoothSocket> socket) { | 133 scoped_refptr<device::BluetoothSocket> socket) { |
132 if (status() != IN_PROGRESS) { | 134 if (status() != IN_PROGRESS) { |
133 // This case is reachable if the client of |this| connection called | 135 // This case is reachable if the client of |this| connection called |
134 // |Disconnect()| while the backing Bluetooth connection was pending. | 136 // |Disconnect()| while the backing Bluetooth connection was pending. |
135 DCHECK_EQ(status(), DISCONNECTED); | 137 DCHECK_EQ(status(), DISCONNECTED); |
136 VLOG(1) << "[BC] Ignoring successful backend Bluetooth connection to an " | 138 PA_LOG(WARNING) << "Ignoring successful backend Bluetooth connection to an " |
137 << "already disconnected logical connection."; | 139 << "already disconnected logical connection."; |
138 return; | 140 return; |
139 } | 141 } |
140 | 142 |
141 VLOG(1) << "[BC] Connection established with " | 143 PA_LOG(INFO) << "Connection established with " |
142 << remote_device().bluetooth_address; | 144 << remote_device().bluetooth_address; |
143 socket_ = socket; | 145 socket_ = socket; |
144 SetStatus(CONNECTED); | 146 SetStatus(CONNECTED); |
145 StartReceive(); | 147 StartReceive(); |
146 } | 148 } |
147 | 149 |
148 void BluetoothConnection::OnConnectionError(const std::string& error_message) { | 150 void BluetoothConnection::OnConnectionError(const std::string& error_message) { |
149 VLOG(1) << "[BC] Connection failed: " << error_message; | 151 PA_LOG(WARNING) << "Connection failed: " << error_message; |
150 Disconnect(); | 152 Disconnect(); |
151 } | 153 } |
152 | 154 |
153 void BluetoothConnection::OnSend(int bytes_sent) { | 155 void BluetoothConnection::OnSend(int bytes_sent) { |
154 VLOG(1) << "[BC] Successfully sent " << bytes_sent << " bytes."; | 156 PA_LOG(INFO) << "Successfully sent " << bytes_sent << " bytes."; |
155 OnDidSendMessage(*pending_message_, true); | 157 OnDidSendMessage(*pending_message_, true); |
156 pending_message_.reset(); | 158 pending_message_.reset(); |
157 } | 159 } |
158 | 160 |
159 void BluetoothConnection::OnSendError(const std::string& error_message) { | 161 void BluetoothConnection::OnSendError(const std::string& error_message) { |
160 VLOG(1) << "[BC] Error when sending bytes: " << error_message; | 162 PA_LOG(WARNING) << "Error when sending bytes: " << error_message; |
161 OnDidSendMessage(*pending_message_, false); | 163 OnDidSendMessage(*pending_message_, false); |
162 pending_message_.reset(); | 164 pending_message_.reset(); |
163 | 165 |
164 Disconnect(); | 166 Disconnect(); |
165 } | 167 } |
166 | 168 |
167 void BluetoothConnection::OnReceive(int bytes_received, | 169 void BluetoothConnection::OnReceive(int bytes_received, |
168 scoped_refptr<net::IOBuffer> buffer) { | 170 scoped_refptr<net::IOBuffer> buffer) { |
169 VLOG(1) << "[BC] Received " << bytes_received << " bytes."; | 171 PA_LOG(INFO) << "Received " << bytes_received << " bytes."; |
170 OnBytesReceived(std::string(buffer->data(), bytes_received)); | 172 OnBytesReceived(std::string(buffer->data(), bytes_received)); |
171 | 173 |
172 // Post a task to delay the read until the socket is available, as | 174 // Post a task to delay the read until the socket is available, as |
173 // calling StartReceive at this point would error with ERR_IO_PENDING. | 175 // calling StartReceive at this point would error with ERR_IO_PENDING. |
174 base::ThreadTaskRunnerHandle::Get()->PostTask( | 176 base::ThreadTaskRunnerHandle::Get()->PostTask( |
175 FROM_HERE, base::Bind(&BluetoothConnection::StartReceive, | 177 FROM_HERE, base::Bind(&BluetoothConnection::StartReceive, |
176 weak_ptr_factory_.GetWeakPtr())); | 178 weak_ptr_factory_.GetWeakPtr())); |
177 } | 179 } |
178 | 180 |
179 void BluetoothConnection::OnReceiveError( | 181 void BluetoothConnection::OnReceiveError( |
180 device::BluetoothSocket::ErrorReason error_reason, | 182 device::BluetoothSocket::ErrorReason error_reason, |
181 const std::string& error_message) { | 183 const std::string& error_message) { |
182 VLOG(1) << "[BC] Error receiving bytes: " << error_message; | 184 PA_LOG(WARNING) << "Error receiving bytes: " << error_message; |
183 | 185 |
184 // Post a task to delay the read until the socket is available, as | 186 // Post a task to delay the read until the socket is available, as |
185 // calling StartReceive at this point would error with ERR_IO_PENDING. | 187 // calling StartReceive at this point would error with ERR_IO_PENDING. |
186 base::ThreadTaskRunnerHandle::Get()->PostTask( | 188 base::ThreadTaskRunnerHandle::Get()->PostTask( |
187 FROM_HERE, base::Bind(&BluetoothConnection::StartReceive, | 189 FROM_HERE, base::Bind(&BluetoothConnection::StartReceive, |
188 weak_ptr_factory_.GetWeakPtr())); | 190 weak_ptr_factory_.GetWeakPtr())); |
189 } | 191 } |
190 | 192 |
191 } // namespace proximity_auth | 193 } // namespace proximity_auth |
OLD | NEW |