| 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/browser/bad_message.h" | 8 #include "content/browser/bad_message.h" |
| 9 #include "content/common/bluetooth/bluetooth_messages.h" | 9 #include "content/common/bluetooth/bluetooth_messages.h" |
| 10 #include "device/bluetooth/bluetooth_adapter.h" | 10 #include "device/bluetooth/bluetooth_adapter.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 53 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 55 bool handled = true; | 55 bool handled = true; |
| 56 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 56 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) |
| 57 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 57 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) |
| 58 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) | 58 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) |
| 59 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) | 59 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) |
| 60 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) | 60 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) |
| 61 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ReadValue, OnReadValue) |
| 61 IPC_MESSAGE_UNHANDLED(handled = false) | 62 IPC_MESSAGE_UNHANDLED(handled = false) |
| 62 IPC_END_MESSAGE_MAP() | 63 IPC_END_MESSAGE_MAP() |
| 63 return handled; | 64 return handled; |
| 64 } | 65 } |
| 65 | 66 |
| 66 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( | 67 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( |
| 67 scoped_refptr<device::BluetoothAdapter> mock_adapter) { | 68 scoped_refptr<device::BluetoothAdapter> mock_adapter) { |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 69 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 69 current_delay_time_ = kTestingDelayTime; | 70 current_delay_time_ = kTestingDelayTime; |
| 70 set_adapter(mock_adapter.Pass()); | 71 set_adapter(mock_adapter.Pass()); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 device->GetGattService(service_instance_id); | 188 device->GetGattService(service_instance_id); |
| 188 if (!service) { | 189 if (!service) { |
| 189 Send(new BluetoothMsg_GetCharacteristicError( | 190 Send(new BluetoothMsg_GetCharacteristicError( |
| 190 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | 191 thread_id, request_id, BluetoothError::NETWORK_ERROR)); |
| 191 return; | 192 return; |
| 192 } | 193 } |
| 193 | 194 |
| 194 for (BluetoothGattCharacteristic* characteristic : | 195 for (BluetoothGattCharacteristic* characteristic : |
| 195 service->GetCharacteristics()) { | 196 service->GetCharacteristics()) { |
| 196 if (characteristic->GetUUID().canonical_value() == characteristic_uuid) { | 197 if (characteristic->GetUUID().canonical_value() == characteristic_uuid) { |
| 198 const std::string& characteristic_instance_id = |
| 199 characteristic->GetIdentifier(); |
| 200 |
| 201 auto insert_result = characteristic_to_service_.insert( |
| 202 make_pair(characteristic_instance_id, service_instance_id)); |
| 203 |
| 204 // If value is already in map, DCHECK it's valid. |
| 205 if (!insert_result.second) |
| 206 DCHECK(insert_result.first->second == service_instance_id); |
| 207 |
| 197 // TODO(ortuno): Use generated instance ID instead. | 208 // TODO(ortuno): Use generated instance ID instead. |
| 198 // https://crbug.com/495379 | 209 // https://crbug.com/495379 |
| 199 Send(new BluetoothMsg_GetCharacteristicSuccess( | 210 Send(new BluetoothMsg_GetCharacteristicSuccess( |
| 200 thread_id, request_id, characteristic->GetIdentifier())); | 211 thread_id, request_id, characteristic_instance_id)); |
| 201 return; | 212 return; |
| 202 } | 213 } |
| 203 } | 214 } |
| 204 Send(new BluetoothMsg_GetCharacteristicError(thread_id, request_id, | 215 Send(new BluetoothMsg_GetCharacteristicError(thread_id, request_id, |
| 205 BluetoothError::NOT_FOUND)); | 216 BluetoothError::NOT_FOUND)); |
| 206 } | 217 } |
| 207 | 218 |
| 219 void BluetoothDispatcherHost::OnReadValue( |
| 220 int thread_id, |
| 221 int request_id, |
| 222 const std::string& characteristic_instance_id) { |
| 223 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 224 |
| 225 auto characteristic_iter = |
| 226 characteristic_to_service_.find(characteristic_instance_id); |
| 227 // A characteristic_instance_id not in the map implies a hostile renderer |
| 228 // because a renderer obtains the characteristic id from this class and |
| 229 // it will be added to the map at that time. |
| 230 if (characteristic_iter == characteristic_to_service_.end()) { |
| 231 // Kill the renderer |
| 232 bad_message::ReceivedBadMessage(this, |
| 233 bad_message::BDH_INVALID_CHARACTERISTIC_ID); |
| 234 return; |
| 235 } |
| 236 const std::string& service_instance_id = characteristic_iter->second; |
| 237 |
| 238 auto device_iter = service_to_device_.find(service_instance_id); |
| 239 |
| 240 DCHECK(device_iter != service_to_device_.end()); |
| 241 if (device_iter == service_to_device_.end()) { |
| 242 // Change to InvalidStateError: |
| 243 // http://crbug.com/499014 |
| 244 Send(new BluetoothMsg_ReadCharacteristicValueError( |
| 245 thread_id, request_id, BluetoothError::NETWORK_ERROR)); |
| 246 return; |
| 247 } |
| 248 |
| 249 device::BluetoothDevice* device = |
| 250 adapter_->GetDevice(device_iter->second /* device_instance_id */); |
| 251 if (device == nullptr) { |
| 252 Send(new BluetoothMsg_ReadCharacteristicValueError( |
| 253 thread_id, request_id, BluetoothError::NETWORK_ERROR)); |
| 254 return; |
| 255 } |
| 256 |
| 257 BluetoothGattService* service = device->GetGattService(service_instance_id); |
| 258 if (service == nullptr) { |
| 259 // TODO(ortuno): Change to InvalidStateError once implemented: |
| 260 // http://crbug.com/499014 |
| 261 Send(new BluetoothMsg_ReadCharacteristicValueError( |
| 262 thread_id, request_id, BluetoothError::NETWORK_ERROR)); |
| 263 return; |
| 264 } |
| 265 |
| 266 BluetoothGattCharacteristic* characteristic = |
| 267 service->GetCharacteristic(characteristic_instance_id); |
| 268 if (characteristic == nullptr) { |
| 269 Send(new BluetoothMsg_ReadCharacteristicValueError( |
| 270 thread_id, request_id, BluetoothError::NETWORK_ERROR)); |
| 271 return; |
| 272 } |
| 273 |
| 274 characteristic->ReadRemoteCharacteristic( |
| 275 base::Bind(&BluetoothDispatcherHost::OnCharacteristicValueRead, |
| 276 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
| 277 base::Bind(&BluetoothDispatcherHost::OnCharacteristicReadValueError, |
| 278 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
| 279 } |
| 280 |
| 208 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 281 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
| 209 int thread_id, | 282 int thread_id, |
| 210 int request_id, | 283 int request_id, |
| 211 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 284 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
| 212 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 285 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 213 BrowserThread::PostDelayedTask( | 286 BrowserThread::PostDelayedTask( |
| 214 BrowserThread::UI, FROM_HERE, | 287 BrowserThread::UI, FROM_HERE, |
| 215 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 288 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
| 216 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 289 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
| 217 base::Passed(&discovery_session)), | 290 base::Passed(&discovery_session)), |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 return; | 381 return; |
| 309 } | 382 } |
| 310 for (BluetoothGattService* service : device->GetGattServices()) { | 383 for (BluetoothGattService* service : device->GetGattServices()) { |
| 311 if (service->GetUUID().canonical_value() == service_uuid) { | 384 if (service->GetUUID().canonical_value() == service_uuid) { |
| 312 // TODO(ortuno): Use generated instance ID instead. | 385 // TODO(ortuno): Use generated instance ID instead. |
| 313 // https://crbug.com/495379 | 386 // https://crbug.com/495379 |
| 314 const std::string& service_identifier = service->GetIdentifier(); | 387 const std::string& service_identifier = service->GetIdentifier(); |
| 315 auto insert_result = service_to_device_.insert( | 388 auto insert_result = service_to_device_.insert( |
| 316 make_pair(service_identifier, device_instance_id)); | 389 make_pair(service_identifier, device_instance_id)); |
| 317 | 390 |
| 318 // If the service existed already check that device_instance_id is the | 391 // If a value is already in map, DCHECK it's valid. |
| 319 // same. | |
| 320 if (!insert_result.second) | 392 if (!insert_result.second) |
| 321 DCHECK(insert_result.first->second == device_instance_id); | 393 DCHECK(insert_result.first->second == device_instance_id); |
| 322 | 394 |
| 323 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id, | 395 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id, |
| 324 service_identifier)); | 396 service_identifier)); |
| 325 return; | 397 return; |
| 326 } | 398 } |
| 327 } | 399 } |
| 328 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, | 400 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, |
| 329 BluetoothError::NOT_FOUND)); | 401 BluetoothError::NOT_FOUND)); |
| 330 } | 402 } |
| 331 | 403 |
| 404 void BluetoothDispatcherHost::OnCharacteristicValueRead( |
| 405 int thread_id, |
| 406 int request_id, |
| 407 const std::vector<uint8>& value) { |
| 408 Send(new BluetoothMsg_ReadCharacteristicValueSuccess(thread_id, request_id, |
| 409 value)); |
| 410 } |
| 411 |
| 412 void BluetoothDispatcherHost::OnCharacteristicReadValueError( |
| 413 int thread_id, |
| 414 int request_id, |
| 415 device::BluetoothGattService::GattErrorCode) { |
| 416 // TODO(ortuno): Send a different Error based on the GattErrorCode. |
| 417 // http://crbug.com/499542 |
| 418 Send(new BluetoothMsg_ReadCharacteristicValueError( |
| 419 thread_id, request_id, BluetoothError::NETWORK_ERROR)); |
| 420 } |
| 421 |
| 332 } // namespace content | 422 } // namespace content |
| OLD | NEW |