| 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..865a8a0c75e7c35c9dd9708201be618b7c00f980 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,16 +40,45 @@ 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) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| - // Mock implementation util a more complete implementation is built out.
|
| + // TODO(scheib) Extend this very simple mock implementation by using
|
| + // device/bluetooth/test mock adapter and related classes.
|
| 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
|
| + // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed.
|
| + BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
|
| + 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: {
|
|
|