diff options
author | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-23 16:58:01 +0000 |
---|---|---|
committer | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-23 16:58:01 +0000 |
commit | 061b425146105cd456261a9593ea44473c77e5ad (patch) | |
tree | 5a620ea3651038145bd6be3af04699c4a509d4ae /device | |
parent | 6d9234fae2265d89da77d47a83103adbbda1d17b (diff) | |
download | chromium_src-061b425146105cd456261a9593ea44473c77e5ad.zip chromium_src-061b425146105cd456261a9593ea44473c77e5ad.tar.gz chromium_src-061b425146105cd456261a9593ea44473c77e5ad.tar.bz2 |
We cannot run adapter callbacks inside BluetoothAdapterWin because passing scoped_refptr<BluetoothAdapter>(this) will destroy the adapter itself after the callback is finished.
Instead, bluetooth_adapter_factory.cc maintains a vector of callbacks and they will be run once the adapter is initialized.
I also put Bluetooth API initialization code into bluetooth_init_win.h and bluetooth_init_win.cc. From now on any code that wants to use Windows Bluetooth APIs should just include bluetooth_init_win.h.
BUG=135470
Review URL: https://chromiumcodereview.appspot.com/12018024
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@178311 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device')
-rw-r--r-- | device/bluetooth/bluetooth_adapter.cc | 4 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter.h | 8 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_chromeos_unittest.cc | 58 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_devices_chromeos_unittest.cc | 2 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_factory.cc | 32 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_factory.h | 17 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_win.cc | 10 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_win.h | 6 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_win_unittest.cc | 20 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_init_win.cc | 45 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_init_win.h (renamed from device/bluetooth/bluetooth_includes_win.h) | 17 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_service_record_win.cc | 2 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_task_manager_win.cc | 46 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_task_manager_win.h | 5 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_task_manager_win_unittest.cc | 3 | ||||
-rw-r--r-- | device/device.gyp | 3 |
16 files changed, 175 insertions, 103 deletions
diff --git a/device/bluetooth/bluetooth_adapter.cc b/device/bluetooth/bluetooth_adapter.cc index 78b556e..6f036b3 100644 --- a/device/bluetooth/bluetooth_adapter.cc +++ b/device/bluetooth/bluetooth_adapter.cc @@ -22,10 +22,6 @@ const std::string& BluetoothAdapter::name() const { return name_; } -void BluetoothAdapter::QueueAdapterCallback(const AdapterCallback& callback) { - adapter_callbacks_.push_back(callback); -} - BluetoothAdapter::DeviceList BluetoothAdapter::GetDevices() { ConstDeviceList const_devices = const_cast<const BluetoothAdapter *>(this)->GetDevices(); diff --git a/device/bluetooth/bluetooth_adapter.h b/device/bluetooth/bluetooth_adapter.h index 30d7a95..fd7c983 100644 --- a/device/bluetooth/bluetooth_adapter.h +++ b/device/bluetooth/bluetooth_adapter.h @@ -79,9 +79,6 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { typedef base::Callback<void(const BluetoothOutOfBandPairingData& data)> BluetoothOutOfBandPairingDataCallback; - typedef base::Callback<void(scoped_refptr<BluetoothAdapter> adapter)> - AdapterCallback; - // Adds and removes observers for events on this bluetooth adapter, // if monitoring multiple adapters check the |adapter| parameter of // observer methods to determine which adapter is issuing the event. @@ -96,9 +93,6 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { // The name of the adapter. virtual const std::string& name() const; - // Queue adapter callbacks to be run when the adapter is initialized. - virtual void QueueAdapterCallback(const AdapterCallback& callback); - // Indicates whether the adapter is initialized and ready to use. virtual bool IsInitialized() const = 0; @@ -160,8 +154,6 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { // Name of the adapter. std::string name_; - std::vector<AdapterCallback> adapter_callbacks_; - // Devices paired with, connected to, discovered by, or visible to the // adapter. The key is the Bluetooth address of the device and the value // is the BluetoothDevice object whose lifetime is managed by the diff --git a/device/bluetooth/bluetooth_adapter_chromeos_unittest.cc b/device/bluetooth/bluetooth_adapter_chromeos_unittest.cc index e0bfef1..04fe988 100644 --- a/device/bluetooth/bluetooth_adapter_chromeos_unittest.cc +++ b/device/bluetooth/bluetooth_adapter_chromeos_unittest.cc @@ -75,7 +75,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterNotPresent) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); ASSERT_TRUE(adapter_ != NULL); @@ -105,7 +105,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterWithAddress) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -144,7 +144,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterWithoutAddress) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -199,7 +199,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterBecomesPresentWithAddress) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -246,7 +246,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterReplacedWithAddress) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -304,7 +304,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -364,7 +364,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterReplacedWithoutAddress) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -431,7 +431,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterRemoved) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -475,7 +475,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterWithoutAddressRemoved) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -520,7 +520,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -552,7 +552,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -594,7 +594,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -652,7 +652,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterPoweredPropertyChanged) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -703,7 +703,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterPoweredPropertyUnchanged) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -753,7 +753,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -825,7 +825,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -890,7 +890,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -962,7 +962,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1009,7 +1009,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterSetPowered) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1058,7 +1058,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterSetPoweredError) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1108,7 +1108,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1140,7 +1140,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1182,7 +1182,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1240,7 +1240,7 @@ TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterDiscoveringPropertyChanged) { EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1292,7 +1292,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1343,7 +1343,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1415,7 +1415,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1481,7 +1481,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); @@ -1555,7 +1555,7 @@ TEST_F(BluetoothAdapterChromeOsTest, EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) .WillOnce(SaveArg<0>(&adapter_callback)); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterChromeOsTest::SetAdapter, base::Unretained(this))); diff --git a/device/bluetooth/bluetooth_adapter_devices_chromeos_unittest.cc b/device/bluetooth/bluetooth_adapter_devices_chromeos_unittest.cc index bf5a04b..2edfde4 100644 --- a/device/bluetooth/bluetooth_adapter_devices_chromeos_unittest.cc +++ b/device/bluetooth/bluetooth_adapter_devices_chromeos_unittest.cc @@ -52,7 +52,7 @@ class BluetoothAdapterDevicesChromeOsTest : public testing::Test { EXPECT_CALL(*mock_adapter_client_, AddObserver(_)) .Times(1); - BluetoothAdapterFactory::RunCallbackOnAdapterReady( + BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothAdapterDevicesChromeOsTest::SetAdapter, base::Unretained(this))); ASSERT_TRUE(adapter_ != NULL); diff --git a/device/bluetooth/bluetooth_adapter_factory.cc b/device/bluetooth/bluetooth_adapter_factory.cc index e26f642..c2263a6 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; + +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; } diff --git a/device/bluetooth/bluetooth_adapter_factory.h b/device/bluetooth/bluetooth_adapter_factory.h index f2203aa..4975337 100644 --- a/device/bluetooth/bluetooth_adapter_factory.h +++ b/device/bluetooth/bluetooth_adapter_factory.h @@ -18,18 +18,23 @@ namespace device { // adapter" which may change depending on availability. class BluetoothAdapterFactory { public: + typedef base::Callback<void(scoped_refptr<BluetoothAdapter> adapter)> + AdapterCallback; + // Returns true if the Bluetooth adapter is available for the current // platform. static bool IsBluetoothAdapterAvailable(); - // Runs the callback with the shared instance for the default adapter when the - // adapter is available to be used. - static void RunCallbackOnAdapterReady( - const BluetoothAdapter::AdapterCallback& callback); + // Returns the shared instance of the default 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. + static void GetAdapter(const AdapterCallback& callback); - // Returns the shared instance of the adapter that has already been created. + // Returns the shared instance of the adapter that has already been created, + // but may or may not have been initialized. // It returns NULL if no adapter has been created at the time. - static scoped_refptr<BluetoothAdapter> GetAdapter(); + static scoped_refptr<BluetoothAdapter> MaybeGetAdapter(); // Creates an instance for a specific adapter at address |address|. static BluetoothAdapter* Create(const std::string& address); diff --git a/device/bluetooth/bluetooth_adapter_win.cc b/device/bluetooth/bluetooth_adapter_win.cc index 530835c..128ccb3 100644 --- a/device/bluetooth/bluetooth_adapter_win.cc +++ b/device/bluetooth/bluetooth_adapter_win.cc @@ -14,8 +14,10 @@ namespace device { -BluetoothAdapterWin::BluetoothAdapterWin() +BluetoothAdapterWin::BluetoothAdapterWin(const InitCallback& init_callback) : BluetoothAdapter(), + init_callback_(init_callback), + initialized_(false), powered_(false), ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { } @@ -40,7 +42,7 @@ void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) { // TODO(youngki): Return true when |task_manager_| initializes the adapter // state. bool BluetoothAdapterWin::IsInitialized() const { - return true; + return initialized_; } bool BluetoothAdapterWin::IsPresent() const { @@ -98,6 +100,10 @@ void BluetoothAdapterWin::AdapterStateChanged( name_ = state.name; address_ = state.address; powered_ = state.powered; + if (!initialized_) { + init_callback_.Run(); + } + initialized_ = true; } void BluetoothAdapterWin::TrackDefaultAdapter() { diff --git a/device/bluetooth/bluetooth_adapter_win.h b/device/bluetooth/bluetooth_adapter_win.h index 56abf6d..286319d 100644 --- a/device/bluetooth/bluetooth_adapter_win.h +++ b/device/bluetooth/bluetooth_adapter_win.h @@ -22,6 +22,8 @@ class BluetoothDevice; class BluetoothAdapterWin : public BluetoothAdapter, public BluetoothTaskManagerWin::Observer { public: + typedef base::Callback<void()> InitCallback; + // BluetoothAdapter override virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE; virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE; @@ -52,7 +54,7 @@ class BluetoothAdapterWin : public BluetoothAdapter, protected: friend class BluetoothAdapterWinTest; - BluetoothAdapterWin(); + BluetoothAdapterWin(const InitCallback& init_callback); virtual ~BluetoothAdapterWin(); private: @@ -60,6 +62,8 @@ class BluetoothAdapterWin : public BluetoothAdapter, void TrackDefaultAdapter(); + InitCallback init_callback_; + bool initialized_; bool powered_; scoped_refptr<BluetoothTaskManagerWin> task_manager_; diff --git a/device/bluetooth/bluetooth_adapter_win_unittest.cc b/device/bluetooth/bluetooth_adapter_win_unittest.cc index 1f68b28..afd85b6 100644 --- a/device/bluetooth/bluetooth_adapter_win_unittest.cc +++ b/device/bluetooth/bluetooth_adapter_win_unittest.cc @@ -23,13 +23,21 @@ namespace device { class BluetoothAdapterWinTest : public testing::Test { public: BluetoothAdapterWinTest() - : adapter_(new BluetoothAdapterWin()), - adapter_win_(static_cast<BluetoothAdapterWin*>(adapter_.get())) { + : adapter_(new BluetoothAdapterWin( + base::Bind(&BluetoothAdapterWinTest::RunInitCallback, + base::Unretained(this)))), + adapter_win_(static_cast<BluetoothAdapterWin*>(adapter_.get())), + init_callback_called_(false) { + } + + void RunInitCallback() { + init_callback_called_ = true; } protected: scoped_refptr<BluetoothAdapter> adapter_; BluetoothAdapterWin* adapter_win_; + bool init_callback_called_; }; TEST_F(BluetoothAdapterWinTest, AdapterNotPresent) { @@ -46,5 +54,13 @@ TEST_F(BluetoothAdapterWinTest, AdapterPresent) { EXPECT_TRUE(adapter_win_->IsPresent()); } +TEST_F(BluetoothAdapterWinTest, AdapterInitialized) { + EXPECT_FALSE(adapter_win_->IsInitialized()); + EXPECT_FALSE(init_callback_called_); + BluetoothTaskManagerWin::AdapterState state; + adapter_win_->AdapterStateChanged(state); + EXPECT_TRUE(adapter_win_->IsInitialized()); + EXPECT_TRUE(init_callback_called_); +} } // namespace device
\ No newline at end of file diff --git a/device/bluetooth/bluetooth_init_win.cc b/device/bluetooth/bluetooth_init_win.cc new file mode 100644 index 0000000..54d0b20 --- /dev/null +++ b/device/bluetooth/bluetooth_init_win.cc @@ -0,0 +1,45 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "device/bluetooth/bluetooth_init_win.h" + +#include "base/threading/thread_restrictions.h" + +namespace { + +// A frame-based exception handler filter function for a handler for exceptions +// generated by the Visual C++ delay loader helper function. +int FilterVisualCPPExceptions(DWORD exception_code) { + return HRESULT_FACILITY(exception_code) == FACILITY_VISUALCPP ? + EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; +} + +} // namespace + +namespace device { +namespace bluetooth_init_win { + +bool HasBluetoothStack() { + static enum { + HBS_UNKNOWN, + HBS_YES, + HBS_NO, + } has_bluetooth_stack = HBS_UNKNOWN; + + if (has_bluetooth_stack == HBS_UNKNOWN) { + base::ThreadRestrictions::AssertIOAllowed(); + HRESULT hr = E_FAIL; + __try { + hr = __HrLoadAllImportsForDll("bthprops.cpl"); + } __except(FilterVisualCPPExceptions(::GetExceptionCode())) { + hr = E_FAIL; + } + has_bluetooth_stack = SUCCEEDED(hr) ? HBS_YES : HBS_NO; + } + + return has_bluetooth_stack == HBS_YES; +} + +} // namespace bluetooth_init_win +} // namespace device
\ No newline at end of file diff --git a/device/bluetooth/bluetooth_includes_win.h b/device/bluetooth/bluetooth_init_win.h index ed9bd6b..0bc89ea 100644 --- a/device/bluetooth/bluetooth_includes_win.h +++ b/device/bluetooth/bluetooth_init_win.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef DEVICE_BLUETOOTH_BLUETOOTH_INCLUDES_WIN_H_ -#define DEVICE_BLUETOOTH_BLUETOOTH_INCLUDES_WIN_H_ +#ifndef DEVICE_BLUETOOTH_BLUETOOTH_INIT_WIN_H_ +#define DEVICE_BLUETOOTH_BLUETOOTH_INIT_WIN_H_ // windows.h needs to be included before BluetoothAPIs.h. #include <windows.h> @@ -17,4 +17,15 @@ #pragma comment(lib, "Bthprops.lib") -#endif // DEVICE_BLUETOOTH_BLUETOOTH_INCLUDES_WIN_H_
\ No newline at end of file +namespace device { +namespace bluetooth_init_win { + +// Returns true if the machine has a bluetooth stack available. The first call +// to this function will involve file IO, so it should be done on an appropriate +// thread. This function is not threadsafe. +bool HasBluetoothStack(); + +} // namespace bluetooth_init_win +} // namespace device + +#endif // DEVICE_BLUETOOTH_BLUETOOTH_INIT_WIN_H_
\ No newline at end of file diff --git a/device/bluetooth/bluetooth_service_record_win.cc b/device/bluetooth/bluetooth_service_record_win.cc index 54e5fbd..524b17b 100644 --- a/device/bluetooth/bluetooth_service_record_win.cc +++ b/device/bluetooth/bluetooth_service_record_win.cc @@ -8,7 +8,7 @@ #include "base/basictypes.h" #include "base/stringprintf.h" -#include "device/bluetooth/bluetooth_includes_win.h" +#include "device/bluetooth/bluetooth_init_win.h" #include "device/bluetooth/bluetooth_utils.h" namespace { diff --git a/device/bluetooth/bluetooth_task_manager_win.cc b/device/bluetooth/bluetooth_task_manager_win.cc index 144682f6..ddb051e 100644 --- a/device/bluetooth/bluetooth_task_manager_win.cc +++ b/device/bluetooth/bluetooth_task_manager_win.cc @@ -13,22 +13,14 @@ #include "base/stringprintf.h" #include "base/sys_string_conversions.h" #include "base/threading/sequenced_worker_pool.h" -#include "base/threading/thread_restrictions.h" #include "base/win/scoped_handle.h" -#include "device/bluetooth/bluetooth_includes_win.h" +#include "device/bluetooth/bluetooth_init_win.h" namespace { const int kNumThreadsInWorkerPool = 3; const char kBluetoothThreadName[] = "BluetoothPollingThreadWin"; -// A frame-based exception handler filter function for a handler for exceptions -// generated by the Visual C++ delay loader helper function. -int FilterVisualCPPExceptions(DWORD exception_code) { - return HRESULT_FACILITY(exception_code) == FACILITY_VISUALCPP ? - EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; -} // namespace - // Populates bluetooth adapter state using adapter_handle. void GetAdapterState(HANDLE adapter_handle, device::BluetoothTaskManagerWin::AdapterState* state) { @@ -61,28 +53,6 @@ namespace device { // static const int BluetoothTaskManagerWin::kPollIntervalMs = 500; -// static -bool BluetoothTaskManagerWin::HasBluetoothStack() { - static enum { - HBS_UNKNOWN, - HBS_YES, - HBS_NO, - } has_bluetooth_stack = HBS_UNKNOWN; - - if (has_bluetooth_stack == HBS_UNKNOWN) { - base::ThreadRestrictions::AssertIOAllowed(); - HRESULT hr = E_FAIL; - __try { - hr = __HrLoadAllImportsForDll("bthprops.cpl"); - } __except(FilterVisualCPPExceptions(::GetExceptionCode())) { - hr = E_FAIL; - } - has_bluetooth_stack = SUCCEEDED(hr) ? HBS_YES : HBS_NO; - } - - return has_bluetooth_stack == HBS_YES; -} - BluetoothTaskManagerWin::BluetoothTaskManagerWin( scoped_refptr<base::SequencedTaskRunner> ui_task_runner) : ui_task_runner_(ui_task_runner), @@ -128,8 +98,18 @@ void BluetoothTaskManagerWin::StartPolling() { DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); // TODO(youngki): Handle this check where BluetoothAdapter is initialized. - if (HasBluetoothStack()) { + if (device::bluetooth_init_win::HasBluetoothStack()) { PollAdapter(); + } else { + // IF the bluetooth stack is not available, we still send an empty state + // to BluetoothAdapter so that it is marked initialized, but the adapter + // will not be present. + AdapterState* state = new AdapterState(); + ui_task_runner_->PostTask( + FROM_HERE, + base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, + this, + base::Owned(state))); } } @@ -215,4 +195,4 @@ void BluetoothTaskManagerWin::SetPowered( } } -} // namespace device
\ No newline at end of file +} // namespace device diff --git a/device/bluetooth/bluetooth_task_manager_win.h b/device/bluetooth/bluetooth_task_manager_win.h index 93f50cf..1f2e1d6 100644 --- a/device/bluetooth/bluetooth_task_manager_win.h +++ b/device/bluetooth/bluetooth_task_manager_win.h @@ -65,11 +65,6 @@ class BluetoothTaskManagerWin friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>; friend class BluetoothTaskManagerWinTest; - // Returns true if the machine has a bluetooth stack available. The first call - // to this function will involve file IO, so it should be done on an - // appropriate thread. This function is not threadsafe. - static bool HasBluetoothStack(); - static const int kPollIntervalMs; // Constructor to pass |ui_task_runner_| and |bluetooth_task_runner_| for diff --git a/device/bluetooth/bluetooth_task_manager_win_unittest.cc b/device/bluetooth/bluetooth_task_manager_win_unittest.cc index ce2da31..99e7e2f 100644 --- a/device/bluetooth/bluetooth_task_manager_win_unittest.cc +++ b/device/bluetooth/bluetooth_task_manager_win_unittest.cc @@ -8,6 +8,7 @@ #include "base/memory/ref_counted.h" #include "base/test/test_pending_task.h" #include "base/test/test_simple_task_runner.h" +#include "device/bluetooth/bluetooth_init_win.h" #include "device/bluetooth/bluetooth_task_manager_win.h" #include "testing/gtest/include/gtest/gtest.h" @@ -45,7 +46,7 @@ class BluetoothTaskManagerWinTest : public testing::Test { bluetooth_task_runner_(new base::TestSimpleTaskRunner()), task_manager_(new BluetoothTaskManagerWin(ui_task_runner_, bluetooth_task_runner_)), - has_bluetooth_stack_(BluetoothTaskManagerWin::HasBluetoothStack()) { + has_bluetooth_stack_(device::bluetooth_init_win::HasBluetoothStack()) { } virtual void SetUp() { diff --git a/device/device.gyp b/device/device.gyp index 7d3af8e..dae4472 100644 --- a/device/device.gyp +++ b/device/device.gyp @@ -30,7 +30,8 @@ 'bluetooth/bluetooth_device_chromeos.h', 'bluetooth/bluetooth_device_win.cc', 'bluetooth/bluetooth_device_win.h', - 'bluetooth/bluetooth_includes_win.h', + 'bluetooth/bluetooth_init_win.cc', + 'bluetooth/bluetooth_init_win.h', 'bluetooth/bluetooth_out_of_band_pairing_data.h', 'bluetooth/bluetooth_service_record.cc', 'bluetooth/bluetooth_service_record.h', |