This source file includes following definitions.
- pairing_delegate_used_
- RequestPinCode
- ExpectingPinCode
- SetPinCode
- DisplayPinCode
- RequestPasskey
- ExpectingPasskey
- SetPasskey
- DisplayPasskey
- KeysEntered
- RequestConfirmation
- RequestAuthorization
- ExpectingConfirmation
- ConfirmPairing
- RejectPairing
- CancelPairing
- GetPairingDelegate
- ResetCallbacks
- RunPairingCallbacks
#include "device/bluetooth/bluetooth_pairing_chromeos.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_device_chromeos.h"
using device::BluetoothDevice;
namespace {
enum UMAPairingMethod {
UMA_PAIRING_METHOD_NONE,
UMA_PAIRING_METHOD_REQUEST_PINCODE,
UMA_PAIRING_METHOD_REQUEST_PASSKEY,
UMA_PAIRING_METHOD_DISPLAY_PINCODE,
UMA_PAIRING_METHOD_DISPLAY_PASSKEY,
UMA_PAIRING_METHOD_CONFIRM_PASSKEY,
UMA_PAIRING_METHOD_COUNT
};
const uint16 kPasskeyMaxKeysEntered = 7;
}
namespace chromeos {
BluetoothPairingChromeOS::BluetoothPairingChromeOS(
BluetoothDeviceChromeOS* device,
BluetoothDevice::PairingDelegate* pairing_delegate)
: device_(device),
pairing_delegate_(pairing_delegate),
pairing_delegate_used_(false) {
VLOG(1) << "Created BluetoothPairingChromeOS for "
<< device_->GetAddress();
}
BluetoothPairingChromeOS::~BluetoothPairingChromeOS() {
VLOG(1) << "Destroying BluetoothPairingChromeOS for "
<< device_->GetAddress();
if (!pairing_delegate_used_) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_NONE,
UMA_PAIRING_METHOD_COUNT);
}
if (!pincode_callback_.is_null()) {
pincode_callback_.Run(
BluetoothAgentServiceProvider::Delegate::CANCELLED, "");
}
if (!passkey_callback_.is_null()) {
passkey_callback_.Run(
BluetoothAgentServiceProvider::Delegate::CANCELLED, 0);
}
if (!confirmation_callback_.is_null()) {
confirmation_callback_.Run(
BluetoothAgentServiceProvider::Delegate::CANCELLED);
}
pairing_delegate_ = NULL;
}
void BluetoothPairingChromeOS::RequestPinCode(
const BluetoothAgentServiceProvider::Delegate::PinCodeCallback& callback) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_REQUEST_PINCODE,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
pincode_callback_ = callback;
pairing_delegate_used_ = true;
pairing_delegate_->RequestPinCode(device_);
}
bool BluetoothPairingChromeOS::ExpectingPinCode() const {
return !pincode_callback_.is_null();
}
void BluetoothPairingChromeOS::SetPinCode(const std::string& pincode) {
if (pincode_callback_.is_null())
return;
pincode_callback_.Run(BluetoothAgentServiceProvider::Delegate::SUCCESS,
pincode);
pincode_callback_.Reset();
if (!device_->IsConnecting())
device_->EndPairing();
}
void BluetoothPairingChromeOS::DisplayPinCode(const std::string& pincode) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_DISPLAY_PINCODE,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
pairing_delegate_used_ = true;
pairing_delegate_->DisplayPinCode(device_, pincode);
if (!device_->IsConnecting())
device_->EndPairing();
}
void BluetoothPairingChromeOS::RequestPasskey(
const BluetoothAgentServiceProvider::Delegate::PasskeyCallback& callback) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_REQUEST_PASSKEY,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
passkey_callback_ = callback;
pairing_delegate_used_ = true;
pairing_delegate_->RequestPasskey(device_);
}
bool BluetoothPairingChromeOS::ExpectingPasskey() const {
return !passkey_callback_.is_null();
}
void BluetoothPairingChromeOS::SetPasskey(uint32 passkey) {
if (passkey_callback_.is_null())
return;
passkey_callback_.Run(BluetoothAgentServiceProvider::Delegate::SUCCESS,
passkey);
passkey_callback_.Reset();
if (!device_->IsConnecting())
device_->EndPairing();
}
void BluetoothPairingChromeOS::DisplayPasskey(uint32 passkey) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_DISPLAY_PASSKEY,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
pairing_delegate_used_ = true;
pairing_delegate_->DisplayPasskey(device_, passkey);
}
void BluetoothPairingChromeOS::KeysEntered(uint16 entered) {
pairing_delegate_used_ = true;
pairing_delegate_->KeysEntered(device_, entered);
if (entered >= kPasskeyMaxKeysEntered && !device_->IsConnecting())
device_->EndPairing();
}
void BluetoothPairingChromeOS::RequestConfirmation(
uint32 passkey,
const BluetoothAgentServiceProvider::Delegate::ConfirmationCallback&
callback) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_CONFIRM_PASSKEY,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
confirmation_callback_ = callback;
pairing_delegate_used_ = true;
pairing_delegate_->ConfirmPasskey(device_, passkey);
}
void BluetoothPairingChromeOS::RequestAuthorization(
const BluetoothAgentServiceProvider::Delegate::ConfirmationCallback&
callback) {
UMA_HISTOGRAM_ENUMERATION("Bluetooth.PairingMethod",
UMA_PAIRING_METHOD_NONE,
UMA_PAIRING_METHOD_COUNT);
ResetCallbacks();
confirmation_callback_ = callback;
pairing_delegate_used_ = true;
pairing_delegate_->AuthorizePairing(device_);
}
bool BluetoothPairingChromeOS::ExpectingConfirmation() const {
return !confirmation_callback_.is_null();
}
void BluetoothPairingChromeOS::ConfirmPairing() {
if (confirmation_callback_.is_null())
return;
confirmation_callback_.Run(BluetoothAgentServiceProvider::Delegate::SUCCESS);
confirmation_callback_.Reset();
if (!device_->IsConnecting())
device_->EndPairing();
}
bool BluetoothPairingChromeOS::RejectPairing() {
return RunPairingCallbacks(
BluetoothAgentServiceProvider::Delegate::REJECTED);
}
bool BluetoothPairingChromeOS::CancelPairing() {
return RunPairingCallbacks(
BluetoothAgentServiceProvider::Delegate::CANCELLED);
}
BluetoothDevice::PairingDelegate*
BluetoothPairingChromeOS::GetPairingDelegate() const {
return pairing_delegate_;
}
void BluetoothPairingChromeOS::ResetCallbacks() {
pincode_callback_.Reset();
passkey_callback_.Reset();
confirmation_callback_.Reset();
}
bool BluetoothPairingChromeOS::RunPairingCallbacks(
BluetoothAgentServiceProvider::Delegate::Status status) {
pairing_delegate_used_ = true;
bool callback_run = false;
if (!pincode_callback_.is_null()) {
pincode_callback_.Run(status, "");
pincode_callback_.Reset();
callback_run = true;
}
if (!passkey_callback_.is_null()) {
passkey_callback_.Run(status, 0);
passkey_callback_.Reset();
callback_run = true;
}
if (!confirmation_callback_.is_null()) {
confirmation_callback_.Run(status);
confirmation_callback_.Reset();
callback_run = true;
}
if (!device_->IsConnecting())
device_->EndPairing();
return callback_run;
}
}