| 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 "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "content/common/bluetooth/bluetooth_messages.h" | 8 #include "content/common/bluetooth/bluetooth_messages.h" |
| 9 #include "device/bluetooth/bluetooth_adapter.h" | 9 #include "device/bluetooth/bluetooth_adapter.h" |
| 10 #include "device/bluetooth/bluetooth_adapter_factory.h" | 10 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 11 #include "device/bluetooth/bluetooth_device.h" | 11 #include "device/bluetooth/bluetooth_device.h" |
| 12 #include "device/bluetooth/bluetooth_discovery_session.h" | 12 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 13 | 13 |
| 14 using device::BluetoothAdapter; | 14 using device::BluetoothAdapter; |
| 15 using device::BluetoothAdapterFactory; | 15 using device::BluetoothAdapterFactory; |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 const uint32 kUnspecifiedDeviceClass = | |
| 20 0x1F00; // bluetooth.org/en-us/specification/assigned-numbers/baseband | |
| 21 const int kScanTime = 5; // 5 seconds of scan time | 19 const int kScanTime = 5; // 5 seconds of scan time |
| 20 const int kTestingScanTime = 0; // No need to scan for tests |
| 22 | 21 |
| 23 BluetoothDispatcherHost::BluetoothDispatcherHost() | 22 BluetoothDispatcherHost::BluetoothDispatcherHost() |
| 24 : BrowserMessageFilter(BluetoothMsgStart), | 23 : BrowserMessageFilter(BluetoothMsgStart), |
| 25 bluetooth_mock_data_set_(MockData::NOT_MOCKING), | |
| 26 bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND), | |
| 27 weak_ptr_factory_(this) { | 24 weak_ptr_factory_(this) { |
| 28 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 25 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 26 current_scan_time_ = kScanTime; |
| 29 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | 27 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) |
| 30 BluetoothAdapterFactory::GetAdapter( | 28 BluetoothAdapterFactory::GetAdapter( |
| 31 base::Bind(&BluetoothDispatcherHost::set_adapter, | 29 base::Bind(&BluetoothDispatcherHost::set_adapter, |
| 32 weak_ptr_factory_.GetWeakPtr())); | 30 weak_ptr_factory_.GetWeakPtr())); |
| 33 } | 31 } |
| 34 | 32 |
| 35 void BluetoothDispatcherHost::OnDestruct() const { | 33 void BluetoothDispatcherHost::OnDestruct() const { |
| 36 // See class comment: UI Thread Note. | 34 // See class comment: UI Thread Note. |
| 37 BrowserThread::DeleteOnUIThread::Destruct(this); | 35 BrowserThread::DeleteOnUIThread::Destruct(this); |
| 38 } | 36 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 49 bool handled = true; | 47 bool handled = true; |
| 50 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 48 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) |
| 51 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 49 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) |
| 52 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) | 50 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) |
| 53 IPC_MESSAGE_UNHANDLED(handled = false) | 51 IPC_MESSAGE_UNHANDLED(handled = false) |
| 54 IPC_END_MESSAGE_MAP() | 52 IPC_END_MESSAGE_MAP() |
| 55 return handled; | 53 return handled; |
| 56 } | 54 } |
| 57 | 55 |
| 58 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( | 56 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( |
| 59 const std::string& name) { | 57 scoped_refptr<device::BluetoothAdapter> mock_adapter) { |
| 60 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 61 if (name == "RejectRequestDevice_NotFoundError") { | 59 current_scan_time_ = kTestingScanTime; |
| 62 bluetooth_mock_data_set_ = MockData::REJECT; | 60 set_adapter(mock_adapter.Pass()); |
| 63 bluetooth_request_device_reject_type_ = BluetoothError::NOT_FOUND; | |
| 64 } else if (name == "RejectRequestDevice_SecurityError") { | |
| 65 bluetooth_mock_data_set_ = MockData::REJECT; | |
| 66 bluetooth_request_device_reject_type_ = BluetoothError::SECURITY; | |
| 67 } else if (name == "ResolveRequestDevice_Empty" || // TODO(scheib): Remove. | |
| 68 name == "Single Empty Device") { | |
| 69 bluetooth_mock_data_set_ = MockData::RESOLVE; | |
| 70 } else { | |
| 71 bluetooth_mock_data_set_ = MockData::NOT_MOCKING; | |
| 72 } | |
| 73 } | 61 } |
| 74 | 62 |
| 75 BluetoothDispatcherHost::~BluetoothDispatcherHost() { | 63 BluetoothDispatcherHost::~BluetoothDispatcherHost() { |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 77 // Clear adapter, releasing observer references. | 65 // Clear adapter, releasing observer references. |
| 78 set_adapter(scoped_refptr<device::BluetoothAdapter>()); | 66 set_adapter(scoped_refptr<device::BluetoothAdapter>()); |
| 79 } | 67 } |
| 80 | 68 |
| 81 void BluetoothDispatcherHost::set_adapter( | 69 void BluetoothDispatcherHost::set_adapter( |
| 82 scoped_refptr<device::BluetoothAdapter> adapter) { | 70 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 83 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 71 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 84 if (adapter_.get()) | 72 if (adapter_.get()) |
| 85 adapter_->RemoveObserver(this); | 73 adapter_->RemoveObserver(this); |
| 86 adapter_ = adapter; | 74 adapter_ = adapter; |
| 87 if (adapter_.get()) | 75 if (adapter_.get()) |
| 88 adapter_->AddObserver(this); | 76 adapter_->AddObserver(this); |
| 89 } | 77 } |
| 90 | 78 |
| 91 void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { | 79 void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { |
| 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 93 // TODO(scheib) Extend this very simple mock implementation by using | 81 // TODO(scheib): Filter devices by services: crbug.com/440594 |
| 94 // device/bluetooth/test mock adapter and related classes. | 82 // TODO(scheib): Device selection UI: crbug.com/436280 |
| 95 switch (bluetooth_mock_data_set_) { | 83 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed. |
| 96 case MockData::NOT_MOCKING: { | 84 if (adapter_.get()) { |
| 97 // TODO(scheib): Filter devices by services: crbug.com/440594 | 85 adapter_->StartDiscoverySession( |
| 98 // TODO(scheib): Device selection UI: crbug.com/436280 | 86 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, |
| 99 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed. | 87 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
| 100 BluetoothAdapter::DeviceList devices; | 88 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, |
| 101 | 89 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
| 102 if (adapter_.get()) { | 90 } else { |
| 103 adapter_->StartDiscoverySession( | 91 DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice."; |
| 104 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, | 92 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
| 105 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), | 93 BluetoothError::NOT_FOUND)); |
| 106 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, | |
| 107 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | |
| 108 } else { | |
| 109 DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice."; | |
| 110 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | |
| 111 BluetoothError::NOT_FOUND)); | |
| 112 } | |
| 113 return; | |
| 114 } | |
| 115 case MockData::REJECT: { | |
| 116 Send(new BluetoothMsg_RequestDeviceError( | |
| 117 thread_id, request_id, bluetooth_request_device_reject_type_)); | |
| 118 return; | |
| 119 } | |
| 120 case MockData::RESOLVE: { | |
| 121 std::vector<std::string> uuids; | |
| 122 uuids.push_back("00001800-0000-1000-8000-00805f9b34fb"); | |
| 123 uuids.push_back("00001801-0000-1000-8000-00805f9b34fb"); | |
| 124 content::BluetoothDevice device_ipc( | |
| 125 "Empty Mock Device instanceID", // instance_id | |
| 126 base::UTF8ToUTF16("Empty Mock Device name"), // name | |
| 127 kUnspecifiedDeviceClass, // device_class | |
| 128 device::BluetoothDevice::VENDOR_ID_BLUETOOTH, // vendor_id_source | |
| 129 0xFFFF, // vendor_id | |
| 130 1, // product_id | |
| 131 2, // product_version | |
| 132 true, // paired | |
| 133 uuids); // uuids | |
| 134 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, | |
| 135 device_ipc)); | |
| 136 return; | |
| 137 } | |
| 138 } | 94 } |
| 139 NOTREACHED(); | 95 return; |
| 140 } | 96 } |
| 141 | 97 |
| 142 void BluetoothDispatcherHost::OnConnectGATT( | 98 void BluetoothDispatcherHost::OnConnectGATT( |
| 143 int thread_id, | 99 int thread_id, |
| 144 int request_id, | 100 int request_id, |
| 145 const std::string& device_instance_id) { | 101 const std::string& device_instance_id) { |
| 146 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 102 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 147 // TODO(ortuno): Add actual implementation of connectGATT. This needs to be | 103 // TODO(ortuno): Add actual implementation of connectGATT. This needs to be |
| 148 // done after the "allowed devices map" is implemented. | 104 // done after the "allowed devices map" is implemented. |
| 149 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, | 105 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, |
| 150 device_instance_id)); | 106 device_instance_id)); |
| 151 } | 107 } |
| 152 | 108 |
| 153 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 109 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
| 154 int thread_id, | 110 int thread_id, |
| 155 int request_id, | 111 int request_id, |
| 156 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 112 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
| 157 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 158 BrowserThread::PostDelayedTask( | 114 BrowserThread::PostDelayedTask( |
| 159 BrowserThread::UI, FROM_HERE, | 115 BrowserThread::UI, FROM_HERE, |
| 160 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 116 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
| 161 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 117 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
| 162 base::Passed(&discovery_session)), | 118 base::Passed(&discovery_session)), |
| 163 base::TimeDelta::FromSeconds(kScanTime)); | 119 base::TimeDelta::FromSeconds(current_scan_time_)); |
| 164 } | 120 } |
| 165 | 121 |
| 166 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, | 122 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, |
| 167 int request_id) { | 123 int request_id) { |
| 168 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 124 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 169 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; | 125 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; |
| 170 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 126 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
| 171 BluetoothError::NOT_FOUND)); | 127 BluetoothError::NOT_FOUND)); |
| 172 } | 128 } |
| 173 | 129 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 166 |
| 211 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, | 167 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, |
| 212 int request_id) { | 168 int request_id) { |
| 213 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 169 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 214 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; | 170 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; |
| 215 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 171 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
| 216 BluetoothError::NOT_FOUND)); | 172 BluetoothError::NOT_FOUND)); |
| 217 } | 173 } |
| 218 | 174 |
| 219 } // namespace content | 175 } // namespace content |
| OLD | NEW |