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 | |
162 // There are no cases in which a normal renderer could pass | |
scheib
2015/06/04 06:30:48
Move this comment either just above the 'if' line,
ortuno
2015/06/05 19:37:44
Done.
| |
163 // a service_instance_id not in our map so we can assume | |
164 // the renderer is hostile. | |
165 auto device_iter = service_to_device_.find(service_instance_id); | |
166 if (device_iter == service_to_device_.end()) { | |
167 BadMessageReceived(); // Kill the renderer. | |
168 return; | |
169 } | |
170 | |
171 // TODO(ortuno): Check if domain has access to device. | |
172 // https://crbug.com/493459 | |
173 device::BluetoothDevice* device = | |
174 adapter_->GetDevice(device_iter->second /* device_instance_id */); | |
175 | |
176 if (device == NULL) { | |
177 Send(new BluetoothMsg_GetCharacteristicError( | |
178 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | |
179 return; | |
180 } | |
181 | |
182 // TODO(ortuno): Check if domain has access to service | |
183 // http://crbug.com/493460 | |
184 device::BluetoothGattService* service = | |
185 device->GetGattService(service_instance_id); | |
186 if (!service) { | |
187 Send(new BluetoothMsg_GetCharacteristicError( | |
188 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | |
189 return; | |
190 } | |
191 | |
192 for (BluetoothGattCharacteristic* characteristic : | |
193 service->GetCharacteristics()) { | |
194 if (characteristic->GetUUID().canonical_value() == characteristic_uuid) { | |
195 // TODO(ortuno): Use generated instance ID instead. | |
196 // https://crbug.com/495379 | |
197 Send(new BluetoothMsg_GetCharacteristicSuccess( | |
198 thread_id, request_id, characteristic->GetIdentifier())); | |
199 return; | |
200 } | |
201 } | |
202 Send(new BluetoothMsg_GetCharacteristicError(thread_id, request_id, | |
203 BluetoothError::NOT_FOUND)); | |
204 } | |
205 | |
152 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 206 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
153 int thread_id, | 207 int thread_id, |
154 int request_id, | 208 int request_id, |
155 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 209 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
156 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 210 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
157 BrowserThread::PostDelayedTask( | 211 BrowserThread::PostDelayedTask( |
158 BrowserThread::UI, FROM_HERE, | 212 BrowserThread::UI, FROM_HERE, |
159 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 213 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
160 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 214 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
161 base::Passed(&discovery_session)), | 215 base::Passed(&discovery_session)), |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 300 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
247 | 301 |
248 device::BluetoothDevice* device = adapter_->GetDevice(device_instance_id); | 302 device::BluetoothDevice* device = adapter_->GetDevice(device_instance_id); |
249 if (device == NULL) { | 303 if (device == NULL) { |
250 Send(new BluetoothMsg_GetPrimaryServiceError( | 304 Send(new BluetoothMsg_GetPrimaryServiceError( |
251 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | 305 thread_id, request_id, BluetoothError::NETWORK_ERROR)); |
252 return; | 306 return; |
253 } | 307 } |
254 for (BluetoothGattService* service : device->GetGattServices()) { | 308 for (BluetoothGattService* service : device->GetGattServices()) { |
255 if (service->GetUUID().canonical_value() == service_uuid) { | 309 if (service->GetUUID().canonical_value() == service_uuid) { |
310 // TODO(ortuno): Use generated instance ID instead. | |
311 // https://crbug.com/495379 | |
312 const std::string& service_identifier = service->GetIdentifier(); | |
313 service_to_device_.insert( | |
scheib
2015/06/04 06:30:48
Maybe for paranoia:
auto insert_result = service_
ortuno
2015/06/05 19:37:44
If you call getPrimaryService("some uuid") twice t
scheib
2015/06/08 17:02:07
Whoops, overlooked this. Discussed in person too,
| |
314 make_pair(service_identifier, device_instance_id)); | |
256 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id, | 315 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id, |
257 service->GetIdentifier())); | 316 service_identifier)); |
258 return; | 317 return; |
259 } | 318 } |
260 } | 319 } |
261 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, | 320 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, |
262 BluetoothError::NOT_FOUND)); | 321 BluetoothError::NOT_FOUND)); |
263 } | 322 } |
264 | 323 |
265 } // namespace content | 324 } // namespace content |
OLD | NEW |