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

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

Issue 2718583002: Refactor WebBluetoothServiceClient in the web_bluetooth.mojom (Closed)
Patch Set: address more comments Created 3 years, 9 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: A service, characteristic, or descriptor ID not in the 5 // ID Not In Map Note: A service, characteristic, or descriptor ID not in the
6 // corresponding WebBluetoothServiceImpl map [service_id_to_device_address_, 6 // corresponding WebBluetoothServiceImpl map [service_id_to_device_address_,
7 // characteristic_id_to_service_id_, descriptor_id_to_characteristic_id_] 7 // characteristic_id_to_service_id_, descriptor_id_to_characteristic_id_]
8 // implies a hostile renderer because a renderer obtains the corresponding ID 8 // implies a hostile renderer because a renderer obtains the corresponding ID
9 // from this class and it will be added to the map at that time. 9 // from this class and it will be added to the map at that time.
10 10
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 DCHECK_CURRENTLY_ON(BrowserThread::UI); 226 DCHECK_CURRENTLY_ON(BrowserThread::UI);
227 227
228 if (device_chooser_controller_.get()) { 228 if (device_chooser_controller_.get()) {
229 device_chooser_controller_->AddFilteredDevice(*device); 229 device_chooser_controller_->AddFilteredDevice(*device);
230 } 230 }
231 231
232 if (!device->IsGattConnected()) { 232 if (!device->IsGattConnected()) {
233 base::Optional<WebBluetoothDeviceId> device_id = 233 base::Optional<WebBluetoothDeviceId> device_id =
234 connected_devices_->CloseConnectionToDeviceWithAddress( 234 connected_devices_->CloseConnectionToDeviceWithAddress(
235 device->GetAddress()); 235 device->GetAddress());
236 if (device_id && client_) {
237 client_->GattServerDisconnected(device_id.value());
238 }
239 } 236 }
240 } 237 }
241 238
242 void WebBluetoothServiceImpl::GattServicesDiscovered( 239 void WebBluetoothServiceImpl::GattServicesDiscovered(
243 device::BluetoothAdapter* adapter, 240 device::BluetoothAdapter* adapter,
244 device::BluetoothDevice* device) { 241 device::BluetoothDevice* device) {
245 if (device_chooser_controller_.get()) { 242 if (device_chooser_controller_.get()) {
246 device_chooser_controller_->AddFilteredDevice(*device); 243 device_chooser_controller_->AddFilteredDevice(*device);
247 } 244 }
248 245
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 base::Bind(&WebBluetoothServiceImpl::NotifyCharacteristicValueChanged, 282 base::Bind(&WebBluetoothServiceImpl::NotifyCharacteristicValueChanged,
286 weak_ptr_factory_.GetWeakPtr(), 283 weak_ptr_factory_.GetWeakPtr(),
287 characteristic->GetIdentifier(), value))) { 284 characteristic->GetIdentifier(), value))) {
288 LOG(WARNING) << "No TaskRunner."; 285 LOG(WARNING) << "No TaskRunner.";
289 } 286 }
290 } 287 }
291 288
292 void WebBluetoothServiceImpl::NotifyCharacteristicValueChanged( 289 void WebBluetoothServiceImpl::NotifyCharacteristicValueChanged(
293 const std::string& characteristic_instance_id, 290 const std::string& characteristic_instance_id,
294 const std::vector<uint8_t>& value) { 291 const std::vector<uint8_t>& value) {
295 if (client_) { 292 auto iter =
296 client_->RemoteCharacteristicValueChanged(characteristic_instance_id, 293 characteristic_id_to_notify_session_.find(characteristic_instance_id);
ortuno 2017/03/06 11:31:20 You can move all this logic up to GattCharacterist
juncai 2017/03/09 07:30:57 Done.
297 value); 294 if (iter != characteristic_id_to_notify_session_.end()) {
295 iter->second.second->RemoteCharacteristicValueChanged(value);
298 } 296 }
299 } 297 }
300 298
301 void WebBluetoothServiceImpl::SetClient(
302 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) {
303 DCHECK(!client_.get());
304 client_.Bind(std::move(client));
305 }
306
307 void WebBluetoothServiceImpl::RequestDevice( 299 void WebBluetoothServiceImpl::RequestDevice(
308 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, 300 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options,
309 const RequestDeviceCallback& callback) { 301 const RequestDeviceCallback& callback) {
310 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); 302 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE);
311 RecordRequestDeviceOptions(options); 303 RecordRequestDeviceOptions(options);
312 304
313 if (!GetAdapter()) { 305 if (!GetAdapter()) {
314 if (BluetoothAdapterFactoryWrapper::Get().IsLowEnergyAvailable()) { 306 if (BluetoothAdapterFactoryWrapper::Get().IsLowEnergyAvailable()) {
315 BluetoothAdapterFactoryWrapper::Get().AcquireAdapter( 307 BluetoothAdapterFactoryWrapper::Get().AcquireAdapter(
316 this, base::Bind(&WebBluetoothServiceImpl::RequestDeviceImpl, 308 this, base::Bind(&WebBluetoothServiceImpl::RequestDeviceImpl,
(...skipping 14 matching lines...) Expand all
331 void WebBluetoothServiceImpl::RemoteServerConnect( 323 void WebBluetoothServiceImpl::RemoteServerConnect(
332 const WebBluetoothDeviceId& device_id, 324 const WebBluetoothDeviceId& device_id,
333 const RemoteServerConnectCallback& callback) { 325 const RemoteServerConnectCallback& callback) {
334 DCHECK_CURRENTLY_ON(BrowserThread::UI); 326 DCHECK_CURRENTLY_ON(BrowserThread::UI);
335 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT); 327 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT);
336 328
337 const CacheQueryResult query_result = QueryCacheForDevice(device_id); 329 const CacheQueryResult query_result = QueryCacheForDevice(device_id);
338 330
339 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { 331 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
340 RecordConnectGATTOutcome(query_result.outcome); 332 RecordConnectGATTOutcome(query_result.outcome);
341 callback.Run(query_result.GetWebResult()); 333 callback.Run(query_result.GetWebResult(), nullptr /* client_request */);
342 return; 334 return;
343 } 335 }
344 336
345 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) { 337 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) {
346 DVLOG(1) << "Already connected."; 338 DVLOG(1) << "Already connected.";
347 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 339 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
340 nullptr /* client_request */);
348 return; 341 return;
349 } 342 }
350 343
351 // It's possible for WebBluetoothServiceImpl to issue two successive 344 // It's possible for WebBluetoothServiceImpl to issue two successive
352 // connection requests for which it would get two successive responses 345 // connection requests for which it would get two successive responses
353 // and consequently try to insert two BluetoothGattConnections for the 346 // and consequently try to insert two BluetoothGattConnections for the
354 // same device. WebBluetoothServiceImpl should reject or queue connection 347 // same device. WebBluetoothServiceImpl should reject or queue connection
355 // requests if there is a pending connection already, but the platform 348 // requests if there is a pending connection already, but the platform
356 // abstraction doesn't currently support checking for pending connections. 349 // abstraction doesn't currently support checking for pending connections.
357 // TODO(ortuno): CHECK that this never happens once the platform 350 // TODO(ortuno): CHECK that this never happens once the platform
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications( 675 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications(
683 const std::string& characteristic_instance_id, 676 const std::string& characteristic_instance_id,
684 const RemoteCharacteristicStartNotificationsCallback& callback) { 677 const RemoteCharacteristicStartNotificationsCallback& callback) {
685 DCHECK_CURRENTLY_ON(BrowserThread::UI); 678 DCHECK_CURRENTLY_ON(BrowserThread::UI);
686 RecordWebBluetoothFunctionCall( 679 RecordWebBluetoothFunctionCall(
687 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); 680 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS);
688 681
689 auto iter = 682 auto iter =
690 characteristic_id_to_notify_session_.find(characteristic_instance_id); 683 characteristic_id_to_notify_session_.find(characteristic_instance_id);
691 if (iter != characteristic_id_to_notify_session_.end() && 684 if (iter != characteristic_id_to_notify_session_.end() &&
692 iter->second->IsActive()) { 685 iter->second.first->IsActive()) {
693 // If the frame has already started notifications and the notifications 686 // If the frame has already started notifications and the notifications
694 // are active we return SUCCESS. 687 // are active we return SUCCESS.
695 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 688 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
689 nullptr /* client_request */);
696 return; 690 return;
697 } 691 }
698 692
699 const CacheQueryResult query_result = 693 const CacheQueryResult query_result =
700 QueryCacheForCharacteristic(characteristic_instance_id); 694 QueryCacheForCharacteristic(characteristic_instance_id);
701 695
702 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { 696 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
703 return; 697 return;
704 } 698 }
705 699
706 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { 700 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
707 RecordStartNotificationsOutcome(query_result.outcome); 701 RecordStartNotificationsOutcome(query_result.outcome);
708 callback.Run(query_result.GetWebResult()); 702 callback.Run(query_result.GetWebResult(), nullptr /* client_request */);
709 return; 703 return;
710 } 704 }
711 705
712 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate = 706 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate =
713 query_result.characteristic->GetProperties() & 707 query_result.characteristic->GetProperties() &
714 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY | 708 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY |
715 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE); 709 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE);
716 if (!notify_or_indicate) { 710 if (!notify_or_indicate) {
717 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED); 711 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED,
712 nullptr /* client_request */);
718 return; 713 return;
719 } 714 }
720 715
721 query_result.characteristic->StartNotifySession( 716 query_result.characteristic->StartNotifySession(
722 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, 717 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess,
723 weak_ptr_factory_.GetWeakPtr(), callback), 718 weak_ptr_factory_.GetWeakPtr(), callback),
724 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, 719 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed,
725 weak_ptr_factory_.GetWeakPtr(), callback)); 720 weak_ptr_factory_.GetWeakPtr(), callback));
726 } 721 }
727 722
(...skipping 12 matching lines...) Expand all
740 } 735 }
741 736
742 auto notify_session_iter = 737 auto notify_session_iter =
743 characteristic_id_to_notify_session_.find(characteristic_instance_id); 738 characteristic_id_to_notify_session_.find(characteristic_instance_id);
744 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { 739 if (notify_session_iter == characteristic_id_to_notify_session_.end()) {
745 // If the frame hasn't subscribed to notifications before we just 740 // If the frame hasn't subscribed to notifications before we just
746 // run the callback. 741 // run the callback.
747 callback.Run(); 742 callback.Run();
748 return; 743 return;
749 } 744 }
750 notify_session_iter->second->Stop(base::Bind( 745 notify_session_iter->second.first->Stop(base::Bind(
751 &WebBluetoothServiceImpl::OnStopNotifySessionComplete, 746 &WebBluetoothServiceImpl::OnStopNotifySessionComplete,
752 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback)); 747 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback));
753 } 748 }
754 749
755 void WebBluetoothServiceImpl::RemoteDescriptorReadValue( 750 void WebBluetoothServiceImpl::RemoteDescriptorReadValue(
756 const std::string& descriptor_instance_id, 751 const std::string& descriptor_instance_id,
757 const RemoteDescriptorReadValueCallback& callback) { 752 const RemoteDescriptorReadValueCallback& callback) {
758 DCHECK_CURRENTLY_ON(BrowserThread::UI); 753 DCHECK_CURRENTLY_ON(BrowserThread::UI);
759 RecordWebBluetoothFunctionCall( 754 RecordWebBluetoothFunctionCall(
760 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE); 755 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 945
951 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess( 946 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess(
952 const WebBluetoothDeviceId& device_id, 947 const WebBluetoothDeviceId& device_id,
953 base::TimeTicks start_time, 948 base::TimeTicks start_time,
954 const RemoteServerConnectCallback& callback, 949 const RemoteServerConnectCallback& callback,
955 std::unique_ptr<device::BluetoothGattConnection> connection) { 950 std::unique_ptr<device::BluetoothGattConnection> connection) {
956 DCHECK_CURRENTLY_ON(BrowserThread::UI); 951 DCHECK_CURRENTLY_ON(BrowserThread::UI);
957 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); 952 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time);
958 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); 953 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS);
959 954
960 connected_devices_->Insert(device_id, std::move(connection)); 955 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) {
ortuno 2017/03/06 11:31:20 // It's possible for WebBluetoothServiceImpl to is
juncai 2017/03/09 07:30:57 Done.
961 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 956 DVLOG(1) << "Already connected.";
957 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
958 nullptr /* client_request */);
959 return;
960 }
961
962 blink::mojom::WebBluetoothServerClientAssociatedPtr client;
963 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
964 mojo::MakeRequest(&client));
965 connected_devices_->Insert(device_id, std::move(connection),
966 std::move(client));
962 } 967 }
963 968
964 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed( 969 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed(
965 base::TimeTicks start_time, 970 base::TimeTicks start_time,
966 const RemoteServerConnectCallback& callback, 971 const RemoteServerConnectCallback& callback,
967 device::BluetoothDevice::ConnectErrorCode error_code) { 972 device::BluetoothDevice::ConnectErrorCode error_code) {
968 DCHECK_CURRENTLY_ON(BrowserThread::UI); 973 DCHECK_CURRENTLY_ON(BrowserThread::UI);
969 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time); 974 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time);
970 callback.Run(TranslateConnectErrorAndRecord(error_code)); 975 callback.Run(TranslateConnectErrorAndRecord(error_code),
976 nullptr /* client_request */);
971 } 977 }
972 978
973 void WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess( 979 void WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess(
974 const RemoteCharacteristicReadValueCallback& callback, 980 const RemoteCharacteristicReadValueCallback& callback,
975 const std::vector<uint8_t>& value) { 981 const std::vector<uint8_t>& value) {
976 DCHECK_CURRENTLY_ON(BrowserThread::UI); 982 DCHECK_CURRENTLY_ON(BrowserThread::UI);
977 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS); 983 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS);
978 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, value); 984 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, value);
979 } 985 }
980 986
(...skipping 22 matching lines...) Expand all
1003 } 1009 }
1004 1010
1005 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( 1011 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess(
1006 const RemoteCharacteristicStartNotificationsCallback& callback, 1012 const RemoteCharacteristicStartNotificationsCallback& callback,
1007 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { 1013 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) {
1008 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1014 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1009 // Copy Characteristic Instance ID before passing a unique pointer because 1015 // Copy Characteristic Instance ID before passing a unique pointer because
1010 // compilers may evaluate arguments in any order. 1016 // compilers may evaluate arguments in any order.
1011 std::string characteristic_instance_id = 1017 std::string characteristic_instance_id =
1012 notify_session->GetCharacteristicIdentifier(); 1018 notify_session->GetCharacteristicIdentifier();
1019
1020 auto iter =
1021 characteristic_id_to_notify_session_.find(characteristic_instance_id);
1022 if (iter != characteristic_id_to_notify_session_.end() &&
1023 iter->second.first->IsActive()) {
1024 // If the frame has already started notifications and the notifications
1025 // are active we return SUCCESS.
1026 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
1027 nullptr /* client_request */);
1028 return;
1029 }
1030
1031 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtr client;
1032 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
1033 mojo::MakeRequest(&client));
1013 // Saving the BluetoothGattNotifySession keeps notifications active. 1034 // Saving the BluetoothGattNotifySession keeps notifications active.
1014 characteristic_id_to_notify_session_[characteristic_instance_id] = 1035 characteristic_id_to_notify_session_[characteristic_instance_id] =
1015 std::move(notify_session); 1036 std::make_pair(std::move(notify_session), std::move(client));
1016 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
1017 } 1037 }
1018 1038
1019 void WebBluetoothServiceImpl::OnStartNotifySessionFailed( 1039 void WebBluetoothServiceImpl::OnStartNotifySessionFailed(
1020 const RemoteCharacteristicStartNotificationsCallback& callback, 1040 const RemoteCharacteristicStartNotificationsCallback& callback,
1021 device::BluetoothRemoteGattService::GattErrorCode error_code) { 1041 device::BluetoothRemoteGattService::GattErrorCode error_code) {
1022 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1042 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1023 callback.Run(TranslateGATTErrorAndRecord( 1043 callback.Run(TranslateGATTErrorAndRecord(
1024 error_code, UMAGATTOperation::START_NOTIFICATIONS)); 1044 error_code, UMAGATTOperation::START_NOTIFICATIONS),
1045 nullptr /* client_request */);
1025 } 1046 }
1026 1047
1027 void WebBluetoothServiceImpl::OnStopNotifySessionComplete( 1048 void WebBluetoothServiceImpl::OnStopNotifySessionComplete(
1028 const std::string& characteristic_instance_id, 1049 const std::string& characteristic_instance_id,
1029 const RemoteCharacteristicStopNotificationsCallback& callback) { 1050 const RemoteCharacteristicStopNotificationsCallback& callback) {
1030 characteristic_id_to_notify_session_.erase(characteristic_instance_id); 1051 characteristic_id_to_notify_session_.erase(characteristic_instance_id);
1031 callback.Run(); 1052 callback.Run();
1032 } 1053 }
1033 1054
1034 void WebBluetoothServiceImpl::OnDescriptorReadValueSuccess( 1055 void WebBluetoothServiceImpl::OnDescriptorReadValueSuccess(
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 descriptor_id_to_characteristic_id_.clear(); 1228 descriptor_id_to_characteristic_id_.clear();
1208 characteristic_id_to_service_id_.clear(); 1229 characteristic_id_to_service_id_.clear();
1209 service_id_to_device_address_.clear(); 1230 service_id_to_device_address_.clear();
1210 connected_devices_.reset( 1231 connected_devices_.reset(
1211 new FrameConnectedBluetoothDevices(render_frame_host_)); 1232 new FrameConnectedBluetoothDevices(render_frame_host_));
1212 device_chooser_controller_.reset(); 1233 device_chooser_controller_.reset();
1213 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); 1234 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this);
1214 } 1235 }
1215 1236
1216 } // namespace content 1237 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698