diff options
author | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-25 09:26:01 +0000 |
---|---|---|
committer | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-25 09:26:01 +0000 |
commit | 063d9430289fdf33106cc23d7225391ec56c9e93 (patch) | |
tree | cb13ee8851648ec5fa3fdfb7efb6056612dd86ed | |
parent | 6dcc0079d3b89d46b6f3a68570c0eca538aff32d (diff) | |
download | chromium_src-063d9430289fdf33106cc23d7225391ec56c9e93.zip chromium_src-063d9430289fdf33106cc23d7225391ec56c9e93.tar.gz chromium_src-063d9430289fdf33106cc23d7225391ec56c9e93.tar.bz2 |
Resubmitting asynchronous bluetooth adapter init CL with fix.
The original CL: https://chromiumcodereview.appspot.com/12018024/ was reverted because it violated the style guide that the struct-level global variable is prohibited and it failed the static analyzer test, in bluetooth_adapter_factory.cc. In order to resolve the issue, I changed adapter_callbacks to a lazy instance.
BUG=135470
Review URL: https://chromiumcodereview.appspot.com/12045051
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@178798 0039d316-1c4b-4281-b951-d872f2087c98
24 files changed, 216 insertions, 142 deletions
diff --git a/chrome/browser/chromeos/system/ash_system_tray_delegate.cc b/chrome/browser/chromeos/system/ash_system_tray_delegate.cc index 8e30cd6..62ef639 100644 --- a/chrome/browser/chromeos/system/ash_system_tray_delegate.cc +++ b/chrome/browser/chromeos/system/ash_system_tray_delegate.cc @@ -269,7 +269,7 @@ class SystemTrayDelegate : public ash::SystemTrayDelegate, network_icon_->SetResourceColorTheme(NetworkMenuIcon::COLOR_LIGHT); network_icon_dark_->SetResourceColorTheme(NetworkMenuIcon::COLOR_DARK); - device::BluetoothAdapterFactory::RunCallbackOnAdapterReady( + device::BluetoothAdapterFactory::GetAdapter( base::Bind(&SystemTrayDelegate::InitializeOnAdapterReady, ui_weak_ptr_factory_->GetWeakPtr())); } diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc index b6b786a..bbe7244 100644 --- a/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc +++ b/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc @@ -47,14 +47,14 @@ bool ExtensionBluetoothEventRouter::IsBluetoothSupported() const { device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); } -void ExtensionBluetoothEventRouter::RunCallbackOnAdapterReady( - const device::BluetoothAdapter::AdapterCallback& callback) { +void ExtensionBluetoothEventRouter::GetAdapter( + const device::BluetoothAdapterFactory::AdapterCallback& callback) { if (adapter_) { callback.Run(scoped_refptr<device::BluetoothAdapter>(adapter_)); return; } - device::BluetoothAdapterFactory::RunCallbackOnAdapterReady(callback); + device::BluetoothAdapterFactory::GetAdapter(callback); } void ExtensionBluetoothEventRouter::OnListenerAdded() { @@ -186,9 +186,8 @@ void ExtensionBluetoothEventRouter::DeviceAdded( void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { if (!adapter_) { - RunCallbackOnAdapterReady( - base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter, - weak_ptr_factory_.GetWeakPtr())); + GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter, + weak_ptr_factory_.GetWeakPtr())); } } diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h b/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h index 2353626..d8f8024 100644 --- a/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h +++ b/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h @@ -29,8 +29,8 @@ class ExtensionBluetoothEventRouter // adapter is available for the current platform. bool IsBluetoothSupported() const; - void RunCallbackOnAdapterReady( - const device::BluetoothAdapter::AdapterCallback& callback); + void GetAdapter( + const device::BluetoothAdapterFactory::AdapterCallback& callback); // Called when a bluetooth event listener is added. void OnListenerAdded(); diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.cc index ed63f8a..3b6baa8 100644 --- a/chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.cc +++ b/chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.cc @@ -24,10 +24,10 @@ bool IsBluetoothSupported(Profile* profile) { return GetEventRouter(profile)->IsBluetoothSupported(); } -void RunCallbackOnAdapterReady( - const device::BluetoothAdapter::AdapterCallback callback, +void GetAdapter( + const device::BluetoothAdapterFactory::AdapterCallback callback, Profile* profile) { - GetEventRouter(profile)->RunCallbackOnAdapterReady(callback); + GetEventRouter(profile)->GetAdapter(callback); } } // namespace @@ -36,8 +36,7 @@ namespace extensions { namespace api { -BluetoothExtensionFunction::BluetoothExtensionFunction() - : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { +BluetoothExtensionFunction::BluetoothExtensionFunction() { } BluetoothExtensionFunction::~BluetoothExtensionFunction() { @@ -48,10 +47,9 @@ bool BluetoothExtensionFunction::RunImpl() { SetError(kPlatformNotSupported); return false; } - RunCallbackOnAdapterReady( - base::Bind(&BluetoothExtensionFunction::RunOnAdapterReady, - weak_ptr_factory_.GetWeakPtr()), - profile()); + GetAdapter(base::Bind(&BluetoothExtensionFunction::RunOnAdapterReady, this), + profile()); + return true; } diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h b/chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h index c6c4ddb..d24f654 100644 --- a/chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h +++ b/chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h @@ -39,8 +39,6 @@ class BluetoothExtensionFunction : public AsyncExtensionFunction { // automatically once |adapter| has been initialized. virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) = 0; - base::WeakPtrFactory<BluetoothExtensionFunction> weak_ptr_factory_; - DISALLOW_COPY_AND_ASSIGN(BluetoothExtensionFunction); }; diff --git a/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc b/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc index 99ca95b..9d8732a 100644 --- a/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc +++ b/chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.cc @@ -183,7 +183,7 @@ void BluetoothOptionsHandler::RegisterMessages() { } void BluetoothOptionsHandler::InitializeHandler() { - device::BluetoothAdapterFactory::RunCallbackOnAdapterReady( + device::BluetoothAdapterFactory::GetAdapter( base::Bind(&BluetoothOptionsHandler::InitializeAdapter, weak_ptr_factory_.GetWeakPtr())); } diff --git a/chrome/common/extensions/permissions/bluetooth_device_permission.cc b/chrome/common/extensions/permissions/bluetooth_device_permission.cc index fccd9e5..8cb0b18 100644 --- a/chrome/common/extensions/permissions/bluetooth_device_permission.cc +++ b/chrome/common/extensions/permissions/bluetooth_device_permission.cc @@ -39,7 +39,7 @@ PermissionMessages BluetoothDevicePermission::GetMessages() const { PermissionMessages result; scoped_refptr<device::BluetoothAdapter> bluetooth_adapter = - device::BluetoothAdapterFactory::GetAdapter(); + device::BluetoothAdapterFactory::MaybeGetAdapter(); for (std::set<BluetoothDevicePermissionData>::const_iterator i = data_set_.begin(); i != data_set_.end(); ++i) { 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..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; } 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_includes_win.h b/device/bluetooth/bluetooth_includes_win.h deleted file mode 100644 index ed9bd6b..0000000 --- a/device/bluetooth/bluetooth_includes_win.h +++ /dev/null @@ -1,20 +0,0 @@ -// 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. - -#ifndef DEVICE_BLUETOOTH_BLUETOOTH_INCLUDES_WIN_H_ -#define DEVICE_BLUETOOTH_BLUETOOTH_INCLUDES_WIN_H_ - -// windows.h needs to be included before BluetoothAPIs.h. -#include <windows.h> - -#include <BluetoothAPIs.h> -#if defined(_WIN32_WINNT_WIN8) && _MSC_VER < 1700 -// The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h. -#undef FACILITY_VISUALCPP -#endif -#include <delayimp.h> - -#pragma comment(lib, "Bthprops.lib") - -#endif // DEVICE_BLUETOOTH_BLUETOOTH_INCLUDES_WIN_H_
\ 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_init_win.h b/device/bluetooth/bluetooth_init_win.h new file mode 100644 index 0000000..da0ed75 --- /dev/null +++ b/device/bluetooth/bluetooth_init_win.h @@ -0,0 +1,31 @@ +// Copyright (c) 2012 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. + +#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> + +#include <BluetoothAPIs.h> +#if defined(_WIN32_WINNT_WIN8) && _MSC_VER < 1700 +// The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h. +#undef FACILITY_VISUALCPP +#endif +#include <delayimp.h> + +#pragma comment(lib, "Bthprops.lib") + +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_ 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 803d846b..8537eee 100644 --- a/device/device.gyp +++ b/device/device.gyp @@ -32,7 +32,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', |