Index: chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc |
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc |
index bb0a492dc8c6b3c3fa4e9257786b90d981571394..99caaac4acdafe39546ebe34ef16db0a69212bfb 100644 |
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc |
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_event_router.cc |
@@ -29,7 +29,8 @@ ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) |
profile_(profile), |
adapter_(NULL), |
num_event_listeners_(0), |
- next_socket_id_(1) { |
+ next_socket_id_(1), |
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
DCHECK(profile_); |
} |
@@ -41,12 +42,19 @@ ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { |
} |
} |
-scoped_refptr<device::BluetoothAdapter> |
-ExtensionBluetoothEventRouter::GetAdapter() { |
- if (adapter_) |
- return adapter_; |
+bool ExtensionBluetoothEventRouter::IsBluetoothSupported() const { |
+ return adapter_ || |
+ device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); |
+} |
+ |
+void ExtensionBluetoothEventRouter::RunCallbackOnAdapterReady( |
+ const device::BluetoothAdapterFactory::AdapterCallback& callback) { |
+ if (adapter_) { |
+ callback.Run(scoped_refptr<device::BluetoothAdapter>(adapter_)); |
+ return; |
+ } |
- return device::BluetoothAdapterFactory::DefaultAdapter(); |
+ device::BluetoothAdapterFactory::RunCallbackOnAdapterReady(callback); |
} |
void ExtensionBluetoothEventRouter::OnListenerAdded() { |
@@ -178,11 +186,19 @@ void ExtensionBluetoothEventRouter::DeviceAdded( |
void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { |
if (!adapter_) { |
- adapter_ = GetAdapter(); |
- adapter_->AddObserver(this); |
+ RunCallbackOnAdapterReady( |
+ base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter, |
+ weak_ptr_factory_.GetWeakPtr())); |
} |
} |
+void ExtensionBluetoothEventRouter::InitializeAdapter( |
+ scoped_refptr<device::BluetoothAdapter> adapter) { |
+ adapter_ = adapter; |
+ if (adapter_) |
+ adapter_->AddObserver(this); |
+} |
+ |
void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() { |
if (adapter_ && num_event_listeners_ == 0) { |
adapter_->RemoveObserver(this); |