Chromium Code Reviews| 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 #include "device/bluetooth/bluetooth_gatt_service.h" | |
| 13 | 14 |
| 14 using device::BluetoothAdapter; | 15 using device::BluetoothAdapter; |
| 15 using device::BluetoothAdapterFactory; | 16 using device::BluetoothAdapterFactory; |
| 17 using device::BluetoothGattService; | |
| 16 | 18 |
| 17 namespace content { | 19 namespace content { |
| 18 | 20 |
| 19 const int kScanTime = 5; // 5 seconds of scan time | 21 const int kDelayTime = 5; // 5 seconds for scanning and discovering |
|
Jeffrey Yasskin
2015/05/27 18:36:59
Maybe add a TODO here that this is supposed to ent
ortuno
2015/05/27 21:07:45
Done.
| |
| 20 const int kTestingScanTime = 0; // No need to scan for tests | 22 const int kTestingDelayTime = 0; // No need to wait during tests |
| 21 | 23 |
| 22 BluetoothDispatcherHost::BluetoothDispatcherHost() | 24 BluetoothDispatcherHost::BluetoothDispatcherHost() |
| 23 : BrowserMessageFilter(BluetoothMsgStart), | 25 : BrowserMessageFilter(BluetoothMsgStart), |
| 24 weak_ptr_factory_(this) { | 26 weak_ptr_factory_(this) { |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 27 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 26 current_scan_time_ = kScanTime; | 28 current_delay_time_ = kDelayTime; |
| 27 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | 29 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) |
| 28 BluetoothAdapterFactory::GetAdapter( | 30 BluetoothAdapterFactory::GetAdapter( |
| 29 base::Bind(&BluetoothDispatcherHost::set_adapter, | 31 base::Bind(&BluetoothDispatcherHost::set_adapter, |
| 30 weak_ptr_factory_.GetWeakPtr())); | 32 weak_ptr_factory_.GetWeakPtr())); |
| 31 } | 33 } |
| 32 | 34 |
| 33 void BluetoothDispatcherHost::OnDestruct() const { | 35 void BluetoothDispatcherHost::OnDestruct() const { |
| 34 // See class comment: UI Thread Note. | 36 // See class comment: UI Thread Note. |
| 35 BrowserThread::DeleteOnUIThread::Destruct(this); | 37 BrowserThread::DeleteOnUIThread::Destruct(this); |
| 36 } | 38 } |
| 37 | 39 |
| 38 void BluetoothDispatcherHost::OverrideThreadForMessage( | 40 void BluetoothDispatcherHost::OverrideThreadForMessage( |
| 39 const IPC::Message& message, | 41 const IPC::Message& message, |
| 40 content::BrowserThread::ID* thread) { | 42 content::BrowserThread::ID* thread) { |
| 41 // See class comment: UI Thread Note. | 43 // See class comment: UI Thread Note. |
| 42 *thread = BrowserThread::UI; | 44 *thread = BrowserThread::UI; |
| 43 } | 45 } |
| 44 | 46 |
| 45 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 47 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 48 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 47 bool handled = true; | 49 bool handled = true; |
| 48 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 50 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) |
| 49 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 51 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) |
| 50 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) | 52 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) |
| 53 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) | |
| 51 IPC_MESSAGE_UNHANDLED(handled = false) | 54 IPC_MESSAGE_UNHANDLED(handled = false) |
| 52 IPC_END_MESSAGE_MAP() | 55 IPC_END_MESSAGE_MAP() |
| 53 return handled; | 56 return handled; |
| 54 } | 57 } |
| 55 | 58 |
| 56 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( | 59 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( |
| 57 scoped_refptr<device::BluetoothAdapter> mock_adapter) { | 60 scoped_refptr<device::BluetoothAdapter> mock_adapter) { |
| 58 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 59 current_scan_time_ = kTestingScanTime; | 62 current_delay_time_ = kTestingDelayTime; |
| 60 set_adapter(mock_adapter.Pass()); | 63 set_adapter(mock_adapter.Pass()); |
| 61 } | 64 } |
| 62 | 65 |
| 63 BluetoothDispatcherHost::~BluetoothDispatcherHost() { | 66 BluetoothDispatcherHost::~BluetoothDispatcherHost() { |
| 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 65 // Clear adapter, releasing observer references. | 68 // Clear adapter, releasing observer references. |
| 66 set_adapter(scoped_refptr<device::BluetoothAdapter>()); | 69 set_adapter(scoped_refptr<device::BluetoothAdapter>()); |
| 67 } | 70 } |
| 68 | 71 |
| 69 void BluetoothDispatcherHost::set_adapter( | 72 void BluetoothDispatcherHost::set_adapter( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 } | 119 } |
| 117 device->CreateGattConnection( | 120 device->CreateGattConnection( |
| 118 base::Bind(&BluetoothDispatcherHost::OnGATTConnectionCreated, | 121 base::Bind(&BluetoothDispatcherHost::OnGATTConnectionCreated, |
| 119 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 122 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
| 120 device_instance_id), | 123 device_instance_id), |
| 121 base::Bind(&BluetoothDispatcherHost::OnCreateGATTConnectionError, | 124 base::Bind(&BluetoothDispatcherHost::OnCreateGATTConnectionError, |
| 122 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 125 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
| 123 device_instance_id)); | 126 device_instance_id)); |
| 124 } | 127 } |
| 125 | 128 |
| 129 void BluetoothDispatcherHost::OnGetPrimaryService( | |
| 130 int thread_id, | |
| 131 int request_id, | |
| 132 const std::string& device_instance_id, | |
| 133 const std::string& service_uuid) { | |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 135 // TODO(ortuno): Right now it's pointless to check if the domain has access to | |
| 136 // the device, because any domain can connect to any device. But once | |
| 137 // permissions are implemented we should check that the domain has access to | |
| 138 // the device. https://crbug.com/484745 | |
| 139 // For now just wait two seconds and call OnServiceDiscovered. | |
| 140 // TODO(ortuno): Use callback once it's implemented http://crbug.com/484504 | |
| 141 BrowserThread::PostDelayedTask( | |
| 142 BrowserThread::UI, FROM_HERE, | |
| 143 base::Bind(&BluetoothDispatcherHost::OnServicesDiscovered, | |
| 144 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | |
| 145 device_instance_id, service_uuid), | |
| 146 base::TimeDelta::FromSeconds(current_delay_time_)); | |
| 147 } | |
| 148 | |
| 126 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 149 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
| 127 int thread_id, | 150 int thread_id, |
| 128 int request_id, | 151 int request_id, |
| 129 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 152 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
| 130 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 131 BrowserThread::PostDelayedTask( | 154 BrowserThread::PostDelayedTask( |
| 132 BrowserThread::UI, FROM_HERE, | 155 BrowserThread::UI, FROM_HERE, |
| 133 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 156 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
| 134 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 157 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
| 135 base::Passed(&discovery_session)), | 158 base::Passed(&discovery_session)), |
| 136 base::TimeDelta::FromSeconds(current_scan_time_)); | 159 base::TimeDelta::FromSeconds(current_delay_time_)); |
| 137 } | 160 } |
| 138 | 161 |
| 139 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, | 162 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, |
| 140 int request_id) { | 163 int request_id) { |
| 141 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 164 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 142 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; | 165 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; |
| 143 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 166 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
| 144 BluetoothError::NOT_FOUND)); | 167 BluetoothError::NOT_FOUND)); |
| 145 } | 168 } |
| 146 | 169 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 int request_id, | 228 int request_id, |
| 206 const std::string& device_instance_id, | 229 const std::string& device_instance_id, |
| 207 device::BluetoothDevice::ConnectErrorCode error_code) { | 230 device::BluetoothDevice::ConnectErrorCode error_code) { |
| 208 // There was an error creating the ATT Bearer so we reject with | 231 // There was an error creating the ATT Bearer so we reject with |
| 209 // NetworkError. | 232 // NetworkError. |
| 210 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-connect gatt | 233 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-connect gatt |
| 211 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id, | 234 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id, |
| 212 BluetoothError::NETWORK_ERROR)); | 235 BluetoothError::NETWORK_ERROR)); |
| 213 } | 236 } |
| 214 | 237 |
| 238 void BluetoothDispatcherHost::OnServicesDiscovered( | |
| 239 int thread_id, | |
| 240 int request_id, | |
| 241 const std::string& device_instance_id, | |
| 242 const std::string& service_uuid) { | |
| 243 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 244 | |
| 245 device::BluetoothDevice* device = adapter_->GetDevice(device_instance_id); | |
| 246 if (device == NULL) { | |
| 247 Send(new BluetoothMsg_GetPrimaryServiceError( | |
| 248 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | |
| 249 return; | |
| 250 } | |
| 251 for (BluetoothGattService* service : device->GetGattServices()) { | |
| 252 if (service->GetUUID().canonical_value() == service_uuid) { | |
| 253 Send(new BluetoothMsg_GetPrimaryServiceSuccess( | |
| 254 thread_id, request_id, device_instance_id, | |
| 255 service->GetUUID().canonical_value())); | |
|
Jeffrey Yasskin
2015/05/27 18:36:59
There can be multiple services in a device with th
ortuno
2015/05/27 21:07:45
Ah. Good point. Done.
| |
| 256 return; | |
| 257 } | |
| 258 } | |
| 259 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, | |
| 260 BluetoothError::NOT_FOUND)); | |
| 261 } | |
| 262 | |
| 215 } // namespace content | 263 } // namespace content |
| OLD | NEW |