| Index: extensions/browser/api/bluetooth/bluetooth_private_api.cc
|
| diff --git a/extensions/browser/api/bluetooth/bluetooth_private_api.cc b/extensions/browser/api/bluetooth/bluetooth_private_api.cc
|
| index 43fca8ca2ccc2897b7873eee8c415ed8204a1a6a..794ad6a241ef94a852c28b77e5282b7320f45ae9 100644
|
| --- a/extensions/browser/api/bluetooth/bluetooth_private_api.cc
|
| +++ b/extensions/browser/api/bluetooth/bluetooth_private_api.cc
|
| @@ -20,9 +20,18 @@ namespace SetDiscoveryFilter = bt_private::SetDiscoveryFilter;
|
|
|
| namespace extensions {
|
|
|
| -static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothPrivateAPI> >
|
| +static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothPrivateAPI>>
|
| g_factory = LAZY_INSTANCE_INITIALIZER;
|
|
|
| +namespace {
|
| +
|
| +std::string GetListenerId(const EventListenerInfo& details) {
|
| + return !details.extension_id.empty() ? details.extension_id
|
| + : details.listener_url.host();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| // static
|
| BrowserContextKeyedAPIFactory<BluetoothPrivateAPI>*
|
| BluetoothPrivateAPI::GetFactoryInstance() {
|
| @@ -47,8 +56,9 @@ void BluetoothPrivateAPI::OnListenerAdded(const EventListenerInfo& details) {
|
| if (!details.browser_context)
|
| return;
|
|
|
| - BluetoothAPI::Get(browser_context_)->event_router()->AddPairingDelegate(
|
| - details.extension_id);
|
| + BluetoothAPI::Get(browser_context_)
|
| + ->event_router()
|
| + ->AddPairingDelegate(GetListenerId(details));
|
| }
|
|
|
| void BluetoothPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) {
|
| @@ -57,8 +67,9 @@ void BluetoothPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) {
|
| if (!details.browser_context)
|
| return;
|
|
|
| - BluetoothAPI::Get(browser_context_)->event_router()->RemovePairingDelegate(
|
| - details.extension_id);
|
| + BluetoothAPI::Get(browser_context_)
|
| + ->event_router()
|
| + ->RemovePairingDelegate(GetListenerId(details));
|
| }
|
|
|
| namespace api {
|
| @@ -68,27 +79,15 @@ namespace {
|
| const char kNameProperty[] = "name";
|
| const char kPoweredProperty[] = "powered";
|
| const char kDiscoverableProperty[] = "discoverable";
|
| -
|
| const char kSetAdapterPropertyError[] = "Error setting adapter properties: $1";
|
| -
|
| -const char kDeviceNotFoundError[] =
|
| - "Given address is not a valid Bluetooth device.";
|
| -
|
| -const char kDeviceNotConnectedError[] = "Device is not connected";
|
| -
|
| -const char kPairingNotEnabled[] =
|
| - "Pairing must be enabled to set a pairing response.";
|
| -
|
| +const char kDeviceNotFoundError[] = "Invalid Bluetooth device";
|
| +const char kDeviceNotConnectedError[] = "Device not connected";
|
| +const char kPairingNotEnabled[] = "Pairing not enabled";
|
| const char kInvalidPairingResponseOptions[] =
|
| "Invalid pairing response options";
|
| -
|
| -const char kAdapterNotPresent[] =
|
| - "Could not find a Bluetooth adapter.";
|
| -
|
| +const char kAdapterNotPresent[] = "Failed to find a Bluetooth adapter";
|
| const char kDisconnectError[] = "Failed to disconnect device";
|
| -
|
| const char kSetDiscoveryFilterFailed[] = "Failed to set discovery filter";
|
| -
|
| const char kPairingFailed[] = "Pairing failed";
|
|
|
| // Returns true if the pairing response options passed into the
|
| @@ -97,8 +96,8 @@ bool ValidatePairingResponseOptions(
|
| const device::BluetoothDevice* device,
|
| const bt_private::SetPairingResponseOptions& options) {
|
| bool response = options.response != bt_private::PAIRING_RESPONSE_NONE;
|
| - bool pincode = options.pincode.get() != NULL;
|
| - bool passkey = options.passkey.get() != NULL;
|
| + bool pincode = options.pincode.get() != nullptr;
|
| + bool passkey = options.passkey.get() != nullptr;
|
|
|
| if (!response && !pincode && !passkey)
|
| return false;
|
| @@ -125,6 +124,8 @@ bool ValidatePairingResponseOptions(
|
|
|
| } // namespace
|
|
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +
|
| BluetoothPrivateSetAdapterStateFunction::
|
| BluetoothPrivateSetAdapterStateFunction() {}
|
|
|
| @@ -152,23 +153,20 @@ bool BluetoothPrivateSetAdapterStateFunction::DoWork(
|
|
|
| if (name && adapter->GetName() != *name) {
|
| pending_properties_.insert(kNameProperty);
|
| - adapter->SetName(*name,
|
| - CreatePropertySetCallback(kNameProperty),
|
| + adapter->SetName(*name, CreatePropertySetCallback(kNameProperty),
|
| CreatePropertyErrorCallback(kNameProperty));
|
| }
|
|
|
| if (powered && adapter->IsPowered() != *powered) {
|
| pending_properties_.insert(kPoweredProperty);
|
| - adapter->SetPowered(*powered,
|
| - CreatePropertySetCallback(kPoweredProperty),
|
| + adapter->SetPowered(*powered, CreatePropertySetCallback(kPoweredProperty),
|
| CreatePropertyErrorCallback(kPoweredProperty));
|
| }
|
|
|
| if (discoverable && adapter->IsDiscoverable() != *discoverable) {
|
| pending_properties_.insert(kDiscoverableProperty);
|
| adapter->SetDiscoverable(
|
| - *discoverable,
|
| - CreatePropertySetCallback(kDiscoverableProperty),
|
| + *discoverable, CreatePropertySetCallback(kDiscoverableProperty),
|
| CreatePropertyErrorCallback(kDiscoverableProperty));
|
| }
|
|
|
| @@ -181,8 +179,7 @@ base::Closure
|
| BluetoothPrivateSetAdapterStateFunction::CreatePropertySetCallback(
|
| const std::string& property_name) {
|
| return base::Bind(
|
| - &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet,
|
| - this,
|
| + &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet, this,
|
| property_name);
|
| }
|
|
|
| @@ -190,8 +187,7 @@ base::Closure
|
| BluetoothPrivateSetAdapterStateFunction::CreatePropertyErrorCallback(
|
| const std::string& property_name) {
|
| return base::Bind(
|
| - &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertyError,
|
| - this,
|
| + &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertyError, this,
|
| property_name);
|
| }
|
|
|
| @@ -225,18 +221,19 @@ void BluetoothPrivateSetAdapterStateFunction::SendError() {
|
| DCHECK(!failed_properties_.empty());
|
|
|
| std::vector<std::string> failed_vector;
|
| - std::copy(failed_properties_.begin(),
|
| - failed_properties_.end(),
|
| + std::copy(failed_properties_.begin(), failed_properties_.end(),
|
| std::back_inserter(failed_vector));
|
|
|
| std::vector<std::string> replacements(1);
|
| replacements[0] = base::JoinString(failed_vector, ", ");
|
| std::string error = base::ReplaceStringPlaceholders(kSetAdapterPropertyError,
|
| - replacements, NULL);
|
| + replacements, nullptr);
|
| SetError(error);
|
| SendResponse(false);
|
| }
|
|
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +
|
| BluetoothPrivateSetPairingResponseFunction::
|
| BluetoothPrivateSetPairingResponseFunction() {}
|
|
|
| @@ -296,12 +293,13 @@ bool BluetoothPrivateSetPairingResponseFunction::DoWork(
|
| return true;
|
| }
|
|
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +
|
| BluetoothPrivateDisconnectAllFunction::BluetoothPrivateDisconnectAllFunction() {
|
| }
|
|
|
| BluetoothPrivateDisconnectAllFunction::
|
| - ~BluetoothPrivateDisconnectAllFunction() {
|
| -}
|
| + ~BluetoothPrivateDisconnectAllFunction() {}
|
|
|
| bool BluetoothPrivateDisconnectAllFunction::DoWork(
|
| scoped_refptr<device::BluetoothAdapter> adapter) {
|
| @@ -350,14 +348,7 @@ void BluetoothPrivateDisconnectAllFunction::OnErrorCallback(
|
| SendResponse(false);
|
| }
|
|
|
| -void BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback() {
|
| - SendResponse(true);
|
| -}
|
| -
|
| -void BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback() {
|
| - SetError(kSetDiscoveryFilterFailed);
|
| - SendResponse(false);
|
| -}
|
| +////////////////////////////////////////////////////////////////////////////////
|
|
|
| bool BluetoothPrivateSetDiscoveryFilterFunction::DoWork(
|
| scoped_refptr<device::BluetoothAdapter> adapter) {
|
| @@ -420,24 +411,104 @@ bool BluetoothPrivateSetDiscoveryFilterFunction::DoWork(
|
| return true;
|
| }
|
|
|
| -BluetoothPrivatePairFunction::BluetoothPrivatePairFunction() {}
|
| +void BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback() {
|
| + SendResponse(true);
|
| +}
|
|
|
| -BluetoothPrivatePairFunction::~BluetoothPrivatePairFunction() {}
|
| +void BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback() {
|
| + SetError(kSetDiscoveryFilterFailed);
|
| + SendResponse(false);
|
| +}
|
|
|
| -void BluetoothPrivatePairFunction::OnSuccessCallback() {
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +
|
| +BluetoothPrivateConnectFunction::BluetoothPrivateConnectFunction() {}
|
| +
|
| +BluetoothPrivateConnectFunction::~BluetoothPrivateConnectFunction() {}
|
| +
|
| +bool BluetoothPrivateConnectFunction::DoWork(
|
| + scoped_refptr<device::BluetoothAdapter> adapter) {
|
| + scoped_ptr<bt_private::Connect::Params> params(
|
| + bt_private::Connect::Params::Create(*args_));
|
| + EXTENSION_FUNCTION_VALIDATE(params.get());
|
| +
|
| + device::BluetoothDevice* device = adapter->GetDevice(params->device_address);
|
| + if (!device) {
|
| + SetError(kDeviceNotFoundError);
|
| + SendResponse(false);
|
| + return true;
|
| + }
|
| +
|
| + if (device->IsConnected()) {
|
| + results_ = bt_private::Connect::Results::Create(
|
| + bt_private::CONNECT_RESULT_TYPE_ALREADYCONNECTED);
|
| + SendResponse(true);
|
| + return true;
|
| + }
|
| +
|
| + // pairing_delegate may be null for connect.
|
| + device::BluetoothDevice::PairingDelegate* pairing_delegate =
|
| + BluetoothAPI::Get(browser_context())
|
| + ->event_router()
|
| + ->GetPairingDelegate(GetExtensionId());
|
| + device->Connect(
|
| + pairing_delegate,
|
| + base::Bind(&BluetoothPrivateConnectFunction::OnSuccessCallback, this),
|
| + base::Bind(&BluetoothPrivateConnectFunction::OnErrorCallback, this));
|
| + return true;
|
| +}
|
| +
|
| +void BluetoothPrivateConnectFunction::OnSuccessCallback() {
|
| + results_ = bt_private::Connect::Results::Create(
|
| + bt_private::CONNECT_RESULT_TYPE_SUCCESS);
|
| SendResponse(true);
|
| }
|
|
|
| -void BluetoothPrivatePairFunction::OnErrorCallback(
|
| +void BluetoothPrivateConnectFunction::OnErrorCallback(
|
| device::BluetoothDevice::ConnectErrorCode error) {
|
| - SetError(kPairingFailed);
|
| - SendResponse(false);
|
| + bt_private::ConnectResultType result = bt_private::CONNECT_RESULT_TYPE_NONE;
|
| + switch (error) {
|
| + case device::BluetoothDevice::ERROR_UNKNOWN:
|
| + result = bt_private::CONNECT_RESULT_TYPE_UNKNOWNERROR;
|
| + break;
|
| + case device::BluetoothDevice::ERROR_INPROGRESS:
|
| + result = bt_private::CONNECT_RESULT_TYPE_INPROGRESS;
|
| + break;
|
| + case device::BluetoothDevice::ERROR_FAILED:
|
| + result = bt_private::CONNECT_RESULT_TYPE_FAILED;
|
| + break;
|
| + case device::BluetoothDevice::ERROR_AUTH_FAILED:
|
| + result = bt_private::CONNECT_RESULT_TYPE_AUTHFAILED;
|
| + break;
|
| + case device::BluetoothDevice::ERROR_AUTH_CANCELED:
|
| + result = bt_private::CONNECT_RESULT_TYPE_AUTHCANCELED;
|
| + break;
|
| + case device::BluetoothDevice::ERROR_AUTH_REJECTED:
|
| + result = bt_private::CONNECT_RESULT_TYPE_AUTHREJECTED;
|
| + break;
|
| + case device::BluetoothDevice::ERROR_AUTH_TIMEOUT:
|
| + result = bt_private::CONNECT_RESULT_TYPE_AUTHTIMEOUT;
|
| + break;
|
| + case device::BluetoothDevice::ERROR_UNSUPPORTED_DEVICE:
|
| + result = bt_private::CONNECT_RESULT_TYPE_UNSUPPORTEDDEVICE;
|
| + break;
|
| + }
|
| + // Set the result type and respond with true (success).
|
| + results_ = bt_private::Connect::Results::Create(result);
|
| + SendResponse(true);
|
| }
|
|
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +
|
| +BluetoothPrivatePairFunction::BluetoothPrivatePairFunction() {}
|
| +
|
| +BluetoothPrivatePairFunction::~BluetoothPrivatePairFunction() {}
|
| +
|
| bool BluetoothPrivatePairFunction::DoWork(
|
| scoped_refptr<device::BluetoothAdapter> adapter) {
|
| scoped_ptr<bt_private::Pair::Params> params(
|
| bt_private::Pair::Params::Create(*args_));
|
| + EXTENSION_FUNCTION_VALIDATE(params.get());
|
|
|
| device::BluetoothDevice* device = adapter->GetDevice(params->device_address);
|
| if (!device) {
|
| @@ -446,21 +517,38 @@ bool BluetoothPrivatePairFunction::DoWork(
|
| return true;
|
| }
|
|
|
| - BluetoothEventRouter* router =
|
| - BluetoothAPI::Get(browser_context())->event_router();
|
| - if (!router->GetPairingDelegate(GetExtensionId())) {
|
| + device::BluetoothDevice::PairingDelegate* pairing_delegate =
|
| + BluetoothAPI::Get(browser_context())
|
| + ->event_router()
|
| + ->GetPairingDelegate(GetExtensionId());
|
| +
|
| + // pairing_delegate must be set (by adding an onPairing listener) before
|
| + // any calls to pair().
|
| + if (!pairing_delegate) {
|
| SetError(kPairingNotEnabled);
|
| SendResponse(false);
|
| return true;
|
| }
|
|
|
| device->Pair(
|
| - router->GetPairingDelegate(GetExtensionId()),
|
| + pairing_delegate,
|
| base::Bind(&BluetoothPrivatePairFunction::OnSuccessCallback, this),
|
| base::Bind(&BluetoothPrivatePairFunction::OnErrorCallback, this));
|
| return true;
|
| }
|
|
|
| +void BluetoothPrivatePairFunction::OnSuccessCallback() {
|
| + SendResponse(true);
|
| +}
|
| +
|
| +void BluetoothPrivatePairFunction::OnErrorCallback(
|
| + device::BluetoothDevice::ConnectErrorCode error) {
|
| + SetError(kPairingFailed);
|
| + SendResponse(false);
|
| +}
|
| +
|
| +////////////////////////////////////////////////////////////////////////////////
|
| +
|
| } // namespace api
|
|
|
| } // namespace extensions
|
|
|