Commit 7ac7ed50 authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

Remove global wrapper objects for Bluetooth Classic and LE

Having these API wrapper objects be globals is dangerous because each
BluetoothTaskManagerWin creates a new task runner on which to run
Bluetooth tasks. Since this is the only class that uses these objects
it can just own them instead.

To simplify things the weak HANDLE reference in BluetoothTaskManagerWin
has also been removed.

Bug: 820864
Change-Id: I2abaca53c1b7388503e0f18ac6a004426f0e95ac
Reviewed-on: https://chromium-review.googlesource.com/1117445
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575015}
parent 8fc13ed7
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
#include "device/base/features.h" #include "device/base/features.h"
#include "device/bluetooth/bluetooth_adapter_winrt.h" #include "device/bluetooth/bluetooth_adapter_winrt.h"
#include "device/bluetooth/bluetooth_classic_win.h"
#include "device/bluetooth/bluetooth_device_win.h" #include "device/bluetooth/bluetooth_device_win.h"
#include "device/bluetooth/bluetooth_discovery_session_outcome.h" #include "device/bluetooth/bluetooth_discovery_session_outcome.h"
#include "device/bluetooth/bluetooth_socket_thread.h" #include "device/bluetooth/bluetooth_socket_thread.h"
...@@ -354,19 +355,21 @@ void BluetoothAdapterWin::Init() { ...@@ -354,19 +355,21 @@ void BluetoothAdapterWin::Init() {
ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
socket_thread_ = BluetoothSocketThread::Get(); socket_thread_ = BluetoothSocketThread::Get();
task_manager_ = task_manager_ =
new BluetoothTaskManagerWin(ui_task_runner_); base::MakeRefCounted<BluetoothTaskManagerWin>(ui_task_runner_);
task_manager_->AddObserver(this); task_manager_->AddObserver(this);
task_manager_->Initialize(); task_manager_->Initialize();
} }
void BluetoothAdapterWin::InitForTest( void BluetoothAdapterWin::InitForTest(
std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper,
std::unique_ptr<win::BluetoothLowEnergyWrapper> le_wrapper,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner) { scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner) {
ui_task_runner_ = ui_task_runner; ui_task_runner_ = ui_task_runner;
if (!ui_task_runner_) if (!ui_task_runner_)
ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
task_manager_ = task_manager_ = BluetoothTaskManagerWin::CreateForTesting(
new BluetoothTaskManagerWin(ui_task_runner_); std::move(classic_wrapper), std::move(le_wrapper), ui_task_runner_);
task_manager_->AddObserver(this); task_manager_->AddObserver(this);
task_manager_->InitializeWithBluetoothTaskRunner(bluetooth_task_runner); task_manager_->InitializeWithBluetoothTaskRunner(bluetooth_task_runner);
} }
......
...@@ -129,6 +129,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWin ...@@ -129,6 +129,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWin
void Init(); void Init();
void InitForTest( void InitForTest(
std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper,
std::unique_ptr<win::BluetoothLowEnergyWrapper> le_wrapper,
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner); scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/test/test_simple_task_runner.h" #include "base/test/test_simple_task_runner.h"
#include "device/bluetooth/bluetooth_adapter.h" #include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_win.h" #include "device/bluetooth/bluetooth_adapter_win.h"
#include "device/bluetooth/bluetooth_classic_win.h"
#include "device/bluetooth/bluetooth_device.h" #include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_session_outcome.h" #include "device/bluetooth/bluetooth_discovery_session_outcome.h"
#include "device/bluetooth/bluetooth_task_manager_win.h" #include "device/bluetooth/bluetooth_task_manager_win.h"
...@@ -57,7 +58,8 @@ class BluetoothAdapterWinTest : public testing::Test { ...@@ -57,7 +58,8 @@ class BluetoothAdapterWinTest : public testing::Test {
adapter_win_(static_cast<BluetoothAdapterWin*>(adapter_.get())), adapter_win_(static_cast<BluetoothAdapterWin*>(adapter_.get())),
observer_(adapter_), observer_(adapter_),
init_callback_called_(false) { init_callback_called_(false) {
adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); adapter_win_->InitForTest(nullptr, nullptr, ui_task_runner_,
bluetooth_task_runner_);
} }
void SetUp() override { void SetUp() override {
......
...@@ -4,74 +4,37 @@ ...@@ -4,74 +4,37 @@
#include "device/bluetooth/bluetooth_classic_win.h" #include "device/bluetooth/bluetooth_classic_win.h"
namespace {
static device::win::BluetoothClassicWrapper* g_instance_ = nullptr;
} // namespace
namespace device { namespace device {
namespace win { namespace win {
BluetoothClassicWrapper* BluetoothClassicWrapper::GetInstance() {
if (g_instance_ == nullptr) {
g_instance_ = new BluetoothClassicWrapper();
}
return g_instance_;
}
void BluetoothClassicWrapper::DeleteInstance() {
delete g_instance_;
g_instance_ = nullptr;
}
void BluetoothClassicWrapper::SetInstanceForTest(
BluetoothClassicWrapper* instance) {
delete g_instance_;
g_instance_ = instance;
}
BluetoothClassicWrapper::BluetoothClassicWrapper() {} BluetoothClassicWrapper::BluetoothClassicWrapper() {}
BluetoothClassicWrapper::~BluetoothClassicWrapper() {} BluetoothClassicWrapper::~BluetoothClassicWrapper() {}
HBLUETOOTH_RADIO_FIND BluetoothClassicWrapper::FindFirstRadio( HBLUETOOTH_RADIO_FIND BluetoothClassicWrapper::FindFirstRadio(
const BLUETOOTH_FIND_RADIO_PARAMS* params, const BLUETOOTH_FIND_RADIO_PARAMS* params) {
HANDLE* out_handle) {
HANDLE radio_handle = INVALID_HANDLE_VALUE; HANDLE radio_handle = INVALID_HANDLE_VALUE;
HBLUETOOTH_RADIO_FIND radio_find_handle = HBLUETOOTH_RADIO_FIND radio_find_handle =
BluetoothFindFirstRadio(params, &radio_handle); BluetoothFindFirstRadio(params, &radio_handle);
if (radio_find_handle) { if (radio_find_handle) {
// TODO(crbug.com/820864): At some point, we crash when we attempt to close DCHECK_NE(radio_handle, INVALID_HANDLE_VALUE);
// the handle. This and the related checks in this file are designed to
// see if our handle becomes garbage (i.e., "valid", but not closeable) at
// some point.
DWORD info;
CHECK(!base::win::HandleTraits::IsHandleValid(radio_handle) ||
::GetHandleInformation(radio_handle, &info));
opened_radio_handle_.Set(radio_handle); opened_radio_handle_.Set(radio_handle);
*out_handle = opened_radio_handle_.Get();
} }
return radio_find_handle; return radio_find_handle;
} }
DWORD BluetoothClassicWrapper::GetRadioInfo( DWORD BluetoothClassicWrapper::GetRadioInfo(
HANDLE handle,
PBLUETOOTH_RADIO_INFO out_radio_info) { PBLUETOOTH_RADIO_INFO out_radio_info) {
DWORD ret = BluetoothGetRadioInfo(handle, out_radio_info); DCHECK(opened_radio_handle_.IsValid());
DWORD info; return BluetoothGetRadioInfo(opened_radio_handle_.Get(), out_radio_info);
// TODO(crbug.com/820864): Remove this check.
CHECK(::GetHandleInformation(handle, &info));
return ret;
} }
BOOL BluetoothClassicWrapper::FindRadioClose(HBLUETOOTH_RADIO_FIND handle) { BOOL BluetoothClassicWrapper::FindRadioClose(HBLUETOOTH_RADIO_FIND handle) {
return BluetoothFindRadioClose(handle); return BluetoothFindRadioClose(handle);
} }
BOOL BluetoothClassicWrapper::IsConnectable(HANDLE handle) { BOOL BluetoothClassicWrapper::IsConnectable() {
DWORD ret = BluetoothIsConnectable(handle); DCHECK(opened_radio_handle_.IsValid());
DWORD info; return BluetoothIsConnectable(opened_radio_handle_.Get());
// TODO(crbug.com/820864): Remove this check.
CHECK(::GetHandleInformation(handle, &info));
return ret;
} }
HBLUETOOTH_DEVICE_FIND BluetoothClassicWrapper::FindFirstDevice( HBLUETOOTH_DEVICE_FIND BluetoothClassicWrapper::FindFirstDevice(
...@@ -90,26 +53,24 @@ BOOL BluetoothClassicWrapper::FindDeviceClose(HBLUETOOTH_DEVICE_FIND handle) { ...@@ -90,26 +53,24 @@ BOOL BluetoothClassicWrapper::FindDeviceClose(HBLUETOOTH_DEVICE_FIND handle) {
return BluetoothFindDeviceClose(handle); return BluetoothFindDeviceClose(handle);
} }
BOOL BluetoothClassicWrapper::EnableDiscovery(HANDLE handle, BOOL is_enable) { BOOL BluetoothClassicWrapper::EnableDiscovery(BOOL is_enable) {
DWORD ret = BluetoothEnableDiscovery(handle, is_enable); DCHECK(opened_radio_handle_.IsValid());
DWORD info; return BluetoothEnableDiscovery(opened_radio_handle_.Get(), is_enable);
// TODO(crbug.com/820864): Remove this check.
CHECK(::GetHandleInformation(handle, &info));
return ret;
} }
BOOL BluetoothClassicWrapper::EnableIncomingConnections(HANDLE handle, BOOL BluetoothClassicWrapper::EnableIncomingConnections(BOOL is_enable) {
BOOL is_enable) { DCHECK(opened_radio_handle_.IsValid());
DWORD ret = BluetoothEnableIncomingConnections(handle, is_enable); return BluetoothEnableIncomingConnections(opened_radio_handle_.Get(),
DWORD info; is_enable);
// TODO(crbug.com/820864): Remove this check.
CHECK(::GetHandleInformation(handle, &info));
return ret;
} }
DWORD BluetoothClassicWrapper::LastError() { DWORD BluetoothClassicWrapper::LastError() {
return GetLastError(); return GetLastError();
} }
bool BluetoothClassicWrapper::HasHandle() {
return opened_radio_handle_.IsValid();
}
} // namespace win } // namespace win
} // namespace device } // namespace device
...@@ -17,30 +17,24 @@ namespace win { ...@@ -17,30 +17,24 @@ namespace win {
// tests. // tests.
class DEVICE_BLUETOOTH_EXPORT BluetoothClassicWrapper { class DEVICE_BLUETOOTH_EXPORT BluetoothClassicWrapper {
public: public:
static BluetoothClassicWrapper* GetInstance(); BluetoothClassicWrapper();
static void DeleteInstance(); virtual ~BluetoothClassicWrapper();
static void SetInstanceForTest(BluetoothClassicWrapper* instance);
virtual HBLUETOOTH_RADIO_FIND FindFirstRadio( virtual HBLUETOOTH_RADIO_FIND FindFirstRadio(
const BLUETOOTH_FIND_RADIO_PARAMS* params, const BLUETOOTH_FIND_RADIO_PARAMS* params);
HANDLE* out_handle); virtual DWORD GetRadioInfo(PBLUETOOTH_RADIO_INFO out_radio_info);
virtual DWORD GetRadioInfo(HANDLE handle,
PBLUETOOTH_RADIO_INFO out_radio_info);
virtual BOOL FindRadioClose(HBLUETOOTH_RADIO_FIND handle); virtual BOOL FindRadioClose(HBLUETOOTH_RADIO_FIND handle);
virtual BOOL IsConnectable(HANDLE handle); virtual BOOL IsConnectable();
virtual HBLUETOOTH_DEVICE_FIND FindFirstDevice( virtual HBLUETOOTH_DEVICE_FIND FindFirstDevice(
const BLUETOOTH_DEVICE_SEARCH_PARAMS* params, const BLUETOOTH_DEVICE_SEARCH_PARAMS* params,
BLUETOOTH_DEVICE_INFO* out_device_info); BLUETOOTH_DEVICE_INFO* out_device_info);
virtual BOOL FindNextDevice(HBLUETOOTH_DEVICE_FIND handle, virtual BOOL FindNextDevice(HBLUETOOTH_DEVICE_FIND handle,
BLUETOOTH_DEVICE_INFO* out_device_info); BLUETOOTH_DEVICE_INFO* out_device_info);
virtual BOOL FindDeviceClose(HBLUETOOTH_DEVICE_FIND handle); virtual BOOL FindDeviceClose(HBLUETOOTH_DEVICE_FIND handle);
virtual BOOL EnableDiscovery(HANDLE handle, BOOL is_enable); virtual BOOL EnableDiscovery(BOOL is_enable);
virtual BOOL EnableIncomingConnections(HANDLE handle, BOOL is_enable); virtual BOOL EnableIncomingConnections(BOOL is_enable);
virtual DWORD LastError(); virtual DWORD LastError();
virtual bool HasHandle();
protected:
BluetoothClassicWrapper();
virtual ~BluetoothClassicWrapper();
private: private:
base::win::ScopedHandle opened_radio_handle_; base::win::ScopedHandle opened_radio_handle_;
......
...@@ -14,19 +14,16 @@ BluetoothClassicWrapperFake::BluetoothClassicWrapperFake() ...@@ -14,19 +14,16 @@ BluetoothClassicWrapperFake::BluetoothClassicWrapperFake()
BluetoothClassicWrapperFake::~BluetoothClassicWrapperFake() {} BluetoothClassicWrapperFake::~BluetoothClassicWrapperFake() {}
HBLUETOOTH_RADIO_FIND BluetoothClassicWrapperFake::FindFirstRadio( HBLUETOOTH_RADIO_FIND BluetoothClassicWrapperFake::FindFirstRadio(
const BLUETOOTH_FIND_RADIO_PARAMS* params, const BLUETOOTH_FIND_RADIO_PARAMS* params) {
HANDLE* out_handle) {
if (simulated_radios_) { if (simulated_radios_) {
*out_handle = (PVOID)simulated_radios_.get();
last_error_ = ERROR_SUCCESS; last_error_ = ERROR_SUCCESS;
return *out_handle; return (PVOID)simulated_radios_.get();
} }
last_error_ = ERROR_NO_MORE_ITEMS; last_error_ = ERROR_NO_MORE_ITEMS;
return NULL; return NULL;
} }
DWORD BluetoothClassicWrapperFake::GetRadioInfo( DWORD BluetoothClassicWrapperFake::GetRadioInfo(
HANDLE handle,
PBLUETOOTH_RADIO_INFO out_radio_info) { PBLUETOOTH_RADIO_INFO out_radio_info) {
if (simulated_radios_) { if (simulated_radios_) {
*out_radio_info = simulated_radios_->radio_info; *out_radio_info = simulated_radios_->radio_info;
...@@ -38,10 +35,11 @@ DWORD BluetoothClassicWrapperFake::GetRadioInfo( ...@@ -38,10 +35,11 @@ DWORD BluetoothClassicWrapperFake::GetRadioInfo(
} }
BOOL BluetoothClassicWrapperFake::FindRadioClose(HBLUETOOTH_RADIO_FIND handle) { BOOL BluetoothClassicWrapperFake::FindRadioClose(HBLUETOOTH_RADIO_FIND handle) {
DCHECK_EQ(handle, (PVOID)simulated_radios_.get());
return TRUE; return TRUE;
} }
BOOL BluetoothClassicWrapperFake::IsConnectable(HANDLE handle) { BOOL BluetoothClassicWrapperFake::IsConnectable() {
if (simulated_radios_) { if (simulated_radios_) {
last_error_ = ERROR_SUCCESS; last_error_ = ERROR_SUCCESS;
return simulated_radios_->is_connectable; return simulated_radios_->is_connectable;
...@@ -69,13 +67,11 @@ BOOL BluetoothClassicWrapperFake::FindDeviceClose( ...@@ -69,13 +67,11 @@ BOOL BluetoothClassicWrapperFake::FindDeviceClose(
return TRUE; return TRUE;
} }
BOOL BluetoothClassicWrapperFake::EnableDiscovery(HANDLE handle, BOOL BluetoothClassicWrapperFake::EnableDiscovery(BOOL is_enable) {
BOOL is_enable) {
return TRUE; return TRUE;
} }
BOOL BluetoothClassicWrapperFake::EnableIncomingConnections(HANDLE handle, BOOL BluetoothClassicWrapperFake::EnableIncomingConnections(BOOL is_enable) {
BOOL is_enable) {
return TRUE; return TRUE;
} }
...@@ -83,6 +79,10 @@ DWORD BluetoothClassicWrapperFake::LastError() { ...@@ -83,6 +79,10 @@ DWORD BluetoothClassicWrapperFake::LastError() {
return last_error_; return last_error_;
} }
bool BluetoothClassicWrapperFake::HasHandle() {
return bool(simulated_radios_);
}
BluetoothRadio* BluetoothClassicWrapperFake::SimulateARadio( BluetoothRadio* BluetoothClassicWrapperFake::SimulateARadio(
base::string16 name, base::string16 name,
BLUETOOTH_ADDRESS address) { BLUETOOTH_ADDRESS address) {
......
...@@ -25,21 +25,20 @@ class BluetoothClassicWrapperFake : public BluetoothClassicWrapper { ...@@ -25,21 +25,20 @@ class BluetoothClassicWrapperFake : public BluetoothClassicWrapper {
~BluetoothClassicWrapperFake() override; ~BluetoothClassicWrapperFake() override;
HBLUETOOTH_RADIO_FIND FindFirstRadio( HBLUETOOTH_RADIO_FIND FindFirstRadio(
const BLUETOOTH_FIND_RADIO_PARAMS* params, const BLUETOOTH_FIND_RADIO_PARAMS* params) override;
HANDLE* out_handle) override; DWORD GetRadioInfo(PBLUETOOTH_RADIO_INFO out_radio_info) override;
DWORD GetRadioInfo(HANDLE handle,
PBLUETOOTH_RADIO_INFO out_radio_info) override;
BOOL FindRadioClose(HBLUETOOTH_RADIO_FIND handle) override; BOOL FindRadioClose(HBLUETOOTH_RADIO_FIND handle) override;
BOOL IsConnectable(HANDLE handle) override; BOOL IsConnectable() override;
HBLUETOOTH_DEVICE_FIND FindFirstDevice( HBLUETOOTH_DEVICE_FIND FindFirstDevice(
const BLUETOOTH_DEVICE_SEARCH_PARAMS* params, const BLUETOOTH_DEVICE_SEARCH_PARAMS* params,
BLUETOOTH_DEVICE_INFO* out_device_info) override; BLUETOOTH_DEVICE_INFO* out_device_info) override;
BOOL FindNextDevice(HBLUETOOTH_DEVICE_FIND handle, BOOL FindNextDevice(HBLUETOOTH_DEVICE_FIND handle,
BLUETOOTH_DEVICE_INFO* out_device_info) override; BLUETOOTH_DEVICE_INFO* out_device_info) override;
BOOL FindDeviceClose(HBLUETOOTH_DEVICE_FIND handle) override; BOOL FindDeviceClose(HBLUETOOTH_DEVICE_FIND handle) override;
BOOL EnableDiscovery(HANDLE handle, BOOL is_enable) override; BOOL EnableDiscovery(BOOL is_enable) override;
BOOL EnableIncomingConnections(HANDLE handle, BOOL is_enable) override; BOOL EnableIncomingConnections(BOOL is_enable) override;
DWORD LastError() override; DWORD LastError() override;
bool HasHandle() override;
BluetoothRadio* SimulateARadio(base::string16 name, BluetoothRadio* SimulateARadio(base::string16 name,
BLUETOOTH_ADDRESS address); BLUETOOTH_ADDRESS address);
......
...@@ -19,8 +19,6 @@ ...@@ -19,8 +19,6 @@
namespace { namespace {
static device::win::BluetoothLowEnergyWrapper* g_instance_ = nullptr;
using device::win::DeviceRegistryPropertyValue; using device::win::DeviceRegistryPropertyValue;
using device::win::DevicePropertyValue; using device::win::DevicePropertyValue;
using device::win::BluetoothLowEnergyDeviceInfo; using device::win::BluetoothLowEnergyDeviceInfo;
...@@ -651,24 +649,6 @@ bool ExtractBluetoothAddressFromDeviceInstanceIdForTesting( ...@@ -651,24 +649,6 @@ bool ExtractBluetoothAddressFromDeviceInstanceIdForTesting(
return ExtractBluetoothAddressFromDeviceInstanceId(instance_id, btha, error); return ExtractBluetoothAddressFromDeviceInstanceId(instance_id, btha, error);
} }
BluetoothLowEnergyWrapper* BluetoothLowEnergyWrapper::GetInstance() {
if (g_instance_ == nullptr) {
g_instance_ = new BluetoothLowEnergyWrapper();
}
return g_instance_;
}
void BluetoothLowEnergyWrapper::DeleteInstance() {
delete g_instance_;
g_instance_ = nullptr;
}
void BluetoothLowEnergyWrapper::SetInstanceForTest(
BluetoothLowEnergyWrapper* instance) {
delete g_instance_;
g_instance_ = instance;
}
BluetoothLowEnergyWrapper::BluetoothLowEnergyWrapper() {} BluetoothLowEnergyWrapper::BluetoothLowEnergyWrapper() {}
BluetoothLowEnergyWrapper::~BluetoothLowEnergyWrapper() {} BluetoothLowEnergyWrapper::~BluetoothLowEnergyWrapper() {}
......
...@@ -121,9 +121,8 @@ ExtractBluetoothAddressFromDeviceInstanceIdForTesting( ...@@ -121,9 +121,8 @@ ExtractBluetoothAddressFromDeviceInstanceIdForTesting(
// interface that can be replaced with fakes in tests. // interface that can be replaced with fakes in tests.
class DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyWrapper { class DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyWrapper {
public: public:
static BluetoothLowEnergyWrapper* GetInstance(); BluetoothLowEnergyWrapper();
static void DeleteInstance(); virtual ~BluetoothLowEnergyWrapper();
static void SetInstanceForTest(BluetoothLowEnergyWrapper* instance);
// Returns true only on Windows platforms supporting Bluetooth Low Energy. // Returns true only on Windows platforms supporting Bluetooth Low Energy.
virtual bool IsBluetoothLowEnergySupported(); virtual bool IsBluetoothLowEnergySupported();
...@@ -210,10 +209,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyWrapper { ...@@ -210,10 +209,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyWrapper {
virtual HRESULT WriteDescriptorValue(base::FilePath& service_path, virtual HRESULT WriteDescriptorValue(base::FilePath& service_path,
const PBTH_LE_GATT_DESCRIPTOR descriptor, const PBTH_LE_GATT_DESCRIPTOR descriptor,
PBTH_LE_GATT_DESCRIPTOR_VALUE new_value); PBTH_LE_GATT_DESCRIPTOR_VALUE new_value);
protected:
BluetoothLowEnergyWrapper();
virtual ~BluetoothLowEnergyWrapper();
}; };
} // namespace win } // namespace win
......
...@@ -29,6 +29,11 @@ class SequencedTaskRunner; ...@@ -29,6 +29,11 @@ class SequencedTaskRunner;
namespace device { namespace device {
namespace win {
class BluetoothClassicWrapper;
class BluetoothLowEnergyWrapper;
} // namespace win
// Manages the blocking Bluetooth tasks using |SequencedWorkerPool|. It runs // Manages the blocking Bluetooth tasks using |SequencedWorkerPool|. It runs
// bluetooth tasks using |SequencedWorkerPool| and informs its observers of // bluetooth tasks using |SequencedWorkerPool| and informs its observers of
// bluetooth adapter state changes and any other bluetooth device inquiry // bluetooth adapter state changes and any other bluetooth device inquiry
...@@ -111,6 +116,11 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothTaskManagerWin ...@@ -111,6 +116,11 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothTaskManagerWin
explicit BluetoothTaskManagerWin( explicit BluetoothTaskManagerWin(
scoped_refptr<base::SequencedTaskRunner> ui_task_runner); scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
static scoped_refptr<BluetoothTaskManagerWin> CreateForTesting(
std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper,
std::unique_ptr<win::BluetoothLowEnergyWrapper> le_wrapper,
scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
static BluetoothUUID BluetoothLowEnergyUuidToBluetoothUuid( static BluetoothUUID BluetoothLowEnergyUuidToBluetoothUuid(
const BTH_LE_UUID& bth_le_uuid); const BTH_LE_UUID& bth_le_uuid);
...@@ -199,6 +209,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothTaskManagerWin ...@@ -199,6 +209,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothTaskManagerWin
static const int kPollIntervalMs; static const int kPollIntervalMs;
BluetoothTaskManagerWin(
std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper,
std::unique_ptr<win::BluetoothLowEnergyWrapper> le_wrapper,
scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
virtual ~BluetoothTaskManagerWin(); virtual ~BluetoothTaskManagerWin();
// Logs Win32 errors occurring during polling on the worker thread. The method // Logs Win32 errors occurring during polling on the worker thread. The method
...@@ -321,16 +335,16 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothTaskManagerWin ...@@ -321,16 +335,16 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothTaskManagerWin
// List of observers interested in event notifications. // List of observers interested in event notifications.
base::ObserverList<Observer> observers_; base::ObserverList<Observer> observers_;
// Weak reference of the adapter handle, let BluetoothClassicWrapper handle
// the close of |adapter_handle_|.
HANDLE adapter_handle_;
// indicates whether the adapter is in discovery mode or not. // indicates whether the adapter is in discovery mode or not.
bool discovering_; bool discovering_ = false;
// Use for discarding too many log messages. // Use for discarding too many log messages.
base::TimeTicks current_logging_batch_ticks_; base::TimeTicks current_logging_batch_ticks_;
int current_logging_batch_count_; int current_logging_batch_count_ = 0;
// Wrapper around the Windows Bluetooth APIs. Owns the radio handle.
std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper_;
std::unique_ptr<win::BluetoothLowEnergyWrapper> le_wrapper_;
DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin); DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin);
}; };
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/test/test_pending_task.h" #include "base/test/test_pending_task.h"
#include "base/test/test_simple_task_runner.h" #include "base/test/test_simple_task_runner.h"
#include "device/bluetooth/bluetooth_classic_win.h"
#include "device/bluetooth/bluetooth_init_win.h" #include "device/bluetooth/bluetooth_init_win.h"
#include "device/bluetooth/bluetooth_task_manager_win.h" #include "device/bluetooth/bluetooth_task_manager_win.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
......
...@@ -190,7 +190,6 @@ BTH_LE_UUID CanonicalStringToBTH_LE_UUID(base::StringPiece uuid) { ...@@ -190,7 +190,6 @@ BTH_LE_UUID CanonicalStringToBTH_LE_UUID(base::StringPiece uuid) {
BluetoothTestWin::BluetoothTestWin() BluetoothTestWin::BluetoothTestWin()
: ui_task_runner_(new base::TestSimpleTaskRunner()), : ui_task_runner_(new base::TestSimpleTaskRunner()),
bluetooth_task_runner_(new base::TestSimpleTaskRunner()), bluetooth_task_runner_(new base::TestSimpleTaskRunner()),
fake_bt_classic_wrapper_(nullptr),
fake_bt_le_wrapper_(nullptr) {} fake_bt_le_wrapper_(nullptr) {}
BluetoothTestWin::~BluetoothTestWin() {} BluetoothTestWin::~BluetoothTestWin() {}
...@@ -210,23 +209,28 @@ void BluetoothTestWin::InitWithDefaultAdapter() { ...@@ -210,23 +209,28 @@ void BluetoothTestWin::InitWithDefaultAdapter() {
void BluetoothTestWin::InitWithoutDefaultAdapter() { void BluetoothTestWin::InitWithoutDefaultAdapter() {
auto adapter = auto adapter =
base::WrapRefCounted(new BluetoothAdapterWin(base::DoNothing())); base::WrapRefCounted(new BluetoothAdapterWin(base::DoNothing()));
adapter->InitForTest(ui_task_runner_, bluetooth_task_runner_); adapter->InitForTest(nullptr, nullptr, ui_task_runner_,
bluetooth_task_runner_);
adapter_ = std::move(adapter); adapter_ = std::move(adapter);
} }
void BluetoothTestWin::InitWithFakeAdapter() { void BluetoothTestWin::InitWithFakeAdapter() {
fake_bt_classic_wrapper_ = new win::BluetoothClassicWrapperFake(); auto fake_bt_classic_wrapper =
fake_bt_le_wrapper_ = new win::BluetoothLowEnergyWrapperFake(); std::make_unique<win::BluetoothClassicWrapperFake>();
fake_bt_le_wrapper_->AddObserver(this); fake_bt_classic_wrapper->SimulateARadio(
win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_);
win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_);
fake_bt_classic_wrapper_->SimulateARadio(
base::SysUTF8ToWide(kTestAdapterName), base::SysUTF8ToWide(kTestAdapterName),
CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress));
auto fake_bt_le_wrapper =
std::make_unique<win::BluetoothLowEnergyWrapperFake>();
fake_bt_le_wrapper_ = fake_bt_le_wrapper.get();
fake_bt_le_wrapper_->AddObserver(this);
auto adapter = auto adapter =
base::WrapRefCounted(new BluetoothAdapterWin(base::DoNothing())); base::WrapRefCounted(new BluetoothAdapterWin(base::DoNothing()));
adapter->InitForTest(nullptr, bluetooth_task_runner_); adapter->InitForTest(std::move(fake_bt_classic_wrapper),
std::move(fake_bt_le_wrapper), nullptr,
bluetooth_task_runner_);
adapter_ = std::move(adapter); adapter_ = std::move(adapter);
FinishPendingTasks(); FinishPendingTasks();
} }
......
...@@ -86,7 +86,6 @@ class BluetoothTestWin : public BluetoothTestBase, ...@@ -86,7 +86,6 @@ class BluetoothTestWin : public BluetoothTestBase,
scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_; scoped_refptr<base::TestSimpleTaskRunner> ui_task_runner_;
scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_; scoped_refptr<base::TestSimpleTaskRunner> bluetooth_task_runner_;
win::BluetoothClassicWrapperFake* fake_bt_classic_wrapper_;
win::BluetoothLowEnergyWrapperFake* fake_bt_le_wrapper_; win::BluetoothLowEnergyWrapperFake* fake_bt_le_wrapper_;
// This is used for retaining access to a single deleted device. // This is used for retaining access to a single deleted device.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment