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_chromeos.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 "chromeos/dbus/bluetooth_device_client.h" | |
24 #include "chromeos/dbus/bluetooth_profile_manager_client.h" | |
25 #include "chromeos/dbus/bluetooth_profile_service_provider.h" | |
26 #include "chromeos/dbus/dbus_thread_manager.h" | |
27 #include "dbus/bus.h" | 23 #include "dbus/bus.h" |
28 #include "dbus/file_descriptor.h" | 24 #include "dbus/file_descriptor.h" |
29 #include "dbus/object_path.h" | 25 #include "dbus/object_path.h" |
30 #include "device/bluetooth/bluetooth_adapter.h" | 26 #include "device/bluetooth/bluetooth_adapter.h" |
31 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 27 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
32 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" | 28 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" |
33 #include "device/bluetooth/bluetooth_device.h" | 29 #include "device/bluetooth/bluetooth_device.h" |
34 #include "device/bluetooth/bluetooth_device_chromeos.h" | 30 #include "device/bluetooth/bluetooth_device_chromeos.h" |
35 #include "device/bluetooth/bluetooth_socket.h" | 31 #include "device/bluetooth/bluetooth_socket.h" |
36 #include "device/bluetooth/bluetooth_socket_net.h" | 32 #include "device/bluetooth/bluetooth_socket_net.h" |
37 #include "device/bluetooth/bluetooth_socket_thread.h" | 33 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 34 #include "device/bluetooth/dbus/bluetooth_device_client.h" |
| 35 #include "device/bluetooth/dbus/bluetooth_profile_manager_client.h" |
| 36 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.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 { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 DCHECK(!profile_); | 100 DCHECK(!profile_); |
101 | 101 |
102 if (!uuid.IsValid()) { | 102 if (!uuid.IsValid()) { |
103 error_callback.Run(kInvalidUUID); | 103 error_callback.Run(kInvalidUUID); |
104 return; | 104 return; |
105 } | 105 } |
106 | 106 |
107 device_address_ = device->GetAddress(); | 107 device_address_ = device->GetAddress(); |
108 device_path_ = device->object_path(); | 108 device_path_ = device->object_path(); |
109 uuid_ = uuid; | 109 uuid_ = uuid; |
110 options_.reset(new BluetoothProfileManagerClient::Options()); | 110 options_.reset(new bluez::BluetoothProfileManagerClient::Options()); |
111 if (security_level == SECURITY_LEVEL_LOW) | 111 if (security_level == SECURITY_LEVEL_LOW) |
112 options_->require_authentication.reset(new bool(false)); | 112 options_->require_authentication.reset(new bool(false)); |
113 | 113 |
114 adapter_ = device->adapter(); | 114 adapter_ = device->adapter(); |
115 | 115 |
116 RegisterProfile(device->adapter(), success_callback, error_callback); | 116 RegisterProfile(device->adapter(), success_callback, error_callback); |
117 } | 117 } |
118 | 118 |
119 void BluetoothSocketChromeOS::Listen( | 119 void BluetoothSocketChromeOS::Listen( |
120 scoped_refptr<BluetoothAdapter> adapter, | 120 scoped_refptr<BluetoothAdapter> adapter, |
121 SocketType socket_type, | 121 SocketType socket_type, |
122 const BluetoothUUID& uuid, | 122 const BluetoothUUID& uuid, |
123 const BluetoothAdapter::ServiceOptions& service_options, | 123 const BluetoothAdapter::ServiceOptions& service_options, |
124 const base::Closure& success_callback, | 124 const base::Closure& success_callback, |
125 const ErrorCompletionCallback& error_callback) { | 125 const ErrorCompletionCallback& error_callback) { |
126 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 126 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
127 DCHECK(!profile_); | 127 DCHECK(!profile_); |
128 | 128 |
129 if (!uuid.IsValid()) { | 129 if (!uuid.IsValid()) { |
130 error_callback.Run(kInvalidUUID); | 130 error_callback.Run(kInvalidUUID); |
131 return; | 131 return; |
132 } | 132 } |
133 | 133 |
134 adapter_ = adapter; | 134 adapter_ = adapter; |
135 adapter_->AddObserver(this); | 135 adapter_->AddObserver(this); |
136 | 136 |
137 uuid_ = uuid; | 137 uuid_ = uuid; |
138 options_.reset(new BluetoothProfileManagerClient::Options()); | 138 options_.reset(new bluez::BluetoothProfileManagerClient::Options()); |
139 if (service_options.name) | 139 if (service_options.name) |
140 options_->name.reset(new std::string(*service_options.name)); | 140 options_->name.reset(new std::string(*service_options.name)); |
141 | 141 |
142 switch (socket_type) { | 142 switch (socket_type) { |
143 case kRfcomm: | 143 case kRfcomm: |
144 options_->channel.reset( | 144 options_->channel.reset( |
145 new uint16(service_options.channel ? *service_options.channel : 0)); | 145 new uint16(service_options.channel ? *service_options.channel : 0)); |
146 break; | 146 break; |
147 case kL2cap: | 147 case kL2cap: |
148 options_->psm.reset( | 148 options_->psm.reset( |
149 new uint16(service_options.psm ? *service_options.psm : 0)); | 149 new uint16(service_options.psm ? *service_options.psm : 0)); |
150 break; | 150 break; |
151 default: | 151 default: |
152 NOTREACHED(); | 152 NOTREACHED(); |
153 } | 153 } |
154 | 154 |
155 RegisterProfile(static_cast<BluetoothAdapterChromeOS*>(adapter.get()), | 155 RegisterProfile(static_cast<BluetoothAdapterChromeOS*>(adapter.get()), |
156 success_callback, error_callback); | 156 success_callback, error_callback); |
157 } | 157 } |
158 | 158 |
159 void BluetoothSocketChromeOS::Close() { | 159 void BluetoothSocketChromeOS::Close() { |
160 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 160 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
161 | 161 |
162 if (profile_) | 162 if (profile_) |
163 UnregisterProfile(); | 163 UnregisterProfile(); |
164 | 164 |
165 // In the case below, where an asynchronous task gets posted on the socket | 165 // 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 | 166 // thread in BluetoothSocketNet::Close, a reference will be held to this |
167 // socket by the callback. This may cause the BluetoothAdapter to outlive | 167 // socket by the callback. This may cause the BluetoothAdapter to outlive |
168 // DBusThreadManager during shutdown if that callback executes too late. | 168 // BluezDBusManager during shutdown if that callback executes too late. |
169 if (adapter_.get()) { | 169 if (adapter_.get()) { |
170 adapter_->RemoveObserver(this); | 170 adapter_->RemoveObserver(this); |
171 adapter_ = nullptr; | 171 adapter_ = nullptr; |
172 } | 172 } |
173 | 173 |
174 if (!device_path_.value().empty()) { | 174 if (!device_path_.value().empty()) { |
175 BluetoothSocketNet::Close(); | 175 BluetoothSocketNet::Close(); |
176 } else { | 176 } else { |
177 DoCloseListening(); | 177 DoCloseListening(); |
178 } | 178 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 | 257 |
258 if (device_path_.value().empty()) { | 258 if (device_path_.value().empty()) { |
259 VLOG(1) << uuid_.canonical_value() << ": Profile registered."; | 259 VLOG(1) << uuid_.canonical_value() << ": Profile registered."; |
260 success_callback.Run(); | 260 success_callback.Run(); |
261 return; | 261 return; |
262 } | 262 } |
263 | 263 |
264 VLOG(1) << uuid_.canonical_value() << ": Got profile, connecting to " | 264 VLOG(1) << uuid_.canonical_value() << ": Got profile, connecting to " |
265 << device_path_.value(); | 265 << device_path_.value(); |
266 | 266 |
267 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | 267 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( |
268 device_path_, uuid_.canonical_value(), | 268 device_path_, uuid_.canonical_value(), |
269 base::Bind(&BluetoothSocketChromeOS::OnConnectProfile, this, | 269 base::Bind(&BluetoothSocketChromeOS::OnConnectProfile, this, |
270 success_callback), | 270 success_callback), |
271 base::Bind(&BluetoothSocketChromeOS::OnConnectProfileError, this, | 271 base::Bind(&BluetoothSocketChromeOS::OnConnectProfileError, this, |
272 error_callback)); | 272 error_callback)); |
273 } | 273 } |
274 | 274 |
275 void BluetoothSocketChromeOS::OnRegisterProfileError( | 275 void BluetoothSocketChromeOS::OnRegisterProfileError( |
276 const ErrorCompletionCallback& error_callback, | 276 const ErrorCompletionCallback& error_callback, |
277 const std::string& error_message) { | 277 const std::string& error_message) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 void BluetoothSocketChromeOS::Released() { | 348 void BluetoothSocketChromeOS::Released() { |
349 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 349 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
350 DCHECK(profile_); | 350 DCHECK(profile_); |
351 | 351 |
352 VLOG(1) << profile_->object_path().value() << ": Release"; | 352 VLOG(1) << profile_->object_path().value() << ": Release"; |
353 } | 353 } |
354 | 354 |
355 void BluetoothSocketChromeOS::NewConnection( | 355 void BluetoothSocketChromeOS::NewConnection( |
356 const dbus::ObjectPath& device_path, | 356 const dbus::ObjectPath& device_path, |
357 scoped_ptr<dbus::FileDescriptor> fd, | 357 scoped_ptr<dbus::FileDescriptor> fd, |
358 const BluetoothProfileServiceProvider::Delegate::Options& options, | 358 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, |
359 const ConfirmationCallback& callback) { | 359 const ConfirmationCallback& callback) { |
360 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 360 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
361 | 361 |
362 VLOG(1) << uuid_.canonical_value() | 362 VLOG(1) << uuid_.canonical_value() |
363 << ": New connection from device: " << device_path.value(); | 363 << ": New connection from device: " << device_path.value(); |
364 | 364 |
365 if (!device_path_.value().empty()) { | 365 if (!device_path_.value().empty()) { |
366 DCHECK(device_path_ == device_path); | 366 DCHECK(device_path_ == device_path); |
367 | 367 |
368 socket_thread()->task_runner()->PostTask( | 368 socket_thread()->task_runner()->PostTask( |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 request->options, | 453 request->options, |
454 base::Bind(&BluetoothSocketChromeOS::OnNewConnection, | 454 base::Bind(&BluetoothSocketChromeOS::OnNewConnection, |
455 this, | 455 this, |
456 client_socket, | 456 client_socket, |
457 request->callback))); | 457 request->callback))); |
458 } | 458 } |
459 | 459 |
460 void BluetoothSocketChromeOS::DoNewConnection( | 460 void BluetoothSocketChromeOS::DoNewConnection( |
461 const dbus::ObjectPath& device_path, | 461 const dbus::ObjectPath& device_path, |
462 scoped_ptr<dbus::FileDescriptor> fd, | 462 scoped_ptr<dbus::FileDescriptor> fd, |
463 const BluetoothProfileServiceProvider::Delegate::Options& options, | 463 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, |
464 const ConfirmationCallback& callback) { | 464 const ConfirmationCallback& callback) { |
465 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); | 465 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); |
466 base::ThreadRestrictions::AssertIOAllowed(); | 466 base::ThreadRestrictions::AssertIOAllowed(); |
467 fd->CheckValidity(); | 467 fd->CheckValidity(); |
468 | 468 |
469 VLOG(1) << uuid_.canonical_value() << ": Validity check complete."; | 469 VLOG(1) << uuid_.canonical_value() << ": Validity check complete."; |
470 if (!fd->is_valid()) { | 470 if (!fd->is_valid()) { |
471 LOG(WARNING) << uuid_.canonical_value() << " :" << fd->value() | 471 LOG(WARNING) << uuid_.canonical_value() << " :" << fd->value() |
472 << ": Invalid file descriptor received from Bluetooth Daemon."; | 472 << ": Invalid file descriptor received from Bluetooth Daemon."; |
473 ui_task_runner()->PostTask(FROM_HERE, | 473 ui_task_runner()->PostTask(FROM_HERE, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 DCHECK(profile_); | 550 DCHECK(profile_); |
551 | 551 |
552 VLOG(1) << profile_->object_path().value() << ": Release profile"; | 552 VLOG(1) << profile_->object_path().value() << ": Release profile"; |
553 | 553 |
554 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()) | 554 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()) |
555 ->ReleaseProfile(device_path_, profile_); | 555 ->ReleaseProfile(device_path_, profile_); |
556 profile_ = nullptr; | 556 profile_ = nullptr; |
557 } | 557 } |
558 | 558 |
559 } // namespace chromeos | 559 } // namespace chromeos |
OLD | NEW |