OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" | 5 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" |
6 | 6 |
7 #include "content/common/bluetooth/bluetooth_messages.h" | 7 #include "content/common/bluetooth/bluetooth_messages.h" |
| 8 #include "device/bluetooth/bluetooth_adapter.h" |
| 9 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 10 #include "device/bluetooth/bluetooth_device.h" |
| 11 |
| 12 using device::BluetoothAdapter; |
| 13 using device::BluetoothAdapterFactory; |
| 14 using device::BluetoothDevice; |
8 | 15 |
9 namespace content { | 16 namespace content { |
10 | 17 |
11 BluetoothDispatcherHost::BluetoothDispatcherHost() | 18 scoped_refptr<BluetoothDispatcherHost> BluetoothDispatcherHost::Create() { |
12 : BrowserMessageFilter(BluetoothMsgStart), | |
13 bluetooth_mock_data_set_(MockData::NOT_MOCKING), | |
14 bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND) { | |
15 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 19 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 20 |
| 21 // Hold a reference to the BluetoothDispatcherHost because the callback below |
| 22 // may run and would otherwise release the BluetoothDispatcherHost |
| 23 // prematurely. |
| 24 scoped_refptr<BluetoothDispatcherHost> host(new BluetoothDispatcherHost()); |
| 25 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) |
| 26 BluetoothAdapterFactory::GetAdapter( |
| 27 base::Bind(&BluetoothDispatcherHost::set_adapter, host)); |
| 28 return host; |
16 } | 29 } |
17 | 30 |
18 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 31 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
19 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 32 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
20 bool handled = true; | 33 bool handled = true; |
21 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 34 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) |
22 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 35 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) |
23 IPC_MESSAGE_HANDLER(BluetoothHostMsg_SetBluetoothMockDataSetForTesting, | 36 IPC_MESSAGE_HANDLER(BluetoothHostMsg_SetBluetoothMockDataSetForTesting, |
24 OnSetBluetoothMockDataSetForTesting) | 37 OnSetBluetoothMockDataSetForTesting) |
25 IPC_MESSAGE_UNHANDLED(handled = false) | 38 IPC_MESSAGE_UNHANDLED(handled = false) |
26 IPC_END_MESSAGE_MAP() | 39 IPC_END_MESSAGE_MAP() |
27 return handled; | 40 return handled; |
28 } | 41 } |
29 | 42 |
| 43 BluetoothDispatcherHost::BluetoothDispatcherHost() |
| 44 : BrowserMessageFilter(BluetoothMsgStart), |
| 45 bluetooth_mock_data_set_(MockData::NOT_MOCKING), |
| 46 bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND) { |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 48 } |
| 49 |
30 BluetoothDispatcherHost::~BluetoothDispatcherHost() { | 50 BluetoothDispatcherHost::~BluetoothDispatcherHost() { |
| 51 // Clear adapter, releasing observer references. |
| 52 set_adapter(scoped_refptr<device::BluetoothAdapter>()); |
| 53 } |
| 54 |
| 55 void BluetoothDispatcherHost::set_adapter( |
| 56 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 57 if (adapter_.get()) |
| 58 adapter_->RemoveObserver(this); |
| 59 adapter_ = adapter; |
| 60 if (adapter_.get()) |
| 61 adapter_->AddObserver(this); |
31 } | 62 } |
32 | 63 |
33 void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { | 64 void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { |
34 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
35 // Mock implementation util a more complete implementation is built out. | 66 // TODO(scheib) Extend this very simple mock implementation by using |
| 67 // device/bluetooth/test mock adapter and related classes. |
36 switch (bluetooth_mock_data_set_) { | 68 switch (bluetooth_mock_data_set_) { |
37 case MockData::NOT_MOCKING: { | 69 case MockData::NOT_MOCKING: { |
38 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 70 // TODO(scheib): Filter devices by services: crbug.com/440594 |
39 BluetoothError::NOT_FOUND)); | 71 // TODO(scheib): Device selection UI: crbug.com/436280 |
| 72 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed. |
| 73 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
| 74 if (devices.begin() == devices.end()) { |
| 75 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
| 76 BluetoothError::NOT_FOUND)); |
| 77 } else { |
| 78 BluetoothDevice* device = *devices.begin(); |
| 79 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, |
| 80 device->GetAddress())); |
| 81 } |
40 return; | 82 return; |
41 } | 83 } |
42 case MockData::REJECT: { | 84 case MockData::REJECT: { |
43 Send(new BluetoothMsg_RequestDeviceError( | 85 Send(new BluetoothMsg_RequestDeviceError( |
44 thread_id, request_id, bluetooth_request_device_reject_type_)); | 86 thread_id, request_id, bluetooth_request_device_reject_type_)); |
45 return; | 87 return; |
46 } | 88 } |
47 case MockData::RESOLVE: { | 89 case MockData::RESOLVE: { |
48 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, | 90 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, |
49 "Empty Mock deviceId")); | 91 "Empty Mock deviceId")); |
(...skipping 14 matching lines...) Expand all Loading... |
64 bluetooth_request_device_reject_type_ = BluetoothError::SECURITY; | 106 bluetooth_request_device_reject_type_ = BluetoothError::SECURITY; |
65 } else if (name == "ResolveRequestDevice_Empty" || // TODO(scheib): Remove. | 107 } else if (name == "ResolveRequestDevice_Empty" || // TODO(scheib): Remove. |
66 name == "Single Empty Device") { | 108 name == "Single Empty Device") { |
67 bluetooth_mock_data_set_ = MockData::RESOLVE; | 109 bluetooth_mock_data_set_ = MockData::RESOLVE; |
68 } else { | 110 } else { |
69 bluetooth_mock_data_set_ = MockData::NOT_MOCKING; | 111 bluetooth_mock_data_set_ = MockData::NOT_MOCKING; |
70 } | 112 } |
71 } | 113 } |
72 | 114 |
73 } // namespace content | 115 } // namespace content |
OLD | NEW |