Commit 477fded6 authored by Jan Wilken Doerrie's avatar Jan Wilken Doerrie Committed by Commit Bot

[Bluetooth] Enable BluetoothAdapterWinrt by default

This change enables BluetoothAdapterWinrt by default. In order to
continue to support chrome.bluetooth and chrome.bluetoothSockets on
Windows 10, BluetoothAdapterFactory::GetClassicAdapter() is introduced.
This method will always create a BluetoothAdapterWin on Windows, while
it is equivalent to BluetoothAdapterFactory::GetAdapter() on other
platforms.

Bug: 821766
Change-Id: Idfe8e990987de2b72e2ecaee743ad43ee6722ceb
Reviewed-on: https://chromium-review.googlesource.com/1179824
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584258}
parent a4b81e34
......@@ -12,7 +12,7 @@ namespace device {
const base::Feature kNewUsbBackend{"NewUsbBackend",
base::FEATURE_DISABLED_BY_DEFAULT};
const base::Feature kNewBLEWinImplementation{"NewBLEWinImplementation",
base::FEATURE_DISABLED_BY_DEFAULT};
base::FEATURE_ENABLED_BY_DEFAULT};
#endif // defined(OS_WIN)
#if defined(OS_CHROMEOS)
......
......@@ -19,6 +19,7 @@
#endif
#if defined(OS_WIN)
#include "base/win/windows_version.h"
#include "device/bluetooth/bluetooth_adapter_win.h"
#endif
#if defined(ANDROID)
#include "base/android/build_info.h"
......@@ -61,6 +62,26 @@ void RunAdapterCallbacks() {
}
#endif // defined(OS_WIN) || defined(OS_LINUX)
#if defined(OS_WIN)
// Shared classic adapter instance. See above why this is a lazy instance.
// Note: This is only applicable on Windows, as here the default adapter does
// not provide Bluetooth Classic support yet.
base::LazyInstance<base::WeakPtr<BluetoothAdapter>>::Leaky classic_adapter =
LAZY_INSTANCE_INITIALIZER;
base::LazyInstance<AdapterCallbackList>::DestructorAtExit
classic_adapter_callbacks = LAZY_INSTANCE_INITIALIZER;
void RunClassicAdapterCallbacks() {
DCHECK(classic_adapter.Get());
scoped_refptr<BluetoothAdapter> adapter(classic_adapter.Get().get());
for (auto& callback : classic_adapter_callbacks.Get())
callback.Run(adapter);
classic_adapter_callbacks.Get().clear();
}
#endif // defined(OS_WIN)
} // namespace
BluetoothAdapterFactory::~BluetoothAdapterFactory() = default;
......@@ -132,6 +153,31 @@ void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) {
callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get().get()));
}
// static
void BluetoothAdapterFactory::GetClassicAdapter(
const AdapterCallback& callback) {
#if defined(OS_WIN)
if (base::win::GetVersion() < base::win::VERSION_WIN10) {
// Prior to Win10, the default adapter will support Bluetooth classic.
GetAdapter(callback);
return;
}
if (!classic_adapter.Get()) {
classic_adapter.Get() = BluetoothAdapterWin::CreateClassicAdapter(
base::Bind(&RunClassicAdapterCallbacks));
DCHECK(!classic_adapter.Get()->IsInitialized());
}
if (!classic_adapter.Get()->IsInitialized())
classic_adapter_callbacks.Get().push_back(callback);
else
callback.Run(scoped_refptr<BluetoothAdapter>(classic_adapter.Get().get()));
#else
GetAdapter(callback);
#endif // defined(OS_WIN)
}
#if defined(OS_LINUX)
// static
void BluetoothAdapterFactory::Shutdown() {
......@@ -144,6 +190,9 @@ void BluetoothAdapterFactory::Shutdown() {
void BluetoothAdapterFactory::SetAdapterForTesting(
scoped_refptr<BluetoothAdapter> adapter) {
default_adapter.Get() = adapter->GetWeakPtrForTesting();
#if defined(OS_WIN)
classic_adapter.Get() = adapter->GetWeakPtrForTesting();
#endif
}
// static
......
......@@ -52,6 +52,14 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterFactory {
// use.
static void GetAdapter(const AdapterCallback& callback);
// Returns the shared instance of the classic adapter, creating and
// initializing it if necessary. |callback| is called with the adapter
// instance passed only once the adapter is fully initialized and ready to
// use.
// For all platforms except Windows this is equivalent to calling
// GetAdapter(), as the default adapter already supports Bluetooth classic.
static void GetClassicAdapter(const AdapterCallback& callback);
#if defined(OS_LINUX)
// Calls |BluetoothAdapter::Shutdown| on the adapter if
// present.
......
......@@ -44,6 +44,12 @@ base::WeakPtr<BluetoothAdapter> BluetoothAdapterWin::CreateAdapter(
return adapter->weak_ptr_factory_.GetWeakPtr();
}
return BluetoothAdapterWin::CreateClassicAdapter(std::move(init_callback));
}
// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapterWin::CreateClassicAdapter(
InitCallback init_callback) {
auto* adapter = new BluetoothAdapterWin(std::move(init_callback));
adapter->Init();
return adapter->weak_ptr_factory_.GetWeakPtr();
......
......@@ -36,6 +36,9 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWin
static base::WeakPtr<BluetoothAdapter> CreateAdapter(
InitCallback init_callback);
static base::WeakPtr<BluetoothAdapter> CreateClassicAdapter(
InitCallback init_callback);
static bool UseNewBLEWinImplementation();
// BluetoothAdapter:
......
......@@ -90,7 +90,9 @@ void BluetoothEventRouter::GetAdapter(
return;
}
device::BluetoothAdapterFactory::GetAdapter(
// Note: On ChromeOS this will return an adapter that also supports Bluetooth
// Low Energy.
device::BluetoothAdapterFactory::GetClassicAdapter(
base::Bind(&BluetoothEventRouter::OnAdapterInitialized,
weak_ptr_factory_.GetWeakPtr(), callback));
}
......
......@@ -261,7 +261,7 @@ bool BluetoothSocketListenFunction::PreRunValidation(std::string* error) {
ExtensionFunction::ResponseAction BluetoothSocketListenFunction::Run() {
DCHECK_CURRENTLY_ON(work_thread_id());
device::BluetoothAdapterFactory::GetAdapter(
device::BluetoothAdapterFactory::GetClassicAdapter(
base::Bind(&BluetoothSocketListenFunction::OnGetAdapter, this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
......@@ -443,7 +443,7 @@ bool BluetoothSocketAbstractConnectFunction::PreRunValidation(
ExtensionFunction::ResponseAction
BluetoothSocketAbstractConnectFunction::Run() {
DCHECK_CURRENTLY_ON(work_thread_id());
device::BluetoothAdapterFactory::GetAdapter(
device::BluetoothAdapterFactory::GetClassicAdapter(
base::Bind(&BluetoothSocketAbstractConnectFunction::OnGetAdapter, this));
return did_respond() ? AlreadyResponded() : RespondLater();
}
......
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