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" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 81 |
82 // Send it. | 82 // Send it. |
83 pending_message_ = message.Pass(); | 83 pending_message_ = message.Pass(); |
84 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); | 84 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); |
85 socket_->Send(buffer, | 85 socket_->Send(buffer, |
86 message_length, | 86 message_length, |
87 base::Bind(&BluetoothConnection::OnSend, weak_this), | 87 base::Bind(&BluetoothConnection::OnSend, weak_this), |
88 base::Bind(&BluetoothConnection::OnSendError, weak_this)); | 88 base::Bind(&BluetoothConnection::OnSendError, weak_this)); |
89 } | 89 } |
90 | 90 |
| 91 void BluetoothConnection::DeviceChanged(device::BluetoothAdapter* adapter, |
| 92 device::BluetoothDevice* device) { |
| 93 DCHECK_EQ(adapter, adapter_.get()); |
| 94 if (device->GetAddress() == remote_device().bluetooth_address && |
| 95 status() != DISCONNECTED && !device->IsConnected()) { |
| 96 PA_LOG(INFO) << "Device disconnected..."; |
| 97 Disconnect(); |
| 98 } |
| 99 } |
| 100 |
91 void BluetoothConnection::DeviceRemoved(device::BluetoothAdapter* adapter, | 101 void BluetoothConnection::DeviceRemoved(device::BluetoothAdapter* adapter, |
92 device::BluetoothDevice* device) { | 102 device::BluetoothDevice* device) { |
93 DCHECK_EQ(adapter, adapter_.get()); | 103 DCHECK_EQ(adapter, adapter_.get()); |
94 if (device->GetAddress() != remote_device().bluetooth_address) | 104 if (device->GetAddress() != remote_device().bluetooth_address) |
95 return; | 105 return; |
96 | 106 |
97 DCHECK_NE(status(), DISCONNECTED); | 107 DCHECK_NE(status(), DISCONNECTED); |
98 PA_LOG(INFO) << "Device disconnected..."; | 108 PA_LOG(INFO) << "Device disconnected..."; |
99 Disconnect(); | 109 if (status() != DISCONNECTED) |
| 110 Disconnect(); |
100 } | 111 } |
101 | 112 |
102 void BluetoothConnection::StartReceive() { | 113 void BluetoothConnection::StartReceive() { |
103 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); | 114 base::WeakPtr<BluetoothConnection> weak_this = weak_ptr_factory_.GetWeakPtr(); |
104 socket_->Receive(kReceiveBufferSizeBytes, | 115 socket_->Receive(kReceiveBufferSizeBytes, |
105 base::Bind(&BluetoothConnection::OnReceive, weak_this), | 116 base::Bind(&BluetoothConnection::OnReceive, weak_this), |
106 base::Bind(&BluetoothConnection::OnReceiveError, weak_this)); | 117 base::Bind(&BluetoothConnection::OnReceiveError, weak_this)); |
107 } | 118 } |
108 | 119 |
109 void BluetoothConnection::OnAdapterInitialized( | 120 void BluetoothConnection::OnAdapterInitialized( |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 PA_LOG(WARNING) << "Error receiving bytes: " << error_message; | 195 PA_LOG(WARNING) << "Error receiving bytes: " << error_message; |
185 | 196 |
186 // Post a task to delay the read until the socket is available, as | 197 // Post a task to delay the read until the socket is available, as |
187 // calling StartReceive at this point would error with ERR_IO_PENDING. | 198 // calling StartReceive at this point would error with ERR_IO_PENDING. |
188 base::ThreadTaskRunnerHandle::Get()->PostTask( | 199 base::ThreadTaskRunnerHandle::Get()->PostTask( |
189 FROM_HERE, base::Bind(&BluetoothConnection::StartReceive, | 200 FROM_HERE, base::Bind(&BluetoothConnection::StartReceive, |
190 weak_ptr_factory_.GetWeakPtr())); | 201 weak_ptr_factory_.GetWeakPtr())); |
191 } | 202 } |
192 | 203 |
193 } // namespace proximity_auth | 204 } // namespace proximity_auth |
OLD | NEW |