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 // Kill the renderer | |
palmer
2015/07/07 21:37:16
Nit: This comment is superfluous.
ortuno
2015/07/07 22:01:52
Done.
| |
461 bad_message::ReceivedBadMessage(this, | |
462 bad_message::BDH_INVALID_CHARACTERISTIC_ID); | |
463 return; | |
464 } | |
465 const std::string& service_instance_id = characteristic_iter->second; | |
466 | |
467 auto device_iter = service_to_device_.find(service_instance_id); | |
468 | |
469 CHECK(device_iter != service_to_device_.end()); | |
470 | |
471 device::BluetoothDevice* device = | |
472 adapter_->GetDevice(device_iter->second /* device_instance_id */); | |
473 if (device == nullptr) { // See "NETWORK_ERROR Note" above. | |
474 Send(new BluetoothMsg_WriteCharacteristicValueError( | |
475 thread_id, request_id, BluetoothError::NETWORK, | |
476 kDeviceNoLongerInRange)); | |
477 return; | |
478 } | |
479 | |
480 BluetoothGattService* service = device->GetGattService(service_instance_id); | |
481 if (service == nullptr) { | |
482 Send(new BluetoothMsg_WriteCharacteristicValueError( | |
483 thread_id, request_id, BluetoothError::INVALID_STATE, | |
484 kServiceNoLongerExists)); | |
485 return; | |
486 } | |
487 | |
488 BluetoothGattCharacteristic* characteristic = | |
489 service->GetCharacteristic(characteristic_instance_id); | |
490 if (characteristic == nullptr) { | |
491 Send(new BluetoothMsg_WriteCharacteristicValueError( | |
492 thread_id, request_id, BluetoothError::INVALID_STATE, | |
493 kCharacteristicNoLongerExits)); | |
494 return; | |
495 } | |
496 characteristic->WriteRemoteCharacteristic( | |
497 value, base::Bind(&BluetoothDispatcherHost::OnWriteValueSuccess, | |
498 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), | |
499 base::Bind(&BluetoothDispatcherHost::OnWriteValueFailed, | |
500 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | |
501 } | |
502 | |
435 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 503 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
436 int thread_id, | 504 int thread_id, |
437 int request_id, | 505 int request_id, |
438 scoped_ptr<DiscoverySessionOptions> options, | 506 scoped_ptr<DiscoverySessionOptions> options, |
439 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 507 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
440 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 508 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
441 BrowserThread::PostDelayedTask( | 509 BrowserThread::PostDelayedTask( |
442 BrowserThread::UI, FROM_HERE, | 510 BrowserThread::UI, FROM_HERE, |
443 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 511 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
444 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 512 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
575 | 643 |
576 void BluetoothDispatcherHost::OnCharacteristicReadValueError( | 644 void BluetoothDispatcherHost::OnCharacteristicReadValueError( |
577 int thread_id, | 645 int thread_id, |
578 int request_id, | 646 int request_id, |
579 device::BluetoothGattService::GattErrorCode error_code) { | 647 device::BluetoothGattService::GattErrorCode error_code) { |
580 std::pair<BluetoothError, std::string> error = TranslateGATTError(error_code); | 648 std::pair<BluetoothError, std::string> error = TranslateGATTError(error_code); |
581 Send(new BluetoothMsg_ReadCharacteristicValueError( | 649 Send(new BluetoothMsg_ReadCharacteristicValueError( |
582 thread_id, request_id, error.first, error.second)); | 650 thread_id, request_id, error.first, error.second)); |
583 } | 651 } |
584 | 652 |
653 void BluetoothDispatcherHost::OnWriteValueSuccess(int thread_id, | |
654 int request_id) { | |
655 Send(new BluetoothMsg_WriteCharacteristicValueSuccess(thread_id, request_id)); | |
656 } | |
657 | |
658 void BluetoothDispatcherHost::OnWriteValueFailed( | |
659 int thread_id, | |
660 int request_id, | |
661 device::BluetoothGattService::GattErrorCode error_code) { | |
662 std::pair<BluetoothError, std::string> error = TranslateGATTError(error_code); | |
663 | |
664 Send(new BluetoothMsg_WriteCharacteristicValueError( | |
665 thread_id, request_id, error.first, error.second)); | |
666 } | |
667 | |
585 } // namespace content | 668 } // namespace content |
OLD | NEW |