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