Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_device_dbus.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/string16.h" | 16 #include "base/string16.h" |
| 17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 20 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_dbus.h" |
| 21 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" | 21 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" |
| 22 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" | 22 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket_posix.h" |
| 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" | 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" |
| 24 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 25 #include "chromeos/dbus/bluetooth_adapter_client.h" | 25 #include "chromeos/dbus/bluetooth_adapter_client.h" |
| 26 #include "chromeos/dbus/bluetooth_agent_service_provider.h" | 26 #include "chromeos/dbus/bluetooth_agent_service_provider.h" |
| 27 #include "chromeos/dbus/bluetooth_device_client.h" | 27 #include "chromeos/dbus/bluetooth_device_client.h" |
| 28 #include "chromeos/dbus/bluetooth_input_client.h" | 28 #include "chromeos/dbus/bluetooth_input_client.h" |
| 29 #include "chromeos/dbus/bluetooth_out_of_band_client.h" | 29 #include "chromeos/dbus/bluetooth_out_of_band_client.h" |
| 30 #include "chromeos/dbus/bluetooth_out_of_band_pairing_data.h" | |
| 30 #include "chromeos/dbus/dbus_thread_manager.h" | 31 #include "chromeos/dbus/dbus_thread_manager.h" |
| 31 #include "chromeos/dbus/introspectable_client.h" | 32 #include "chromeos/dbus/introspectable_client.h" |
| 32 #include "dbus/bus.h" | 33 #include "dbus/bus.h" |
| 33 #include "dbus/object_path.h" | 34 #include "dbus/object_path.h" |
| 34 #include "grit/generated_resources.h" | 35 #include "grit/generated_resources.h" |
| 35 #include "third_party/cros_system_api/dbus/service_constants.h" | 36 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 36 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 37 | 38 |
| 38 namespace chromeos { | 39 namespace chromeos { |
| 39 | 40 |
| 40 BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter) | 41 BluetoothDeviceDBus::BluetoothDeviceDBus(BluetoothAdapterDBus* adapter) |
| 41 : adapter_(adapter), | 42 : adapter_(adapter), |
| 42 bluetooth_class_(0), | 43 bluetooth_class_(0), |
| 43 visible_(false), | 44 visible_(false), |
| 44 bonded_(false), | 45 bonded_(false), |
| 45 connected_(false), | 46 connected_(false), |
| 46 pairing_delegate_(NULL), | 47 pairing_delegate_(NULL), |
| 47 connecting_applications_counter_(0), | 48 connecting_applications_counter_(0), |
| 48 weak_ptr_factory_(this) { | 49 weak_ptr_factory_(this) { |
| 49 } | 50 } |
| 50 | 51 |
| 51 BluetoothDevice::~BluetoothDevice() { | 52 BluetoothDeviceDBus::~BluetoothDeviceDBus() { |
| 52 } | 53 } |
| 53 | 54 |
| 54 const std::string& BluetoothDevice::address() const { return address_; } | 55 const std::string& BluetoothDeviceDBus::address() const { return address_; } |
| 55 | 56 |
| 56 string16 BluetoothDevice::GetName() const { | 57 string16 BluetoothDeviceDBus::GetName() const { |
| 57 if (!name_.empty()) { | 58 if (!name_.empty()) { |
| 58 return UTF8ToUTF16(name_); | 59 return UTF8ToUTF16(name_); |
| 59 } else { | 60 } else { |
| 60 return GetAddressWithLocalizedDeviceTypeName(); | 61 return GetAddressWithLocalizedDeviceTypeName(); |
| 61 } | 62 } |
| 62 } | 63 } |
| 63 | 64 |
| 64 BluetoothDevice::DeviceType BluetoothDevice::GetDeviceType() const { | 65 BluetoothDevice::DeviceType BluetoothDeviceDBus::GetDeviceType() const { |
|
keybuk
2012/09/13 23:57:30
This method can probably go in the base class, sin
youngki
2012/09/17 21:53:02
Done. I added bluetooth_device.cc to put the imple
| |
| 65 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm | 66 // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm |
| 66 switch ((bluetooth_class_ & 0x1f00) >> 8) { | 67 switch ((bluetooth_class_ & 0x1f00) >> 8) { |
| 67 case 0x01: | 68 case 0x01: |
| 68 // Computer major device class. | 69 // Computer major device class. |
| 69 return DEVICE_COMPUTER; | 70 return DEVICE_COMPUTER; |
| 70 case 0x02: | 71 case 0x02: |
| 71 // Phone major device class. | 72 // Phone major device class. |
| 72 switch ((bluetooth_class_ & 0xfc) >> 2) { | 73 switch ((bluetooth_class_ & 0xfc) >> 2) { |
| 73 case 0x01: | 74 case 0x01: |
| 74 case 0x02: | 75 case 0x02: |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 case 0x03: | 115 case 0x03: |
| 115 // Combo device. | 116 // Combo device. |
| 116 return DEVICE_KEYBOARD_MOUSE_COMBO; | 117 return DEVICE_KEYBOARD_MOUSE_COMBO; |
| 117 } | 118 } |
| 118 break; | 119 break; |
| 119 } | 120 } |
| 120 | 121 |
| 121 return DEVICE_UNKNOWN; | 122 return DEVICE_UNKNOWN; |
| 122 } | 123 } |
| 123 | 124 |
| 124 string16 BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() const { | 125 string16 BluetoothDeviceDBus::GetAddressWithLocalizedDeviceTypeName() const { |
| 125 string16 address = UTF8ToUTF16(address_); | 126 string16 address = UTF8ToUTF16(address_); |
| 126 DeviceType device_type = GetDeviceType(); | 127 BluetoothDevice::DeviceType device_type = GetDeviceType(); |
| 127 switch (device_type) { | 128 switch (device_type) { |
| 128 case DEVICE_COMPUTER: | 129 case DEVICE_COMPUTER: |
| 129 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER, | 130 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_COMPUTER, |
| 130 address); | 131 address); |
| 131 case DEVICE_PHONE: | 132 case DEVICE_PHONE: |
| 132 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE, | 133 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_PHONE, |
| 133 address); | 134 address); |
| 134 case DEVICE_MODEM: | 135 case DEVICE_MODEM: |
| 135 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM, | 136 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_MODEM, |
| 136 address); | 137 address); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 150 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET, | 151 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_TABLET, |
| 151 address); | 152 address); |
| 152 case DEVICE_KEYBOARD_MOUSE_COMBO: | 153 case DEVICE_KEYBOARD_MOUSE_COMBO: |
| 153 return l10n_util::GetStringFUTF16( | 154 return l10n_util::GetStringFUTF16( |
| 154 IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address); | 155 IDS_BLUETOOTH_DEVICE_KEYBOARD_MOUSE_COMBO, address); |
| 155 default: | 156 default: |
| 156 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN, address); | 157 return l10n_util::GetStringFUTF16(IDS_BLUETOOTH_DEVICE_UNKNOWN, address); |
| 157 } | 158 } |
| 158 } | 159 } |
| 159 | 160 |
| 160 bool BluetoothDevice::IsSupported() const { | 161 bool BluetoothDeviceDBus::IsSupported() const { |
| 161 if (CommandLine::ForCurrentProcess()->HasSwitch( | 162 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 162 switches::kEnableUnsupportedBluetoothDevices)) | 163 switches::kEnableUnsupportedBluetoothDevices)) |
| 163 return true; | 164 return true; |
| 164 | 165 |
| 165 DeviceType device_type = GetDeviceType(); | 166 BluetoothDevice::DeviceType device_type = GetDeviceType(); |
| 166 return (device_type == DEVICE_JOYSTICK || | 167 return (device_type == DEVICE_JOYSTICK || |
| 167 device_type == DEVICE_GAMEPAD || | 168 device_type == DEVICE_GAMEPAD || |
| 168 device_type == DEVICE_KEYBOARD || | 169 device_type == DEVICE_KEYBOARD || |
| 169 device_type == DEVICE_MOUSE || | 170 device_type == DEVICE_MOUSE || |
| 170 device_type == DEVICE_TABLET || | 171 device_type == DEVICE_TABLET || |
| 171 device_type == DEVICE_KEYBOARD_MOUSE_COMBO); | 172 device_type == DEVICE_KEYBOARD_MOUSE_COMBO); |
| 172 } | 173 } |
| 173 | 174 |
| 174 bool BluetoothDevice::IsPaired() const { return !object_path_.value().empty(); } | 175 bool BluetoothDeviceDBus::IsPaired() const { |
| 176 return !object_path_.value().empty(); | |
| 177 } | |
| 175 | 178 |
| 176 bool BluetoothDevice::IsBonded() const { return bonded_; } | 179 bool BluetoothDeviceDBus::IsBonded() const { return bonded_; } |
| 177 | 180 |
| 178 bool BluetoothDevice::IsConnected() const { | 181 bool BluetoothDeviceDBus::IsConnected() const { |
| 179 // TODO(keybuk): examine protocol-specific connected state, such as Input | 182 // TODO(keybuk): examine protocol-specific connected state, such as Input |
| 180 return connected_; | 183 return connected_; |
| 181 } | 184 } |
| 182 | 185 |
| 183 void BluetoothDevice::GetServiceRecords(const ServiceRecordsCallback& callback, | 186 void BluetoothDeviceDBus::GetServiceRecords( |
| 184 const ErrorCallback& error_callback) { | 187 const ServiceRecordsCallback& callback, |
| 188 const ErrorCallback& error_callback) { | |
| 185 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 189 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
| 186 DiscoverServices( | 190 DiscoverServices( |
| 187 object_path_, | 191 object_path_, |
| 188 "", // empty pattern to browse all services | 192 "", // empty pattern to browse all services |
| 189 base::Bind(&BluetoothDevice::CollectServiceRecordsCallback, | 193 base::Bind(&BluetoothDeviceDBus::CollectServiceRecordsCallback, |
| 190 weak_ptr_factory_.GetWeakPtr(), | 194 weak_ptr_factory_.GetWeakPtr(), |
| 191 callback, | 195 callback, |
| 192 error_callback)); | 196 error_callback)); |
| 193 } | 197 } |
| 194 | 198 |
| 195 bool BluetoothDevice::ProvidesServiceWithUUID(const std::string& uuid) const { | 199 bool BluetoothDeviceDBus::ProvidesServiceWithUUID( |
| 196 const BluetoothDevice::ServiceList& services = GetServices(); | 200 const std::string& uuid) const { |
| 197 for (BluetoothDevice::ServiceList::const_iterator iter = services.begin(); | 201 const BluetoothDeviceDBus::ServiceList& services = GetServices(); |
| 198 iter != services.end(); ++iter) { | 202 for (BluetoothDeviceDBus::ServiceList::const_iterator iter = |
| 203 services.begin(); | |
| 204 iter != services.end(); ++iter) { | |
| 199 if (bluetooth_utils::CanonicalUuid(*iter) == uuid) | 205 if (bluetooth_utils::CanonicalUuid(*iter) == uuid) |
| 200 return true; | 206 return true; |
| 201 } | 207 } |
| 202 return false; | 208 return false; |
| 203 } | 209 } |
| 204 | 210 |
| 205 void BluetoothDevice::ProvidesServiceWithName(const std::string& name, | 211 void BluetoothDeviceDBus::ProvidesServiceWithName(const std::string& name, |
| 206 const ProvidesServiceCallback& callback) { | 212 const ProvidesServiceCallback& callback) { |
| 207 GetServiceRecords( | 213 GetServiceRecords( |
| 208 base::Bind(&BluetoothDevice::SearchServicesForNameCallback, | 214 base::Bind(&BluetoothDeviceDBus::SearchServicesForNameCallback, |
| 209 weak_ptr_factory_.GetWeakPtr(), | 215 weak_ptr_factory_.GetWeakPtr(), |
| 210 name, | 216 name, |
| 211 callback), | 217 callback), |
| 212 base::Bind(&BluetoothDevice::SearchServicesForNameErrorCallback, | 218 base::Bind(&BluetoothDeviceDBus::SearchServicesForNameErrorCallback, |
| 213 weak_ptr_factory_.GetWeakPtr(), | 219 weak_ptr_factory_.GetWeakPtr(), |
| 214 callback)); | 220 callback)); |
| 215 } | 221 } |
| 216 | 222 |
| 217 void BluetoothDevice::Connect(PairingDelegate* pairing_delegate, | 223 void BluetoothDeviceDBus::Connect(PairingDelegate* pairing_delegate, |
| 218 const base::Closure& callback, | 224 const base::Closure& callback, |
| 219 const ErrorCallback& error_callback) { | 225 const ErrorCallback& error_callback) { |
| 220 if (IsPaired() || IsBonded() || IsConnected()) { | 226 if (IsPaired() || IsBonded() || IsConnected()) { |
| 221 // Connection to already paired or connected device. | 227 // Connection to already paired or connected device. |
| 222 ConnectApplications(callback, error_callback); | 228 ConnectApplications(callback, error_callback); |
| 223 | 229 |
| 224 } else if (!pairing_delegate) { | 230 } else if (!pairing_delegate) { |
| 225 // No pairing delegate supplied, initiate low-security connection only. | 231 // No pairing delegate supplied, initiate low-security connection only. |
| 226 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 232 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 227 CreateDevice(adapter_->object_path_, | 233 CreateDevice(adapter_->object_path_, |
| 228 address_, | 234 address_, |
| 229 base::Bind(&BluetoothDevice::ConnectCallback, | 235 base::Bind(&BluetoothDeviceDBus::ConnectCallback, |
| 230 weak_ptr_factory_.GetWeakPtr(), | 236 weak_ptr_factory_.GetWeakPtr(), |
| 231 callback, | 237 callback, |
| 232 error_callback), | 238 error_callback), |
| 233 base::Bind(&BluetoothDevice::ConnectErrorCallback, | 239 base::Bind(&BluetoothDeviceDBus::ConnectErrorCallback, |
| 234 weak_ptr_factory_.GetWeakPtr(), | 240 weak_ptr_factory_.GetWeakPtr(), |
| 235 error_callback)); | 241 error_callback)); |
| 236 } else { | 242 } else { |
| 237 // Initiate high-security connection with pairing. | 243 // Initiate high-security connection with pairing. |
| 238 DCHECK(!pairing_delegate_); | 244 DCHECK(!pairing_delegate_); |
| 239 pairing_delegate_ = pairing_delegate; | 245 pairing_delegate_ = pairing_delegate; |
| 240 | 246 |
| 241 // The agent path is relatively meaningless, we use the device address | 247 // The agent path is relatively meaningless, we use the device address |
| 242 // to generate it as we only support one pairing attempt at a time for | 248 // to generate it as we only support one pairing attempt at a time for |
| 243 // a given bluetooth device. | 249 // a given bluetooth device. |
| 244 DCHECK(agent_.get() == NULL); | 250 DCHECK(agent_.get() == NULL); |
| 245 | 251 |
| 246 std::string agent_path_basename; | 252 std::string agent_path_basename; |
| 247 ReplaceChars(address_, ":", "_", &agent_path_basename); | 253 ReplaceChars(address_, ":", "_", &agent_path_basename); |
| 248 dbus::ObjectPath agent_path("/org/chromium/bluetooth_agent/" + | 254 dbus::ObjectPath agent_path("/org/chromium/bluetooth_agent/" + |
| 249 agent_path_basename); | 255 agent_path_basename); |
| 250 | 256 |
| 251 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | 257 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); |
| 252 if (system_bus) { | 258 if (system_bus) { |
| 253 agent_.reset(BluetoothAgentServiceProvider::Create(system_bus, | 259 agent_.reset(BluetoothAgentServiceProvider::Create(system_bus, |
| 254 agent_path, | 260 agent_path, |
| 255 this)); | 261 this)); |
| 256 } else { | 262 } else { |
| 257 agent_.reset(NULL); | 263 agent_.reset(NULL); |
| 258 } | 264 } |
| 259 | 265 |
| 260 DVLOG(1) << "Pairing: " << address_; | 266 DVLOG(1) << "Pairing: " << address_; |
| 261 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 267 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 262 CreatePairedDevice(adapter_->object_path_, | 268 CreatePairedDevice( |
| 263 address_, | 269 adapter_->object_path_, |
| 264 agent_path, | 270 address_, |
| 265 bluetooth_agent::kDisplayYesNoCapability, | 271 agent_path, |
| 266 base::Bind(&BluetoothDevice::ConnectCallback, | 272 bluetooth_agent::kDisplayYesNoCapability, |
| 267 weak_ptr_factory_.GetWeakPtr(), | 273 base::Bind(&BluetoothDeviceDBus::ConnectCallback, |
| 268 callback, | 274 weak_ptr_factory_.GetWeakPtr(), |
| 269 error_callback), | 275 callback, |
| 270 base::Bind(&BluetoothDevice::ConnectErrorCallback, | 276 error_callback), |
| 271 weak_ptr_factory_.GetWeakPtr(), | 277 base::Bind(&BluetoothDeviceDBus::ConnectErrorCallback, |
| 272 error_callback)); | 278 weak_ptr_factory_.GetWeakPtr(), |
| 279 error_callback)); | |
| 273 } | 280 } |
| 274 } | 281 } |
| 275 | 282 |
| 276 void BluetoothDevice::SetPinCode(const std::string& pincode) { | 283 void BluetoothDeviceDBus::SetPinCode(const std::string& pincode) { |
| 277 if (!agent_.get() || pincode_callback_.is_null()) | 284 if (!agent_.get() || pincode_callback_.is_null()) |
| 278 return; | 285 return; |
| 279 | 286 |
| 280 pincode_callback_.Run(SUCCESS, pincode); | 287 pincode_callback_.Run(SUCCESS, pincode); |
| 281 pincode_callback_.Reset(); | 288 pincode_callback_.Reset(); |
| 282 } | 289 } |
| 283 | 290 |
| 284 void BluetoothDevice::SetPasskey(uint32 passkey) { | 291 void BluetoothDeviceDBus::SetPasskey(uint32 passkey) { |
| 285 if (!agent_.get() || passkey_callback_.is_null()) | 292 if (!agent_.get() || passkey_callback_.is_null()) |
| 286 return; | 293 return; |
| 287 | 294 |
| 288 passkey_callback_.Run(SUCCESS, passkey); | 295 passkey_callback_.Run(SUCCESS, passkey); |
| 289 passkey_callback_.Reset(); | 296 passkey_callback_.Reset(); |
| 290 } | 297 } |
| 291 | 298 |
| 292 void BluetoothDevice::ConfirmPairing() { | 299 void BluetoothDeviceDBus::ConfirmPairing() { |
| 293 if (!agent_.get() || confirmation_callback_.is_null()) | 300 if (!agent_.get() || confirmation_callback_.is_null()) |
| 294 return; | 301 return; |
| 295 | 302 |
| 296 confirmation_callback_.Run(SUCCESS); | 303 confirmation_callback_.Run(SUCCESS); |
| 297 confirmation_callback_.Reset(); | 304 confirmation_callback_.Reset(); |
| 298 } | 305 } |
| 299 | 306 |
| 300 void BluetoothDevice::RejectPairing() { | 307 void BluetoothDeviceDBus::RejectPairing() { |
| 301 if (!agent_.get()) | 308 if (!agent_.get()) |
| 302 return; | 309 return; |
| 303 | 310 |
| 304 if (!pincode_callback_.is_null()) { | 311 if (!pincode_callback_.is_null()) { |
| 305 pincode_callback_.Run(REJECTED, ""); | 312 pincode_callback_.Run(REJECTED, ""); |
| 306 pincode_callback_.Reset(); | 313 pincode_callback_.Reset(); |
| 307 } | 314 } |
| 308 if (!passkey_callback_.is_null()) { | 315 if (!passkey_callback_.is_null()) { |
| 309 passkey_callback_.Run(REJECTED, 0); | 316 passkey_callback_.Run(REJECTED, 0); |
| 310 passkey_callback_.Reset(); | 317 passkey_callback_.Reset(); |
| 311 } | 318 } |
| 312 if (!confirmation_callback_.is_null()) { | 319 if (!confirmation_callback_.is_null()) { |
| 313 confirmation_callback_.Run(REJECTED); | 320 confirmation_callback_.Run(REJECTED); |
| 314 confirmation_callback_.Reset(); | 321 confirmation_callback_.Reset(); |
| 315 } | 322 } |
| 316 } | 323 } |
| 317 | 324 |
| 318 void BluetoothDevice::CancelPairing() { | 325 void BluetoothDeviceDBus::CancelPairing() { |
| 319 if (!agent_.get()) | 326 if (!agent_.get()) |
| 320 return; | 327 return; |
| 321 | 328 |
| 322 if (!pincode_callback_.is_null()) { | 329 if (!pincode_callback_.is_null()) { |
| 323 pincode_callback_.Run(CANCELLED, ""); | 330 pincode_callback_.Run(CANCELLED, ""); |
| 324 pincode_callback_.Reset(); | 331 pincode_callback_.Reset(); |
| 325 } | 332 } |
| 326 if (!passkey_callback_.is_null()) { | 333 if (!passkey_callback_.is_null()) { |
| 327 passkey_callback_.Run(CANCELLED, 0); | 334 passkey_callback_.Run(CANCELLED, 0); |
| 328 passkey_callback_.Reset(); | 335 passkey_callback_.Reset(); |
| 329 } | 336 } |
| 330 if (!confirmation_callback_.is_null()) { | 337 if (!confirmation_callback_.is_null()) { |
| 331 confirmation_callback_.Run(CANCELLED); | 338 confirmation_callback_.Run(CANCELLED); |
| 332 confirmation_callback_.Reset(); | 339 confirmation_callback_.Reset(); |
| 333 } | 340 } |
| 334 } | 341 } |
| 335 | 342 |
| 336 void BluetoothDevice::Disconnect(const base::Closure& callback, | 343 void BluetoothDeviceDBus::Disconnect(const base::Closure& callback, |
| 337 const ErrorCallback& error_callback) { | 344 const ErrorCallback& error_callback) { |
| 338 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 345 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
| 339 Disconnect(object_path_, | 346 Disconnect(object_path_, |
| 340 base::Bind(&BluetoothDevice::DisconnectCallback, | 347 base::Bind(&BluetoothDeviceDBus::DisconnectCallback, |
| 341 weak_ptr_factory_.GetWeakPtr(), | 348 weak_ptr_factory_.GetWeakPtr(), |
| 342 callback, | 349 callback, |
| 343 error_callback)); | 350 error_callback)); |
| 344 | 351 |
| 345 } | 352 } |
| 346 | 353 |
| 347 void BluetoothDevice::Forget(const ErrorCallback& error_callback) { | 354 void BluetoothDeviceDBus::Forget(const ErrorCallback& error_callback) { |
| 348 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 355 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
| 349 RemoveDevice(adapter_->object_path_, | 356 RemoveDevice(adapter_->object_path_, |
| 350 object_path_, | 357 object_path_, |
| 351 base::Bind(&BluetoothDevice::ForgetCallback, | 358 base::Bind(&BluetoothDeviceDBus::ForgetCallback, |
| 352 weak_ptr_factory_.GetWeakPtr(), | 359 weak_ptr_factory_.GetWeakPtr(), |
| 353 error_callback)); | 360 error_callback)); |
| 354 } | 361 } |
| 355 | 362 |
| 356 void BluetoothDevice::ConnectToService(const std::string& service_uuid, | 363 void BluetoothDeviceDBus::ConnectToService(const std::string& service_uuid, |
| 357 const SocketCallback& callback) { | 364 const SocketCallback& callback) { |
| 358 GetServiceRecords( | 365 GetServiceRecords( |
| 359 base::Bind(&BluetoothDevice::GetServiceRecordsForConnectCallback, | 366 base::Bind(&BluetoothDeviceDBus::GetServiceRecordsForConnectCallback, |
| 360 weak_ptr_factory_.GetWeakPtr(), | 367 weak_ptr_factory_.GetWeakPtr(), |
| 361 service_uuid, | 368 service_uuid, |
| 362 callback), | 369 callback), |
| 363 base::Bind(&BluetoothDevice::GetServiceRecordsForConnectErrorCallback, | 370 base::Bind( |
| 364 weak_ptr_factory_.GetWeakPtr(), | 371 &BluetoothDeviceDBus::GetServiceRecordsForConnectErrorCallback, |
| 365 callback)); | 372 weak_ptr_factory_.GetWeakPtr(), |
| 373 callback)); | |
| 366 } | 374 } |
| 367 | 375 |
| 368 void BluetoothDevice::SetOutOfBandPairingData( | 376 void BluetoothDeviceDBus::SetOutOfBandPairingData( |
| 369 const chromeos::BluetoothOutOfBandPairingData& data, | 377 const chromeos::BluetoothOutOfBandPairingData& data, |
| 370 const base::Closure& callback, | 378 const base::Closure& callback, |
| 371 const ErrorCallback& error_callback) { | 379 const ErrorCallback& error_callback) { |
| 372 DBusThreadManager::Get()->GetBluetoothOutOfBandClient()-> | 380 DBusThreadManager::Get()->GetBluetoothOutOfBandClient()-> |
| 373 AddRemoteData( | 381 AddRemoteData( |
| 374 object_path_, | 382 object_path_, |
| 375 address(), | 383 address(), |
| 376 data, | 384 data, |
| 377 base::Bind(&BluetoothDevice::OnRemoteDataCallback, | 385 base::Bind(&BluetoothDeviceDBus::OnRemoteDataCallback, |
| 378 weak_ptr_factory_.GetWeakPtr(), | 386 weak_ptr_factory_.GetWeakPtr(), |
| 379 callback, | 387 callback, |
| 380 error_callback)); | 388 error_callback)); |
| 381 } | 389 } |
| 382 | 390 |
| 383 void BluetoothDevice::ClearOutOfBandPairingData( | 391 void BluetoothDeviceDBus::ClearOutOfBandPairingData( |
| 384 const base::Closure& callback, | 392 const base::Closure& callback, |
| 385 const ErrorCallback& error_callback) { | 393 const ErrorCallback& error_callback) { |
| 386 DBusThreadManager::Get()->GetBluetoothOutOfBandClient()-> | 394 DBusThreadManager::Get()->GetBluetoothOutOfBandClient()-> |
| 387 RemoveRemoteData( | 395 RemoveRemoteData( |
| 388 object_path_, | 396 object_path_, |
| 389 address(), | 397 address(), |
| 390 base::Bind(&BluetoothDevice::OnRemoteDataCallback, | 398 base::Bind(&BluetoothDeviceDBus::OnRemoteDataCallback, |
| 391 weak_ptr_factory_.GetWeakPtr(), | 399 weak_ptr_factory_.GetWeakPtr(), |
| 392 callback, | 400 callback, |
| 393 error_callback)); | 401 error_callback)); |
| 394 } | 402 } |
| 395 | 403 |
| 396 void BluetoothDevice::SetObjectPath(const dbus::ObjectPath& object_path) { | 404 void BluetoothDeviceDBus::SetObjectPath(const dbus::ObjectPath& object_path) { |
| 397 DCHECK(object_path_ == dbus::ObjectPath("")); | 405 DCHECK(object_path_ == dbus::ObjectPath("")); |
| 398 object_path_ = object_path; | 406 object_path_ = object_path; |
| 399 } | 407 } |
| 400 | 408 |
| 401 void BluetoothDevice::RemoveObjectPath() { | 409 void BluetoothDeviceDBus::RemoveObjectPath() { |
| 402 DCHECK(object_path_ != dbus::ObjectPath("")); | 410 DCHECK(object_path_ != dbus::ObjectPath("")); |
| 403 object_path_ = dbus::ObjectPath(""); | 411 object_path_ = dbus::ObjectPath(""); |
| 404 } | 412 } |
| 405 | 413 |
| 406 void BluetoothDevice::Update( | 414 void BluetoothDeviceDBus::Update( |
| 407 const BluetoothDeviceClient::Properties* properties, | 415 const BluetoothDeviceClient::Properties* properties, |
| 408 bool update_state) { | 416 bool update_state) { |
| 409 std::string address = properties->address.value(); | 417 std::string address = properties->address.value(); |
| 410 std::string name = properties->name.value(); | 418 std::string name = properties->name.value(); |
| 411 uint32 bluetooth_class = properties->bluetooth_class.value(); | 419 uint32 bluetooth_class = properties->bluetooth_class.value(); |
| 412 const std::vector<std::string>& uuids = properties->uuids.value(); | 420 const std::vector<std::string>& uuids = properties->uuids.value(); |
| 413 | 421 |
| 414 if (!address.empty()) | 422 if (!address.empty()) |
| 415 address_ = address; | 423 address_ = address; |
| 416 if (!name.empty()) | 424 if (!name.empty()) |
| 417 name_ = name; | 425 name_ = name; |
| 418 if (bluetooth_class) | 426 if (bluetooth_class) |
| 419 bluetooth_class_ = bluetooth_class; | 427 bluetooth_class_ = bluetooth_class; |
| 420 if (!uuids.empty()) { | 428 if (!uuids.empty()) { |
| 421 service_uuids_.clear(); | 429 service_uuids_.clear(); |
| 422 service_uuids_.assign(uuids.begin(), uuids.end()); | 430 service_uuids_.assign(uuids.begin(), uuids.end()); |
| 423 } | 431 } |
| 424 | 432 |
| 425 if (update_state) { | 433 if (update_state) { |
| 426 // BlueZ uses paired to mean link keys exchanged, whereas the Bluetooth | 434 // BlueZ uses paired to mean link keys exchanged, whereas the Bluetooth |
| 427 // spec refers to this as bonded. Use the spec name for our interface. | 435 // spec refers to this as bonded. Use the spec name for our interface. |
| 428 bonded_ = properties->paired.value(); | 436 bonded_ = properties->paired.value(); |
| 429 connected_ = properties->connected.value(); | 437 connected_ = properties->connected.value(); |
| 430 } | 438 } |
| 431 } | 439 } |
| 432 | 440 |
| 433 void BluetoothDevice::ConnectCallback(const base::Closure& callback, | 441 void BluetoothDeviceDBus::ConnectCallback(const base::Closure& callback, |
| 434 const ErrorCallback& error_callback, | 442 const ErrorCallback& error_callback, |
| 435 const dbus::ObjectPath& device_path) { | 443 const dbus::ObjectPath& device_path) { |
| 436 DVLOG(1) << "Connection successful: " << device_path.value(); | 444 DVLOG(1) << "Connection successful: " << device_path.value(); |
| 437 if (object_path_.value().empty()) { | 445 if (object_path_.value().empty()) { |
| 438 object_path_ = device_path; | 446 object_path_ = device_path; |
| 439 } else { | 447 } else { |
| 440 LOG_IF(WARNING, object_path_ != device_path) | 448 LOG_IF(WARNING, object_path_ != device_path) |
| 441 << "Conflicting device paths for objects, result gave: " | 449 << "Conflicting device paths for objects, result gave: " |
| 442 << device_path.value() << " but signal gave: " | 450 << device_path.value() << " but signal gave: " |
| 443 << object_path_.value(); | 451 << object_path_.value(); |
| 444 } | 452 } |
| 445 | 453 |
| 446 // Mark the device trusted so it can connect to us automatically, and | 454 // Mark the device trusted so it can connect to us automatically, and |
| 447 // we can connect after rebooting. This information is part of the | 455 // we can connect after rebooting. This information is part of the |
| 448 // pairing information of the device, and is unique to the combination | 456 // pairing information of the device, and is unique to the combination |
| 449 // of our bluetooth address and the device's bluetooth address. A | 457 // of our bluetooth address and the device's bluetooth address. A |
| 450 // different host needs a new pairing, so it's not useful to sync. | 458 // different host needs a new pairing, so it's not useful to sync. |
| 451 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 459 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
| 452 GetProperties(object_path_)->trusted.Set( | 460 GetProperties(object_path_)->trusted.Set( |
| 453 true, | 461 true, |
| 454 base::Bind(&BluetoothDevice::OnSetTrusted, | 462 base::Bind(&BluetoothDeviceDBus::OnSetTrusted, |
| 455 weak_ptr_factory_.GetWeakPtr(), | 463 weak_ptr_factory_.GetWeakPtr(), |
| 456 callback, | 464 callback, |
| 457 error_callback)); | 465 error_callback)); |
| 458 | 466 |
| 459 // Connect application-layer protocols. | 467 // Connect application-layer protocols. |
| 460 ConnectApplications(callback, error_callback); | 468 ConnectApplications(callback, error_callback); |
| 461 } | 469 } |
| 462 | 470 |
| 463 void BluetoothDevice::ConnectErrorCallback(const ErrorCallback& error_callback, | 471 void BluetoothDeviceDBus::ConnectErrorCallback( |
| 464 const std::string& error_name, | 472 const ErrorCallback& error_callback, |
| 465 const std::string& error_message) { | 473 const std::string& error_name, |
| 474 const std::string& error_message) { | |
| 466 LOG(WARNING) << "Connection failed: " << address_ | 475 LOG(WARNING) << "Connection failed: " << address_ |
| 467 << ": " << error_name << ": " << error_message; | 476 << ": " << error_name << ": " << error_message; |
| 468 error_callback.Run(); | 477 error_callback.Run(); |
| 469 } | 478 } |
| 470 | 479 |
| 471 void BluetoothDevice::CollectServiceRecordsCallback( | 480 void BluetoothDeviceDBus::CollectServiceRecordsCallback( |
| 472 const ServiceRecordsCallback& callback, | 481 const ServiceRecordsCallback& callback, |
| 473 const ErrorCallback& error_callback, | 482 const ErrorCallback& error_callback, |
| 474 const dbus::ObjectPath& device_path, | 483 const dbus::ObjectPath& device_path, |
| 475 const BluetoothDeviceClient::ServiceMap& service_map, | 484 const BluetoothDeviceClient::ServiceMap& service_map, |
| 476 bool success) { | 485 bool success) { |
| 477 if (!success) { | 486 if (!success) { |
| 478 error_callback.Run(); | 487 error_callback.Run(); |
| 479 return; | 488 return; |
| 480 } | 489 } |
| 481 | 490 |
| 482 ScopedVector<BluetoothServiceRecord> records; | 491 ScopedVector<BluetoothServiceRecord> records; |
| 483 for (BluetoothDeviceClient::ServiceMap::const_iterator i = | 492 for (BluetoothDeviceClient::ServiceMap::const_iterator i = |
| 484 service_map.begin(); i != service_map.end(); ++i) { | 493 service_map.begin(); i != service_map.end(); ++i) { |
| 485 records.push_back( | 494 records.push_back( |
| 486 new BluetoothServiceRecord(address(), i->second)); | 495 new BluetoothServiceRecord(address(), i->second)); |
| 487 } | 496 } |
| 488 callback.Run(records); | 497 callback.Run(records); |
| 489 } | 498 } |
| 490 | 499 |
| 491 void BluetoothDevice::OnSetTrusted(const base::Closure& callback, | 500 void BluetoothDeviceDBus::OnSetTrusted(const base::Closure& callback, |
| 492 const ErrorCallback& error_callback, | 501 const ErrorCallback& error_callback, |
| 493 bool success) { | 502 bool success) { |
| 494 if (success) { | 503 if (success) { |
| 495 callback.Run(); | 504 callback.Run(); |
| 496 } else { | 505 } else { |
| 497 LOG(WARNING) << "Failed to set device as trusted: " << address_; | 506 LOG(WARNING) << "Failed to set device as trusted: " << address_; |
| 498 error_callback.Run(); | 507 error_callback.Run(); |
| 499 } | 508 } |
| 500 } | 509 } |
| 501 | 510 |
| 502 void BluetoothDevice::ConnectApplications(const base::Closure& callback, | 511 void BluetoothDeviceDBus::ConnectApplications(const base::Closure& callback, |
| 503 const ErrorCallback& error_callback) { | 512 const ErrorCallback& error_callback) { |
| 504 // Introspect the device object to determine supported applications. | 513 // Introspect the device object to determine supported applications. |
| 505 DBusThreadManager::Get()->GetIntrospectableClient()-> | 514 DBusThreadManager::Get()->GetIntrospectableClient()-> |
| 506 Introspect(bluetooth_device::kBluetoothDeviceServiceName, | 515 Introspect(bluetooth_device::kBluetoothDeviceServiceName, |
| 507 object_path_, | 516 object_path_, |
| 508 base::Bind(&BluetoothDevice::OnIntrospect, | 517 base::Bind(&BluetoothDeviceDBus::OnIntrospect, |
| 509 weak_ptr_factory_.GetWeakPtr(), | 518 weak_ptr_factory_.GetWeakPtr(), |
| 510 callback, | 519 callback, |
| 511 error_callback)); | 520 error_callback)); |
| 512 } | 521 } |
| 513 | 522 |
| 514 void BluetoothDevice::OnIntrospect(const base::Closure& callback, | 523 void BluetoothDeviceDBus::OnIntrospect(const base::Closure& callback, |
| 515 const ErrorCallback& error_callback, | 524 const ErrorCallback& error_callback, |
| 516 const std::string& service_name, | 525 const std::string& service_name, |
| 517 const dbus::ObjectPath& device_path, | 526 const dbus::ObjectPath& device_path, |
| 518 const std::string& xml_data, | 527 const std::string& xml_data, |
| 519 bool success) { | 528 bool success) { |
| 520 if (!success) { | 529 if (!success) { |
| 521 LOG(WARNING) << "Failed to determine supported applications: " << address_; | 530 LOG(WARNING) << "Failed to determine supported applications: " << address_; |
| 522 error_callback.Run(); | 531 error_callback.Run(); |
| 523 return; | 532 return; |
| 524 } | 533 } |
| 525 | 534 |
| 526 // The introspection data for the device object may list one or more | 535 // The introspection data for the device object may list one or more |
| 527 // additional D-Bus interfaces that BlueZ supports for this particular | 536 // additional D-Bus interfaces that BlueZ supports for this particular |
| 528 // device. Send appropraite Connect calls for each of those interfaces | 537 // device. Send appropraite Connect calls for each of those interfaces |
| 529 // to connect all of the application protocols for this device. | 538 // to connect all of the application protocols for this device. |
| 530 std::vector<std::string> interfaces = | 539 std::vector<std::string> interfaces = |
| 531 IntrospectableClient::GetInterfacesFromIntrospectResult(xml_data); | 540 IntrospectableClient::GetInterfacesFromIntrospectResult(xml_data); |
| 532 | 541 |
| 533 DCHECK_EQ(0, connecting_applications_counter_); | 542 DCHECK_EQ(0, connecting_applications_counter_); |
| 534 connecting_applications_counter_ = 0; | 543 connecting_applications_counter_ = 0; |
| 535 for (std::vector<std::string>::iterator iter = interfaces.begin(); | 544 for (std::vector<std::string>::iterator iter = interfaces.begin(); |
| 536 iter != interfaces.end(); ++iter) { | 545 iter != interfaces.end(); ++iter) { |
| 537 if (*iter == bluetooth_input::kBluetoothInputInterface) { | 546 if (*iter == bluetooth_input::kBluetoothInputInterface) { |
| 538 connecting_applications_counter_++; | 547 connecting_applications_counter_++; |
| 539 // Supports Input interface. | 548 // Supports Input interface. |
| 540 DBusThreadManager::Get()->GetBluetoothInputClient()-> | 549 DBusThreadManager::Get()->GetBluetoothInputClient()-> |
| 541 Connect(object_path_, | 550 Connect(object_path_, |
| 542 base::Bind(&BluetoothDevice::OnConnect, | 551 base::Bind(&BluetoothDeviceDBus::OnConnect, |
| 543 weak_ptr_factory_.GetWeakPtr(), | 552 weak_ptr_factory_.GetWeakPtr(), |
| 544 callback, | 553 callback, |
| 545 *iter), | 554 *iter), |
| 546 base::Bind(&BluetoothDevice::OnConnectError, | 555 base::Bind(&BluetoothDeviceDBus::OnConnectError, |
| 547 weak_ptr_factory_.GetWeakPtr(), | 556 weak_ptr_factory_.GetWeakPtr(), |
| 548 error_callback, *iter)); | 557 error_callback, *iter)); |
| 549 } | 558 } |
| 550 } | 559 } |
| 551 | 560 |
| 552 // If OnConnect has been called for every call to Connect above, then this | 561 // If OnConnect has been called for every call to Connect above, then this |
| 553 // will decrement the counter to -1. In that case, call the callback | 562 // will decrement the counter to -1. In that case, call the callback |
| 554 // directly as it has not been called by any of the OnConnect callbacks. | 563 // directly as it has not been called by any of the OnConnect callbacks. |
| 555 // This is safe because OnIntrospect and OnConnect run on the same thread. | 564 // This is safe because OnIntrospect and OnConnect run on the same thread. |
| 556 connecting_applications_counter_--; | 565 connecting_applications_counter_--; |
| 557 if (connecting_applications_counter_ == -1) | 566 if (connecting_applications_counter_ == -1) |
| 558 callback.Run(); | 567 callback.Run(); |
| 559 } | 568 } |
| 560 | 569 |
| 561 void BluetoothDevice::OnConnect(const base::Closure& callback, | 570 void BluetoothDeviceDBus::OnConnect(const base::Closure& callback, |
| 562 const std::string& interface_name, | 571 const std::string& interface_name, |
| 563 const dbus::ObjectPath& device_path) { | 572 const dbus::ObjectPath& device_path) { |
| 564 DVLOG(1) << "Application connection successful: " << device_path.value() | 573 DVLOG(1) << "Application connection successful: " << device_path.value() |
| 565 << ": " << interface_name; | 574 << ": " << interface_name; |
| 566 | 575 |
| 567 connecting_applications_counter_--; | 576 connecting_applications_counter_--; |
| 568 // |callback| should only be called once, meaning it cannot be called before | 577 // |callback| should only be called once, meaning it cannot be called before |
| 569 // all requests have been started. The extra decrement after all requests | 578 // all requests have been started. The extra decrement after all requests |
| 570 // have been started, and the check for -1 instead of 0 below, insure only a | 579 // have been started, and the check for -1 instead of 0 below, insure only a |
| 571 // single call to |callback| will occur (provided OnConnect and OnIntrospect | 580 // single call to |callback| will occur (provided OnConnect and OnIntrospect |
| 572 // run on the same thread, which is true). | 581 // run on the same thread, which is true). |
| 573 if (connecting_applications_counter_ == -1) { | 582 if (connecting_applications_counter_ == -1) { |
| 574 connecting_applications_counter_ = 0; | 583 connecting_applications_counter_ = 0; |
| 575 callback.Run(); | 584 callback.Run(); |
| 576 } | 585 } |
| 577 } | 586 } |
| 578 | 587 |
| 579 void BluetoothDevice::OnConnectError(const ErrorCallback& error_callback, | 588 void BluetoothDeviceDBus::OnConnectError(const ErrorCallback& error_callback, |
| 580 const std::string& interface_name, | 589 const std::string& interface_name, |
| 581 const dbus::ObjectPath& device_path, | 590 const dbus::ObjectPath& device_path, |
| 582 const std::string& error_name, | 591 const std::string& error_name, |
| 583 const std::string& error_message) { | 592 const std::string& error_message) { |
| 584 LOG(WARNING) << "Connection failed: " << address_ << ": " << interface_name | 593 LOG(WARNING) << "Connection failed: " << address_ << ": " << interface_name |
| 585 << ": " << error_name << ": " << error_message; | 594 << ": " << error_name << ": " << error_message; |
| 586 error_callback.Run(); | 595 error_callback.Run(); |
| 587 } | 596 } |
| 588 | 597 |
| 589 void BluetoothDevice::DisconnectCallback(const base::Closure& callback, | 598 void BluetoothDeviceDBus::DisconnectCallback(const base::Closure& callback, |
| 590 const ErrorCallback& error_callback, | 599 const ErrorCallback& error_callback, |
| 591 const dbus::ObjectPath& device_path, | 600 const dbus::ObjectPath& device_path, |
| 592 bool success) { | 601 bool success) { |
| 593 DCHECK(device_path == object_path_); | 602 DCHECK(device_path == object_path_); |
| 594 if (success) { | 603 if (success) { |
| 595 DVLOG(1) << "Disconnection successful: " << address_; | 604 DVLOG(1) << "Disconnection successful: " << address_; |
| 596 callback.Run(); | 605 callback.Run(); |
| 597 } else { | 606 } else { |
| 598 LOG(WARNING) << "Disconnection failed: " << address_; | 607 LOG(WARNING) << "Disconnection failed: " << address_; |
| 599 error_callback.Run(); | 608 error_callback.Run(); |
| 600 } | 609 } |
| 601 } | 610 } |
| 602 | 611 |
| 603 void BluetoothDevice::ForgetCallback(const ErrorCallback& error_callback, | 612 void BluetoothDeviceDBus::ForgetCallback(const ErrorCallback& error_callback, |
| 604 const dbus::ObjectPath& adapter_path, | 613 const dbus::ObjectPath& adapter_path, |
| 605 bool success) { | 614 bool success) { |
| 606 // It's quite normal that this path never gets called on success; we use a | 615 // It's quite normal that this path never gets called on success; we use a |
| 607 // weak pointer, and bluetoothd might send the DeviceRemoved signal before | 616 // weak pointer, and bluetoothd might send the DeviceRemoved signal before |
| 608 // the method reply, in which case this object is deleted and the | 617 // the method reply, in which case this object is deleted and the |
| 609 // callback never takes place. Therefore don't do anything here for the | 618 // callback never takes place. Therefore don't do anything here for the |
| 610 // success case. | 619 // success case. |
| 611 if (!success) { | 620 if (!success) { |
| 612 LOG(WARNING) << "Forget failed: " << address_; | 621 LOG(WARNING) << "Forget failed: " << address_; |
| 613 error_callback.Run(); | 622 error_callback.Run(); |
| 614 } | 623 } |
| 615 } | 624 } |
| 616 | 625 |
| 617 void BluetoothDevice::SearchServicesForNameErrorCallback( | 626 void BluetoothDeviceDBus::SearchServicesForNameErrorCallback( |
| 618 const ProvidesServiceCallback& callback) { | 627 const ProvidesServiceCallback& callback) { |
| 619 callback.Run(false); | 628 callback.Run(false); |
| 620 } | 629 } |
| 621 | 630 |
| 622 void BluetoothDevice::SearchServicesForNameCallback( | 631 void BluetoothDeviceDBus::SearchServicesForNameCallback( |
| 623 const std::string& name, | 632 const std::string& name, |
| 624 const ProvidesServiceCallback& callback, | 633 const ProvidesServiceCallback& callback, |
| 625 const ServiceRecordList& list) { | 634 const ServiceRecordList& list) { |
| 626 for (ServiceRecordList::const_iterator i = list.begin(); | 635 for (ServiceRecordList::const_iterator i = list.begin(); |
| 627 i != list.end(); ++i) { | 636 i != list.end(); ++i) { |
| 628 if ((*i)->name() == name) { | 637 if ((*i)->name() == name) { |
| 629 callback.Run(true); | 638 callback.Run(true); |
| 630 return; | 639 return; |
| 631 } | 640 } |
| 632 } | 641 } |
| 633 callback.Run(false); | 642 callback.Run(false); |
| 634 } | 643 } |
| 635 | 644 |
| 636 void BluetoothDevice::GetServiceRecordsForConnectErrorCallback( | 645 void BluetoothDeviceDBus::GetServiceRecordsForConnectErrorCallback( |
| 637 const SocketCallback& callback) { | 646 const SocketCallback& callback) { |
| 638 callback.Run(NULL); | 647 callback.Run(NULL); |
| 639 } | 648 } |
| 640 | 649 |
| 641 void BluetoothDevice::GetServiceRecordsForConnectCallback( | 650 void BluetoothDeviceDBus::GetServiceRecordsForConnectCallback( |
| 642 const std::string& service_uuid, | 651 const std::string& service_uuid, |
| 643 const SocketCallback& callback, | 652 const SocketCallback& callback, |
| 644 const ServiceRecordList& list) { | 653 const ServiceRecordList& list) { |
| 645 for (ServiceRecordList::const_iterator i = list.begin(); | 654 for (ServiceRecordList::const_iterator i = list.begin(); |
| 646 i != list.end(); ++i) { | 655 i != list.end(); ++i) { |
| 647 if ((*i)->uuid() == service_uuid) { | 656 if ((*i)->uuid() == service_uuid) { |
| 648 // If multiple service records are found, use the first one that works. | 657 // If multiple service records are found, use the first one that works. |
| 649 scoped_refptr<BluetoothSocket> socket( | 658 scoped_refptr<BluetoothSocket> socket( |
| 650 BluetoothSocket::CreateBluetoothSocket(**i)); | 659 BluetoothSocketPosix::CreateBluetoothSocket(**i)); |
| 651 if (socket.get() != NULL) { | 660 if (socket.get() != NULL) { |
| 652 callback.Run(socket); | 661 callback.Run(socket); |
| 653 return; | 662 return; |
| 654 } | 663 } |
| 655 } | 664 } |
| 656 } | 665 } |
| 657 callback.Run(NULL); | 666 callback.Run(NULL); |
| 658 } | 667 } |
| 659 | 668 |
| 660 void BluetoothDevice::OnRemoteDataCallback(const base::Closure& callback, | 669 void BluetoothDeviceDBus::OnRemoteDataCallback(const base::Closure& callback, |
| 661 const ErrorCallback& error_callback, | 670 const ErrorCallback& error_callback, |
| 662 bool success) { | 671 bool success) { |
| 663 if (success) | 672 if (success) |
| 664 callback.Run(); | 673 callback.Run(); |
| 665 else | 674 else |
| 666 error_callback.Run(); | 675 error_callback.Run(); |
| 667 } | 676 } |
| 668 | 677 |
| 669 void BluetoothDevice::DisconnectRequested(const dbus::ObjectPath& object_path) { | 678 void BluetoothDeviceDBus::DisconnectRequested( |
| 679 const dbus::ObjectPath& object_path) { | |
| 670 DCHECK(object_path == object_path_); | 680 DCHECK(object_path == object_path_); |
| 671 } | 681 } |
| 672 | 682 |
| 673 void BluetoothDevice::Release() { | 683 void BluetoothDeviceDBus::Release() { |
| 674 DCHECK(agent_.get()); | 684 DCHECK(agent_.get()); |
| 675 DVLOG(1) << "Release: " << address_; | 685 DVLOG(1) << "Release: " << address_; |
| 676 | 686 |
| 677 DCHECK(pairing_delegate_); | 687 DCHECK(pairing_delegate_); |
| 678 pairing_delegate_->DismissDisplayOrConfirm(); | 688 pairing_delegate_->DismissDisplayOrConfirm(); |
| 679 pairing_delegate_ = NULL; | 689 pairing_delegate_ = NULL; |
| 680 | 690 |
| 681 pincode_callback_.Reset(); | 691 pincode_callback_.Reset(); |
| 682 passkey_callback_.Reset(); | 692 passkey_callback_.Reset(); |
| 683 confirmation_callback_.Reset(); | 693 confirmation_callback_.Reset(); |
| 684 | 694 |
| 685 agent_.reset(); | 695 agent_.reset(); |
| 686 } | 696 } |
| 687 | 697 |
| 688 void BluetoothDevice::RequestPinCode(const dbus::ObjectPath& device_path, | 698 void BluetoothDeviceDBus::RequestPinCode(const dbus::ObjectPath& device_path, |
| 689 const PinCodeCallback& callback) { | 699 const PinCodeCallback& callback) { |
| 690 DCHECK(agent_.get()); | 700 DCHECK(agent_.get()); |
| 691 DVLOG(1) << "RequestPinCode: " << device_path.value(); | 701 DVLOG(1) << "RequestPinCode: " << device_path.value(); |
| 692 | 702 |
| 693 DCHECK(pairing_delegate_); | 703 DCHECK(pairing_delegate_); |
| 694 DCHECK(pincode_callback_.is_null()); | 704 DCHECK(pincode_callback_.is_null()); |
| 695 pincode_callback_ = callback; | 705 pincode_callback_ = callback; |
| 696 pairing_delegate_->RequestPinCode(this); | 706 pairing_delegate_->RequestPinCode(this); |
| 697 } | 707 } |
| 698 | 708 |
| 699 void BluetoothDevice::RequestPasskey(const dbus::ObjectPath& device_path, | 709 void BluetoothDeviceDBus::RequestPasskey(const dbus::ObjectPath& device_path, |
| 700 const PasskeyCallback& callback) { | 710 const PasskeyCallback& callback) { |
| 701 DCHECK(agent_.get()); | 711 DCHECK(agent_.get()); |
| 702 DCHECK(device_path == object_path_); | 712 DCHECK(device_path == object_path_); |
| 703 DVLOG(1) << "RequestPasskey: " << device_path.value(); | 713 DVLOG(1) << "RequestPasskey: " << device_path.value(); |
| 704 | 714 |
| 705 DCHECK(pairing_delegate_); | 715 DCHECK(pairing_delegate_); |
| 706 DCHECK(passkey_callback_.is_null()); | 716 DCHECK(passkey_callback_.is_null()); |
| 707 passkey_callback_ = callback; | 717 passkey_callback_ = callback; |
| 708 pairing_delegate_->RequestPasskey(this); | 718 pairing_delegate_->RequestPasskey(this); |
| 709 } | 719 } |
| 710 | 720 |
| 711 void BluetoothDevice::DisplayPinCode(const dbus::ObjectPath& device_path, | 721 void BluetoothDeviceDBus::DisplayPinCode(const dbus::ObjectPath& device_path, |
| 712 const std::string& pincode) { | 722 const std::string& pincode) { |
| 713 DCHECK(agent_.get()); | 723 DCHECK(agent_.get()); |
| 714 DCHECK(device_path == object_path_); | 724 DCHECK(device_path == object_path_); |
| 715 DVLOG(1) << "DisplayPinCode: " << device_path.value() << " " << pincode; | 725 DVLOG(1) << "DisplayPinCode: " << device_path.value() << " " << pincode; |
| 716 | 726 |
| 717 DCHECK(pairing_delegate_); | 727 DCHECK(pairing_delegate_); |
| 718 pairing_delegate_->DisplayPinCode(this, pincode); | 728 pairing_delegate_->DisplayPinCode(this, pincode); |
| 719 } | 729 } |
| 720 | 730 |
| 721 void BluetoothDevice::DisplayPasskey(const dbus::ObjectPath& device_path, | 731 void BluetoothDeviceDBus::DisplayPasskey(const dbus::ObjectPath& device_path, |
| 722 uint32 passkey) { | 732 uint32 passkey) { |
| 723 DCHECK(agent_.get()); | 733 DCHECK(agent_.get()); |
| 724 DCHECK(device_path == object_path_); | 734 DCHECK(device_path == object_path_); |
| 725 DVLOG(1) << "DisplayPasskey: " << device_path.value() << " " << passkey; | 735 DVLOG(1) << "DisplayPasskey: " << device_path.value() << " " << passkey; |
| 726 | 736 |
| 727 DCHECK(pairing_delegate_); | 737 DCHECK(pairing_delegate_); |
| 728 pairing_delegate_->DisplayPasskey(this, passkey); | 738 pairing_delegate_->DisplayPasskey(this, passkey); |
| 729 } | 739 } |
| 730 | 740 |
| 731 void BluetoothDevice::RequestConfirmation( | 741 void BluetoothDeviceDBus::RequestConfirmation( |
| 732 const dbus::ObjectPath& device_path, | 742 const dbus::ObjectPath& device_path, |
| 733 uint32 passkey, | 743 uint32 passkey, |
| 734 const ConfirmationCallback& callback) { | 744 const ConfirmationCallback& callback) { |
| 735 DCHECK(agent_.get()); | 745 DCHECK(agent_.get()); |
| 736 DCHECK(device_path == object_path_); | 746 DCHECK(device_path == object_path_); |
| 737 DVLOG(1) << "RequestConfirmation: " << device_path.value() << " " << passkey; | 747 DVLOG(1) << "RequestConfirmation: " << device_path.value() << " " << passkey; |
| 738 | 748 |
| 739 DCHECK(pairing_delegate_); | 749 DCHECK(pairing_delegate_); |
| 740 DCHECK(confirmation_callback_.is_null()); | 750 DCHECK(confirmation_callback_.is_null()); |
| 741 confirmation_callback_ = callback; | 751 confirmation_callback_ = callback; |
| 742 pairing_delegate_->ConfirmPasskey(this, passkey); | 752 pairing_delegate_->ConfirmPasskey(this, passkey); |
| 743 } | 753 } |
| 744 | 754 |
| 745 void BluetoothDevice::Authorize(const dbus::ObjectPath& device_path, | 755 void BluetoothDeviceDBus::Authorize(const dbus::ObjectPath& device_path, |
| 746 const std::string& uuid, | 756 const std::string& uuid, |
| 747 const ConfirmationCallback& callback) { | 757 const ConfirmationCallback& callback) { |
| 748 DCHECK(agent_.get()); | 758 DCHECK(agent_.get()); |
| 749 DCHECK(device_path == object_path_); | 759 DCHECK(device_path == object_path_); |
| 750 LOG(WARNING) << "Rejected authorization for service: " << uuid | 760 LOG(WARNING) << "Rejected authorization for service: " << uuid |
| 751 << " requested from device: " << device_path.value(); | 761 << " requested from device: " << device_path.value(); |
| 752 callback.Run(REJECTED); | 762 callback.Run(REJECTED); |
| 753 } | 763 } |
| 754 | 764 |
| 755 void BluetoothDevice::ConfirmModeChange(Mode mode, | 765 void BluetoothDeviceDBus::ConfirmModeChange( |
| 756 const ConfirmationCallback& callback) { | 766 Mode mode, |
| 767 const ConfirmationCallback& callback) { | |
| 757 DCHECK(agent_.get()); | 768 DCHECK(agent_.get()); |
| 758 LOG(WARNING) << "Rejected adapter-level mode change: " << mode | 769 LOG(WARNING) << "Rejected adapter-level mode change: " << mode |
| 759 << " made on agent for device: " << address_; | 770 << " made on agent for device: " << address_; |
| 760 callback.Run(REJECTED); | 771 callback.Run(REJECTED); |
| 761 } | 772 } |
| 762 | 773 |
| 763 void BluetoothDevice::Cancel() { | 774 void BluetoothDeviceDBus::Cancel() { |
| 764 DCHECK(agent_.get()); | 775 DCHECK(agent_.get()); |
| 765 DVLOG(1) << "Cancel: " << address_; | 776 DVLOG(1) << "Cancel: " << address_; |
| 766 | 777 |
| 767 DCHECK(pairing_delegate_); | 778 DCHECK(pairing_delegate_); |
| 768 pairing_delegate_->DismissDisplayOrConfirm(); | 779 pairing_delegate_->DismissDisplayOrConfirm(); |
| 769 } | 780 } |
| 770 | 781 |
| 771 | 782 |
| 772 // static | 783 // static |
| 773 BluetoothDevice* BluetoothDevice::Create(BluetoothAdapter* adapter) { | 784 BluetoothDeviceDBus* BluetoothDeviceDBus::Create( |
| 774 return new BluetoothDevice(adapter); | 785 BluetoothAdapterDBus* adapter) { |
| 786 return new BluetoothDeviceDBus(adapter); | |
| 775 } | 787 } |
| 776 | 788 |
| 777 } // namespace chromeos | 789 } // namespace chromeos |
| OLD | NEW |