 Chromium Code Reviews
 Chromium Code Reviews Issue 12018024:
  Implemented Asynchronous Initialization of BluetoothAdapter.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 12018024:
  Implemented Asynchronous Initialization of BluetoothAdapter.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: device/bluetooth/bluetooth_adapter_factory.cc | 
| diff --git a/device/bluetooth/bluetooth_adapter_factory.cc b/device/bluetooth/bluetooth_adapter_factory.cc | 
| index e26f64254f3379e62fea6734ef47ec3e1cd950f1..c2263a67dd7f567332220a2f06b4f905e8201fea 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,20 @@ namespace { | 
| base::LazyInstance<base::WeakPtr<device::BluetoothAdapter> >::Leaky | 
| default_adapter = LAZY_INSTANCE_INITIALIZER; | 
| +std::vector<BluetoothAdapterFactory::AdapterCallback> adapter_callbacks; | 
| 
Ami GONE FROM CHROMIUM
2013/01/23 17:40:58
Post-commit note:
This violates the style guide w
 | 
| + | 
| +void RunAdapterCallbacks() { | 
| + CHECK(default_adapter.Get().get()); | 
| + scoped_refptr<BluetoothAdapter> adapter(default_adapter.Get()); | 
| + for (std::vector<BluetoothAdapterFactory::AdapterCallback>::const_iterator | 
| + iter = adapter_callbacks.begin(); | 
| + iter != adapter_callbacks.end(); | 
| + ++iter) { | 
| + iter->Run(adapter); | 
| + } | 
| + adapter_callbacks.clear(); | 
| +} | 
| + | 
| } // namespace | 
| namespace device { | 
| @@ -39,8 +59,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 +67,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 +77,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.push_back(callback); | 
| } | 
| } | 
| // static | 
| -scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::GetAdapter() { | 
| +scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::MaybeGetAdapter() { | 
| return scoped_refptr<BluetoothAdapter>(default_adapter.Get()); | 
| } | 
| @@ -75,7 +95,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; | 
| } |