1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/common/bluetooth/bluetooth_messages.h"
9 #include "device/bluetooth/bluetooth_adapter.h"
10 #include "device/bluetooth/bluetooth_adapter_factory.h"
11 #include "device/bluetooth/bluetooth_device.h"
13 using device::BluetoothAdapter
;
14 using device::BluetoothAdapterFactory
;
18 const uint32 kUnspecifiedDeviceClass
=
19 0x1F00; // bluetooth.org/en-us/specification/assigned-numbers/baseband
21 scoped_refptr
<BluetoothDispatcherHost
> BluetoothDispatcherHost::Create() {
22 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
24 // Hold a reference to the BluetoothDispatcherHost because the callback below
25 // may run and would otherwise release the BluetoothDispatcherHost
27 scoped_refptr
<BluetoothDispatcherHost
> host(new BluetoothDispatcherHost());
28 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable())
29 BluetoothAdapterFactory::GetAdapter(
30 base::Bind(&BluetoothDispatcherHost::set_adapter
, host
));
34 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message
& message
) {
35 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
37 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost
, message
)
38 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice
, OnRequestDevice
)
39 IPC_MESSAGE_HANDLER(BluetoothHostMsg_SetBluetoothMockDataSetForTesting
,
40 OnSetBluetoothMockDataSetForTesting
)
41 IPC_MESSAGE_UNHANDLED(handled
= false)
46 BluetoothDispatcherHost::BluetoothDispatcherHost()
47 : BrowserMessageFilter(BluetoothMsgStart
),
48 bluetooth_mock_data_set_(MockData::NOT_MOCKING
),
49 bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND
) {
50 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
53 BluetoothDispatcherHost::~BluetoothDispatcherHost() {
54 // Clear adapter, releasing observer references.
55 set_adapter(scoped_refptr
<device::BluetoothAdapter
>());
58 void BluetoothDispatcherHost::set_adapter(
59 scoped_refptr
<device::BluetoothAdapter
> adapter
) {
61 adapter_
->RemoveObserver(this);
64 adapter_
->AddObserver(this);
67 void BluetoothDispatcherHost::OnRequestDevice(int thread_id
, int request_id
) {
68 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
69 // TODO(scheib) Extend this very simple mock implementation by using
70 // device/bluetooth/test mock adapter and related classes.
71 switch (bluetooth_mock_data_set_
) {
72 case MockData::NOT_MOCKING
: {
73 // TODO(scheib): Filter devices by services: crbug.com/440594
74 // TODO(scheib): Device selection UI: crbug.com/436280
75 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed.
76 BluetoothAdapter::DeviceList devices
;
78 devices
= adapter_
->GetDevices();
80 DLOG(WARNING
) << "No BluetoothAdapter. Can't serve requestDevice.";
82 if (devices
.begin() == devices
.end()) {
83 Send(new BluetoothMsg_RequestDeviceError(thread_id
, request_id
,
84 BluetoothError::NOT_FOUND
));
86 device::BluetoothDevice
* device
= *devices
.begin();
87 content::BluetoothDevice
device_ipc(
88 device
->GetAddress(), // instance_id
89 device
->GetName(), // name
90 device
->GetBluetoothClass(), // device_class
91 device
->GetVendorIDSource(), // vendor_id_source
92 device
->GetVendorID(), // vendor_id
93 device
->GetProductID(), // product_id
94 device
->GetDeviceID(), // product_version
95 device
->IsPaired(), // paired
96 device
->IsConnected(), // connected
97 content::BluetoothDevice::UUIDsFromBluetoothUUIDs(
98 device
->GetUUIDs())); // uuids
99 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id
, request_id
,
104 case MockData::REJECT
: {
105 Send(new BluetoothMsg_RequestDeviceError(
106 thread_id
, request_id
, bluetooth_request_device_reject_type_
));
109 case MockData::RESOLVE
: {
110 std::vector
<std::string
> uuids
;
111 uuids
.push_back("00001800-0000-1000-8000-00805f9b34fb");
112 uuids
.push_back("00001801-0000-1000-8000-00805f9b34fb");
113 content::BluetoothDevice
device_ipc(
114 "Empty Mock Device instanceID", // instance_id
115 base::UTF8ToUTF16("Empty Mock Device name"), // name
116 kUnspecifiedDeviceClass
, // device_class
117 device::BluetoothDevice::VENDOR_ID_BLUETOOTH
, // vendor_id_source
120 2, // product_version
124 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id
, request_id
,
132 void BluetoothDispatcherHost::OnSetBluetoothMockDataSetForTesting(
133 const std::string
& name
) {
134 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
135 if (name
== "RejectRequestDevice_NotFoundError") {
136 bluetooth_mock_data_set_
= MockData::REJECT
;
137 bluetooth_request_device_reject_type_
= BluetoothError::NOT_FOUND
;
138 } else if (name
== "RejectRequestDevice_SecurityError") {
139 bluetooth_mock_data_set_
= MockData::REJECT
;
140 bluetooth_request_device_reject_type_
= BluetoothError::SECURITY
;
141 } else if (name
== "ResolveRequestDevice_Empty" || // TODO(scheib): Remove.
142 name
== "Single Empty Device") {
143 bluetooth_mock_data_set_
= MockData::RESOLVE
;
145 bluetooth_mock_data_set_
= MockData::NOT_MOCKING
;
149 } // namespace content