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

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 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);
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(
334 query_result.GetWebResult(),
335 nullptr /* blink::mojom::WebBluetoothServerClientAssociatedRequest */);
ortuno 2017/03/01 04:52:05 optional: you can just use client_request.
juncai 2017/03/02 03:23:42 Done.
342 return; 336 return;
343 } 337 }
344 338
345 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) { 339 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) {
346 DVLOG(1) << "Already connected."; 340 DVLOG(1) << "Already connected.";
347 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 341 callback.Run(
342 blink::mojom::WebBluetoothResult::SUCCESS,
343 nullptr /* blink::mojom::WebBluetoothServerClientAssociatedRequest */);
348 return; 344 return;
349 } 345 }
350 346
351 // It's possible for WebBluetoothServiceImpl to issue two successive 347 // It's possible for WebBluetoothServiceImpl to issue two successive
352 // connection requests for which it would get two successive responses 348 // connection requests for which it would get two successive responses
353 // and consequently try to insert two BluetoothGattConnections for the 349 // and consequently try to insert two BluetoothGattConnections for the
354 // same device. WebBluetoothServiceImpl should reject or queue connection 350 // same device. WebBluetoothServiceImpl should reject or queue connection
355 // requests if there is a pending connection already, but the platform 351 // requests if there is a pending connection already, but the platform
356 // abstraction doesn't currently support checking for pending connections. 352 // abstraction doesn't currently support checking for pending connections.
357 // TODO(ortuno): CHECK that this never happens once the platform 353 // 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( 678 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications(
683 const std::string& characteristic_instance_id, 679 const std::string& characteristic_instance_id,
684 const RemoteCharacteristicStartNotificationsCallback& callback) { 680 const RemoteCharacteristicStartNotificationsCallback& callback) {
685 DCHECK_CURRENTLY_ON(BrowserThread::UI); 681 DCHECK_CURRENTLY_ON(BrowserThread::UI);
686 RecordWebBluetoothFunctionCall( 682 RecordWebBluetoothFunctionCall(
687 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); 683 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS);
688 684
689 auto iter = 685 auto iter =
690 characteristic_id_to_notify_session_.find(characteristic_instance_id); 686 characteristic_id_to_notify_session_.find(characteristic_instance_id);
691 if (iter != characteristic_id_to_notify_session_.end() && 687 if (iter != characteristic_id_to_notify_session_.end() &&
692 iter->second->IsActive()) { 688 iter->second.first->IsActive()) {
693 // If the frame has already started notifications and the notifications 689 // If the frame has already started notifications and the notifications
694 // are active we return SUCCESS. 690 // are active we return SUCCESS.
695 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 691 callback.Run(
692 blink::mojom::WebBluetoothResult::SUCCESS, nullptr
693 /* blink::mojom::WebBluetoothCharacteristicClientAssociatedRequest */);
696 return; 694 return;
697 } 695 }
698 696
699 const CacheQueryResult query_result = 697 const CacheQueryResult query_result =
700 QueryCacheForCharacteristic(characteristic_instance_id); 698 QueryCacheForCharacteristic(characteristic_instance_id);
701 699
702 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { 700 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
703 return; 701 return;
704 } 702 }
705 703
706 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { 704 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
707 RecordStartNotificationsOutcome(query_result.outcome); 705 RecordStartNotificationsOutcome(query_result.outcome);
708 callback.Run(query_result.GetWebResult()); 706 callback.Run(
707 query_result.GetWebResult(), nullptr
708 /* blink::mojom::WebBluetoothCharacteristicClientAssociatedRequest */);
709 return; 709 return;
710 } 710 }
711 711
712 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate = 712 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate =
713 query_result.characteristic->GetProperties() & 713 query_result.characteristic->GetProperties() &
714 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY | 714 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY |
715 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE); 715 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE);
716 if (!notify_or_indicate) { 716 if (!notify_or_indicate) {
717 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED); 717 callback.Run(
718 blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED, nullptr
719 /* blink::mojom::WebBluetoothCharacteristicClientAssociatedRequest */);
718 return; 720 return;
719 } 721 }
720 722
721 query_result.characteristic->StartNotifySession( 723 query_result.characteristic->StartNotifySession(
722 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, 724 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess,
723 weak_ptr_factory_.GetWeakPtr(), callback), 725 weak_ptr_factory_.GetWeakPtr(), callback),
724 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, 726 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed,
725 weak_ptr_factory_.GetWeakPtr(), callback)); 727 weak_ptr_factory_.GetWeakPtr(), callback));
726 } 728 }
727 729
(...skipping 12 matching lines...) Expand all
740 } 742 }
741 743
742 auto notify_session_iter = 744 auto notify_session_iter =
743 characteristic_id_to_notify_session_.find(characteristic_instance_id); 745 characteristic_id_to_notify_session_.find(characteristic_instance_id);
744 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { 746 if (notify_session_iter == characteristic_id_to_notify_session_.end()) {
745 // If the frame hasn't subscribed to notifications before we just 747 // If the frame hasn't subscribed to notifications before we just
746 // run the callback. 748 // run the callback.
747 callback.Run(); 749 callback.Run();
748 return; 750 return;
749 } 751 }
750 notify_session_iter->second->Stop(base::Bind( 752 notify_session_iter->second.first->Stop(base::Bind(
751 &WebBluetoothServiceImpl::OnStopNotifySessionComplete, 753 &WebBluetoothServiceImpl::OnStopNotifySessionComplete,
752 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback)); 754 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback));
753 } 755 }
754 756
755 void WebBluetoothServiceImpl::RemoteDescriptorReadValue( 757 void WebBluetoothServiceImpl::RemoteDescriptorReadValue(
756 const std::string& descriptor_instance_id, 758 const std::string& descriptor_instance_id,
757 const RemoteDescriptorReadValueCallback& callback) { 759 const RemoteDescriptorReadValueCallback& callback) {
758 DCHECK_CURRENTLY_ON(BrowserThread::UI); 760 DCHECK_CURRENTLY_ON(BrowserThread::UI);
759 RecordWebBluetoothFunctionCall( 761 RecordWebBluetoothFunctionCall(
760 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE); 762 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 952
951 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess( 953 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess(
952 const WebBluetoothDeviceId& device_id, 954 const WebBluetoothDeviceId& device_id,
953 base::TimeTicks start_time, 955 base::TimeTicks start_time,
954 const RemoteServerConnectCallback& callback, 956 const RemoteServerConnectCallback& callback,
955 std::unique_ptr<device::BluetoothGattConnection> connection) { 957 std::unique_ptr<device::BluetoothGattConnection> connection) {
956 DCHECK_CURRENTLY_ON(BrowserThread::UI); 958 DCHECK_CURRENTLY_ON(BrowserThread::UI);
957 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); 959 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time);
958 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); 960 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS);
959 961
960 connected_devices_->Insert(device_id, std::move(connection)); 962 blink::mojom::WebBluetoothServerClientAssociatedPtr client;
ortuno 2017/03/01 04:52:05 Seems a bit wasteful to be sending request when th
juncai 2017/03/02 03:23:40 Done.
961 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 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(
976 TranslateConnectErrorAndRecord(error_code),
977 nullptr /* blink::mojom::WebBluetoothServerClientAssociatedRequest */);
971 } 978 }
972 979
973 void WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess( 980 void WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess(
974 const RemoteCharacteristicReadValueCallback& callback, 981 const RemoteCharacteristicReadValueCallback& callback,
975 const std::vector<uint8_t>& value) { 982 const std::vector<uint8_t>& value) {
976 DCHECK_CURRENTLY_ON(BrowserThread::UI); 983 DCHECK_CURRENTLY_ON(BrowserThread::UI);
977 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS); 984 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS);
978 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, value); 985 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, value);
979 } 986 }
980 987
(...skipping 22 matching lines...) Expand all
1003 } 1010 }
1004 1011
1005 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( 1012 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess(
1006 const RemoteCharacteristicStartNotificationsCallback& callback, 1013 const RemoteCharacteristicStartNotificationsCallback& callback,
1007 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { 1014 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) {
1008 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1015 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1009 // Copy Characteristic Instance ID before passing a unique pointer because 1016 // Copy Characteristic Instance ID before passing a unique pointer because
1010 // compilers may evaluate arguments in any order. 1017 // compilers may evaluate arguments in any order.
1011 std::string characteristic_instance_id = 1018 std::string characteristic_instance_id =
1012 notify_session->GetCharacteristicIdentifier(); 1019 notify_session->GetCharacteristicIdentifier();
1020
1021 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtr client;
ortuno 2017/03/01 04:52:05 Seems a bit wasteful to be sending request when th
juncai 2017/03/02 03:23:41 Done.
1022 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
1023 mojo::MakeRequest(&client));
1013 // Saving the BluetoothGattNotifySession keeps notifications active. 1024 // Saving the BluetoothGattNotifySession keeps notifications active.
1014 characteristic_id_to_notify_session_[characteristic_instance_id] = 1025 characteristic_id_to_notify_session_[characteristic_instance_id] =
1015 std::move(notify_session); 1026 std::make_pair(std::move(notify_session), std::move(client));
1016 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
1017 } 1027 }
1018 1028
1019 void WebBluetoothServiceImpl::OnStartNotifySessionFailed( 1029 void WebBluetoothServiceImpl::OnStartNotifySessionFailed(
1020 const RemoteCharacteristicStartNotificationsCallback& callback, 1030 const RemoteCharacteristicStartNotificationsCallback& callback,
1021 device::BluetoothRemoteGattService::GattErrorCode error_code) { 1031 device::BluetoothRemoteGattService::GattErrorCode error_code) {
1022 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1032 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1023 callback.Run(TranslateGATTErrorAndRecord( 1033 callback.Run(
1024 error_code, UMAGATTOperation::START_NOTIFICATIONS)); 1034 TranslateGATTErrorAndRecord(error_code,
1035 UMAGATTOperation::START_NOTIFICATIONS),
1036 nullptr
1037 /* blink::mojom::WebBluetoothCharacteristicClientAssociatedRequest */);
1025 } 1038 }
1026 1039
1027 void WebBluetoothServiceImpl::OnStopNotifySessionComplete( 1040 void WebBluetoothServiceImpl::OnStopNotifySessionComplete(
1028 const std::string& characteristic_instance_id, 1041 const std::string& characteristic_instance_id,
1029 const RemoteCharacteristicStopNotificationsCallback& callback) { 1042 const RemoteCharacteristicStopNotificationsCallback& callback) {
1030 characteristic_id_to_notify_session_.erase(characteristic_instance_id); 1043 characteristic_id_to_notify_session_.erase(characteristic_instance_id);
1031 callback.Run(); 1044 callback.Run();
1032 } 1045 }
1033 1046
1034 void WebBluetoothServiceImpl::OnDescriptorReadValueSuccess( 1047 void WebBluetoothServiceImpl::OnDescriptorReadValueSuccess(
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 descriptor_id_to_characteristic_id_.clear(); 1220 descriptor_id_to_characteristic_id_.clear();
1208 characteristic_id_to_service_id_.clear(); 1221 characteristic_id_to_service_id_.clear();
1209 service_id_to_device_address_.clear(); 1222 service_id_to_device_address_.clear();
1210 connected_devices_.reset( 1223 connected_devices_.reset(
1211 new FrameConnectedBluetoothDevices(render_frame_host_)); 1224 new FrameConnectedBluetoothDevices(render_frame_host_));
1212 device_chooser_controller_.reset(); 1225 device_chooser_controller_.reset();
1213 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); 1226 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this);
1214 } 1227 }
1215 1228
1216 } // namespace content 1229 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698