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