summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_adapter_factory.cc
diff options
context:
space:
mode:
Diffstat (limited to 'device/bluetooth/bluetooth_adapter_factory.cc')
-rw-r--r--device/bluetooth/bluetooth_adapter_factory.cc39
1 files changed, 33 insertions, 6 deletions
diff --git a/device/bluetooth/bluetooth_adapter_factory.cc b/device/bluetooth/bluetooth_adapter_factory.cc
index e26f642..0dea927 100644
--- a/device/bluetooth/bluetooth_adapter_factory.cc
+++ b/device/bluetooth/bluetooth_adapter_factory.cc
@@ -4,6 +4,9 @@
#include "device/bluetooth/bluetooth_adapter_factory.h"
+#include <vector>
+
+#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
@@ -17,6 +20,9 @@
namespace {
+using device::BluetoothAdapter;
+using device::BluetoothAdapterFactory;
+
// Shared default adapter instance, we don't want to keep this class around
// if nobody is using it so use a WeakPtr and create the object when needed;
// since Google C++ Style (and clang's static analyzer) forbids us having
@@ -24,6 +30,27 @@ namespace {
base::LazyInstance<base::WeakPtr<device::BluetoothAdapter> >::Leaky
default_adapter = LAZY_INSTANCE_INITIALIZER;
+typedef std::vector<BluetoothAdapterFactory::AdapterCallback>
+ AdapterCallbackList;
+
+// List of adapter callbacks to be called once the adapter is initialized.
+// Since Google C++ Style (and clang's static analyzer) forbids us having
+// exit-time destructors we use a lazy instance for it.
+base::LazyInstance<AdapterCallbackList> adapter_callbacks =
+ LAZY_INSTANCE_INITIALIZER;
+
+void RunAdapterCallbacks() {
+ CHECK(default_adapter.Get().get());
+ scoped_refptr<BluetoothAdapter> adapter(default_adapter.Get());
+ for (std::vector<BluetoothAdapterFactory::AdapterCallback>::const_iterator
+ iter = adapter_callbacks.Get().begin();
+ iter != adapter_callbacks.Get().end();
+ ++iter) {
+ iter->Run(adapter);
+ }
+ adapter_callbacks.Get().clear();
+}
+
} // namespace
namespace device {
@@ -39,8 +66,7 @@ bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() {
}
// static
-void BluetoothAdapterFactory::RunCallbackOnAdapterReady(
- const BluetoothAdapter::AdapterCallback& callback) {
+void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) {
if (!default_adapter.Get().get()) {
#if defined(OS_CHROMEOS)
chromeos::BluetoothAdapterChromeOs* new_adapter =
@@ -48,7 +74,8 @@ void BluetoothAdapterFactory::RunCallbackOnAdapterReady(
new_adapter->TrackDefaultAdapter();
default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr();
#elif defined(OS_WIN)
- BluetoothAdapterWin* new_adapter = new BluetoothAdapterWin();
+ BluetoothAdapterWin* new_adapter = new BluetoothAdapterWin(
+ base::Bind(&RunAdapterCallbacks));
new_adapter->TrackDefaultAdapter();
default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr();
#endif
@@ -57,12 +84,12 @@ void BluetoothAdapterFactory::RunCallbackOnAdapterReady(
if (default_adapter.Get()->IsInitialized()) {
callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get()));
} else {
- default_adapter.Get()->QueueAdapterCallback(callback);
+ adapter_callbacks.Get().push_back(callback);
}
}
// static
-scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::GetAdapter() {
+scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::MaybeGetAdapter() {
return scoped_refptr<BluetoothAdapter>(default_adapter.Get());
}
@@ -75,7 +102,7 @@ BluetoothAdapter* BluetoothAdapterFactory::Create(const std::string& address) {
adapter_chromeos->FindAdapter(address);
adapter = adapter_chromeos;
#elif defined(OS_WIN)
- adapter = new BluetoothAdapterWin();
+ adapter = new BluetoothAdapterWin(base::Bind(&RunAdapterCallbacks));
#endif
return adapter;
}