| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/bluetooth/bluetooth_pairing_chromeos.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "device/bluetooth/bluetooth_device.h" | |
| 10 #include "device/bluetooth/bluetooth_device_chromeos.h" | |
| 11 | |
| 12 using device::BluetoothDevice; | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Histogram enumerations for pairing methods. | |
| 17 enum UMAPairingMethod { | |
| 18 UMA_PAIRING_METHOD_NONE, | |
| 19 UMA_PAIRING_METHOD_REQUEST_PINCODE, | |
| 20 UMA_PAIRING_METHOD_REQUEST_PASSKEY, | |
| 21 UMA_PAIRING_METHOD_DISPLAY_PINCODE, | |
| 22 UMA_PAIRING_METHOD_DISPLAY_PASSKEY, | |
| 23 UMA_PAIRING_METHOD_CONFIRM_PASSKEY, | |
| 24 // NOTE: Add new pairing methods immediately above this line. Make sure to | |
| 25 // update the enum list in tools/histogram/histograms.xml accordingly. | |
| 26 UMA_PAIRING_METHOD_COUNT | |
| 27 }; | |
| 28 | |
| 29 // Number of keys that will be entered for a passkey, six digits plus the | |
| 30 // final enter. | |
| 31 const uint16 kPasskeyMaxKeysEntered = 7; | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 namespace chromeos { | |
| 36 | |
| 37 BluetoothPairingChromeOS::BluetoothPairingChromeOS( | |
| 38 BluetoothDeviceChromeOS* device, | |
| 39 BluetoothDevice::PairingDelegate* pairing_delegate) | |
| 40 : device_(device), | |
| 41 pairing_delegate_(pairing_delegate), | |
| 42 pairing_delegate_used_(false) { | |
| 43 VLOG(1) << "Created BluetoothPairingChromeOS for " | |
| 44 << device_->GetAddress(); | |
| 45 } | |
| 46 | |
| 47 BluetoothPairingChromeOS::~BluetoothPairingChromeOS() { | |
| 48 VLOG(1) << "Destroying BluetoothPairingChromeOS for " | |
| 49 << device_->GetAddress(); | |
| 50 | |
| 51 if (!pairing_delegate_used_) { | |
| 52 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
| 53 UMA_PAIRING_METHOD_NONE, | |
| 54 UMA_PAIRING_METHOD_COUNT); | |
| 55 } | |
| 56 | |
| 57 if (!pincode_callback_.is_null()) { | |
| 58 pincode_callback_.Run( | |
| 59 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED, ""); | |
| 60 } | |
| 61 | |
| 62 if (!passkey_callback_.is_null()) { | |
| 63 passkey_callback_.Run( | |
| 64 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED, 0); | |
| 65 } | |
| 66 | |
| 67 if (!confirmation_callback_.is_null()) { | |
| 68 confirmation_callback_.Run( | |
| 69 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED); | |
| 70 } | |
| 71 | |
| 72 pairing_delegate_ = NULL; | |
| 73 } | |
| 74 | |
| 75 void BluetoothPairingChromeOS::RequestPinCode( | |
| 76 const bluez::BluetoothAgentServiceProvider::Delegate::PinCodeCallback& | |
| 77 callback) { | |
| 78 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
| 79 UMA_PAIRING_METHOD_REQUEST_PINCODE, | |
| 80 UMA_PAIRING_METHOD_COUNT); | |
| 81 | |
| 82 ResetCallbacks(); | |
| 83 pincode_callback_ = callback; | |
| 84 pairing_delegate_used_ = true; | |
| 85 pairing_delegate_->RequestPinCode(device_); | |
| 86 } | |
| 87 | |
| 88 bool BluetoothPairingChromeOS::ExpectingPinCode() const { | |
| 89 return !pincode_callback_.is_null(); | |
| 90 } | |
| 91 | |
| 92 void BluetoothPairingChromeOS::SetPinCode(const std::string& pincode) { | |
| 93 if (pincode_callback_.is_null()) | |
| 94 return; | |
| 95 | |
| 96 pincode_callback_.Run(bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS, | |
| 97 pincode); | |
| 98 pincode_callback_.Reset(); | |
| 99 | |
| 100 // If this is not an outgoing connection to the device, clean up the pairing | |
| 101 // context since the pairing is done. The outgoing connection case is cleaned | |
| 102 // up in the callback for the underlying Pair() call. | |
| 103 if (!device_->IsConnecting()) | |
| 104 device_->EndPairing(); | |
| 105 } | |
| 106 | |
| 107 void BluetoothPairingChromeOS::DisplayPinCode(const std::string& pincode) { | |
| 108 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
| 109 UMA_PAIRING_METHOD_DISPLAY_PINCODE, | |
| 110 UMA_PAIRING_METHOD_COUNT); | |
| 111 | |
| 112 ResetCallbacks(); | |
| 113 pairing_delegate_used_ = true; | |
| 114 pairing_delegate_->DisplayPinCode(device_, pincode); | |
| 115 | |
| 116 // If this is not an outgoing connection to the device, the pairing context | |
| 117 // needs to be cleaned up again as there's no reliable indication of | |
| 118 // completion of incoming pairing. | |
| 119 if (!device_->IsConnecting()) | |
| 120 device_->EndPairing(); | |
| 121 } | |
| 122 | |
| 123 void BluetoothPairingChromeOS::RequestPasskey( | |
| 124 const bluez::BluetoothAgentServiceProvider::Delegate::PasskeyCallback& | |
| 125 callback) { | |
| 126 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
| 127 UMA_PAIRING_METHOD_REQUEST_PASSKEY, | |
| 128 UMA_PAIRING_METHOD_COUNT); | |
| 129 | |
| 130 ResetCallbacks(); | |
| 131 passkey_callback_ = callback; | |
| 132 pairing_delegate_used_ = true; | |
| 133 pairing_delegate_->RequestPasskey(device_); | |
| 134 } | |
| 135 | |
| 136 bool BluetoothPairingChromeOS::ExpectingPasskey() const { | |
| 137 return !passkey_callback_.is_null(); | |
| 138 } | |
| 139 | |
| 140 void BluetoothPairingChromeOS::SetPasskey(uint32 passkey) { | |
| 141 if (passkey_callback_.is_null()) | |
| 142 return; | |
| 143 | |
| 144 passkey_callback_.Run(bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS, | |
| 145 passkey); | |
| 146 passkey_callback_.Reset(); | |
| 147 | |
| 148 // If this is not an outgoing connection to the device, clean up the pairing | |
| 149 // context since the pairing is done. The outgoing connection case is cleaned | |
| 150 // up in the callback for the underlying Pair() call. | |
| 151 if (!device_->IsConnecting()) | |
| 152 device_->EndPairing(); | |
| 153 } | |
| 154 | |
| 155 void BluetoothPairingChromeOS::DisplayPasskey(uint32 passkey) { | |
| 156 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
| 157 UMA_PAIRING_METHOD_DISPLAY_PASSKEY, | |
| 158 UMA_PAIRING_METHOD_COUNT); | |
| 159 | |
| 160 ResetCallbacks(); | |
| 161 pairing_delegate_used_ = true; | |
| 162 pairing_delegate_->DisplayPasskey(device_, passkey); | |
| 163 | |
| 164 } | |
| 165 | |
| 166 void BluetoothPairingChromeOS::KeysEntered(uint16 entered) { | |
| 167 pairing_delegate_used_ = true; | |
| 168 pairing_delegate_->KeysEntered(device_, entered); | |
| 169 | |
| 170 // If this is not an outgoing connection to the device, the pairing context | |
| 171 // needs to be cleaned up again as there's no reliable indication of | |
| 172 // completion of incoming pairing. | |
| 173 if (entered >= kPasskeyMaxKeysEntered && !device_->IsConnecting()) | |
| 174 device_->EndPairing(); | |
| 175 } | |
| 176 | |
| 177 void BluetoothPairingChromeOS::RequestConfirmation( | |
| 178 uint32 passkey, | |
| 179 const bluez::BluetoothAgentServiceProvider::Delegate::ConfirmationCallback& | |
| 180 callback) { | |
| 181 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
| 182 UMA_PAIRING_METHOD_CONFIRM_PASSKEY, | |
| 183 UMA_PAIRING_METHOD_COUNT); | |
| 184 | |
| 185 ResetCallbacks(); | |
| 186 confirmation_callback_ = callback; | |
| 187 pairing_delegate_used_ = true; | |
| 188 pairing_delegate_->ConfirmPasskey(device_, passkey); | |
| 189 } | |
| 190 | |
| 191 void BluetoothPairingChromeOS::RequestAuthorization( | |
| 192 const bluez::BluetoothAgentServiceProvider::Delegate::ConfirmationCallback& | |
| 193 callback) { | |
| 194 UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod", | |
| 195 UMA_PAIRING_METHOD_NONE, | |
| 196 UMA_PAIRING_METHOD_COUNT); | |
| 197 | |
| 198 ResetCallbacks(); | |
| 199 confirmation_callback_ = callback; | |
| 200 pairing_delegate_used_ = true; | |
| 201 pairing_delegate_->AuthorizePairing(device_); | |
| 202 } | |
| 203 | |
| 204 bool BluetoothPairingChromeOS::ExpectingConfirmation() const { | |
| 205 return !confirmation_callback_.is_null(); | |
| 206 } | |
| 207 | |
| 208 void BluetoothPairingChromeOS::ConfirmPairing() { | |
| 209 if (confirmation_callback_.is_null()) | |
| 210 return; | |
| 211 | |
| 212 confirmation_callback_.Run( | |
| 213 bluez::BluetoothAgentServiceProvider::Delegate::SUCCESS); | |
| 214 confirmation_callback_.Reset(); | |
| 215 | |
| 216 // If this is not an outgoing connection to the device, clean up the pairing | |
| 217 // context since the pairing is done. The outgoing connection case is cleaned | |
| 218 // up in the callback for the underlying Pair() call. | |
| 219 if (!device_->IsConnecting()) | |
| 220 device_->EndPairing(); | |
| 221 } | |
| 222 | |
| 223 bool BluetoothPairingChromeOS::RejectPairing() { | |
| 224 return RunPairingCallbacks( | |
| 225 bluez::BluetoothAgentServiceProvider::Delegate::REJECTED); | |
| 226 } | |
| 227 | |
| 228 bool BluetoothPairingChromeOS::CancelPairing() { | |
| 229 return RunPairingCallbacks( | |
| 230 bluez::BluetoothAgentServiceProvider::Delegate::CANCELLED); | |
| 231 } | |
| 232 | |
| 233 BluetoothDevice::PairingDelegate* | |
| 234 BluetoothPairingChromeOS::GetPairingDelegate() const { | |
| 235 return pairing_delegate_; | |
| 236 } | |
| 237 | |
| 238 void BluetoothPairingChromeOS::ResetCallbacks() { | |
| 239 pincode_callback_.Reset(); | |
| 240 passkey_callback_.Reset(); | |
| 241 confirmation_callback_.Reset(); | |
| 242 } | |
| 243 | |
| 244 bool BluetoothPairingChromeOS::RunPairingCallbacks( | |
| 245 bluez::BluetoothAgentServiceProvider::Delegate::Status status) { | |
| 246 pairing_delegate_used_ = true; | |
| 247 | |
| 248 bool callback_run = false; | |
| 249 if (!pincode_callback_.is_null()) { | |
| 250 pincode_callback_.Run(status, ""); | |
| 251 pincode_callback_.Reset(); | |
| 252 callback_run = true; | |
| 253 } | |
| 254 | |
| 255 if (!passkey_callback_.is_null()) { | |
| 256 passkey_callback_.Run(status, 0); | |
| 257 passkey_callback_.Reset(); | |
| 258 callback_run = true; | |
| 259 } | |
| 260 | |
| 261 if (!confirmation_callback_.is_null()) { | |
| 262 confirmation_callback_.Run(status); | |
| 263 confirmation_callback_.Reset(); | |
| 264 callback_run = true; | |
| 265 } | |
| 266 | |
| 267 // If this is not an outgoing connection to the device, clean up the pairing | |
| 268 // context since the pairing is done. The outgoing connection case is cleaned | |
| 269 // up in the callback for the underlying Pair() call. | |
| 270 if (!device_->IsConnecting()) | |
| 271 device_->EndPairing(); | |
| 272 | |
| 273 return callback_run; | |
| 274 } | |
| 275 | |
| 276 } // namespace chromeos | |
| OLD | NEW |