Index: content/browser/bluetooth/bluetooth_dispatcher_host.cc |
diff --git a/content/browser/bluetooth/bluetooth_dispatcher_host.cc b/content/browser/bluetooth/bluetooth_dispatcher_host.cc |
index ff92d932ef45ce28c8fcae7c1a8c50d75a5aa179..c21dbc1546c39f66fe5a8755367ca0857e762e65 100644 |
--- a/content/browser/bluetooth/bluetooth_dispatcher_host.cc |
+++ b/content/browser/bluetooth/bluetooth_dispatcher_host.cc |
@@ -5,14 +5,27 @@ |
#include "content/browser/bluetooth/bluetooth_dispatcher_host.h" |
#include "content/common/bluetooth/bluetooth_messages.h" |
+#include "device/bluetooth/bluetooth_adapter.h" |
+#include "device/bluetooth/bluetooth_adapter_factory.h" |
+#include "device/bluetooth/bluetooth_device.h" |
+ |
+using device::BluetoothAdapter; |
+using device::BluetoothAdapterFactory; |
+using device::BluetoothDevice; |
namespace content { |
-BluetoothDispatcherHost::BluetoothDispatcherHost() |
- : BrowserMessageFilter(BluetoothMsgStart), |
- bluetooth_mock_data_set_(MockData::NOT_MOCKING), |
- bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND) { |
+scoped_refptr<BluetoothDispatcherHost> BluetoothDispatcherHost::Create() { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ // Hold a reference to the BluetoothDispatcherHost because the callback below |
+ // may run and would otherwise release the BluetoothDispatcherHost |
+ // prematurely. |
+ scoped_refptr<BluetoothDispatcherHost> host(new BluetoothDispatcherHost()); |
+ if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) |
+ BluetoothAdapterFactory::GetAdapter( |
+ base::Bind(&BluetoothDispatcherHost::set_adapter, host)); |
+ return host; |
} |
bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
@@ -27,7 +40,25 @@ bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
return handled; |
} |
+BluetoothDispatcherHost::BluetoothDispatcherHost() |
+ : BrowserMessageFilter(BluetoothMsgStart), |
+ bluetooth_mock_data_set_(MockData::NOT_MOCKING), |
+ bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+} |
+ |
BluetoothDispatcherHost::~BluetoothDispatcherHost() { |
+ // Clear adapter, releasing observer references. |
+ set_adapter(scoped_refptr<device::BluetoothAdapter>()); |
+} |
+ |
+void BluetoothDispatcherHost::set_adapter( |
+ scoped_refptr<device::BluetoothAdapter> adapter) { |
+ if (adapter_.get()) |
+ adapter_->RemoveObserver(this); |
+ adapter_ = adapter; |
+ if (adapter_.get()) |
+ adapter_->AddObserver(this); |
} |
void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { |
@@ -35,8 +66,17 @@ void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { |
// Mock implementation util a more complete implementation is built out. |
armansito
2014/12/10 00:40:04
nit: s/util/until/ ?
scheib
2014/12/10 17:54:02
Done.
|
switch (bluetooth_mock_data_set_) { |
case MockData::NOT_MOCKING: { |
- Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
- BluetoothError::NOT_FOUND)); |
+ // TODO(scheib): Filter devices by services: crbug.com/440594 |
+ // TODO(scheib): Device selection UI: crbug.com/436280 |
+ BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
armansito
2014/12/10 00:40:04
I don't know what the final behavior will look lik
scheib
2014/12/10 17:54:02
Done.
|
+ if (devices.begin() == devices.end()) { |
+ Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
+ BluetoothError::NOT_FOUND)); |
+ } else { |
+ BluetoothDevice* device = *devices.begin(); |
+ Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, |
+ device->GetAddress())); |
+ } |
return; |
} |
case MockData::REJECT: { |