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 // NETWORK_ERROR Note: | 5 // NETWORK_ERROR Note: |
6 // When a device can't be found in the BluetoothAdapter, that generally | 6 // When a device can't be found in the BluetoothAdapter, that generally |
7 // indicates that it's gone out of range. We reject with a NetworkError in that | 7 // indicates that it's gone out of range. We reject with a NetworkError in that |
8 // case. | 8 // case. |
9 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothdevice-conne
ctgatt | 9 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothdevice-conne
ctgatt |
10 | 10 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 182 |
183 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 183 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
184 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 184 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
185 bool handled = true; | 185 bool handled = true; |
186 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 186 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) |
187 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 187 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) |
188 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) | 188 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ConnectGATT, OnConnectGATT) |
189 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) | 189 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetPrimaryService, OnGetPrimaryService) |
190 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) | 190 IPC_MESSAGE_HANDLER(BluetoothHostMsg_GetCharacteristic, OnGetCharacteristic) |
191 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ReadValue, OnReadValue) | 191 IPC_MESSAGE_HANDLER(BluetoothHostMsg_ReadValue, OnReadValue) |
| 192 IPC_MESSAGE_HANDLER(BluetoothHostMsg_WriteValue, OnWriteValue) |
192 IPC_MESSAGE_UNHANDLED(handled = false) | 193 IPC_MESSAGE_UNHANDLED(handled = false) |
193 IPC_END_MESSAGE_MAP() | 194 IPC_END_MESSAGE_MAP() |
194 return handled; | 195 return handled; |
195 } | 196 } |
196 | 197 |
197 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( | 198 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( |
198 scoped_refptr<device::BluetoothAdapter> mock_adapter) { | 199 scoped_refptr<device::BluetoothAdapter> mock_adapter) { |
199 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 200 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
200 current_delay_time_ = kTestingDelayTime; | 201 current_delay_time_ = kTestingDelayTime; |
201 set_adapter(mock_adapter.Pass()); | 202 set_adapter(mock_adapter.Pass()); |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 return; | 426 return; |
426 } | 427 } |
427 | 428 |
428 characteristic->ReadRemoteCharacteristic( | 429 characteristic->ReadRemoteCharacteristic( |
429 base::Bind(&BluetoothDispatcherHost::OnCharacteristicValueRead, | 430 base::Bind(&BluetoothDispatcherHost::OnCharacteristicValueRead, |
430 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), | 431 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
431 base::Bind(&BluetoothDispatcherHost::OnCharacteristicReadValueError, | 432 base::Bind(&BluetoothDispatcherHost::OnCharacteristicReadValueError, |
432 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | 433 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
433 } | 434 } |
434 | 435 |
| 436 void BluetoothDispatcherHost::OnWriteValue( |
| 437 int thread_id, |
| 438 int request_id, |
| 439 const std::string& characteristic_instance_id, |
| 440 const std::vector<uint8_t>& value) { |
| 441 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 442 |
| 443 // Length check per step 3 of writeValue algorithm: |
| 444 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattcharac
teristic-writevalue |
| 445 // We perform the length check on the renderer side. So if we |
| 446 // get a value with length > 512, we can assume it's a hostile |
| 447 // renderer and kill it. |
| 448 if (value.size() > 512) { |
| 449 bad_message::ReceivedBadMessage( |
| 450 this, bad_message::BDH_INVALID_WRITE_VALUE_LENGTH); |
| 451 return; |
| 452 } |
| 453 |
| 454 auto characteristic_iter = |
| 455 characteristic_to_service_.find(characteristic_instance_id); |
| 456 // A characteristic_instance_id not in the map implies a hostile renderer |
| 457 // because a renderer obtains the characteristic id from this class and |
| 458 // it will be added to the map at that time. |
| 459 if (characteristic_iter == characteristic_to_service_.end()) { |
| 460 bad_message::ReceivedBadMessage(this, |
| 461 bad_message::BDH_INVALID_CHARACTERISTIC_ID); |
| 462 return; |
| 463 } |
| 464 const std::string& service_instance_id = characteristic_iter->second; |
| 465 |
| 466 auto device_iter = service_to_device_.find(service_instance_id); |
| 467 |
| 468 CHECK(device_iter != service_to_device_.end()); |
| 469 |
| 470 device::BluetoothDevice* device = |
| 471 adapter_->GetDevice(device_iter->second /* device_instance_id */); |
| 472 if (device == nullptr) { // See "NETWORK_ERROR Note" above. |
| 473 Send(new BluetoothMsg_WriteCharacteristicValueError( |
| 474 thread_id, request_id, BluetoothError::NETWORK, |
| 475 kDeviceNoLongerInRange)); |
| 476 return; |
| 477 } |
| 478 |
| 479 BluetoothGattService* service = device->GetGattService(service_instance_id); |
| 480 if (service == nullptr) { |
| 481 Send(new BluetoothMsg_WriteCharacteristicValueError( |
| 482 thread_id, request_id, BluetoothError::INVALID_STATE, |
| 483 kServiceNoLongerExists)); |
| 484 return; |
| 485 } |
| 486 |
| 487 BluetoothGattCharacteristic* characteristic = |
| 488 service->GetCharacteristic(characteristic_instance_id); |
| 489 if (characteristic == nullptr) { |
| 490 Send(new BluetoothMsg_WriteCharacteristicValueError( |
| 491 thread_id, request_id, BluetoothError::INVALID_STATE, |
| 492 kCharacteristicNoLongerExits)); |
| 493 return; |
| 494 } |
| 495 characteristic->WriteRemoteCharacteristic( |
| 496 value, base::Bind(&BluetoothDispatcherHost::OnWriteValueSuccess, |
| 497 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
| 498 base::Bind(&BluetoothDispatcherHost::OnWriteValueFailed, |
| 499 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
| 500 } |
| 501 |
435 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 502 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
436 int thread_id, | 503 int thread_id, |
437 int request_id, | 504 int request_id, |
438 scoped_ptr<DiscoverySessionOptions> options, | 505 scoped_ptr<DiscoverySessionOptions> options, |
439 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 506 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
440 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 507 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
441 BrowserThread::PostDelayedTask( | 508 BrowserThread::PostDelayedTask( |
442 BrowserThread::UI, FROM_HERE, | 509 BrowserThread::UI, FROM_HERE, |
443 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 510 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
444 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 511 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 | 642 |
576 void BluetoothDispatcherHost::OnCharacteristicReadValueError( | 643 void BluetoothDispatcherHost::OnCharacteristicReadValueError( |
577 int thread_id, | 644 int thread_id, |
578 int request_id, | 645 int request_id, |
579 device::BluetoothGattService::GattErrorCode error_code) { | 646 device::BluetoothGattService::GattErrorCode error_code) { |
580 std::pair<BluetoothError, std::string> error = TranslateGATTError(error_code); | 647 std::pair<BluetoothError, std::string> error = TranslateGATTError(error_code); |
581 Send(new BluetoothMsg_ReadCharacteristicValueError( | 648 Send(new BluetoothMsg_ReadCharacteristicValueError( |
582 thread_id, request_id, error.first, error.second)); | 649 thread_id, request_id, error.first, error.second)); |
583 } | 650 } |
584 | 651 |
| 652 void BluetoothDispatcherHost::OnWriteValueSuccess(int thread_id, |
| 653 int request_id) { |
| 654 Send(new BluetoothMsg_WriteCharacteristicValueSuccess(thread_id, request_id)); |
| 655 } |
| 656 |
| 657 void BluetoothDispatcherHost::OnWriteValueFailed( |
| 658 int thread_id, |
| 659 int request_id, |
| 660 device::BluetoothGattService::GattErrorCode error_code) { |
| 661 std::pair<BluetoothError, std::string> error = TranslateGATTError(error_code); |
| 662 |
| 663 Send(new BluetoothMsg_WriteCharacteristicValueError( |
| 664 thread_id, request_id, error.first, error.second)); |
| 665 } |
| 666 |
585 } // namespace content | 667 } // namespace content |
OLD | NEW |