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

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

Issue 2466223002: Implement WebBluetooth getDescriptor[s] (Closed)
Patch Set: Ensure that we throw a kGattServerNotConnected error if getDescriptor[s] is called while not connec… Created 4 years, 1 month 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_blacklist.h" 17 #include "content/browser/bluetooth/bluetooth_blacklist.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 std::vector<device::BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID(
133 device::BluetoothRemoteGattCharacteristic* characteristic,
134 const BluetoothUUID& descriptor_uuid) {
135 std::vector<device::BluetoothRemoteGattDescriptor*> descriptors;
136 VLOG(1) << "Looking for descriptor: " << descriptor_uuid.canonical_value();
137 for (device::BluetoothRemoteGattDescriptor* descriptor :
138 characteristic->GetDescriptors()) {
139 VLOG(1) << "Descriptor in cache: "
140 << descriptor->GetUUID().canonical_value();
141 if (descriptor->GetUUID() == descriptor_uuid) {
142 descriptors.push_back(descriptor);
143 }
144 }
145 return descriptors;
146 }
132 147
133 // TODO(ortuno): This should really be a BluetoothDevice method. 148 // TODO(ortuno): This should really be a BluetoothDevice method.
134 // Replace when implemented. http://crbug.com/552022 149 // Replace when implemented. http://crbug.com/552022
135 std::vector<device::BluetoothRemoteGattCharacteristic*> 150 std::vector<device::BluetoothRemoteGattCharacteristic*>
136 GetCharacteristicsByUUID(device::BluetoothRemoteGattService* service, 151 GetCharacteristicsByUUID(device::BluetoothRemoteGattService* service,
137 const BluetoothUUID& characteristic_uuid) { 152 const BluetoothUUID& characteristic_uuid) {
138 std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics; 153 std::vector<device::BluetoothRemoteGattCharacteristic*> characteristics;
139 VLOG(1) << "Looking for characteristic: " 154 VLOG(1) << "Looking for characteristic: "
140 << characteristic_uuid.canonical_value(); 155 << characteristic_uuid.canonical_value();
141 for (device::BluetoothRemoteGattCharacteristic* characteristic : 156 for (device::BluetoothRemoteGattCharacteristic* characteristic :
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 323
309 void WebBluetoothServiceImpl::NotifyCharacteristicValueChanged( 324 void WebBluetoothServiceImpl::NotifyCharacteristicValueChanged(
310 const std::string& characteristic_instance_id, 325 const std::string& characteristic_instance_id,
311 std::vector<uint8_t> value) { 326 std::vector<uint8_t> value) {
312 if (client_) { 327 if (client_) {
313 client_->RemoteCharacteristicValueChanged( 328 client_->RemoteCharacteristicValueChanged(
314 characteristic_instance_id, mojo::Array<uint8_t>(std::move(value))); 329 characteristic_instance_id, mojo::Array<uint8_t>(std::move(value)));
315 } 330 }
316 } 331 }
317 332
333 void WebBluetoothServiceImpl::GattDescriptorValueChanged(
334 device::BluetoothAdapter* adapter,
335 device::BluetoothRemoteGattDescriptor* descriptor,
336 const std::vector<uint8_t>& value) {
337 // Don't notify of descriptors that we haven't returned.
338 if (!base::ContainsKey(descriptor_id_to_characteristic_id_,
339 descriptor->GetIdentifier())) {
340 return;
341 }
342 if (!base::ThreadTaskRunnerHandle::Get()->PostTask(
343 FROM_HERE,
344 base::Bind(&WebBluetoothServiceImpl::NotifyDescriptorValueChanged,
345 weak_ptr_factory_.GetWeakPtr(),
346 descriptor->GetIdentifier(), value))) {
347 LOG(WARNING) << "No TaskRunner.";
348 }
349 }
350
351 void WebBluetoothServiceImpl::NotifyDescriptorValueChanged(
352 const std::string& descriptor_instance_id,
353 std::vector<uint8_t> value) {
354 if (client_) {
355 client_->RemoteDescriptorValueChanged(
356 descriptor_instance_id, mojo::Array<uint8_t>(std::move(value)));
357 }
358 }
359
318 void WebBluetoothServiceImpl::SetClient( 360 void WebBluetoothServiceImpl::SetClient(
319 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) { 361 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) {
320 DCHECK(!client_.get()); 362 DCHECK(!client_.get());
321 client_.Bind(std::move(client)); 363 client_.Bind(std::move(client));
322 } 364 }
323 365
324 void WebBluetoothServiceImpl::RequestDevice( 366 void WebBluetoothServiceImpl::RequestDevice(
325 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, 367 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options,
326 const RequestDeviceCallback& callback) { 368 const RequestDeviceCallback& callback) {
327 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); 369 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE);
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 RecordGetCharacteristicsOutcome( 566 RecordGetCharacteristicsOutcome(
525 quantity, characteristics_uuid 567 quantity, characteristics_uuid
526 ? UMAGetCharacteristicOutcome::NOT_FOUND 568 ? UMAGetCharacteristicOutcome::NOT_FOUND
527 : UMAGetCharacteristicOutcome::NO_CHARACTERISTICS); 569 : UMAGetCharacteristicOutcome::NO_CHARACTERISTICS);
528 callback.Run(characteristics_uuid 570 callback.Run(characteristics_uuid
529 ? blink::mojom::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND 571 ? blink::mojom::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND
530 : blink::mojom::WebBluetoothResult::NO_CHARACTERISTICS_FOUND, 572 : blink::mojom::WebBluetoothResult::NO_CHARACTERISTICS_FOUND,
531 nullptr /* characteristics */); 573 nullptr /* characteristics */);
532 } 574 }
533 575
576 void WebBluetoothServiceImpl::RemoteCharacteristicGetDescriptors(
577 const mojo::String& characteristic_instance_id,
578 blink::mojom::WebBluetoothGATTQueryQuantity quantity,
579 const base::Optional<BluetoothUUID>& descriptors_uuid,
580 const RemoteCharacteristicGetDescriptorsCallback& callback) {
581 DCHECK_CURRENTLY_ON(BrowserThread::UI);
582
583 /* todo dft
ortuno 2016/11/21 03:34:08 TODO(crbug.com/[new bug number]): Record descripto
dougt 2016/11/22 01:47:15 Acknowledged.
584 RecordWebBluetoothFunctionCall(
585 quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE
586 ? UMAWebBluetoothFunction::SERVICE_GET_CHARACTERISTIC
587 : UMAWebBluetoothFunction::SERVICE_GET_CHARACTERISTICS);
588 RecordGetCharacteristicsCharacteristic(quantity, characteristics_uuid);
589 */
590 if (descriptors_uuid &&
591 BluetoothBlacklist::Get().IsExcluded(descriptors_uuid.value())) {
592 /*
593 todo DFT
ortuno 2016/11/21 03:34:08 TODO(crbug.com/[new bug number]): Record descripto
dougt 2016/11/22 01:47:15 Acknowledged.
594 RecordGetCharacteristicsOutcome(quantity,
595 UMAGetCharacteristicOutcome::BLACKLISTED);
596 */
597 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_DESCRIPTOR_UUID,
598 nullptr /* descriptor */);
599 return;
600 }
601
602 const CacheQueryResult query_result =
603 QueryCacheForCharacteristic(characteristic_instance_id);
604
605 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
606 return;
607 }
608
609 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
610 // TODO dft. RecordGetCharacteristicsOutcome(quantity,
ortuno 2016/11/21 03:34:08 Same format as above.
dougt 2016/11/22 01:47:16 Acknowledged.
611 // query_result.outcome);
612 callback.Run(query_result.GetWebResult(), nullptr /* descriptor */);
613 return;
614 }
615
616 std::vector<device::BluetoothRemoteGattDescriptor*> descriptors =
617 descriptors_uuid ? GetDescriptorsByUUID(query_result.characteristic,
618 descriptors_uuid.value())
619 : query_result.characteristic->GetDescriptors();
620
621 mojo::Array<blink::mojom::WebBluetoothRemoteGATTDescriptorPtr>
622 response_descriptors;
623 for (device::BluetoothRemoteGattDescriptor* descriptor : descriptors) {
624 if (BluetoothBlacklist::Get().IsExcluded(descriptor->GetUUID())) {
625 continue;
626 }
627 std::string descriptor_instance_id = descriptor->GetIdentifier();
628
629 auto insert_result = descriptor_id_to_characteristic_id_.insert(
ortuno 2016/11/21 03:34:08 .insert({descriptor_instance_id, characteristic_in
dougt 2016/11/22 01:47:15 Acknowledged.
630 std::make_pair(descriptor_instance_id, characteristic_instance_id));
631 // If value is already in map, DCHECK it's valid.
632 if (!insert_result.second)
633 DCHECK(insert_result.first->second == characteristic_instance_id);
634
635 blink::mojom::WebBluetoothRemoteGATTDescriptorPtr descriptor_ptr =
636 blink::mojom::WebBluetoothRemoteGATTDescriptor::New();
637
638 descriptor_ptr->instance_id = mojo::String(descriptor_instance_id);
ortuno 2016/11/21 03:34:08 No need for mojo::String
dougt 2016/11/22 01:47:15 Acknowledged.
639 descriptor_ptr->uuid = descriptor->GetUUID().canonical_value();
640 response_descriptors.push_back(std::move(descriptor_ptr));
641
642 if (quantity == blink::mojom::WebBluetoothGATTQueryQuantity::SINGLE) {
643 break;
644 }
645 }
646
647 if (!response_descriptors.empty()) {
648 /* TODO dft
ortuno 2016/11/21 03:34:08 Same format as above. No need for the commented ou
dougt 2016/11/22 01:47:15 Acknowledged.
649 RecordGetCharacteristicsOutcome(quantity,
650 UMAGetCharacteristicOutcome::SUCCESS);
651 */
652 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
653 std::move(response_descriptors));
654 return;
655 }
656
657 /* TODO dft
ortuno 2016/11/21 03:34:08 Same format as above. No need for the commented ou
dougt 2016/11/22 01:47:15 Acknowledged.
658 RecordGetCharacteristicsOutcome(
659 quantity, characteristics_uuid
660 ? UMAGetCharacteristicOutcome::NOT_FOUND
661 : UMAGetCharacteristicOutcome::NO_CHARACTERISTICS);
662 */
663 callback.Run(descriptors_uuid
664 ? blink::mojom::WebBluetoothResult::DESCRIPTOR_NOT_FOUND
665 : blink::mojom::WebBluetoothResult::NO_DESCRIPTORS_FOUND,
666 nullptr /* descriptors */);
667 }
668
669 void WebBluetoothServiceImpl::RemoteDescriptorReadValue(
670 const mojo::String& descriptor_instance_id,
671 const RemoteDescriptorReadValueCallback& callback) {
672 DCHECK_CURRENTLY_ON(BrowserThread::UI);
673
674 // TODO dft
ortuno 2016/11/21 03:34:08 Same format as above. Same for all instances in th
dougt 2016/11/22 01:47:15 Acknowledged.
675 // RecordWebBluetoothFunctionCall(
676 // UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE);
677
678 const CacheQueryResult query_result =
679 QueryCacheForDescriptor(descriptor_instance_id);
680
681 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
682 return;
683 }
684
685 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
686 // TODO dft RecordDescriptorReadValueOutcome(query_result.outcome);
687 callback.Run(query_result.GetWebResult(), nullptr /* value */);
688 return;
689 }
690
691 if (BluetoothBlacklist::Get().IsExcludedFromReads(
692 query_result.descriptor->GetUUID())) {
693 // TODO dft
694 // RecordDescriptorReadValueOutcome(UMAGATTOperationOutcome::BLACKLISTED);
695 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_READ,
696 nullptr /* value */);
697 return;
698 }
699
700 query_result.descriptor->ReadRemoteDescriptor(
701 base::Bind(&WebBluetoothServiceImpl::OnDescriptorReadValueSuccess,
702 weak_ptr_factory_.GetWeakPtr(), callback),
703 base::Bind(&WebBluetoothServiceImpl::OnDescriptorReadValueFailed,
704 weak_ptr_factory_.GetWeakPtr(), callback));
705 }
706
707 void WebBluetoothServiceImpl::RemoteDescriptorWriteValue(
708 const mojo::String& descriptor_instance_id,
709 mojo::Array<uint8_t> value,
710 const RemoteDescriptorWriteValueCallback& callback) {
711 DCHECK_CURRENTLY_ON(BrowserThread::UI);
712 // TODO dft RecordWebBluetoothFunctionCall(
713 // UMAWebBluetoothFunction::DESCRIPTOR_WRITE_VALUE);
714
715 // We perform the length check on the renderer side. So if we
716 // get a value with length > 512, we can assume it's a hostile
717 // renderer and kill it.
718 if (value.size() > 512) {
719 CrashRendererAndClosePipe(bad_message::BDH_INVALID_WRITE_VALUE_LENGTH);
720 return;
721 }
722
723 const CacheQueryResult query_result =
724 QueryCacheForDescriptor(descriptor_instance_id);
725
726 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
727 return;
728 }
729
730 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
731 // TODO dft RecordDescriptorWriteValueOutcome(query_result.outcome);
732 callback.Run(query_result.GetWebResult());
733 return;
734 }
735
736 if (BluetoothBlacklist::Get().IsExcludedFromWrites(
737 query_result.descriptor->GetUUID())) {
738 // TODO dft
739 // RecordDescriptorWriteValueOutcome(UMAGATTOperationOutcome::BLACKLISTED);
740 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_WRITE);
741 return;
742 }
743
744 query_result.descriptor->WriteRemoteDescriptor(
745 value.To<std::vector<uint8_t>>(),
746 base::Bind(&WebBluetoothServiceImpl::OnDescriptorWriteValueSuccess,
747 weak_ptr_factory_.GetWeakPtr(), callback),
748 base::Bind(&WebBluetoothServiceImpl::OnDescriptorWriteValueFailed,
749 weak_ptr_factory_.GetWeakPtr(), callback));
750 }
751
534 void WebBluetoothServiceImpl::RemoteCharacteristicReadValue( 752 void WebBluetoothServiceImpl::RemoteCharacteristicReadValue(
535 const mojo::String& characteristic_instance_id, 753 const mojo::String& characteristic_instance_id,
536 const RemoteCharacteristicReadValueCallback& callback) { 754 const RemoteCharacteristicReadValueCallback& callback) {
537 DCHECK_CURRENTLY_ON(BrowserThread::UI); 755 DCHECK_CURRENTLY_ON(BrowserThread::UI);
538 RecordWebBluetoothFunctionCall( 756 RecordWebBluetoothFunctionCall(
539 UMAWebBluetoothFunction::CHARACTERISTIC_READ_VALUE); 757 UMAWebBluetoothFunction::CHARACTERISTIC_READ_VALUE);
540 758
541 const CacheQueryResult query_result = 759 const CacheQueryResult query_result =
542 QueryCacheForCharacteristic(characteristic_instance_id); 760 QueryCacheForCharacteristic(characteristic_instance_id);
543 761
544 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { 762 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
545 return; 763 return;
546 } 764 }
547 765
548 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { 766 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
549 RecordCharacteristicReadValueOutcome(query_result.outcome); 767 RecordCharacteristicReadValueOutcome(query_result.outcome);
550 callback.Run(query_result.GetWebResult(), nullptr /* value */); 768 callback.Run(query_result.GetWebResult(), nullptr /* value */);
551 return; 769 return;
552 } 770 }
553 771
554 if (BluetoothBlacklist::Get().IsExcludedFromReads( 772 if (BluetoothBlacklist::Get().IsExcludedFromReads(
555 query_result.characteristic->GetUUID())) { 773 query_result.characteristic->GetUUID())) {
556 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::BLACKLISTED); 774 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::BLACKLISTED);
557 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_READ, 775 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_READ,
558 nullptr /* value */); 776 nullptr /* value */);
559 return; 777 return;
560 } 778 }
561 779
562 query_result.characteristic->ReadRemoteCharacteristic( 780 query_result.characteristic->ReadRemoteCharacteristic(
563 base::Bind(&WebBluetoothServiceImpl::OnReadValueSuccess, 781 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess,
564 weak_ptr_factory_.GetWeakPtr(), callback), 782 weak_ptr_factory_.GetWeakPtr(), callback),
565 base::Bind(&WebBluetoothServiceImpl::OnReadValueFailed, 783 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicReadValueFailed,
566 weak_ptr_factory_.GetWeakPtr(), callback)); 784 weak_ptr_factory_.GetWeakPtr(), callback));
567 } 785 }
568 786
569 void WebBluetoothServiceImpl::RemoteCharacteristicWriteValue( 787 void WebBluetoothServiceImpl::RemoteCharacteristicWriteValue(
570 const mojo::String& characteristic_instance_id, 788 const mojo::String& characteristic_instance_id,
571 mojo::Array<uint8_t> value, 789 mojo::Array<uint8_t> value,
572 const RemoteCharacteristicWriteValueCallback& callback) { 790 const RemoteCharacteristicWriteValueCallback& callback) {
573 DCHECK_CURRENTLY_ON(BrowserThread::UI); 791 DCHECK_CURRENTLY_ON(BrowserThread::UI);
574 RecordWebBluetoothFunctionCall( 792 RecordWebBluetoothFunctionCall(
575 UMAWebBluetoothFunction::CHARACTERISTIC_WRITE_VALUE); 793 UMAWebBluetoothFunction::CHARACTERISTIC_WRITE_VALUE);
(...skipping 21 matching lines...) Expand all
597 815
598 if (BluetoothBlacklist::Get().IsExcludedFromWrites( 816 if (BluetoothBlacklist::Get().IsExcludedFromWrites(
599 query_result.characteristic->GetUUID())) { 817 query_result.characteristic->GetUUID())) {
600 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::BLACKLISTED); 818 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::BLACKLISTED);
601 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_WRITE); 819 callback.Run(blink::mojom::WebBluetoothResult::BLACKLISTED_WRITE);
602 return; 820 return;
603 } 821 }
604 822
605 query_result.characteristic->WriteRemoteCharacteristic( 823 query_result.characteristic->WriteRemoteCharacteristic(
606 value.To<std::vector<uint8_t>>(), 824 value.To<std::vector<uint8_t>>(),
607 base::Bind(&WebBluetoothServiceImpl::OnWriteValueSuccess, 825 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueSuccess,
608 weak_ptr_factory_.GetWeakPtr(), callback), 826 weak_ptr_factory_.GetWeakPtr(), callback),
609 base::Bind(&WebBluetoothServiceImpl::OnWriteValueFailed, 827 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed,
610 weak_ptr_factory_.GetWeakPtr(), callback)); 828 weak_ptr_factory_.GetWeakPtr(), callback));
611 } 829 }
612 830
613 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications( 831 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications(
614 const mojo::String& characteristic_instance_id, 832 const mojo::String& characteristic_instance_id,
615 const RemoteCharacteristicStartNotificationsCallback& callback) { 833 const RemoteCharacteristicStartNotificationsCallback& callback) {
616 DCHECK_CURRENTLY_ON(BrowserThread::UI); 834 DCHECK_CURRENTLY_ON(BrowserThread::UI);
617 RecordWebBluetoothFunctionCall( 835 RecordWebBluetoothFunctionCall(
618 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); 836 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS);
619 837
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 1035
818 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed( 1036 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed(
819 base::TimeTicks start_time, 1037 base::TimeTicks start_time,
820 const RemoteServerConnectCallback& callback, 1038 const RemoteServerConnectCallback& callback,
821 device::BluetoothDevice::ConnectErrorCode error_code) { 1039 device::BluetoothDevice::ConnectErrorCode error_code) {
822 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1040 DCHECK_CURRENTLY_ON(BrowserThread::UI);
823 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time); 1041 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time);
824 callback.Run(TranslateConnectErrorAndRecord(error_code)); 1042 callback.Run(TranslateConnectErrorAndRecord(error_code));
825 } 1043 }
826 1044
827 void WebBluetoothServiceImpl::OnReadValueSuccess( 1045 void WebBluetoothServiceImpl::OnDescriptorReadValueSuccess(
1046 const RemoteDescriptorReadValueCallback& callback,
1047 const std::vector<uint8_t>& value) {
1048 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1049 // TODO dft
1050 // RecordDescriptorReadValueOutcome(UMAGATTOperationOutcome::SUCCESS);
1051 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
1052 mojo::Array<uint8_t>::From(value));
1053 }
1054
1055 void WebBluetoothServiceImpl::OnDescriptorReadValueFailed(
1056 const RemoteDescriptorReadValueCallback& callback,
1057 device::BluetoothRemoteGattService::GattErrorCode error_code) {
1058 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1059 callback.Run(TranslateGATTErrorAndRecord(error_code,
1060 UMAGATTOperation::DESCRIPTOR_READ),
1061 nullptr /* value */);
1062 }
1063
1064 void WebBluetoothServiceImpl::OnDescriptorWriteValueSuccess(
1065 const RemoteDescriptorWriteValueCallback& callback) {
1066 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1067 // TODO dft
1068 // RecordDescriptorWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS);
1069 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
1070 }
1071
1072 void WebBluetoothServiceImpl::OnDescriptorWriteValueFailed(
1073 const RemoteDescriptorWriteValueCallback& callback,
1074 device::BluetoothRemoteGattService::GattErrorCode error_code) {
1075 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1076 callback.Run(TranslateGATTErrorAndRecord(error_code,
1077 UMAGATTOperation::DESCRIPTOR_WRITE));
1078 }
1079
1080 void WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess(
828 const RemoteCharacteristicReadValueCallback& callback, 1081 const RemoteCharacteristicReadValueCallback& callback,
829 const std::vector<uint8_t>& value) { 1082 const std::vector<uint8_t>& value) {
830 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1083 DCHECK_CURRENTLY_ON(BrowserThread::UI);
831 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS); 1084 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS);
832 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, 1085 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
833 mojo::Array<uint8_t>::From(value)); 1086 mojo::Array<uint8_t>::From(value));
834 } 1087 }
835 1088
836 void WebBluetoothServiceImpl::OnReadValueFailed( 1089 void WebBluetoothServiceImpl::OnCharacteristicReadValueFailed(
837 const RemoteCharacteristicReadValueCallback& callback, 1090 const RemoteCharacteristicReadValueCallback& callback,
838 device::BluetoothRemoteGattService::GattErrorCode error_code) { 1091 device::BluetoothRemoteGattService::GattErrorCode error_code) {
839 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1092 DCHECK_CURRENTLY_ON(BrowserThread::UI);
840 callback.Run(TranslateGATTErrorAndRecord( 1093 callback.Run(TranslateGATTErrorAndRecord(
841 error_code, UMAGATTOperation::CHARACTERISTIC_READ), 1094 error_code, UMAGATTOperation::CHARACTERISTIC_READ),
842 nullptr /* value */); 1095 nullptr /* value */);
843 } 1096 }
844 1097
845 void WebBluetoothServiceImpl::OnWriteValueSuccess( 1098 void WebBluetoothServiceImpl::OnCharacteristicWriteValueSuccess(
846 const RemoteCharacteristicWriteValueCallback& callback) { 1099 const RemoteCharacteristicWriteValueCallback& callback) {
847 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1100 DCHECK_CURRENTLY_ON(BrowserThread::UI);
848 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS); 1101 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS);
849 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 1102 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
850 } 1103 }
851 1104
852 void WebBluetoothServiceImpl::OnWriteValueFailed( 1105 void WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed(
853 const RemoteCharacteristicWriteValueCallback& callback, 1106 const RemoteCharacteristicWriteValueCallback& callback,
854 device::BluetoothRemoteGattService::GattErrorCode error_code) { 1107 device::BluetoothRemoteGattService::GattErrorCode error_code) {
855 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1108 DCHECK_CURRENTLY_ON(BrowserThread::UI);
856 callback.Run(TranslateGATTErrorAndRecord( 1109 callback.Run(TranslateGATTErrorAndRecord(
857 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE)); 1110 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE));
858 } 1111 }
859 1112
860 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( 1113 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess(
861 const RemoteCharacteristicStartNotificationsCallback& callback, 1114 const RemoteCharacteristicStartNotificationsCallback& callback,
862 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { 1115 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 result.characteristic = 1214 result.characteristic =
962 result.service->GetCharacteristic(characteristic_instance_id); 1215 result.service->GetCharacteristic(characteristic_instance_id);
963 1216
964 if (result.characteristic == nullptr) { 1217 if (result.characteristic == nullptr) {
965 result.outcome = CacheQueryOutcome::NO_CHARACTERISTIC; 1218 result.outcome = CacheQueryOutcome::NO_CHARACTERISTIC;
966 } 1219 }
967 1220
968 return result; 1221 return result;
969 } 1222 }
970 1223
1224 CacheQueryResult WebBluetoothServiceImpl::QueryCacheForDescriptor(
1225 const std::string& descriptor_instance_id) {
1226 auto descriptor_iter =
1227 descriptor_id_to_characteristic_id_.find(descriptor_instance_id);
1228
1229 // Kill the render, see "ID Not in Map Note" above.
1230 if (descriptor_iter == descriptor_id_to_characteristic_id_.end()) {
1231 CrashRendererAndClosePipe(bad_message::BDH_INVALID_DESCRIPTOR_ID);
1232 return CacheQueryResult(CacheQueryOutcome::BAD_RENDERER);
1233 }
1234
1235 CacheQueryResult result =
1236 QueryCacheForCharacteristic(descriptor_iter->second);
1237 if (result.outcome != CacheQueryOutcome::SUCCESS) {
1238 return result;
1239 }
1240
1241 result.descriptor =
1242 result.characteristic->GetDescriptor(descriptor_instance_id);
1243
1244 if (result.descriptor == nullptr) {
1245 result.outcome = CacheQueryOutcome::NO_DESCRIPTOR;
1246 }
1247
1248 return result;
1249 }
1250
971 RenderProcessHost* WebBluetoothServiceImpl::GetRenderProcessHost() { 1251 RenderProcessHost* WebBluetoothServiceImpl::GetRenderProcessHost() {
972 return render_frame_host_->GetProcess(); 1252 return render_frame_host_->GetProcess();
973 } 1253 }
974 1254
975 device::BluetoothAdapter* WebBluetoothServiceImpl::GetAdapter() { 1255 device::BluetoothAdapter* WebBluetoothServiceImpl::GetAdapter() {
976 return BluetoothAdapterFactoryWrapper::Get().GetAdapter(this); 1256 return BluetoothAdapterFactoryWrapper::Get().GetAdapter(this);
977 } 1257 }
978 1258
979 void WebBluetoothServiceImpl::CrashRendererAndClosePipe( 1259 void WebBluetoothServiceImpl::CrashRendererAndClosePipe(
980 bad_message::BadMessageReason reason) { 1260 bad_message::BadMessageReason reason) {
981 bad_message::ReceivedBadMessage(GetRenderProcessHost(), reason); 1261 bad_message::ReceivedBadMessage(GetRenderProcessHost(), reason);
982 binding_.Close(); 1262 binding_.Close();
983 } 1263 }
984 1264
985 url::Origin WebBluetoothServiceImpl::GetOrigin() { 1265 url::Origin WebBluetoothServiceImpl::GetOrigin() {
986 return render_frame_host_->GetLastCommittedOrigin(); 1266 return render_frame_host_->GetLastCommittedOrigin();
987 } 1267 }
988 1268
989 void WebBluetoothServiceImpl::ClearState() { 1269 void WebBluetoothServiceImpl::ClearState() {
990 characteristic_id_to_notify_session_.clear(); 1270 characteristic_id_to_notify_session_.clear();
991 pending_primary_services_requests_.clear(); 1271 pending_primary_services_requests_.clear();
992 characteristic_id_to_service_id_.clear(); 1272 characteristic_id_to_service_id_.clear();
1273 descriptor_id_to_characteristic_id_.clear();
ortuno 2016/11/21 03:34:08 nit: Move this above characteristic_id_to_service_
dougt 2016/11/22 01:47:15 Acknowledged.
993 service_id_to_device_address_.clear(); 1274 service_id_to_device_address_.clear();
994 connected_devices_.reset( 1275 connected_devices_.reset(
995 new FrameConnectedBluetoothDevices(render_frame_host_)); 1276 new FrameConnectedBluetoothDevices(render_frame_host_));
996 allowed_devices_map_ = BluetoothAllowedDevicesMap(); 1277 allowed_devices_map_ = BluetoothAllowedDevicesMap();
997 device_chooser_controller_.reset(); 1278 device_chooser_controller_.reset();
998 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); 1279 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this);
999 } 1280 }
1000 1281
1001 } // namespace content 1282 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698