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/location.h" |
9 #include "base/numerics/safe_conversions.h" | 9 #include "base/numerics/safe_conversions.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/thread_task_runner_handle.h" |
10 #include "components/proximity_auth/remote_device.h" | 12 #include "components/proximity_auth/remote_device.h" |
11 #include "components/proximity_auth/wire_message.h" | 13 #include "components/proximity_auth/wire_message.h" |
12 #include "device/bluetooth/bluetooth_adapter_factory.h" | 14 #include "device/bluetooth/bluetooth_adapter_factory.h" |
13 #include "device/bluetooth/bluetooth_device.h" | 15 #include "device/bluetooth/bluetooth_device.h" |
14 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
15 | 17 |
16 namespace proximity_auth { | 18 namespace proximity_auth { |
17 namespace { | 19 namespace { |
18 const int kReceiveBufferSizeBytes = 1024; | 20 const int kReceiveBufferSizeBytes = 1024; |
19 } | 21 } |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 Disconnect(); | 164 Disconnect(); |
163 } | 165 } |
164 | 166 |
165 void BluetoothConnection::OnReceive(int bytes_received, | 167 void BluetoothConnection::OnReceive(int bytes_received, |
166 scoped_refptr<net::IOBuffer> buffer) { | 168 scoped_refptr<net::IOBuffer> buffer) { |
167 VLOG(1) << "[BC] Received " << bytes_received << " bytes."; | 169 VLOG(1) << "[BC] Received " << bytes_received << " bytes."; |
168 OnBytesReceived(std::string(buffer->data(), bytes_received)); | 170 OnBytesReceived(std::string(buffer->data(), bytes_received)); |
169 | 171 |
170 // Post a task to delay the read until the socket is available, as | 172 // Post a task to delay the read until the socket is available, as |
171 // calling StartReceive at this point would error with ERR_IO_PENDING. | 173 // calling StartReceive at this point would error with ERR_IO_PENDING. |
172 base::MessageLoop::current()->PostTask( | 174 base::ThreadTaskRunnerHandle::Get()->PostTask( |
173 FROM_HERE, | 175 FROM_HERE, base::Bind(&BluetoothConnection::StartReceive, |
174 base::Bind(&BluetoothConnection::StartReceive, | 176 weak_ptr_factory_.GetWeakPtr())); |
175 weak_ptr_factory_.GetWeakPtr())); | |
176 } | 177 } |
177 | 178 |
178 void BluetoothConnection::OnReceiveError( | 179 void BluetoothConnection::OnReceiveError( |
179 device::BluetoothSocket::ErrorReason error_reason, | 180 device::BluetoothSocket::ErrorReason error_reason, |
180 const std::string& error_message) { | 181 const std::string& error_message) { |
181 VLOG(1) << "[BC] Error receiving bytes: " << error_message; | 182 VLOG(1) << "[BC] Error receiving bytes: " << error_message; |
182 | 183 |
183 // Post a task to delay the read until the socket is available, as | 184 // Post a task to delay the read until the socket is available, as |
184 // calling StartReceive at this point would error with ERR_IO_PENDING. | 185 // calling StartReceive at this point would error with ERR_IO_PENDING. |
185 base::MessageLoop::current()->PostTask( | 186 base::ThreadTaskRunnerHandle::Get()->PostTask( |
186 FROM_HERE, | 187 FROM_HERE, base::Bind(&BluetoothConnection::StartReceive, |
187 base::Bind(&BluetoothConnection::StartReceive, | 188 weak_ptr_factory_.GetWeakPtr())); |
188 weak_ptr_factory_.GetWeakPtr())); | |
189 } | 189 } |
190 | 190 |
191 } // namespace proximity_auth | 191 } // namespace proximity_auth |
OLD | NEW |