| Index: device/bluetooth/bluetooth_profile_win.cc
|
| diff --git a/device/bluetooth/bluetooth_profile_win.cc b/device/bluetooth/bluetooth_profile_win.cc
|
| index f0729bb4fb7877c3cbefe4d7d99f341fdaadc033..72fac14bc494be21f75899ea8773c3c0a4300e7e 100644
|
| --- a/device/bluetooth/bluetooth_profile_win.cc
|
| +++ b/device/bluetooth/bluetooth_profile_win.cc
|
| @@ -4,11 +4,105 @@
|
|
|
| #include "device/bluetooth/bluetooth_profile_win.h"
|
|
|
| +#include "base/bind.h"
|
| #include "base/memory/ref_counted.h"
|
| +#include "base/sequenced_task_runner.h"
|
| +#include "device/bluetooth/bluetooth_adapter_factory.h"
|
| #include "device/bluetooth/bluetooth_device_win.h"
|
| #include "device/bluetooth/bluetooth_service_record.h"
|
| #include "device/bluetooth/bluetooth_socket_win.h"
|
|
|
| +namespace {
|
| +
|
| +using device::BluetoothAdapter;
|
| +using device::BluetoothDevice;
|
| +using device::BluetoothProfileWin;
|
| +using device::BluetoothSocket;
|
| +using device::BluetoothSocketWin;
|
| +
|
| +const char kNoConnectionCallback[] = "Connection callback not set";
|
| +const char kProfileNotFound[] = "Profile not found";
|
| +
|
| +void OnSocketConnectErrorUI(
|
| + const BluetoothProfileWin::ErrorCallback& error_callback,
|
| + const std::string& error) {
|
| + error_callback.Run(error);
|
| +}
|
| +
|
| +void OnSocketConnectSuccess(
|
| + scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
|
| + scoped_refptr<base::SequencedTaskRunner> file_task_runner,
|
| + const base::Closure& success_callback) {
|
| + DCHECK(file_task_runner->RunsTasksOnCurrentThread());
|
| + ui_task_runner->PostTask(FROM_HERE, success_callback);
|
| +}
|
| +
|
| +void OnSocketConnectError(
|
| + scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
|
| + scoped_refptr<base::SequencedTaskRunner> file_task_runner,
|
| + const BluetoothProfileWin::ErrorCallback& error_callback,
|
| + const std::string& error) {
|
| + DCHECK(file_task_runner->RunsTasksOnCurrentThread());
|
| + ui_task_runner->PostTask(
|
| + FROM_HERE, base::Bind(OnSocketConnectErrorUI, error_callback, error));
|
| +}
|
| +
|
| +void OnSocketConnect(scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
|
| + scoped_refptr<base::SequencedTaskRunner> file_task_runner,
|
| + scoped_refptr<BluetoothSocket> socket,
|
| + const base::Closure& success_callback,
|
| + const BluetoothProfileWin::ErrorCallback& error_callback) {
|
| + DCHECK(file_task_runner->RunsTasksOnCurrentThread());
|
| + socket->Connect(base::Bind(OnSocketConnectSuccess,
|
| + ui_task_runner,
|
| + file_task_runner,
|
| + success_callback),
|
| + base::Bind(OnSocketConnectError,
|
| + ui_task_runner,
|
| + file_task_runner,
|
| + error_callback));
|
| +}
|
| +
|
| +void OnConnectSuccessUIWithAdapter(
|
| + scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
|
| + const base::Closure& callback,
|
| + const BluetoothProfileWin::ConnectionCallback& connection_callback,
|
| + const std::string& device_address,
|
| + scoped_refptr<BluetoothSocket> socket,
|
| + scoped_refptr<BluetoothAdapter> adapter) {
|
| + DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
|
| + const BluetoothDevice* device = adapter->GetDevice(device_address);
|
| + if (device) {
|
| + connection_callback.Run(device, socket);
|
| + callback.Run();
|
| + }
|
| +}
|
| +
|
| +void OnConnectSuccessUI(
|
| + scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
|
| + const base::Closure& callback,
|
| + const BluetoothProfileWin::ConnectionCallback& connection_callback,
|
| + const std::string& device_address,
|
| + scoped_refptr<BluetoothSocket> socket) {
|
| + DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
|
| + device::BluetoothAdapterFactory::GetAdapter(
|
| + base::Bind(&OnConnectSuccessUIWithAdapter,
|
| + ui_task_runner,
|
| + callback,
|
| + connection_callback,
|
| + device_address,
|
| + socket));
|
| +}
|
| +
|
| +void OnConnectErrorUI(scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
|
| + const BluetoothProfileWin::ErrorCallback& error_callback,
|
| + const std::string& error) {
|
| + DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
|
| + error_callback.Run(error);
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| namespace device {
|
|
|
| BluetoothProfileWin::BluetoothProfileWin(const std::string& uuid,
|
| @@ -28,20 +122,43 @@ void BluetoothProfileWin::SetConnectionCallback(
|
| connection_callback_ = callback;
|
| }
|
|
|
| -bool BluetoothProfileWin::Connect(const BluetoothDeviceWin* device) {
|
| - if (connection_callback_.is_null())
|
| - return false;
|
| +void BluetoothProfileWin::Connect(
|
| + const BluetoothDeviceWin* device,
|
| + scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
|
| + scoped_refptr<base::SequencedTaskRunner> file_task_runner,
|
| + net::NetLog* net_log,
|
| + const net::NetLog::Source& source,
|
| + const base::Closure& success_callback,
|
| + const ErrorCallback& error_callback) {
|
| + DCHECK(ui_task_runner->RunsTasksOnCurrentThread());
|
| + if (connection_callback_.is_null()) {
|
| + error_callback.Run(kNoConnectionCallback);
|
| + return;
|
| + }
|
|
|
| const BluetoothServiceRecord* record = device->GetServiceRecord(uuid_);
|
| - if (record) {
|
| - scoped_refptr<BluetoothSocket> socket(
|
| - BluetoothSocketWin::CreateBluetoothSocket(*record));
|
| - if (socket.get() != NULL) {
|
| - connection_callback_.Run(device, socket);
|
| - return true;
|
| - }
|
| + if (!record) {
|
| + error_callback.Run(kProfileNotFound);
|
| + return;
|
| }
|
| - return false;
|
| +
|
| + scoped_refptr<BluetoothSocket> socket(
|
| + BluetoothSocketWin::CreateBluetoothSocket(
|
| + *record, file_task_runner, net_log, source));
|
| +
|
| + file_task_runner->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&OnSocketConnect,
|
| + ui_task_runner,
|
| + file_task_runner,
|
| + socket,
|
| + base::Bind(OnConnectSuccessUI,
|
| + ui_task_runner,
|
| + success_callback,
|
| + connection_callback_,
|
| + device->GetAddress(),
|
| + socket),
|
| + base::Bind(OnConnectErrorUI, ui_task_runner, error_callback)));
|
| }
|
|
|
| } // namespace device
|
|
|