Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Side by Side Diff: content/browser/bluetooth/web_bluetooth_service_impl.cc

Issue 2466223002: Implement WebBluetooth getDescriptor[s] (Closed)
Patch Set: Implement WebBluetooth getDescriptor[s] Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 // ID Not In Map Note: 5 // ID Not In Map Note: A service, characteristic, or descriptor ID not in the
6 // A service, characteristic, or descriptor ID not in the corresponding 6 // corresponding WebBluetoothServiceImpl map [service_id_to_device_address_,
7 // WebBluetoothServiceImpl map [service_id_to_device_address_, 7 // characteristic_id_to_service_id_, descriptor_id_to_characteristic_id_]
8 // characteristic_id_to_service_id_, descriptor_to_characteristic_] implies a 8 // implies a hostile renderer because a renderer obtains the corresponding ID
9 // hostile renderer because a renderer obtains the corresponding ID from this 9 // from this class and it will be added to the map at that time.
10 // class and it will be added to the map at that time.
11 10
12 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" 11 #include "content/browser/bluetooth/web_bluetooth_service_impl.h"
13 12
14 #include <algorithm> 13 #include <algorithm>
15 14
16 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
18 #include "content/browser/bluetooth/bluetooth_blocklist.h" 17 #include "content/browser/bluetooth/bluetooth_blocklist.h"
19 #include "content/browser/bluetooth/bluetooth_device_chooser_controller.h" 18 #include "content/browser/bluetooth/bluetooth_device_chooser_controller.h"
20 #include "content/browser/bluetooth/bluetooth_metrics.h" 19 #include "content/browser/bluetooth/bluetooth_metrics.h"
21 #include "content/browser/bluetooth/frame_connected_bluetooth_devices.h" 20 #include "content/browser/bluetooth/frame_connected_bluetooth_devices.h"
22 #include "content/browser/renderer_host/render_process_host_impl.h" 21 #include "content/browser/renderer_host/render_process_host_impl.h"
23 #include "content/common/bluetooth/web_bluetooth_device_id.h" 22 #include "content/common/bluetooth/web_bluetooth_device_id.h"
24 #include "content/public/browser/browser_thread.h" 23 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/navigation_handle.h" 24 #include "content/public/browser/navigation_handle.h"
26 #include "content/public/browser/render_frame_host.h" 25 #include "content/public/browser/render_frame_host.h"
27 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
28 #include "device/bluetooth/bluetooth_adapter_factory_wrapper.h" 27 #include "device/bluetooth/bluetooth_adapter_factory_wrapper.h"
29 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" 28 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
29 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h"
30 30
31 using device::BluetoothAdapterFactoryWrapper; 31 using device::BluetoothAdapterFactoryWrapper;
32 using device::BluetoothUUID; 32 using device::BluetoothUUID;
33 33
34 namespace content { 34 namespace content {
35 35
36 namespace { 36 namespace {
37 37
38 blink::mojom::WebBluetoothResult TranslateConnectErrorAndRecord( 38 blink::mojom::WebBluetoothResult TranslateConnectErrorAndRecord(
39 device::BluetoothDevice::ConnectErrorCode error_code) { 39 device::BluetoothDevice::ConnectErrorCode error_code) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 UMAGATTOperationOutcome::NOT_PAIRED); 122 UMAGATTOperationOutcome::NOT_PAIRED);
123 return blink::mojom::WebBluetoothResult::GATT_NOT_PAIRED; 123 return blink::mojom::WebBluetoothResult::GATT_NOT_PAIRED;
124 case device::BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED: 124 case device::BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED:
125 RecordGATTOperationOutcome(operation, 125 RecordGATTOperationOutcome(operation,
126 UMAGATTOperationOutcome::NOT_SUPPORTED); 126 UMAGATTOperationOutcome::NOT_SUPPORTED);
127 return blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED; 127 return blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED;
128 } 128 }
129 NOTREACHED(); 129 NOTREACHED();
130 return blink::mojom::WebBluetoothResult::GATT_UNTRANSLATED_ERROR_CODE; 130 return blink::mojom::WebBluetoothResult::GATT_UNTRANSLATED_ERROR_CODE;
131 } 131 }
132
133 } // namespace 132 } // namespace
134 133
135 // Struct that holds the result of a cache query. 134 // Struct that holds the result of a cache query.
136 struct CacheQueryResult { 135 struct CacheQueryResult {
137 CacheQueryResult() : outcome(CacheQueryOutcome::SUCCESS) {} 136 CacheQueryResult() : outcome(CacheQueryOutcome::SUCCESS) {}
138 137
139 explicit CacheQueryResult(CacheQueryOutcome outcome) : outcome(outcome) {} 138 explicit CacheQueryResult(CacheQueryOutcome outcome) : outcome(outcome) {}
140 139
141 ~CacheQueryResult() {} 140 ~CacheQueryResult() {}
142 141
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 RecordGetCharacteristicsOutcome( 512 RecordGetCharacteristicsOutcome(
514 quantity, characteristics_uuid 513 quantity, characteristics_uuid
515 ? UMAGetCharacteristicOutcome::NOT_FOUND 514 ? UMAGetCharacteristicOutcome::NOT_FOUND
516 : UMAGetCharacteristicOutcome::NO_CHARACTERISTICS); 515 : UMAGetCharacteristicOutcome::NO_CHARACTERISTICS);
517 callback.Run(characteristics_uuid 516 callback.Run(characteristics_uuid
518 ? blink::mojom::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND 517 ? blink::mojom::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND
519 : blink::mojom::WebBluetoothResult::NO_CHARACTERISTICS_FOUND, 518 : blink::mojom::WebBluetoothResult::NO_CHARACTERISTICS_FOUND,
520 base::nullopt /* characteristics */); 519 base::nullopt /* characteristics */);
521 } 520 }
522 521
522 void WebBluetoothServiceImpl::RemoteCharacteristicGetDescriptors(
523 const std::string& characteristic_instance_id,
524 blink::mojom::WebBluetoothGATTQueryQuantity quantity,
525 const base::Optional<BluetoothUUID>& descriptors_uuid,
526 const RemoteCharacteristicGetDescriptorsCallback& callback) {
527 DCHECK_CURRENTLY_ON(BrowserThread::UI);
528 if (descriptors_uuid &&
529 BluetoothBlocklist::Get().IsExcluded(descriptors_uuid.value())) {
530 callback.Run(blink::mojom::WebBluetoothResult::BLOCKLISTED_DESCRIPTOR_UUID,
531 base::nullopt /* descriptor */);
532 return;
533 }
534
535 const CacheQueryResult query_result =
536 QueryCacheForCharacteristic(characteristic_instance_id);
537
538 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
539 return;
540 }
541
542 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
543 callback.Run(query_result.GetWebResult(), base::nullopt /* descriptor */);
544 return;
545 }
546
547 auto descriptors =
548 descriptors_uuid
549 ? query_result.device->GetDescriptorsByUUID(
550 query_result.characteristic, descriptors_uuid.value())
551 : query_result.characteristic->GetDescriptors();
552
553 std::vector<blink::mojom::WebBluetoothRemoteGATTDescriptorPtr>
554 response_descriptors;
555 for (device::BluetoothRemoteGattDescriptor* descriptor : descriptors) {
556 if (BluetoothBlocklist::Get().IsExcluded(descriptor->GetUUID())) {
557 continue;
558 }
559 std::string descriptor_instance_id = descriptor->GetIdentifier();
560 auto insert_result = descriptor_id_to_characteristic_id_.insert(
561 {descriptor_instance_id, characteristic_instance_id});
562 // If value is already in map, DCHECK it's valid.
563 if (!insert_result.second)
564 DCHECK(insert_result.first->second == characteristic_instance_id);
565
566 auto descriptor_ptr(blink::mojom::WebBluetoothRemoteGATTDescriptor::New());
567 descriptor_ptr->instance_id = descriptor_instance_id;
568 descriptor_ptr->uuid = descriptor->GetUUID().canonical_value();
569 response_descriptors.push_back(std::move(descriptor_ptr));
570
571 if (quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE) {
572 break;
573 }
574 }
575
576 if (!response_descriptors.empty()) {
577 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
578 std::move(response_descriptors));
579 return;
580 }
581
582 callback.Run(descriptors_uuid
583 ? blink::mojom::WebBluetoothResult::DESCRIPTOR_NOT_FOUND
584 : blink::mojom::WebBluetoothResult::NO_DESCRIPTORS_FOUND,
585 base::nullopt /* descriptors */);
586 }
587
523 void WebBluetoothServiceImpl::RemoteCharacteristicReadValue( 588 void WebBluetoothServiceImpl::RemoteCharacteristicReadValue(
524 const std::string& characteristic_instance_id, 589 const std::string& characteristic_instance_id,
525 const RemoteCharacteristicReadValueCallback& callback) { 590 const RemoteCharacteristicReadValueCallback& callback) {
526 DCHECK_CURRENTLY_ON(BrowserThread::UI); 591 DCHECK_CURRENTLY_ON(BrowserThread::UI);
527 RecordWebBluetoothFunctionCall( 592 RecordWebBluetoothFunctionCall(
528 UMAWebBluetoothFunction::CHARACTERISTIC_READ_VALUE); 593 UMAWebBluetoothFunction::CHARACTERISTIC_READ_VALUE);
529 594
530 const CacheQueryResult query_result = 595 const CacheQueryResult query_result =
531 QueryCacheForCharacteristic(characteristic_instance_id); 596 QueryCacheForCharacteristic(characteristic_instance_id);
532 597
533 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { 598 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
534 return; 599 return;
535 } 600 }
536 601
537 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { 602 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
538 RecordCharacteristicReadValueOutcome(query_result.outcome); 603 RecordCharacteristicReadValueOutcome(query_result.outcome);
539 callback.Run(query_result.GetWebResult(), base::nullopt /* value */); 604 callback.Run(query_result.GetWebResult(), base::nullopt /* value */);
540 return; 605 return;
541 } 606 }
542 607
543 if (BluetoothBlocklist::Get().IsExcludedFromReads( 608 if (BluetoothBlocklist::Get().IsExcludedFromReads(
544 query_result.characteristic->GetUUID())) { 609 query_result.characteristic->GetUUID())) {
545 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::BLOCKLISTED); 610 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::BLOCKLISTED);
546 callback.Run(blink::mojom::WebBluetoothResult::BLOCKLISTED_READ, 611 callback.Run(blink::mojom::WebBluetoothResult::BLOCKLISTED_READ,
547 base::nullopt /* value */); 612 base::nullopt /* value */);
548 return; 613 return;
549 } 614 }
550 615
551 query_result.characteristic->ReadRemoteCharacteristic( 616 query_result.characteristic->ReadRemoteCharacteristic(
552 base::Bind(&WebBluetoothServiceImpl::OnReadValueSuccess, 617 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess,
553 weak_ptr_factory_.GetWeakPtr(), callback), 618 weak_ptr_factory_.GetWeakPtr(), callback),
554 base::Bind(&WebBluetoothServiceImpl::OnReadValueFailed, 619 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicReadValueFailed,
555 weak_ptr_factory_.GetWeakPtr(), callback)); 620 weak_ptr_factory_.GetWeakPtr(), callback));
556 } 621 }
557 622
558 void WebBluetoothServiceImpl::RemoteCharacteristicWriteValue( 623 void WebBluetoothServiceImpl::RemoteCharacteristicWriteValue(
559 const std::string& characteristic_instance_id, 624 const std::string& characteristic_instance_id,
560 const std::vector<uint8_t>& value, 625 const std::vector<uint8_t>& value,
561 const RemoteCharacteristicWriteValueCallback& callback) { 626 const RemoteCharacteristicWriteValueCallback& callback) {
562 DCHECK_CURRENTLY_ON(BrowserThread::UI); 627 DCHECK_CURRENTLY_ON(BrowserThread::UI);
563 RecordWebBluetoothFunctionCall( 628 RecordWebBluetoothFunctionCall(
564 UMAWebBluetoothFunction::CHARACTERISTIC_WRITE_VALUE); 629 UMAWebBluetoothFunction::CHARACTERISTIC_WRITE_VALUE);
(...skipping 20 matching lines...) Expand all
585 } 650 }
586 651
587 if (BluetoothBlocklist::Get().IsExcludedFromWrites( 652 if (BluetoothBlocklist::Get().IsExcludedFromWrites(
588 query_result.characteristic->GetUUID())) { 653 query_result.characteristic->GetUUID())) {
589 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::BLOCKLISTED); 654 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::BLOCKLISTED);
590 callback.Run(blink::mojom::WebBluetoothResult::BLOCKLISTED_WRITE); 655 callback.Run(blink::mojom::WebBluetoothResult::BLOCKLISTED_WRITE);
591 return; 656 return;
592 } 657 }
593 658
594 query_result.characteristic->WriteRemoteCharacteristic( 659 query_result.characteristic->WriteRemoteCharacteristic(
595 value, base::Bind(&WebBluetoothServiceImpl::OnWriteValueSuccess, 660 value,
596 weak_ptr_factory_.GetWeakPtr(), callback), 661 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueSuccess,
597 base::Bind(&WebBluetoothServiceImpl::OnWriteValueFailed, 662 weak_ptr_factory_.GetWeakPtr(), callback),
663 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed,
598 weak_ptr_factory_.GetWeakPtr(), callback)); 664 weak_ptr_factory_.GetWeakPtr(), callback));
599 } 665 }
600 666
601 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications( 667 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications(
602 const std::string& characteristic_instance_id, 668 const std::string& characteristic_instance_id,
603 const RemoteCharacteristicStartNotificationsCallback& callback) { 669 const RemoteCharacteristicStartNotificationsCallback& callback) {
604 DCHECK_CURRENTLY_ON(BrowserThread::UI); 670 DCHECK_CURRENTLY_ON(BrowserThread::UI);
605 RecordWebBluetoothFunctionCall( 671 RecordWebBluetoothFunctionCall(
606 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); 672 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS);
607 673
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 870
805 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed( 871 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed(
806 base::TimeTicks start_time, 872 base::TimeTicks start_time,
807 const RemoteServerConnectCallback& callback, 873 const RemoteServerConnectCallback& callback,
808 device::BluetoothDevice::ConnectErrorCode error_code) { 874 device::BluetoothDevice::ConnectErrorCode error_code) {
809 DCHECK_CURRENTLY_ON(BrowserThread::UI); 875 DCHECK_CURRENTLY_ON(BrowserThread::UI);
810 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time); 876 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time);
811 callback.Run(TranslateConnectErrorAndRecord(error_code)); 877 callback.Run(TranslateConnectErrorAndRecord(error_code));
812 } 878 }
813 879
814 void WebBluetoothServiceImpl::OnReadValueSuccess( 880 void WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess(
815 const RemoteCharacteristicReadValueCallback& callback, 881 const RemoteCharacteristicReadValueCallback& callback,
816 const std::vector<uint8_t>& value) { 882 const std::vector<uint8_t>& value) {
817 DCHECK_CURRENTLY_ON(BrowserThread::UI); 883 DCHECK_CURRENTLY_ON(BrowserThread::UI);
818 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS); 884 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS);
819 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, value); 885 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, value);
820 } 886 }
821 887
822 void WebBluetoothServiceImpl::OnReadValueFailed( 888 void WebBluetoothServiceImpl::OnCharacteristicReadValueFailed(
823 const RemoteCharacteristicReadValueCallback& callback, 889 const RemoteCharacteristicReadValueCallback& callback,
824 device::BluetoothRemoteGattService::GattErrorCode error_code) { 890 device::BluetoothRemoteGattService::GattErrorCode error_code) {
825 DCHECK_CURRENTLY_ON(BrowserThread::UI); 891 DCHECK_CURRENTLY_ON(BrowserThread::UI);
826 callback.Run(TranslateGATTErrorAndRecord( 892 callback.Run(TranslateGATTErrorAndRecord(
827 error_code, UMAGATTOperation::CHARACTERISTIC_READ), 893 error_code, UMAGATTOperation::CHARACTERISTIC_READ),
828 base::nullopt /* value */); 894 base::nullopt /* value */);
829 } 895 }
830 896
831 void WebBluetoothServiceImpl::OnWriteValueSuccess( 897 void WebBluetoothServiceImpl::OnCharacteristicWriteValueSuccess(
832 const RemoteCharacteristicWriteValueCallback& callback) { 898 const RemoteCharacteristicWriteValueCallback& callback) {
833 DCHECK_CURRENTLY_ON(BrowserThread::UI); 899 DCHECK_CURRENTLY_ON(BrowserThread::UI);
834 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS); 900 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS);
835 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 901 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
836 } 902 }
837 903
838 void WebBluetoothServiceImpl::OnWriteValueFailed( 904 void WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed(
839 const RemoteCharacteristicWriteValueCallback& callback, 905 const RemoteCharacteristicWriteValueCallback& callback,
840 device::BluetoothRemoteGattService::GattErrorCode error_code) { 906 device::BluetoothRemoteGattService::GattErrorCode error_code) {
841 DCHECK_CURRENTLY_ON(BrowserThread::UI); 907 DCHECK_CURRENTLY_ON(BrowserThread::UI);
842 callback.Run(TranslateGATTErrorAndRecord( 908 callback.Run(TranslateGATTErrorAndRecord(
843 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE)); 909 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE));
844 } 910 }
845 911
846 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( 912 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess(
847 const RemoteCharacteristicStartNotificationsCallback& callback, 913 const RemoteCharacteristicStartNotificationsCallback& callback,
848 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { 914 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 binding_.Close(); 1034 binding_.Close();
969 } 1035 }
970 1036
971 url::Origin WebBluetoothServiceImpl::GetOrigin() { 1037 url::Origin WebBluetoothServiceImpl::GetOrigin() {
972 return render_frame_host_->GetLastCommittedOrigin(); 1038 return render_frame_host_->GetLastCommittedOrigin();
973 } 1039 }
974 1040
975 void WebBluetoothServiceImpl::ClearState() { 1041 void WebBluetoothServiceImpl::ClearState() {
976 characteristic_id_to_notify_session_.clear(); 1042 characteristic_id_to_notify_session_.clear();
977 pending_primary_services_requests_.clear(); 1043 pending_primary_services_requests_.clear();
1044 descriptor_id_to_characteristic_id_.clear();
978 characteristic_id_to_service_id_.clear(); 1045 characteristic_id_to_service_id_.clear();
979 service_id_to_device_address_.clear(); 1046 service_id_to_device_address_.clear();
980 connected_devices_.reset( 1047 connected_devices_.reset(
981 new FrameConnectedBluetoothDevices(render_frame_host_)); 1048 new FrameConnectedBluetoothDevices(render_frame_host_));
982 allowed_devices_map_ = BluetoothAllowedDevicesMap(); 1049 allowed_devices_map_ = BluetoothAllowedDevicesMap();
983 device_chooser_controller_.reset(); 1050 device_chooser_controller_.reset();
984 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); 1051 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this);
985 } 1052 }
986 1053
987 } // namespace content 1054 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698