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_characteristic.h" | |
13 #include "device/bluetooth/bluetooth_gatt_service.h" | 14 #include "device/bluetooth/bluetooth_gatt_service.h" |
14 | 15 |
15 using device::BluetoothAdapter; | 16 using device::BluetoothAdapter; |
16 using device::BluetoothAdapterFactory; | 17 using device::BluetoothAdapterFactory; |
18 using device::BluetoothGattCharacteristic; | |
17 using device::BluetoothGattService; | 19 using device::BluetoothGattService; |
18 | 20 |
19 namespace content { | 21 namespace content { |
20 | 22 |
21 // TODO(ortuno): Once we have a chooser for scanning and the right | 23 // TODO(ortuno): Once we have a chooser for scanning and the right |
22 // callback for discovered services we should delete these constants. | 24 // callback for discovered services we should delete these constants. |
23 // https://crbug.com/436280 and https://crbug.com/484504 | 25 // https://crbug.com/436280 and https://crbug.com/484504 |
24 const int kDelayTime = 5; // 5 seconds for scanning and discovering | 26 const int kDelayTime = 5; // 5 seconds for scanning and discovering |
25 const int kTestingDelayTime = 0; // No need to wait during tests | 27 const int kTestingDelayTime = 0; // No need to wait during tests |
26 | 28 |
(...skipping 20 matching lines...) Expand all Loading... | |
47 *thread = BrowserThread::UI; | 49 *thread = BrowserThread::UI; |
48 } | 50 } |
49 | 51 |
50 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 52 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
51 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 53 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
52 bool handled = true; | 54 bool handled = true; |
53 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 55 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) |
54 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 56 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) |
55 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) | 57 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) |
56 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) | 58 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) |
59 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) | |
57 IPC_MESSAGE_UNHANDLED(handled = false) | 60 IPC_MESSAGE_UNHANDLED(handled = false) |
58 IPC_END_MESSAGE_MAP() | 61 IPC_END_MESSAGE_MAP() |
59 return handled; | 62 return handled; |
60 } | 63 } |
61 | 64 |
62 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( | 65 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( |
63 scoped_refptr<device::BluetoothAdapter> mock_adapter) { | 66 scoped_refptr<device::BluetoothAdapter> mock_adapter) { |
64 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
65 current_delay_time_ = kTestingDelayTime; | 68 current_delay_time_ = kTestingDelayTime; |
66 set_adapter(mock_adapter.Pass()); | 69 set_adapter(mock_adapter.Pass()); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 // For now just wait a fixed time and call OnServiceDiscovered. | 145 // For now just wait a fixed time and call OnServiceDiscovered. |
143 // TODO(ortuno): Use callback once it's implemented http://crbug.com/484504 | 146 // TODO(ortuno): Use callback once it's implemented http://crbug.com/484504 |
144 BrowserThread::PostDelayedTask( | 147 BrowserThread::PostDelayedTask( |
145 BrowserThread::UI, FROM_HERE, | 148 BrowserThread::UI, FROM_HERE, |
146 base::Bind(&BluetoothDispatcherHost::OnServicesDiscovered, | 149 base::Bind(&BluetoothDispatcherHost::OnServicesDiscovered, |
147 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 150 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
148 device_instance_id, service_uuid), | 151 device_instance_id, service_uuid), |
149 base::TimeDelta::FromSeconds(current_delay_time_)); | 152 base::TimeDelta::FromSeconds(current_delay_time_)); |
150 } | 153 } |
151 | 154 |
155 void BluetoothDispatcherHost::OnGetCharacteristic( | |
156 int thread_id, | |
157 int request_id, | |
158 const std::string& service_instance_id, | |
159 const std::string& characteristic_uuid) { | |
160 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
161 // TODO(ortuno): Check if domain has access to device. | |
162 // https://crbug.com/493459 | |
163 device::BluetoothDevice* device = | |
164 adapter_->GetDevice(service_to_device_[service_instance_id]); | |
Jeffrey Yasskin
2015/06/02 22:12:28
Generally avoid using operator[] on a map to check
ortuno
2015/06/03 20:27:22
Done.
| |
165 if (device == NULL) { | |
166 Send(new BluetoothMsg_GetCharacteristicError( | |
167 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | |
168 return; | |
169 } | |
170 // TODO(ortuno): Check if domain has access to service | |
171 // http://crbug.com/493460 | |
172 device::BluetoothGattService* service = | |
173 device->GetGattService(service_instance_id); | |
174 if (!service) { | |
175 Send(new BluetoothMsg_GetCharacteristicError( | |
176 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | |
177 return; | |
178 } | |
179 | |
180 for (BluetoothGattCharacteristic* characteristic : | |
181 service->GetCharacteristics()) { | |
182 if (characteristic->GetUUID().canonical_value() == characteristic_uuid) { | |
183 // TODO(ortuno): Use generated instance ID instead. | |
184 // https://crbug.com/495379 | |
185 Send(new BluetoothMsg_GetCharacteristicSuccess( | |
186 thread_id, request_id, characteristic->GetIdentifier())); | |
187 return; | |
188 } | |
189 } | |
190 Send(new BluetoothMsg_GetCharacteristicError(thread_id, request_id, | |
191 BluetoothError::NOT_FOUND)); | |
192 } | |
193 | |
152 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 194 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
153 int thread_id, | 195 int thread_id, |
154 int request_id, | 196 int request_id, |
155 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 197 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
156 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 198 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
157 BrowserThread::PostDelayedTask( | 199 BrowserThread::PostDelayedTask( |
158 BrowserThread::UI, FROM_HERE, | 200 BrowserThread::UI, FROM_HERE, |
159 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 201 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
160 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 202 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
161 base::Passed(&discovery_session)), | 203 base::Passed(&discovery_session)), |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 // from it. | 266 // from it. |
225 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, | 267 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, |
226 device_instance_id)); | 268 device_instance_id)); |
227 } | 269 } |
228 | 270 |
229 void BluetoothDispatcherHost::OnCreateGATTConnectionError( | 271 void BluetoothDispatcherHost::OnCreateGATTConnectionError( |
230 int thread_id, | 272 int thread_id, |
231 int request_id, | 273 int request_id, |
232 const std::string& device_instance_id, | 274 const std::string& device_instance_id, |
233 device::BluetoothDevice::ConnectErrorCode error_code) { | 275 device::BluetoothDevice::ConnectErrorCode error_code) { |
234 // There was an error creating the ATT Bearer so we reject with | 276 // There was an error 'creating the ATT Bearer so we reject with |
Jeffrey Yasskin
2015/06/02 22:12:29
s/'//
ortuno
2015/06/03 20:27:22
Done.
| |
235 // NetworkError. | 277 // NetworkError. |
236 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-connect gatt | 278 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-connect gatt |
237 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id, | 279 Send(new BluetoothMsg_ConnectGATTError(thread_id, request_id, |
238 BluetoothError::NETWORK_ERROR)); | 280 BluetoothError::NETWORK_ERROR)); |
239 } | 281 } |
240 | 282 |
241 void BluetoothDispatcherHost::OnServicesDiscovered( | 283 void BluetoothDispatcherHost::OnServicesDiscovered( |
242 int thread_id, | 284 int thread_id, |
243 int request_id, | 285 int request_id, |
244 const std::string& device_instance_id, | 286 const std::string& device_instance_id, |
245 const std::string& service_uuid) { | 287 const std::string& service_uuid) { |
246 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 288 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
247 | 289 |
248 device::BluetoothDevice* device = adapter_->GetDevice(device_instance_id); | 290 device::BluetoothDevice* device = adapter_->GetDevice(device_instance_id); |
249 if (device == NULL) { | 291 if (device == NULL) { |
250 Send(new BluetoothMsg_GetPrimaryServiceError( | 292 Send(new BluetoothMsg_GetPrimaryServiceError( |
251 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | 293 thread_id, request_id, BluetoothError::NETWORK_ERROR)); |
252 return; | 294 return; |
253 } | 295 } |
254 for (BluetoothGattService* service : device->GetGattServices()) { | 296 for (BluetoothGattService* service : device->GetGattServices()) { |
255 if (service->GetUUID().canonical_value() == service_uuid) { | 297 if (service->GetUUID().canonical_value() == service_uuid) { |
298 // TODO(ortuno): Use generated instance ID instead. | |
299 // https://crbug.com/495379 | |
300 const std::string& service_identifier = service->GetIdentifier(); | |
301 service_to_device_.insert( | |
302 make_pair(service_identifier, device_instance_id)); | |
256 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id, | 303 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id, |
257 service->GetIdentifier())); | 304 service_identifier)); |
258 return; | 305 return; |
259 } | 306 } |
260 } | 307 } |
261 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, | 308 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, |
262 BluetoothError::NOT_FOUND)); | 309 BluetoothError::NOT_FOUND)); |
263 } | 310 } |
264 | 311 |
265 } // namespace content | 312 } // namespace content |
OLD | NEW |