| 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" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "base/threading/worker_pool.h" | 21 #include "base/threading/worker_pool.h" |
| 22 #include "chromeos/dbus/bluetooth_device_client.h" | 22 #include "chromeos/dbus/bluetooth_device_client.h" |
| 23 #include "chromeos/dbus/bluetooth_profile_manager_client.h" | 23 #include "chromeos/dbus/bluetooth_profile_manager_client.h" |
| 24 #include "chromeos/dbus/bluetooth_profile_service_provider.h" | 24 #include "chromeos/dbus/bluetooth_profile_service_provider.h" |
| 25 #include "chromeos/dbus/dbus_thread_manager.h" | 25 #include "chromeos/dbus/dbus_thread_manager.h" |
| 26 #include "dbus/bus.h" | 26 #include "dbus/bus.h" |
| 27 #include "dbus/file_descriptor.h" | 27 #include "dbus/file_descriptor.h" |
| 28 #include "dbus/object_path.h" | 28 #include "dbus/object_path.h" |
| 29 #include "device/bluetooth/bluetooth_adapter.h" | 29 #include "device/bluetooth/bluetooth_adapter.h" |
| 30 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | 30 #include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| 31 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" | |
| 32 #include "device/bluetooth/bluetooth_device.h" | 31 #include "device/bluetooth/bluetooth_device.h" |
| 33 #include "device/bluetooth/bluetooth_device_chromeos.h" | 32 #include "device/bluetooth/bluetooth_device_chromeos.h" |
| 34 #include "device/bluetooth/bluetooth_socket.h" | 33 #include "device/bluetooth/bluetooth_socket.h" |
| 35 #include "device/bluetooth/bluetooth_socket_net.h" | 34 #include "device/bluetooth/bluetooth_socket_net.h" |
| 36 #include "device/bluetooth/bluetooth_socket_thread.h" | 35 #include "device/bluetooth/bluetooth_socket_thread.h" |
| 37 #include "net/base/ip_endpoint.h" | 36 #include "net/base/ip_endpoint.h" |
| 38 #include "net/base/net_errors.h" | 37 #include "net/base/net_errors.h" |
| 39 #include "third_party/cros_system_api/dbus/service_constants.h" | 38 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 40 | 39 |
| 41 using device::BluetoothAdapter; | 40 using device::BluetoothAdapter; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 70 | 69 |
| 71 BluetoothSocketChromeOS::ConnectionRequest::ConnectionRequest() | 70 BluetoothSocketChromeOS::ConnectionRequest::ConnectionRequest() |
| 72 : accepting(false), | 71 : accepting(false), |
| 73 cancelled(false) {} | 72 cancelled(false) {} |
| 74 | 73 |
| 75 BluetoothSocketChromeOS::ConnectionRequest::~ConnectionRequest() {} | 74 BluetoothSocketChromeOS::ConnectionRequest::~ConnectionRequest() {} |
| 76 | 75 |
| 77 BluetoothSocketChromeOS::BluetoothSocketChromeOS( | 76 BluetoothSocketChromeOS::BluetoothSocketChromeOS( |
| 78 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 77 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 79 scoped_refptr<BluetoothSocketThread> socket_thread) | 78 scoped_refptr<BluetoothSocketThread> socket_thread) |
| 80 : BluetoothSocketNet(ui_task_runner, socket_thread), profile_(nullptr) { | 79 : BluetoothSocketNet(ui_task_runner, socket_thread) { |
| 81 } | 80 } |
| 82 | 81 |
| 83 BluetoothSocketChromeOS::~BluetoothSocketChromeOS() { | 82 BluetoothSocketChromeOS::~BluetoothSocketChromeOS() { |
| 84 DCHECK(!profile_); | 83 DCHECK(object_path_.value().empty()); |
| 84 DCHECK(profile_.get() == NULL); |
| 85 | 85 |
| 86 if (adapter_.get()) { | 86 if (adapter_.get()) { |
| 87 adapter_->RemoveObserver(this); | 87 adapter_->RemoveObserver(this); |
| 88 adapter_ = NULL; | 88 adapter_ = NULL; |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 void BluetoothSocketChromeOS::Connect( | 92 void BluetoothSocketChromeOS::Connect( |
| 93 const BluetoothDeviceChromeOS* device, | 93 const BluetoothDeviceChromeOS* device, |
| 94 const BluetoothUUID& uuid, | 94 const BluetoothUUID& uuid, |
| 95 SecurityLevel security_level, | 95 SecurityLevel security_level, |
| 96 const base::Closure& success_callback, | 96 const base::Closure& success_callback, |
| 97 const ErrorCompletionCallback& error_callback) { | 97 const ErrorCompletionCallback& error_callback) { |
| 98 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 98 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 99 DCHECK(!profile_); | 99 DCHECK(object_path_.value().empty()); |
| 100 DCHECK(!profile_.get()); |
| 100 | 101 |
| 101 if (!uuid.IsValid()) { | 102 if (!uuid.IsValid()) { |
| 102 error_callback.Run(kInvalidUUID); | 103 error_callback.Run(kInvalidUUID); |
| 103 return; | 104 return; |
| 104 } | 105 } |
| 105 | 106 |
| 106 device_address_ = device->GetAddress(); | 107 device_address_ = device->GetAddress(); |
| 107 device_path_ = device->object_path(); | 108 device_path_ = device->object_path(); |
| 108 uuid_ = uuid; | 109 uuid_ = uuid; |
| 109 options_.reset(new BluetoothProfileManagerClient::Options()); | 110 options_.reset(new BluetoothProfileManagerClient::Options()); |
| 110 if (security_level == SECURITY_LEVEL_LOW) | 111 if (security_level == SECURITY_LEVEL_LOW) |
| 111 options_->require_authentication.reset(new bool(false)); | 112 options_->require_authentication.reset(new bool(false)); |
| 112 | 113 |
| 113 RegisterProfile(device->adapter(), success_callback, error_callback); | 114 RegisterProfile(success_callback, error_callback); |
| 114 } | 115 } |
| 115 | 116 |
| 116 void BluetoothSocketChromeOS::Listen( | 117 void BluetoothSocketChromeOS::Listen( |
| 117 scoped_refptr<BluetoothAdapter> adapter, | 118 scoped_refptr<BluetoothAdapter> adapter, |
| 118 SocketType socket_type, | 119 SocketType socket_type, |
| 119 const BluetoothUUID& uuid, | 120 const BluetoothUUID& uuid, |
| 120 const BluetoothAdapter::ServiceOptions& service_options, | 121 const BluetoothAdapter::ServiceOptions& service_options, |
| 121 const base::Closure& success_callback, | 122 const base::Closure& success_callback, |
| 122 const ErrorCompletionCallback& error_callback) { | 123 const ErrorCompletionCallback& error_callback) { |
| 123 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 124 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 124 DCHECK(!profile_); | 125 DCHECK(object_path_.value().empty()); |
| 126 DCHECK(!profile_.get()); |
| 125 | 127 |
| 126 if (!uuid.IsValid()) { | 128 if (!uuid.IsValid()) { |
| 127 error_callback.Run(kInvalidUUID); | 129 error_callback.Run(kInvalidUUID); |
| 128 return; | 130 return; |
| 129 } | 131 } |
| 130 | 132 |
| 131 adapter_ = adapter; | 133 adapter_ = adapter; |
| 132 adapter_->AddObserver(this); | 134 adapter_->AddObserver(this); |
| 133 | 135 |
| 134 uuid_ = uuid; | 136 uuid_ = uuid; |
| 135 options_.reset(new BluetoothProfileManagerClient::Options()); | 137 options_.reset(new BluetoothProfileManagerClient::Options()); |
| 136 if (service_options.name) | 138 if (service_options.name) |
| 137 options_->name.reset(new std::string(*service_options.name)); | 139 options_->name.reset(new std::string(*service_options.name)); |
| 138 | 140 |
| 139 switch (socket_type) { | 141 switch (socket_type) { |
| 140 case kRfcomm: | 142 case kRfcomm: |
| 141 options_->channel.reset( | 143 options_->channel.reset( |
| 142 new uint16(service_options.channel ? *service_options.channel : 0)); | 144 new uint16(service_options.channel ? *service_options.channel : 0)); |
| 143 break; | 145 break; |
| 144 case kL2cap: | 146 case kL2cap: |
| 145 options_->psm.reset( | 147 options_->psm.reset( |
| 146 new uint16(service_options.psm ? *service_options.psm : 0)); | 148 new uint16(service_options.psm ? *service_options.psm : 0)); |
| 147 break; | 149 break; |
| 148 default: | 150 default: |
| 149 NOTREACHED(); | 151 NOTREACHED(); |
| 150 } | 152 } |
| 151 | 153 |
| 152 RegisterProfile(static_cast<BluetoothAdapterChromeOS*>(adapter_.get()), | 154 RegisterProfile(success_callback, error_callback); |
| 153 success_callback, error_callback); | |
| 154 } | 155 } |
| 155 | 156 |
| 156 void BluetoothSocketChromeOS::Close() { | 157 void BluetoothSocketChromeOS::Close() { |
| 157 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 158 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 158 | 159 |
| 159 if (profile_) | 160 if (profile_) |
| 160 UnregisterProfile(); | 161 UnregisterProfile(); |
| 161 | 162 |
| 162 // In the case below, where an asynchronous task gets posted on the socket | 163 // In the case below, where an asynchronous task gets posted on the socket |
| 163 // thread in BluetoothSocketNet::Close, a reference will be held to this | 164 // thread in BluetoothSocketNet::Close, a reference will be held to this |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 accept_request_.reset(new AcceptRequest); | 209 accept_request_.reset(new AcceptRequest); |
| 209 accept_request_->success_callback = success_callback; | 210 accept_request_->success_callback = success_callback; |
| 210 accept_request_->error_callback = error_callback; | 211 accept_request_->error_callback = error_callback; |
| 211 | 212 |
| 212 if (connection_request_queue_.size() >= 1) { | 213 if (connection_request_queue_.size() >= 1) { |
| 213 AcceptConnectionRequest(); | 214 AcceptConnectionRequest(); |
| 214 } | 215 } |
| 215 } | 216 } |
| 216 | 217 |
| 217 void BluetoothSocketChromeOS::RegisterProfile( | 218 void BluetoothSocketChromeOS::RegisterProfile( |
| 218 BluetoothAdapterChromeOS* adapter, | |
| 219 const base::Closure& success_callback, | 219 const base::Closure& success_callback, |
| 220 const ErrorCompletionCallback& error_callback) { | 220 const ErrorCompletionCallback& error_callback) { |
| 221 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 221 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 222 DCHECK(!profile_); | 222 DCHECK(object_path_.value().empty()); |
| 223 DCHECK(adapter); | 223 DCHECK(!profile_.get()); |
| 224 | 224 |
| 225 // If the adapter is not present, this is a listening socket and the | 225 // The object path is relatively meaningless, but has to be unique, so for |
| 226 // adapter isn't running yet. Report success and carry on; | 226 // connecting profiles use a combination of the device address and profile |
| 227 // UUID. |
| 228 std::string device_address_path, uuid_path; |
| 229 base::ReplaceChars(device_address_, ":-", "_", &device_address_path); |
| 230 base::ReplaceChars(uuid_.canonical_value(), ":-", "_", &uuid_path); |
| 231 if (!device_address_path.empty()) { |
| 232 object_path_ = dbus::ObjectPath("/org/chromium/bluetooth_profile/" + |
| 233 device_address_path + "/" + uuid_path); |
| 234 } else { |
| 235 object_path_ = dbus::ObjectPath("/org/chromium/bluetooth_profile/" + |
| 236 uuid_path); |
| 237 } |
| 238 |
| 239 // Create the service provider for the profile object. |
| 240 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); |
| 241 profile_.reset(BluetoothProfileServiceProvider::Create( |
| 242 system_bus, object_path_, this)); |
| 243 DCHECK(profile_.get()); |
| 244 |
| 245 // Before reaching out to the Bluetooth Daemon to register a listening socket, |
| 246 // make sure it's actually running. If not, report success and carry on; |
| 227 // the profile will be registered when the daemon becomes available. | 247 // the profile will be registered when the daemon becomes available. |
| 228 if (!adapter->IsPresent()) { | 248 if (adapter_.get() && !adapter_->IsPresent()) { |
| 229 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() | 249 VLOG(1) << object_path_.value() << ": Delaying profile registration."; |
| 230 << ": Delaying profile registration."; | 250 success_callback.Run(); |
| 231 base::MessageLoop::current()->PostTask(FROM_HERE, success_callback); | |
| 232 return; | 251 return; |
| 233 } | 252 } |
| 234 | 253 |
| 235 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() | 254 VLOG(1) << object_path_.value() << ": Registering profile."; |
| 236 << ": Acquiring profile."; | 255 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()-> |
| 237 | 256 RegisterProfile( |
| 238 adapter->UseProfile( | 257 object_path_, |
| 239 uuid_, device_path_, *options_, this, | 258 uuid_.canonical_value(), |
| 240 base::Bind(&BluetoothSocketChromeOS::OnRegisterProfile, this, | 259 *options_, |
| 241 success_callback, error_callback), | 260 base::Bind(&BluetoothSocketChromeOS::OnRegisterProfile, |
| 242 base::Bind(&BluetoothSocketChromeOS::OnRegisterProfileError, this, | 261 this, |
| 243 error_callback)); | 262 success_callback, |
| 263 error_callback), |
| 264 base::Bind(&BluetoothSocketChromeOS::OnRegisterProfileError, |
| 265 this, |
| 266 error_callback)); |
| 244 } | 267 } |
| 245 | 268 |
| 246 void BluetoothSocketChromeOS::OnRegisterProfile( | 269 void BluetoothSocketChromeOS::OnRegisterProfile( |
| 247 const base::Closure& success_callback, | 270 const base::Closure& success_callback, |
| 248 const ErrorCompletionCallback& error_callback, | 271 const ErrorCompletionCallback& error_callback) { |
| 249 BluetoothAdapterProfileChromeOS* profile) { | |
| 250 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 272 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 251 DCHECK(!profile_); | 273 if (!device_path_.value().empty()) { |
| 274 VLOG(1) << object_path_.value() << ": Profile registered, connecting to " |
| 275 << device_path_.value(); |
| 252 | 276 |
| 253 profile_ = profile; | 277 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
| 254 | 278 ConnectProfile( |
| 255 if (device_path_.value().empty()) { | 279 device_path_, |
| 256 VLOG(1) << uuid_.canonical_value() << ": Profile registered."; | 280 uuid_.canonical_value(), |
| 281 base::Bind( |
| 282 &BluetoothSocketChromeOS::OnConnectProfile, |
| 283 this, |
| 284 success_callback), |
| 285 base::Bind( |
| 286 &BluetoothSocketChromeOS::OnConnectProfileError, |
| 287 this, |
| 288 error_callback)); |
| 289 } else { |
| 290 VLOG(1) << object_path_.value() << ": Profile registered."; |
| 257 success_callback.Run(); | 291 success_callback.Run(); |
| 258 return; | |
| 259 } | 292 } |
| 260 | |
| 261 VLOG(1) << uuid_.canonical_value() << ": Got profile, connecting to " | |
| 262 << device_path_.value(); | |
| 263 | |
| 264 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
| 265 device_path_, uuid_.canonical_value(), | |
| 266 base::Bind(&BluetoothSocketChromeOS::OnConnectProfile, this, | |
| 267 success_callback), | |
| 268 base::Bind(&BluetoothSocketChromeOS::OnConnectProfileError, this, | |
| 269 error_callback)); | |
| 270 } | 293 } |
| 271 | 294 |
| 272 void BluetoothSocketChromeOS::OnRegisterProfileError( | 295 void BluetoothSocketChromeOS::OnRegisterProfileError( |
| 273 const ErrorCompletionCallback& error_callback, | 296 const ErrorCompletionCallback& error_callback, |
| 297 const std::string& error_name, |
| 274 const std::string& error_message) { | 298 const std::string& error_message) { |
| 275 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 299 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 276 | 300 LOG(WARNING) << object_path_.value() << ": Failed to register profile: " |
| 277 LOG(WARNING) << uuid_.canonical_value() | 301 << error_name << ": " << error_message; |
| 278 << ": Failed to register profile: " << error_message; | |
| 279 error_callback.Run(error_message); | 302 error_callback.Run(error_message); |
| 280 } | 303 } |
| 281 | 304 |
| 282 void BluetoothSocketChromeOS::OnConnectProfile( | 305 void BluetoothSocketChromeOS::OnConnectProfile( |
| 283 const base::Closure& success_callback) { | 306 const base::Closure& success_callback) { |
| 284 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 307 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 285 DCHECK(profile_); | 308 VLOG(1) << object_path_.value() << ": Profile connected."; |
| 286 | |
| 287 VLOG(1) << profile_->object_path().value() << ": Profile connected."; | |
| 288 UnregisterProfile(); | 309 UnregisterProfile(); |
| 289 success_callback.Run(); | 310 success_callback.Run(); |
| 290 } | 311 } |
| 291 | 312 |
| 292 void BluetoothSocketChromeOS::OnConnectProfileError( | 313 void BluetoothSocketChromeOS::OnConnectProfileError( |
| 293 const ErrorCompletionCallback& error_callback, | 314 const ErrorCompletionCallback& error_callback, |
| 294 const std::string& error_name, | 315 const std::string& error_name, |
| 295 const std::string& error_message) { | 316 const std::string& error_message) { |
| 296 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 317 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 297 DCHECK(profile_); | 318 LOG(WARNING) << object_path_.value() << ": Failed to connect profile: " |
| 298 | 319 << error_name << ": " << error_message; |
| 299 LOG(WARNING) << profile_->object_path().value() | |
| 300 << ": Failed to connect profile: " << error_name << ": " | |
| 301 << error_message; | |
| 302 UnregisterProfile(); | 320 UnregisterProfile(); |
| 303 error_callback.Run(error_message); | 321 error_callback.Run(error_message); |
| 304 } | 322 } |
| 305 | 323 |
| 306 void BluetoothSocketChromeOS::AdapterPresentChanged(BluetoothAdapter* adapter, | 324 void BluetoothSocketChromeOS::AdapterPresentChanged(BluetoothAdapter* adapter, |
| 307 bool present) { | 325 bool present) { |
| 308 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 326 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 327 DCHECK(!object_path_.value().empty()); |
| 328 DCHECK(profile_.get()); |
| 309 | 329 |
| 310 if (!present) { | 330 if (!present) |
| 311 // Adapter removed, the profile is now invalid. | |
| 312 UnregisterProfile(); | |
| 313 return; | 331 return; |
| 314 } | |
| 315 | 332 |
| 316 DCHECK(!profile_); | 333 VLOG(1) << object_path_.value() << ": Re-register profile."; |
| 317 | 334 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()-> |
| 318 VLOG(1) << uuid_.canonical_value() << " on " << device_path_.value() | 335 RegisterProfile( |
| 319 << ": Acquiring profile."; | 336 object_path_, |
| 320 | 337 uuid_.canonical_value(), |
| 321 static_cast<BluetoothAdapterChromeOS*>(adapter)->UseProfile( | 338 *options_, |
| 322 uuid_, device_path_, *options_, this, | 339 base::Bind(&BluetoothSocketChromeOS::OnInternalRegisterProfile, |
| 323 base::Bind(&BluetoothSocketChromeOS::OnInternalRegisterProfile, this), | 340 this), |
| 324 base::Bind(&BluetoothSocketChromeOS::OnInternalRegisterProfileError, | 341 base::Bind(&BluetoothSocketChromeOS::OnInternalRegisterProfileError, |
| 325 this)); | 342 this)); |
| 326 } | 343 } |
| 327 | 344 |
| 328 void BluetoothSocketChromeOS::OnInternalRegisterProfile( | 345 void BluetoothSocketChromeOS::OnInternalRegisterProfile() { |
| 329 BluetoothAdapterProfileChromeOS* profile) { | |
| 330 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 346 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 331 DCHECK(!profile_); | |
| 332 | 347 |
| 333 profile_ = profile; | 348 VLOG(1) << object_path_.value() << ": Profile re-registered"; |
| 334 | |
| 335 VLOG(1) << uuid_.canonical_value() << ": Profile re-registered"; | |
| 336 } | 349 } |
| 337 | 350 |
| 338 void BluetoothSocketChromeOS::OnInternalRegisterProfileError( | 351 void BluetoothSocketChromeOS::OnInternalRegisterProfileError( |
| 352 const std::string& error_name, |
| 339 const std::string& error_message) { | 353 const std::string& error_message) { |
| 340 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 354 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 341 | 355 |
| 342 LOG(WARNING) << "Failed to re-register profile: " << error_message; | 356 // It's okay if the profile already exists, it means we registered it on |
| 357 // initialization. |
| 358 if (error_name == bluetooth_profile_manager::kErrorAlreadyExists) |
| 359 return; |
| 360 |
| 361 LOG(WARNING) << object_path_.value() << ": Failed to re-register profile: " |
| 362 << error_name << ": " << error_message; |
| 343 } | 363 } |
| 344 | 364 |
| 345 void BluetoothSocketChromeOS::Released() { | 365 void BluetoothSocketChromeOS::Released() { |
| 346 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 366 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 347 DCHECK(profile_); | 367 VLOG(1) << object_path_.value() << ": Release"; |
| 348 | |
| 349 VLOG(1) << profile_->object_path().value() << ": Release"; | |
| 350 } | 368 } |
| 351 | 369 |
| 352 void BluetoothSocketChromeOS::NewConnection( | 370 void BluetoothSocketChromeOS::NewConnection( |
| 353 const dbus::ObjectPath& device_path, | 371 const dbus::ObjectPath& device_path, |
| 354 scoped_ptr<dbus::FileDescriptor> fd, | 372 scoped_ptr<dbus::FileDescriptor> fd, |
| 355 const BluetoothProfileServiceProvider::Delegate::Options& options, | 373 const BluetoothProfileServiceProvider::Delegate::Options& options, |
| 356 const ConfirmationCallback& callback) { | 374 const ConfirmationCallback& callback) { |
| 357 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 375 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 358 | 376 VLOG(1) << object_path_.value() << ": New connection from device: " |
| 359 VLOG(1) << uuid_.canonical_value() | 377 << device_path.value(); |
| 360 << ": New connection from device: " << device_path.value(); | |
| 361 | 378 |
| 362 if (!device_path_.value().empty()) { | 379 if (!device_path_.value().empty()) { |
| 363 DCHECK(device_path_ == device_path); | 380 DCHECK(device_path_ == device_path); |
| 364 | 381 |
| 365 socket_thread()->task_runner()->PostTask( | 382 socket_thread()->task_runner()->PostTask( |
| 366 FROM_HERE, | 383 FROM_HERE, |
| 367 base::Bind( | 384 base::Bind( |
| 368 &BluetoothSocketChromeOS::DoNewConnection, | 385 &BluetoothSocketChromeOS::DoNewConnection, |
| 369 this, | 386 this, |
| 370 device_path_, | 387 device_path_, |
| 371 base::Passed(&fd), | 388 base::Passed(&fd), |
| 372 options, | 389 options, |
| 373 callback)); | 390 callback)); |
| 374 } else { | 391 } else { |
| 375 linked_ptr<ConnectionRequest> request(new ConnectionRequest()); | 392 linked_ptr<ConnectionRequest> request(new ConnectionRequest()); |
| 376 request->device_path = device_path; | 393 request->device_path = device_path; |
| 377 request->fd = fd.Pass(); | 394 request->fd = fd.Pass(); |
| 378 request->options = options; | 395 request->options = options; |
| 379 request->callback = callback; | 396 request->callback = callback; |
| 380 | 397 |
| 381 connection_request_queue_.push(request); | 398 connection_request_queue_.push(request); |
| 382 VLOG(1) << uuid_.canonical_value() << ": Connection is now pending."; | 399 VLOG(1) << object_path_.value() << ": Connection is now pending."; |
| 383 if (accept_request_) { | 400 if (accept_request_) { |
| 384 AcceptConnectionRequest(); | 401 AcceptConnectionRequest(); |
| 385 } | 402 } |
| 386 } | 403 } |
| 387 } | 404 } |
| 388 | 405 |
| 389 void BluetoothSocketChromeOS::RequestDisconnection( | 406 void BluetoothSocketChromeOS::RequestDisconnection( |
| 390 const dbus::ObjectPath& device_path, | 407 const dbus::ObjectPath& device_path, |
| 391 const ConfirmationCallback& callback) { | 408 const ConfirmationCallback& callback) { |
| 392 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 409 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 393 DCHECK(profile_); | 410 VLOG(1) << object_path_.value() << ": Request disconnection"; |
| 394 | |
| 395 VLOG(1) << profile_->object_path().value() << ": Request disconnection"; | |
| 396 callback.Run(SUCCESS); | 411 callback.Run(SUCCESS); |
| 397 } | 412 } |
| 398 | 413 |
| 399 void BluetoothSocketChromeOS::Cancel() { | 414 void BluetoothSocketChromeOS::Cancel() { |
| 400 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 415 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 401 DCHECK(profile_); | 416 VLOG(1) << object_path_.value() << ": Cancel"; |
| 402 | |
| 403 VLOG(1) << profile_->object_path().value() << ": Cancel"; | |
| 404 | 417 |
| 405 if (!connection_request_queue_.size()) | 418 if (!connection_request_queue_.size()) |
| 406 return; | 419 return; |
| 407 | 420 |
| 408 // If the front request is being accepted mark it as cancelled, otherwise | 421 // If the front request is being accepted mark it as cancelled, otherwise |
| 409 // just pop it from the queue. | 422 // just pop it from the queue. |
| 410 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 423 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
| 411 if (!request->accepting) { | 424 if (!request->accepting) { |
| 412 request->cancelled = true; | 425 request->cancelled = true; |
| 413 } else { | 426 } else { |
| 414 connection_request_queue_.pop(); | 427 connection_request_queue_.pop(); |
| 415 } | 428 } |
| 416 } | 429 } |
| 417 | 430 |
| 418 void BluetoothSocketChromeOS::AcceptConnectionRequest() { | 431 void BluetoothSocketChromeOS::AcceptConnectionRequest() { |
| 419 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 432 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 420 DCHECK(accept_request_.get()); | 433 DCHECK(accept_request_.get()); |
| 421 DCHECK(connection_request_queue_.size() >= 1); | 434 DCHECK(connection_request_queue_.size() >= 1); |
| 422 DCHECK(profile_); | |
| 423 | 435 |
| 424 VLOG(1) << profile_->object_path().value() | 436 VLOG(1) << object_path_.value() << ": Accepting pending connection."; |
| 425 << ": Accepting pending connection."; | |
| 426 | 437 |
| 427 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 438 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
| 428 request->accepting = true; | 439 request->accepting = true; |
| 429 | 440 |
| 430 BluetoothDeviceChromeOS* device = | 441 BluetoothDeviceChromeOS* device = |
| 431 static_cast<BluetoothAdapterChromeOS*>(adapter_.get())-> | 442 static_cast<BluetoothAdapterChromeOS*>(adapter_.get())-> |
| 432 GetDeviceWithPath(request->device_path); | 443 GetDeviceWithPath(request->device_path); |
| 433 DCHECK(device); | 444 DCHECK(device); |
| 434 | 445 |
| 435 scoped_refptr<BluetoothSocketChromeOS> client_socket = | 446 scoped_refptr<BluetoothSocketChromeOS> client_socket = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 456 | 467 |
| 457 void BluetoothSocketChromeOS::DoNewConnection( | 468 void BluetoothSocketChromeOS::DoNewConnection( |
| 458 const dbus::ObjectPath& device_path, | 469 const dbus::ObjectPath& device_path, |
| 459 scoped_ptr<dbus::FileDescriptor> fd, | 470 scoped_ptr<dbus::FileDescriptor> fd, |
| 460 const BluetoothProfileServiceProvider::Delegate::Options& options, | 471 const BluetoothProfileServiceProvider::Delegate::Options& options, |
| 461 const ConfirmationCallback& callback) { | 472 const ConfirmationCallback& callback) { |
| 462 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); | 473 DCHECK(socket_thread()->task_runner()->RunsTasksOnCurrentThread()); |
| 463 base::ThreadRestrictions::AssertIOAllowed(); | 474 base::ThreadRestrictions::AssertIOAllowed(); |
| 464 fd->CheckValidity(); | 475 fd->CheckValidity(); |
| 465 | 476 |
| 466 VLOG(1) << uuid_.canonical_value() << ": Validity check complete."; | 477 VLOG(1) << object_path_.value() << ": Validity check complete."; |
| 467 if (!fd->is_valid()) { | 478 if (!fd->is_valid()) { |
| 468 LOG(WARNING) << uuid_.canonical_value() << " :" << fd->value() | 479 LOG(WARNING) << object_path_.value() << " :" << fd->value() |
| 469 << ": Invalid file descriptor received from Bluetooth Daemon."; | 480 << ": Invalid file descriptor received from Bluetooth Daemon."; |
| 470 ui_task_runner()->PostTask(FROM_HERE, | 481 ui_task_runner()->PostTask(FROM_HERE, |
| 471 base::Bind(callback, REJECTED));; | 482 base::Bind(callback, REJECTED));; |
| 472 return; | 483 return; |
| 473 } | 484 } |
| 474 | 485 |
| 475 if (tcp_socket()) { | 486 if (tcp_socket()) { |
| 476 LOG(WARNING) << uuid_.canonical_value() << ": Already connected"; | 487 LOG(WARNING) << object_path_.value() << ": Already connected"; |
| 477 ui_task_runner()->PostTask(FROM_HERE, | 488 ui_task_runner()->PostTask(FROM_HERE, |
| 478 base::Bind(callback, REJECTED));; | 489 base::Bind(callback, REJECTED));; |
| 479 return; | 490 return; |
| 480 } | 491 } |
| 481 | 492 |
| 482 ResetTCPSocket(); | 493 ResetTCPSocket(); |
| 483 | 494 |
| 484 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the | 495 // Note: We don't have a meaningful |IPEndPoint|, but that is ok since the |
| 485 // TCPSocket implementation does not actually require one. | 496 // TCPSocket implementation does not actually require one. |
| 486 int net_result = tcp_socket()->AdoptConnectedSocket(fd->value(), | 497 int net_result = tcp_socket()->AdoptConnectedSocket(fd->value(), |
| 487 net::IPEndPoint()); | 498 net::IPEndPoint()); |
| 488 if (net_result != net::OK) { | 499 if (net_result != net::OK) { |
| 489 LOG(WARNING) << uuid_.canonical_value() << ": Error adopting socket: " | 500 LOG(WARNING) << object_path_.value() << ": Error adopting socket: " |
| 490 << std::string(net::ErrorToString(net_result)); | 501 << std::string(net::ErrorToString(net_result)); |
| 491 ui_task_runner()->PostTask(FROM_HERE, | 502 ui_task_runner()->PostTask(FROM_HERE, |
| 492 base::Bind(callback, REJECTED));; | 503 base::Bind(callback, REJECTED));; |
| 493 return; | 504 return; |
| 494 } | 505 } |
| 495 | 506 |
| 496 VLOG(2) << uuid_.canonical_value() | 507 VLOG(2) << object_path_.value() << ": Taking descriptor, confirming success."; |
| 497 << ": Taking descriptor, confirming success."; | |
| 498 fd->TakeValue(); | 508 fd->TakeValue(); |
| 499 ui_task_runner()->PostTask(FROM_HERE, | 509 ui_task_runner()->PostTask(FROM_HERE, |
| 500 base::Bind(callback, SUCCESS));; | 510 base::Bind(callback, SUCCESS));; |
| 501 } | 511 } |
| 502 | 512 |
| 503 void BluetoothSocketChromeOS::OnNewConnection( | 513 void BluetoothSocketChromeOS::OnNewConnection( |
| 504 scoped_refptr<BluetoothSocket> socket, | 514 scoped_refptr<BluetoothSocket> socket, |
| 505 const ConfirmationCallback& callback, | 515 const ConfirmationCallback& callback, |
| 506 Status status) { | 516 Status status) { |
| 507 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 517 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 537 | 547 |
| 538 while (connection_request_queue_.size() > 0) { | 548 while (connection_request_queue_.size() > 0) { |
| 539 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); | 549 linked_ptr<ConnectionRequest> request = connection_request_queue_.front(); |
| 540 request->callback.Run(REJECTED); | 550 request->callback.Run(REJECTED); |
| 541 connection_request_queue_.pop(); | 551 connection_request_queue_.pop(); |
| 542 } | 552 } |
| 543 } | 553 } |
| 544 | 554 |
| 545 void BluetoothSocketChromeOS::UnregisterProfile() { | 555 void BluetoothSocketChromeOS::UnregisterProfile() { |
| 546 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); | 556 DCHECK(ui_task_runner()->RunsTasksOnCurrentThread()); |
| 547 DCHECK(profile_); | 557 DCHECK(!object_path_.value().empty()); |
| 558 DCHECK(profile_.get()); |
| 548 | 559 |
| 549 VLOG(1) << profile_->object_path().value() << ": Release profile"; | 560 VLOG(1) << object_path_.value() << ": Unregister profile"; |
| 561 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()-> |
| 562 UnregisterProfile( |
| 563 object_path_, |
| 564 base::Bind(&BluetoothSocketChromeOS::OnUnregisterProfile, |
| 565 this, |
| 566 object_path_), |
| 567 base::Bind(&BluetoothSocketChromeOS::OnUnregisterProfileError, |
| 568 this, |
| 569 object_path_)); |
| 550 | 570 |
| 551 profile_->RemoveDelegate( | 571 profile_.reset(); |
| 552 device_path_, base::Bind(&BluetoothSocketChromeOS::ReleaseProfile, this)); | 572 object_path_ = dbus::ObjectPath(""); |
| 553 | |
| 554 profile_ = nullptr; | |
| 555 } | 573 } |
| 556 | 574 |
| 557 void BluetoothSocketChromeOS::ReleaseProfile() { | 575 void BluetoothSocketChromeOS::OnUnregisterProfile( |
| 558 if (adapter_) | 576 const dbus::ObjectPath& object_path) { |
| 559 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()) | 577 VLOG(1) << object_path.value() << ": Profile unregistered"; |
| 560 ->ReleaseProfile(uuid_); | 578 } |
| 579 |
| 580 void BluetoothSocketChromeOS::OnUnregisterProfileError( |
| 581 const dbus::ObjectPath& object_path, |
| 582 const std::string& error_name, |
| 583 const std::string& error_message) { |
| 584 // It's okay if the profile doesn't exist, it means we haven't registered it |
| 585 // yet. |
| 586 if (error_name == bluetooth_profile_manager::kErrorDoesNotExist) |
| 587 return; |
| 588 |
| 589 LOG(WARNING) << object_path_.value() << ": Failed to unregister profile: " |
| 590 << error_name << ": " << error_message; |
| 561 } | 591 } |
| 562 | 592 |
| 563 } // namespace chromeos | 593 } // namespace chromeos |
| OLD | NEW |