OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "device/bluetooth/bluetooth_socket_chromeos.h" | 5 #include "device/bluetooth/bluetooth_socket_bluez.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/sequenced_task_runner.h" | 18 #include "base/sequenced_task_runner.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
21 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" |
22 #include "base/threading/worker_pool.h" | 22 #include "base/threading/worker_pool.h" |
23 #include "dbus/bus.h" | 23 #include "dbus/bus.h" |
24 #include "dbus/file_descriptor.h" | 24 #include "dbus/file_descriptor.h" |
25 #include "dbus/object_path.h" | 25 #include "dbus/object_path.h" |
26 #include "device/bluetooth/bluetooth_adapter.h" | 26 #include "device/bluetooth/bluetooth_adapter.h" |
27 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 27 #include "device/bluetooth/bluetooth_adapter_bluez.h" |
28 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" | 28 #include "device/bluetooth/bluetooth_adapter_profile_bluez.h" |
29 #include "device/bluetooth/bluetooth_device.h" | 29 #include "device/bluetooth/bluetooth_device.h" |
30 #include "device/bluetooth/bluetooth_device_chromeos.h" | 30 #include "device/bluetooth/bluetooth_device_bluez.h" |
31 #include "device/bluetooth/bluetooth_socket.h" | 31 #include "device/bluetooth/bluetooth_socket.h" |
32 #include "device/bluetooth/bluetooth_socket_net.h" | 32 #include "device/bluetooth/bluetooth_socket_net.h" |
33 #include "device/bluetooth/bluetooth_socket_thread.h" | 33 #include "device/bluetooth/bluetooth_socket_thread.h" |
34 #include "device/bluetooth/dbus/bluetooth_device_client.h" | 34 #include "device/bluetooth/dbus/bluetooth_device_client.h" |
35 #include "device/bluetooth/dbus/bluetooth_profile_manager_client.h" | 35 #include "device/bluetooth/dbus/bluetooth_profile_manager_client.h" |
36 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h" | 36 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h" |
37 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 37 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
38 #include "net/base/ip_endpoint.h" | 38 #include "net/base/ip_endpoint.h" |
39 #include "net/base/net_errors.h" | 39 #include "net/base/net_errors.h" |
40 #include "third_party/cros_system_api/dbus/service_constants.h" | 40 #include "third_party/cros_system_api/dbus/service_constants.h" |
41 | 41 |
42 using device::BluetoothAdapter; | 42 using device::BluetoothAdapter; |
43 using device::BluetoothDevice; | 43 using device::BluetoothDevice; |
44 using device::BluetoothSocketThread; | 44 using device::BluetoothSocketThread; |
45 using device::BluetoothUUID; | 45 using device::BluetoothUUID; |
46 | 46 |
47 namespace { | 47 namespace { |
48 | 48 |
49 const char kAcceptFailed[] = "Failed to accept connection."; | 49 const char kAcceptFailed[] = "Failed to accept connection."; |
50 const char kInvalidUUID[] = "Invalid UUID"; | 50 const char kInvalidUUID[] = "Invalid UUID"; |
51 const char kSocketNotListening[] = "Socket is not listening."; | 51 const char kSocketNotListening[] = "Socket is not listening."; |
52 | 52 |
53 } // namespace | 53 } // namespace |
54 | 54 |
55 namespace chromeos { | 55 namespace bluez { |
56 | 56 |
57 // static | 57 // static |
58 scoped_refptr<BluetoothSocketChromeOS> | 58 scoped_refptr<BluetoothSocketBlueZ> BluetoothSocketBlueZ::CreateBluetoothSocket( |
59 BluetoothSocketChromeOS::CreateBluetoothSocket( | |
60 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 59 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
61 scoped_refptr<BluetoothSocketThread> socket_thread) { | 60 scoped_refptr<BluetoothSocketThread> socket_thread) { |
62 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); | 61 DCHECK(ui_task_runner->RunsTasksOnCurrentThread()); |
63 | 62 |
64 return make_scoped_refptr( | 63 return make_scoped_refptr( |
65 new BluetoothSocketChromeOS(ui_task_runner, socket_thread)); | 64 new BluetoothSocketBlueZ(ui_task_runner, socket_thread)); |
66 } | 65 } |
67 | 66 |
68 BluetoothSocketChromeOS::AcceptRequest::AcceptRequest() {} | 67 BluetoothSocketBlueZ::AcceptRequest::AcceptRequest() {} |
69 | 68 |
70 BluetoothSocketChromeOS::AcceptRequest::~AcceptRequest() {} | 69 BluetoothSocketBlueZ::AcceptRequest::~AcceptRequest() {} |
71 | 70 |
72 BluetoothSocketChromeOS::ConnectionRequest::ConnectionRequest() | 71 BluetoothSocketBlueZ::ConnectionRequest::ConnectionRequest() |
73 : accepting(false), | 72 : accepting(false), cancelled(false) {} |
74 cancelled(false) {} | |
75 | 73 |
76 BluetoothSocketChromeOS::ConnectionRequest::~ConnectionRequest() {} | 74 BluetoothSocketBlueZ::ConnectionRequest::~ConnectionRequest() {} |
77 | 75 |
78 BluetoothSocketChromeOS::BluetoothSocketChromeOS( | 76 BluetoothSocketBlueZ::BluetoothSocketBlueZ( |
79 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 77 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
80 scoped_refptr<BluetoothSocketThread> socket_thread) | 78 scoped_refptr<BluetoothSocketThread> socket_thread) |
81 : BluetoothSocketNet(ui_task_runner, socket_thread), profile_(nullptr) { | 79 : BluetoothSocketNet(ui_task_runner, socket_thread), profile_(nullptr) {} |
82 } | |
83 | 80 |
84 BluetoothSocketChromeOS::~BluetoothSocketChromeOS() { | 81 BluetoothSocketBlueZ::~BluetoothSocketBlueZ() { |
85 DCHECK(!profile_); | 82 DCHECK(!profile_); |
86 | 83 |
87 if (adapter_.get()) { | 84 if (adapter_.get()) { |
88 adapter_->RemoveObserver(this); | 85 adapter_->RemoveObserver(this); |
89 adapter_ = nullptr; | 86 adapter_ = nullptr; |
90 } | 87 } |
91 } | 88 } |
92 | 89 |
93 void BluetoothSocketChromeOS::Connect( | 90 void BluetoothSocketBlueZ::Connect( |
94 const BluetoothDeviceChromeOS* device, | 91 const BluetoothDeviceBlueZ* device, |
95 const BluetoothUUID& uuid, | 92 const BluetoothUUID& uuid, |
96 SecurityLevel security_level, | 93 SecurityLevel security_level, |
97 const base::Closure& success_callback, | 94 const base::Closure& success_callback, |
98 const ErrorCompletionCallback& error_callback) { | 95 const ErrorCompletionCallback& error_callback) { |
99 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 96 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
100 DCHECK(!profile_); | 97 DCHECK(!profile_); |
101 | 98 |
102 if (!uuid.IsValid()) { | 99 if (!uuid.IsValid()) { |
103 error_callback.Run(kInvalidUUID); | 100 error_callback.Run(kInvalidUUID); |
104 return; | 101 return; |
105 } | 102 } |
106 | 103 |
107 device_address_ = device->GetAddress(); | 104 device_address_ = device->GetAddress(); |
108 device_path_ = device->object_path(); | 105 device_path_ = device->object_path(); |
109 uuid_ = uuid; | 106 uuid_ = uuid; |
110 options_.reset(new bluez::BluetoothProfileManagerClient::Options()); | 107 options_.reset(new bluez::BluetoothProfileManagerClient::Options()); |
111 if (security_level == SECURITY_LEVEL_LOW) | 108 if (security_level == SECURITY_LEVEL_LOW) |
112 options_->require_authentication.reset(new bool(false)); | 109 options_->require_authentication.reset(new bool(false)); |
113 | 110 |
114 adapter_ = device->adapter(); | 111 adapter_ = device->adapter(); |
115 | 112 |
116 RegisterProfile(device->adapter(), success_callback, error_callback); | 113 RegisterProfile(device->adapter(), success_callback, error_callback); |
117 } | 114 } |
118 | 115 |
119 void BluetoothSocketChromeOS::Listen( | 116 void BluetoothSocketBlueZ::Listen( |
120 scoped_refptr<BluetoothAdapter> adapter, | 117 scoped_refptr<BluetoothAdapter> adapter, |
121 SocketType socket_type, | 118 SocketType socket_type, |
122 const BluetoothUUID& uuid, | 119 const BluetoothUUID& uuid, |
123 const BluetoothAdapter::ServiceOptions& service_options, | 120 const BluetoothAdapter::ServiceOptions& service_options, |
124 const base::Closure& success_callback, | 121 const base::Closure& success_callback, |
125 const ErrorCompletionCallback& error_callback) { | 122 const ErrorCompletionCallback& error_callback) { |
126 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 123 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
127 DCHECK(!profile_); | 124 DCHECK(!profile_); |
128 | 125 |
129 if (!uuid.IsValid()) { | 126 if (!uuid.IsValid()) { |
(...skipping 15 matching lines...) Expand all Loading... | |
145 new uint16(service_options.channel ? *service_options.channel : 0)); | 142 new uint16(service_options.channel ? *service_options.channel : 0)); |
146 break; | 143 break; |
147 case kL2cap: | 144 case kL2cap: |
148 options_->psm.reset( | 145 options_->psm.reset( |
149 new uint16(service_options.psm ? *service_options.psm : 0)); | 146 new uint16(service_options.psm ? *service_options.psm : 0)); |
150 break; | 147 break; |
151 default: | 148 default: |
152 NOTREACHED(); | 149 NOTREACHED(); |
153 } | 150 } |
154 | 151 |
155 RegisterProfile(static_cast<BluetoothAdapterChromeOS*>(adapter.get()), | 152 RegisterProfile(static_cast<BluetoothAdapterBlueZ*>(adapter.get()), |
156 success_callback, error_callback); | 153 success_callback, error_callback); |
157 } | 154 } |
158 | 155 |
159 void BluetoothSocketChromeOS::Close() { | 156 void BluetoothSocketBlueZ::Close() { |
160 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 157 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
161 | 158 |
162 if (profile_) | 159 if (profile_) |
163 UnregisterProfile(); | 160 UnregisterProfile(); |
164 | 161 |
165 // In the case below, where an asynchronous task gets posted on the socket | 162 // In the case below, where an asynchronous task gets posted on the socket |
166 // thread in BluetoothSocketNet::Close, a reference will be held to this | 163 // thread in BluetoothSocketNet::Close, a reference will be held to this |
167 // socket by the callback. This may cause the BluetoothAdapter to outlive | 164 // socket by the callback. This may cause the BluetoothAdapter to outlive |
168 // BluezDBusManager during shutdown if that callback executes too late. | 165 // BluezDBusManager during shutdown if that callback executes too late. |
169 if (adapter_.get()) { | 166 if (adapter_.get()) { |
170 adapter_->RemoveObserver(this); | 167 adapter_->RemoveObserver(this); |
171 adapter_ = nullptr; | 168 adapter_ = nullptr; |
172 } | 169 } |
173 | 170 |
174 if (!device_path_.value().empty()) { | 171 if (!device_path_.value().empty()) { |
175 BluetoothSocketNet::Close(); | 172 BluetoothSocketNet::Close(); |
176 } else { | 173 } else { |
177 DoCloseListening(); | 174 DoCloseListening(); |
178 } | 175 } |
179 } | 176 } |
180 | 177 |
181 void BluetoothSocketChromeOS::Disconnect(const base::Closure& callback) { | 178 void BluetoothSocketBlueZ::Disconnect(const base::Closure& callback) { |
182 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 179 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
183 | 180 |
184 if (profile_) | 181 if (profile_) |
185 UnregisterProfile(); | 182 UnregisterProfile(); |
186 | 183 |
187 if (!device_path_.value().empty()) { | 184 if (!device_path_.value().empty()) { |
188 BluetoothSocketNet::Disconnect(callback); | 185 BluetoothSocketNet::Disconnect(callback); |
189 } else { | 186 } else { |
190 DoCloseListening(); | 187 DoCloseListening(); |
191 callback.Run(); | 188 callback.Run(); |
192 } | 189 } |
193 } | 190 } |
194 | 191 |
195 void BluetoothSocketChromeOS::Accept( | 192 void BluetoothSocketBlueZ::Accept( |
196 const AcceptCompletionCallback& success_callback, | 193 const AcceptCompletionCallback& success_callback, |
197 const ErrorCompletionCallback& error_callback) { | 194 const ErrorCompletionCallback& error_callback) { |
198 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 195 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
199 | 196 |
200 if (!device_path_.value().empty()) { | 197 if (!device_path_.value().empty()) { |
201 error_callback.Run(kSocketNotListening); | 198 error_callback.Run(kSocketNotListening); |
202 return; | 199 return; |
203 } | 200 } |
204 | 201 |
205 // Only one pending accept at a time | 202 // Only one pending accept at a time |
206 if (accept_request_.get()) { | 203 if (accept_request_.get()) { |
207 error_callback.Run(net::ErrorToString(net::ERR_IO_PENDING)); | 204 error_callback.Run(net::ErrorToString(net::ERR_IO_PENDING)); |
208 return; | 205 return; |
209 } | 206 } |
210 | 207 |
211 accept_request_.reset(new AcceptRequest); | 208 accept_request_.reset(new AcceptRequest); |
212 accept_request_->success_callback = success_callback; | 209 accept_request_->success_callback = success_callback; |
213 accept_request_->error_callback = error_callback; | 210 accept_request_->error_callback = error_callback; |
214 | 211 |
215 if (connection_request_queue_.size() >= 1) { | 212 if (connection_request_queue_.size() >= 1) { |
216 AcceptConnectionRequest(); | 213 AcceptConnectionRequest(); |
217 } | 214 } |
218 } | 215 } |
219 | 216 |
220 void BluetoothSocketChromeOS::RegisterProfile( | 217 void BluetoothSocketBlueZ::RegisterProfile( |
221 BluetoothAdapterChromeOS* adapter, | 218 BluetoothAdapterBlueZ* adapter, |
222 const base::Closure& success_callback, | 219 const base::Closure& success_callback, |
223 const ErrorCompletionCallback& error_callback) { | 220 const ErrorCompletionCallback& error_callback) { |
224 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 221 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
225 DCHECK(!profile_); | 222 DCHECK(!profile_); |
226 DCHECK(adapter); | 223 DCHECK(adapter); |
227 | 224 |
228 // If the adapter is not present, this is a listening socket and the | 225 // If the adapter is not present, this is a listening socket and the |
229 // adapter isn't running yet. Report success and carry on; | 226 // adapter isn't running yet. Report success and carry on; |
230 // the profile will be registered when the daemon becomes available. | 227 // the profile will be registered when the daemon becomes available. |
231 if (!adapter->IsPresent()) { | 228 if (!adapter->IsPresent()) { |
232 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() | 229 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() |
233 << ": Delaying profile registration."; | 230 << ": Delaying profile registration."; |
234 base::MessageLoop::current()->PostTask(FROM_HERE, success_callback); | 231 base::MessageLoop::current()->PostTask(FROM_HERE, success_callback); |
235 return; | 232 return; |
236 } | 233 } |
237 | 234 |
238 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() | 235 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() |
239 << ": Acquiring profile."; | 236 << ": Acquiring profile."; |
240 | 237 |
241 adapter->UseProfile( | 238 adapter->UseProfile(uuid_, device_path_, *options_, this, |
242 uuid_, device_path_, *options_, this, | 239 base::Bind(&BluetoothSocketBlueZ::OnRegisterProfile, this, |
243 base::Bind(&BluetoothSocketChromeOS::OnRegisterProfile, this, | 240 success_callback, error_callback), |
244 success_callback, error_callback), | 241 base::Bind(&BluetoothSocketBlueZ::OnRegisterProfileError, |
245 base::Bind(&BluetoothSocketChromeOS::OnRegisterProfileError, this, | 242 this, error_callback)); |
246 error_callback)); | |
247 } | 243 } |
248 | 244 |
249 void BluetoothSocketChromeOS::OnRegisterProfile( | 245 void BluetoothSocketBlueZ::OnRegisterProfile( |
250 const base::Closure& success_callback, | 246 const base::Closure& success_callback, |
251 const ErrorCompletionCallback& error_callback, | 247 const ErrorCompletionCallback& error_callback, |
252 BluetoothAdapterProfileChromeOS* profile) { | 248 BluetoothAdapterProfileBlueZ* profile) { |
253 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 249 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
254 DCHECK(!profile_); | 250 DCHECK(!profile_); |
255 | 251 |
256 profile_ = profile; | 252 profile_ = profile; |
257 | 253 |
258 if (device_path_.value().empty()) { | 254 if (device_path_.value().empty()) { |
259 VLOG(1) << uuid_.canonical_value() << ": Profile registered."; | 255 VLOG(1) << uuid_.canonical_value() << ": Profile registered."; |
260 success_callback.Run(); | 256 success_callback.Run(); |
261 return; | 257 return; |
262 } | 258 } |
263 | 259 |
264 VLOG(1) << uuid_.canonical_value() << ": Got profile, connecting to " | 260 VLOG(1) << uuid_.canonical_value() << ": Got profile, connecting to " |
265 << device_path_.value(); | 261 << device_path_.value(); |
266 | 262 |
267 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | 263 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( |
268 device_path_, uuid_.canonical_value(), | 264 device_path_, uuid_.canonical_value(), |
269 base::Bind(&BluetoothSocketChromeOS::OnConnectProfile, this, | 265 base::Bind(&BluetoothSocketBlueZ::OnConnectProfile, this, |
270 success_callback), | 266 success_callback), |
271 base::Bind(&BluetoothSocketChromeOS::OnConnectProfileError, this, | 267 base::Bind(&BluetoothSocketBlueZ::OnConnectProfileError, this, |
272 error_callback)); | 268 error_callback)); |
273 } | 269 } |
274 | 270 |
275 void BluetoothSocketChromeOS::OnRegisterProfileError( | 271 void BluetoothSocketBlueZ::OnRegisterProfileError( |
276 const ErrorCompletionCallback& error_callback, | 272 const ErrorCompletionCallback& error_callback, |
277 const std::string& error_message) { | 273 const std::string& error_message) { |
278 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 274 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
279 | 275 |
280 LOG(WARNING) << uuid_.canonical_value() | 276 LOG(WARNING) << uuid_.canonical_value() |
281 << ": Failed to register profile: " << error_message; | 277 << ": Failed to register profile: " << error_message; |
282 error_callback.Run(error_message); | 278 error_callback.Run(error_message); |
283 } | 279 } |
284 | 280 |
285 void BluetoothSocketChromeOS::OnConnectProfile( | 281 void BluetoothSocketBlueZ::OnConnectProfile( |
286 const base::Closure& success_callback) { | 282 const base::Closure& success_callback) { |
287 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 283 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
288 DCHECK(profile_); | 284 DCHECK(profile_); |
289 | 285 |
290 VLOG(1) << profile_->object_path().value() << ": Profile connected."; | 286 VLOG(1) << profile_->object_path().value() << ": Profile connected."; |
291 UnregisterProfile(); | 287 UnregisterProfile(); |
292 success_callback.Run(); | 288 success_callback.Run(); |
293 } | 289 } |
294 | 290 |
295 void BluetoothSocketChromeOS::OnConnectProfileError( | 291 void BluetoothSocketBlueZ::OnConnectProfileError( |
296 const ErrorCompletionCallback& error_callback, | 292 const ErrorCompletionCallback& error_callback, |
297 const std::string& error_name, | 293 const std::string& error_name, |
298 const std::string& error_message) { | 294 const std::string& error_message) { |
299 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 295 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
300 DCHECK(profile_); | 296 DCHECK(profile_); |
301 | 297 |
302 LOG(WARNING) << profile_->object_path().value() | 298 LOG(WARNING) << profile_->object_path().value() |
303 << ": Failed to connect profile: " << error_name << ": " | 299 << ": Failed to connect profile: " << error_name << ": " |
304 << error_message; | 300 << error_message; |
305 UnregisterProfile(); | 301 UnregisterProfile(); |
306 error_callback.Run(error_message); | 302 error_callback.Run(error_message); |
307 } | 303 } |
308 | 304 |
309 void BluetoothSocketChromeOS::AdapterPresentChanged(BluetoothAdapter* adapter, | 305 void BluetoothSocketBlueZ::AdapterPresentChanged(BluetoothAdapter* adapter, |
310 bool present) { | 306 bool present) { |
311 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 307 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
312 | 308 |
313 if (!present) { | 309 if (!present) { |
314 // Adapter removed, we can't use the profile anymore. | 310 // Adapter removed, we can't use the profile anymore. |
315 UnregisterProfile(); | 311 UnregisterProfile(); |
316 return; | 312 return; |
317 } | 313 } |
318 | 314 |
319 DCHECK(!profile_); | 315 DCHECK(!profile_); |
320 | 316 |
321 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() | 317 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() |
322 << ": Acquiring profile."; | 318 << ": Acquiring profile."; |
323 | 319 |
324 static_cast<BluetoothAdapterChromeOS*>(adapter)->UseProfile( | 320 static_cast<BluetoothAdapterBlueZ*>(adapter)->UseProfile( |
325 uuid_, device_path_, *options_, this, | 321 uuid_, device_path_, *options_, this, |
326 base::Bind(&BluetoothSocketChromeOS::OnInternalRegisterProfile, this), | 322 base::Bind(&BluetoothSocketBlueZ::OnInternalRegisterProfile, this), |
327 base::Bind(&BluetoothSocketChromeOS::OnInternalRegisterProfileError, | 323 base::Bind(&BluetoothSocketBlueZ::OnInternalRegisterProfileError, this)); |
328 this)); | |
329 } | 324 } |
330 | 325 |
331 void BluetoothSocketChromeOS::OnInternalRegisterProfile( | 326 void BluetoothSocketBlueZ::OnInternalRegisterProfile( |
332 BluetoothAdapterProfileChromeOS* profile) { | 327 BluetoothAdapterProfileBlueZ* profile) { |
333 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 328 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
334 DCHECK(!profile_); | 329 DCHECK(!profile_); |
335 | 330 |
336 profile_ = profile; | 331 profile_ = profile; |
337 | 332 |
338 VLOG(1) << uuid_.canonical_value() << ": Profile re-registered"; | 333 VLOG(1) << uuid_.canonical_value() << ": Profile re-registered"; |
339 } | 334 } |
340 | 335 |
341 void BluetoothSocketChromeOS::OnInternalRegisterProfileError( | 336 void BluetoothSocketBlueZ::OnInternalRegisterProfileError( |
342 const std::string& error_message) { | 337 const std::string& error_message) { |
343 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 338 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
344 | 339 |
345 LOG(WARNING) << "Failed to re-register profile: " << error_message; | 340 LOG(WARNING) << "Failed to re-register profile: " << error_message; |
346 } | 341 } |
347 | 342 |
348 void BluetoothSocketChromeOS::Released() { | 343 void BluetoothSocketBlueZ::Released() { |
349 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 344 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
350 DCHECK(profile_); | 345 DCHECK(profile_); |
351 | 346 |
352 VLOG(1) << profile_->object_path().value() << ": Release"; | 347 VLOG(1) << profile_->object_path().value() << ": Release"; |
353 } | 348 } |
354 | 349 |
355 void BluetoothSocketChromeOS::NewConnection( | 350 void BluetoothSocketBlueZ::NewConnection( |
356 const dbus::ObjectPath& device_path, | 351 const dbus::ObjectPath& device_path, |
357 scoped_ptr<dbus::FileDescriptor> fd, | 352 scoped_ptr<dbus::FileDescriptor> fd, |
358 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, | 353 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, |
359 const ConfirmationCallback& callback) { | 354 const ConfirmationCallback& callback) { |
360 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 355 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
361 | 356 |
362 VLOG(1) << uuid_.canonical_value() | 357 VLOG(1) << uuid_.canonical_value() |
363 << ": New connection from device: " << device_path.value(); | 358 << ": New connection from device: " << device_path.value(); |
364 | 359 |
365 if (!device_path_.value().empty()) { | 360 if (!device_path_.value().empty()) { |
366 DCHECK(device_path_ == device_path); | 361 DCHECK(device_path_ == device_path); |
367 | 362 |
368 socket_thread()->task_runner()->PostTask( | 363 socket_thread()->task_runner()->PostTask( |
369 FROM_HERE, | 364 FROM_HERE, |
370 base::Bind( | 365 base::Bind(&BluetoothSocketBlueZ::DoNewConnection, this, device_path_, |
371 &BluetoothSocketChromeOS::DoNewConnection, | 366 base::Passed(&fd), options, callback)); |
372 this, | |
373 device_path_, | |
374 base::Passed(&fd), | |
375 options, | |
376 callback)); | |
377 } else { | 367 } else { |
378 linked_ptr<ConnectionRequest> request(new ConnectionRequest()); | 368 linked_ptr<ConnectionRequest> request(new ConnectionRequest()); |
379 request->device_path = device_path; | 369 request->device_path = device_path; |
380 request->fd = fd.Pass(); | 370 request->fd = fd.Pass(); |
381 request->options = options; | 371 request->options = options; |
382 request->callback = callback; | 372 request->callback = callback; |
383 | 373 |
384 connection_request_queue_.push(request); | 374 connection_request_queue_.push(request); |
385 VLOG(1) << uuid_.canonical_value() << ": Connection is now pending."; | 375 VLOG(1) << uuid_.canonical_value() << ": Connection is now pending."; |
386 if (accept_request_) { | 376 if (accept_request_) { |
387 AcceptConnectionRequest(); | 377 AcceptConnectionRequest(); |
388 } | 378 } |
389 } | 379 } |
390 } | 380 } |
391 | 381 |
392 void BluetoothSocketChromeOS::RequestDisconnection( | 382 void BluetoothSocketBlueZ::RequestDisconnection( |
393 const dbus::ObjectPath& device_path, | 383 const dbus::ObjectPath& device_path, |
394 const ConfirmationCallback& callback) { | 384 const ConfirmationCallback& callback) { |
395 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 385 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
396 DCHECK(profile_); | 386 DCHECK(profile_); |
397 | 387 |
398 VLOG(1) << profile_->object_path().value() << ": Request disconnection"; | 388 VLOG(1) << profile_->object_path().value() << ": Request disconnection"; |
399 callback.Run(SUCCESS); | 389 callback.Run(SUCCESS); |
400 } | 390 } |
401 | 391 |
402 void BluetoothSocketChromeOS::Cancel() { | 392 void BluetoothSocketBlueZ::Cancel() { |
403 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 393 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
404 DCHECK(profile_); | 394 DCHECK(profile_); |
405 | 395 |
406 VLOG(1) << profile_->object_path().value() << ": Cancel"; | 396 VLOG(1) << profile_->object_path().value() << ": Cancel"; |
407 | 397 |
408 if (!connection_request_queue_.size()) | 398 if (!connection_request_queue_.size()) |
409 return; | 399 return; |
410 | 400 |
411 // If the front request is being accepted mark it as cancelled, otherwise | 401 // If the front request is being accepted mark it as cancelled, otherwise |
412 // just pop it from the queue. | 402 // just pop it from the queue. |
413 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 403 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
414 if (!request->accepting) { | 404 if (!request->accepting) { |
415 request->cancelled = true; | 405 request->cancelled = true; |
416 } else { | 406 } else { |
417 connection_request_queue_.pop(); | 407 connection_request_queue_.pop(); |
418 } | 408 } |
419 } | 409 } |
420 | 410 |
421 void BluetoothSocketChromeOS::AcceptConnectionRequest() { | 411 void BluetoothSocketBlueZ::AcceptConnectionRequest() { |
422 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 412 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
423 DCHECK(accept_request_.get()); | 413 DCHECK(accept_request_.get()); |
424 DCHECK(connection_request_queue_.size() >= 1); | 414 DCHECK(connection_request_queue_.size() >= 1); |
425 DCHECK(profile_); | 415 DCHECK(profile_); |
426 | 416 |
427 VLOG(1) << profile_->object_path().value() | 417 VLOG(1) << profile_->object_path().value() |
428 << ": Accepting pending connection."; | 418 << ": Accepting pending connection."; |
429 | 419 |
430 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 420 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
431 request->accepting = true; | 421 request->accepting = true; |
432 | 422 |
433 BluetoothDeviceChromeOS* device = | 423 BluetoothDeviceBlueZ* device = |
434 static_cast<BluetoothAdapterChromeOS*>(adapter_.get())-> | 424 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()) |
435 GetDeviceWithPath(request->device_path); | 425 ->GetDeviceWithPath(request->device_path); |
436 DCHECK(device); | 426 DCHECK(device); |
437 | 427 |
438 scoped_refptr<BluetoothSocketChromeOS> client_socket = | 428 scoped_refptr<BluetoothSocketBlueZ> client_socket = |
439 BluetoothSocketChromeOS::CreateBluetoothSocket( | 429 BluetoothSocketBlueZ::CreateBluetoothSocket(ui_task_runner(), |
440 ui_task_runner(), socket_thread()); | 430 socket_thread()); |
441 | 431 |
442 client_socket->device_address_ = device->GetAddress(); | 432 client_socket->device_address_ = device->GetAddress(); |
443 client_socket->device_path_ = request->device_path; | 433 client_socket->device_path_ = request->device_path; |
444 client_socket->uuid_ = uuid_; | 434 client_socket->uuid_ = uuid_; |
445 | 435 |
446 socket_thread()->task_runner()->PostTask( | 436 socket_thread()->task_runner()->PostTask( |
447 FROM_HERE, | 437 FROM_HERE, |
448 base::Bind( | 438 base::Bind(&BluetoothSocketBlueZ::DoNewConnection, client_socket, |
449 &BluetoothSocketChromeOS::DoNewConnection, | 439 request->device_path, base::Passed(&request->fd), |
450 client_socket, | 440 request->options, |
451 request->device_path, | 441 base::Bind(&BluetoothSocketBlueZ::OnNewConnection, this, |
452 base::Passed(&request->fd), | 442 client_socket, request->callback))); |
453 request->options, | |
454 base::Bind(&BluetoothSocketChromeOS::OnNewConnection, | |
455 this, | |
456 client_socket, | |
457 request->callback))); | |
458 } | 443 } |
459 | 444 |
460 void BluetoothSocketChromeOS::DoNewConnection( | 445 void BluetoothSocketBlueZ::DoNewConnection( |
461 const dbus::ObjectPath& device_path, | 446 const dbus::ObjectPath& device_path, |
462 scoped_ptr<dbus::FileDescriptor> fd, | 447 scoped_ptr<dbus::FileDescriptor> fd, |
463 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, | 448 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, |
464 const ConfirmationCallback& callback) { | 449 const ConfirmationCallback& callback) { |
465 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); | 450 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); |
466 base::ThreadRestrictions::AssertIOAllowed(); | 451 base::ThreadRestrictions::AssertIOAllowed(); |
467 fd->CheckValidity(); | 452 fd->CheckValidity(); |
468 | 453 |
469 VLOG(1) << uuid_.canonical_value() << ": Validity check complete."; | 454 VLOG(1) << uuid_.canonical_value() << ": Validity check complete."; |
470 if (!fd->is_valid()) { | 455 if (!fd->is_valid()) { |
471 LOG(WARNING) << uuid_.canonical_value() << " :" << fd->value() | 456 LOG(WARNING) << uuid_.canonical_value() << " :" << fd->value() |
472 << ": Invalid file descriptor received from Bluetooth Daemon."; | 457 << ": Invalid file descriptor received from Bluetooth Daemon."; |
473 ui_task_runner()->PostTask(FROM_HERE, | 458 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); |
474 base::Bind(callback, REJECTED));; | 459 ; |
ortuno
2015/11/06 18:15:42
Why the extra ';'?
rkc
2015/11/25 00:48:17
The original code had the extra ;
git cl format ju
| |
475 return; | 460 return; |
476 } | 461 } |
477 | 462 |
478 if (tcp_socket()) { | 463 if (tcp_socket()) { |
479 LOG(WARNING) << uuid_.canonical_value() << ": Already connected"; | 464 LOG(WARNING) << uuid_.canonical_value() << ": Already connected"; |
480 ui_task_runner()->PostTask(FROM_HERE, | 465 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); |
481 base::Bind(callback, REJECTED));; | 466 ; |
ortuno
2015/11/06 18:15:42
Same here.
rkc
2015/11/25 00:48:17
Done.
| |
482 return; | 467 return; |
483 } | 468 } |
484 | 469 |
485 ResetTCPSocket(); | 470 ResetTCPSocket(); |
486 | 471 |
487 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the | 472 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the |
488 // TCPSocket implementation does not actually require one. | 473 // TCPSocket implementation does not actually require one. |
489 int net_result = tcp_socket()->AdoptConnectedSocket(fd->value(), | 474 int net_result = |
490 net::IPEndPoint()); | 475 tcp_socket()->AdoptConnectedSocket(fd->value(), net::IPEndPoint()); |
491 if (net_result != net::OK) { | 476 if (net_result != net::OK) { |
492 LOG(WARNING) << uuid_.canonical_value() << ": Error adopting socket: " | 477 LOG(WARNING) << uuid_.canonical_value() << ": Error adopting socket: " |
493 << std::string(net::ErrorToString(net_result)); | 478 << std::string(net::ErrorToString(net_result)); |
494 ui_task_runner()->PostTask(FROM_HERE, | 479 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, REJECTED)); |
495 base::Bind(callback, REJECTED));; | 480 ; |
ortuno
2015/11/06 18:15:42
Same here.
rkc
2015/11/25 00:48:17
Done.
| |
496 return; | 481 return; |
497 } | 482 } |
498 | 483 |
499 VLOG(2) << uuid_.canonical_value() | 484 VLOG(2) << uuid_.canonical_value() |
500 << ": Taking descriptor, confirming success."; | 485 << ": Taking descriptor, confirming success."; |
501 fd->TakeValue(); | 486 fd->TakeValue(); |
502 ui_task_runner()->PostTask(FROM_HERE, | 487 ui_task_runner()->PostTask(FROM_HERE, base::Bind(callback, SUCCESS)); |
503 base::Bind(callback, SUCCESS));; | 488 ; |
ortuno
2015/11/06 18:15:42
Same here.
rkc
2015/11/25 00:48:17
Done.
| |
504 } | 489 } |
505 | 490 |
506 void BluetoothSocketChromeOS::OnNewConnection( | 491 void BluetoothSocketBlueZ::OnNewConnection( |
507 scoped_refptr<BluetoothSocket> socket, | 492 scoped_refptr<BluetoothSocket> socket, |
508 const ConfirmationCallback& callback, | 493 const ConfirmationCallback& callback, |
509 Status status) { | 494 Status status) { |
510 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 495 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
511 DCHECK(accept_request_.get()); | 496 DCHECK(accept_request_.get()); |
512 DCHECK(connection_request_queue_.size() >= 1); | 497 DCHECK(connection_request_queue_.size() >= 1); |
513 | 498 |
514 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 499 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
515 if (status == SUCCESS && !request->cancelled) { | 500 if (status == SUCCESS && !request->cancelled) { |
516 BluetoothDeviceChromeOS* device = | 501 BluetoothDeviceBlueZ* device = |
517 static_cast<BluetoothAdapterChromeOS*>(adapter_.get())-> | 502 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()) |
518 GetDeviceWithPath(request->device_path); | 503 ->GetDeviceWithPath(request->device_path); |
519 DCHECK(device); | 504 DCHECK(device); |
520 | 505 |
521 accept_request_->success_callback.Run(device, socket); | 506 accept_request_->success_callback.Run(device, socket); |
522 } else { | 507 } else { |
523 accept_request_->error_callback.Run(kAcceptFailed); | 508 accept_request_->error_callback.Run(kAcceptFailed); |
524 } | 509 } |
525 | 510 |
526 accept_request_.reset(nullptr); | 511 accept_request_.reset(nullptr); |
527 connection_request_queue_.pop(); | 512 connection_request_queue_.pop(); |
528 | 513 |
529 callback.Run(status); | 514 callback.Run(status); |
530 } | 515 } |
531 | 516 |
532 void BluetoothSocketChromeOS::DoCloseListening() { | 517 void BluetoothSocketBlueZ::DoCloseListening() { |
533 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 518 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
534 | 519 |
535 if (accept_request_) { | 520 if (accept_request_) { |
536 accept_request_->error_callback.Run( | 521 accept_request_->error_callback.Run( |
537 net::ErrorToString(net::ERR_CONNECTION_CLOSED)); | 522 net::ErrorToString(net::ERR_CONNECTION_CLOSED)); |
538 accept_request_.reset(nullptr); | 523 accept_request_.reset(nullptr); |
539 } | 524 } |
540 | 525 |
541 while (connection_request_queue_.size() > 0) { | 526 while (connection_request_queue_.size() > 0) { |
542 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 527 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
543 request->callback.Run(REJECTED); | 528 request->callback.Run(REJECTED); |
544 connection_request_queue_.pop(); | 529 connection_request_queue_.pop(); |
545 } | 530 } |
546 } | 531 } |
547 | 532 |
548 void BluetoothSocketChromeOS::UnregisterProfile() { | 533 void BluetoothSocketBlueZ::UnregisterProfile() { |
549 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 534 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
550 DCHECK(profile_); | 535 DCHECK(profile_); |
551 | 536 |
552 VLOG(1) << profile_->object_path().value() << ": Release profile"; | 537 VLOG(1) << profile_->object_path().value() << ": Release profile"; |
553 | 538 |
554 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()) | 539 static_cast<BluetoothAdapterBlueZ*>(adapter_.get()) |
555 ->ReleaseProfile(device_path_, profile_); | 540 ->ReleaseProfile(device_path_, profile_); |
556 profile_ = nullptr; | 541 profile_ = nullptr; |
557 } | 542 } |
558 | 543 |
559 } // namespace chromeos | 544 } // namespace bluez |
OLD | NEW |