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/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 the characteristic existed already check that service_instance_id is | |
| 205 // the same. | |
| 206 if (!insert_result.second) | |
| 207 DCHECK(insert_result.first->second == service_instance_id); | |
| 208 | |
| 197 // TODO(ortuno): Use generated instance ID instead. | 209 // TODO(ortuno): Use generated instance ID instead. |
| 198 // https://crbug.com/495379 | 210 // https://crbug.com/495379 |
| 199 Send(new BluetoothMsg_GetCharacteristicSuccess( | 211 Send(new BluetoothMsg_GetCharacteristicSuccess( |
| 200 thread_id, request_id, characteristic->GetIdentifier())); | 212 thread_id, request_id, characteristic_instance_id)); |
| 201 return; | 213 return; |
| 202 } | 214 } |
| 203 } | 215 } |
| 204 Send(new BluetoothMsg_GetCharacteristicError(thread_id, request_id, | 216 Send(new BluetoothMsg_GetCharacteristicError(thread_id, request_id, |
| 205 BluetoothError::NOT_FOUND)); | 217 BluetoothError::NOT_FOUND)); |
| 206 } | 218 } |
| 207 | 219 |
| 220 void BluetoothDispatcherHost::OnReadValue( | |
| 221 int thread_id, | |
| 222 int request_id, | |
| 223 const std::string& characteristic_instance_id) { | |
| 224 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 225 | |
| 226 auto characteristic_iter = | |
| 227 characteristic_to_service_.find(characteristic_instance_id); | |
| 228 // A characteristic_instance_id not in the map implies a hostile renderer | |
| 229 // because a renderer obtains the characteristic id from this class and | |
| 230 // it will be added to the map at that time. | |
| 231 if (characteristic_iter == characteristic_to_service_.end()) { | |
| 232 // Kill the renderer | |
| 233 bad_message::ReceivedBadMessage(this, | |
| 234 bad_message::BDH_INVALID_CHARACTERISTIC_ID); | |
| 235 return; | |
| 236 } | |
| 237 const std::string& service_instance_id = characteristic_iter->second; | |
| 238 | |
| 239 auto device_iter = service_to_device_.find(service_instance_id); | |
| 240 // A service_instance_id not in the map implies a hostile renderer | |
| 241 // because a renderer obtains the service id from this class and | |
| 242 // it will be added to the map at that time. | |
| 243 if (device_iter == service_to_device_.end()) { | |
| 244 // Kill the renderer | |
| 245 bad_message::ReceivedBadMessage(this, bad_message::BDH_INVALID_SERVICE_ID); | |
| 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 Send(new BluetoothMsg_ReadCharacteristicValueError( | |
| 260 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | |
|
Jeffrey Yasskin
2015/06/10 21:03:45
Hm, I'm not sure this is notionally a network erro
ortuno
2015/06/10 22:03:01
Added a TODO for when the error is implemented.
| |
| 261 return; | |
| 262 } | |
| 263 | |
| 264 BluetoothGattCharacteristic* characteristic = | |
| 265 service->GetCharacteristic(characteristic_instance_id); | |
| 266 if (characteristic == nullptr) { | |
| 267 Send(new BluetoothMsg_ReadCharacteristicValueError( | |
| 268 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | |
| 269 return; | |
| 270 } | |
| 271 | |
| 272 characteristic->ReadRemoteCharacteristic( | |
| 273 base::Bind(&BluetoothDispatcherHost::OnCharacteristicValueRead, | |
| 274 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), | |
| 275 base::Bind(&BluetoothDispatcherHost::OnCharacteristicReadValueError, | |
| 276 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | |
| 277 } | |
| 278 | |
| 208 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 279 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
| 209 int thread_id, | 280 int thread_id, |
| 210 int request_id, | 281 int request_id, |
| 211 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 282 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
| 212 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 283 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 213 BrowserThread::PostDelayedTask( | 284 BrowserThread::PostDelayedTask( |
| 214 BrowserThread::UI, FROM_HERE, | 285 BrowserThread::UI, FROM_HERE, |
| 215 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 286 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
| 216 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 287 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
| 217 base::Passed(&discovery_session)), | 288 base::Passed(&discovery_session)), |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 | 393 |
| 323 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id, | 394 Send(new BluetoothMsg_GetPrimaryServiceSuccess(thread_id, request_id, |
| 324 service_identifier)); | 395 service_identifier)); |
| 325 return; | 396 return; |
| 326 } | 397 } |
| 327 } | 398 } |
| 328 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, | 399 Send(new BluetoothMsg_GetPrimaryServiceError(thread_id, request_id, |
| 329 BluetoothError::NOT_FOUND)); | 400 BluetoothError::NOT_FOUND)); |
| 330 } | 401 } |
| 331 | 402 |
| 403 void BluetoothDispatcherHost::OnCharacteristicValueRead( | |
| 404 int thread_id, | |
| 405 int request_id, | |
| 406 const std::vector<uint8>& value) { | |
| 407 Send(new BluetoothMsg_ReadCharacteristicValueSuccess(thread_id, request_id, | |
| 408 value)); | |
| 409 } | |
| 410 | |
| 411 void BluetoothDispatcherHost::OnCharacteristicReadValueError( | |
| 412 int thread_id, | |
| 413 int request_id, | |
| 414 device::BluetoothGattService::GattErrorCode) { | |
| 415 Send(new BluetoothMsg_ReadCharacteristicValueError( | |
| 416 thread_id, request_id, BluetoothError::NETWORK_ERROR)); | |
|
Jeffrey Yasskin
2015/06/10 21:03:45
https://webbluetoothcg.github.io/web-bluetooth/#er
ortuno
2015/06/10 22:03:01
I added a TODO here as well. The CL that introduce
Jeffrey Yasskin
2015/06/10 22:55:49
Sure.
| |
| 417 } | |
| 418 | |
| 332 } // namespace content | 419 } // namespace content |
| OLD | NEW |