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

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

Issue 2718583002: Refactor WebBluetoothServiceClient in the web_bluetooth.mojom (Closed)
Patch Set: fix content unit tests Created 3 years, 10 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(query_result.GetWebResult(), nullptr);
ortuno 2017/02/24 03:28:52 nit: comment what nullptr is. Same below.
juncai 2017/03/01 02:04:12 Done.
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, nullptr);
348 return; 340 return;
349 } 341 }
350 342
351 // It's possible for WebBluetoothServiceImpl to issue two successive 343 // It's possible for WebBluetoothServiceImpl to issue two successive
352 // connection requests for which it would get two successive responses 344 // connection requests for which it would get two successive responses
353 // and consequently try to insert two BluetoothGattConnections for the 345 // and consequently try to insert two BluetoothGattConnections for the
354 // same device. WebBluetoothServiceImpl should reject or queue connection 346 // same device. WebBluetoothServiceImpl should reject or queue connection
355 // requests if there is a pending connection already, but the platform 347 // requests if there is a pending connection already, but the platform
356 // abstraction doesn't currently support checking for pending connections. 348 // abstraction doesn't currently support checking for pending connections.
357 // TODO(ortuno): CHECK that this never happens once the platform 349 // 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( 674 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications(
683 const std::string& characteristic_instance_id, 675 const std::string& characteristic_instance_id,
684 const RemoteCharacteristicStartNotificationsCallback& callback) { 676 const RemoteCharacteristicStartNotificationsCallback& callback) {
685 DCHECK_CURRENTLY_ON(BrowserThread::UI); 677 DCHECK_CURRENTLY_ON(BrowserThread::UI);
686 RecordWebBluetoothFunctionCall( 678 RecordWebBluetoothFunctionCall(
687 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); 679 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS);
688 680
689 auto iter = 681 auto iter =
690 characteristic_id_to_notify_session_.find(characteristic_instance_id); 682 characteristic_id_to_notify_session_.find(characteristic_instance_id);
691 if (iter != characteristic_id_to_notify_session_.end() && 683 if (iter != characteristic_id_to_notify_session_.end() &&
692 iter->second->IsActive()) { 684 iter->second.first->IsActive()) {
693 // If the frame has already started notifications and the notifications 685 // If the frame has already started notifications and the notifications
694 // are active we return SUCCESS. 686 // are active we return SUCCESS.
695 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 687 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, nullptr);
696 return; 688 return;
697 } 689 }
698 690
699 const CacheQueryResult query_result = 691 const CacheQueryResult query_result =
700 QueryCacheForCharacteristic(characteristic_instance_id); 692 QueryCacheForCharacteristic(characteristic_instance_id);
701 693
702 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { 694 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
703 return; 695 return;
704 } 696 }
705 697
706 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { 698 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
707 RecordStartNotificationsOutcome(query_result.outcome); 699 RecordStartNotificationsOutcome(query_result.outcome);
708 callback.Run(query_result.GetWebResult()); 700 callback.Run(query_result.GetWebResult(), nullptr);
709 return; 701 return;
710 } 702 }
711 703
712 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate = 704 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate =
713 query_result.characteristic->GetProperties() & 705 query_result.characteristic->GetProperties() &
714 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY | 706 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY |
715 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE); 707 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE);
716 if (!notify_or_indicate) { 708 if (!notify_or_indicate) {
717 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED); 709 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED, nullptr);
718 return; 710 return;
719 } 711 }
720 712
721 query_result.characteristic->StartNotifySession( 713 query_result.characteristic->StartNotifySession(
722 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, 714 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess,
723 weak_ptr_factory_.GetWeakPtr(), callback), 715 weak_ptr_factory_.GetWeakPtr(), callback),
724 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, 716 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed,
725 weak_ptr_factory_.GetWeakPtr(), callback)); 717 weak_ptr_factory_.GetWeakPtr(), callback));
726 } 718 }
727 719
(...skipping 12 matching lines...) Expand all
740 } 732 }
741 733
742 auto notify_session_iter = 734 auto notify_session_iter =
743 characteristic_id_to_notify_session_.find(characteristic_instance_id); 735 characteristic_id_to_notify_session_.find(characteristic_instance_id);
744 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { 736 if (notify_session_iter == characteristic_id_to_notify_session_.end()) {
745 // If the frame hasn't subscribed to notifications before we just 737 // If the frame hasn't subscribed to notifications before we just
746 // run the callback. 738 // run the callback.
747 callback.Run(); 739 callback.Run();
748 return; 740 return;
749 } 741 }
750 notify_session_iter->second->Stop(base::Bind( 742 notify_session_iter->second.first->Stop(base::Bind(
751 &WebBluetoothServiceImpl::OnStopNotifySessionComplete, 743 &WebBluetoothServiceImpl::OnStopNotifySessionComplete,
752 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback)); 744 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback));
753 } 745 }
754 746
755 void WebBluetoothServiceImpl::RemoteDescriptorReadValue( 747 void WebBluetoothServiceImpl::RemoteDescriptorReadValue(
756 const std::string& descriptor_instance_id, 748 const std::string& descriptor_instance_id,
757 const RemoteDescriptorReadValueCallback& callback) { 749 const RemoteDescriptorReadValueCallback& callback) {
758 DCHECK_CURRENTLY_ON(BrowserThread::UI); 750 DCHECK_CURRENTLY_ON(BrowserThread::UI);
759 RecordWebBluetoothFunctionCall( 751 RecordWebBluetoothFunctionCall(
760 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE); 752 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 942
951 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess( 943 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess(
952 const WebBluetoothDeviceId& device_id, 944 const WebBluetoothDeviceId& device_id,
953 base::TimeTicks start_time, 945 base::TimeTicks start_time,
954 const RemoteServerConnectCallback& callback, 946 const RemoteServerConnectCallback& callback,
955 std::unique_ptr<device::BluetoothGattConnection> connection) { 947 std::unique_ptr<device::BluetoothGattConnection> connection) {
956 DCHECK_CURRENTLY_ON(BrowserThread::UI); 948 DCHECK_CURRENTLY_ON(BrowserThread::UI);
957 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); 949 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time);
958 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); 950 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS);
959 951
960 connected_devices_->Insert(device_id, std::move(connection)); 952 blink::mojom::WebBluetoothServerClientAssociatedPtr client;
961 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 953 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
954 mojo::MakeRequest(&client));
955 connected_devices_->Insert(device_id, std::move(connection),
956 std::move(client));
ortuno 2017/02/24 03:28:52 Would it make sense to remove the RemoteGATTServer
juncai 2017/03/01 02:04:12 Making sense, since it also needs to change the we
juncai 2017/03/02 03:23:38 I opened an issue for that: https://bugs.chromium.
962 } 957 }
963 958
964 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed( 959 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed(
965 base::TimeTicks start_time, 960 base::TimeTicks start_time,
966 const RemoteServerConnectCallback& callback, 961 const RemoteServerConnectCallback& callback,
967 device::BluetoothDevice::ConnectErrorCode error_code) { 962 device::BluetoothDevice::ConnectErrorCode error_code) {
968 DCHECK_CURRENTLY_ON(BrowserThread::UI); 963 DCHECK_CURRENTLY_ON(BrowserThread::UI);
969 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time); 964 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time);
970 callback.Run(TranslateConnectErrorAndRecord(error_code)); 965 callback.Run(TranslateConnectErrorAndRecord(error_code), nullptr);
971 } 966 }
972 967
973 void WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess( 968 void WebBluetoothServiceImpl::OnCharacteristicReadValueSuccess(
974 const RemoteCharacteristicReadValueCallback& callback, 969 const RemoteCharacteristicReadValueCallback& callback,
975 const std::vector<uint8_t>& value) { 970 const std::vector<uint8_t>& value) {
976 DCHECK_CURRENTLY_ON(BrowserThread::UI); 971 DCHECK_CURRENTLY_ON(BrowserThread::UI);
977 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS); 972 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS);
978 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, value); 973 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS, value);
979 } 974 }
980 975
(...skipping 22 matching lines...) Expand all
1003 } 998 }
1004 999
1005 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( 1000 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess(
1006 const RemoteCharacteristicStartNotificationsCallback& callback, 1001 const RemoteCharacteristicStartNotificationsCallback& callback,
1007 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { 1002 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) {
1008 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1003 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1009 // Copy Characteristic Instance ID before passing a unique pointer because 1004 // Copy Characteristic Instance ID before passing a unique pointer because
1010 // compilers may evaluate arguments in any order. 1005 // compilers may evaluate arguments in any order.
1011 std::string characteristic_instance_id = 1006 std::string characteristic_instance_id =
1012 notify_session->GetCharacteristicIdentifier(); 1007 notify_session->GetCharacteristicIdentifier();
1008
1009 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtr client;
1010 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS,
1011 mojo::MakeRequest(&client));
1013 // Saving the BluetoothGattNotifySession keeps notifications active. 1012 // Saving the BluetoothGattNotifySession keeps notifications active.
1014 characteristic_id_to_notify_session_[characteristic_instance_id] = 1013 characteristic_id_to_notify_session_[characteristic_instance_id] =
1015 std::move(notify_session); 1014 std::make_pair(std::move(notify_session), std::move(client));
1016 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
1017 } 1015 }
1018 1016
1019 void WebBluetoothServiceImpl::OnStartNotifySessionFailed( 1017 void WebBluetoothServiceImpl::OnStartNotifySessionFailed(
1020 const RemoteCharacteristicStartNotificationsCallback& callback, 1018 const RemoteCharacteristicStartNotificationsCallback& callback,
1021 device::BluetoothRemoteGattService::GattErrorCode error_code) { 1019 device::BluetoothRemoteGattService::GattErrorCode error_code) {
1022 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1020 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1023 callback.Run(TranslateGATTErrorAndRecord( 1021 callback.Run(TranslateGATTErrorAndRecord(
1024 error_code, UMAGATTOperation::START_NOTIFICATIONS)); 1022 error_code, UMAGATTOperation::START_NOTIFICATIONS),
1023 nullptr);
1025 } 1024 }
1026 1025
1027 void WebBluetoothServiceImpl::OnStopNotifySessionComplete( 1026 void WebBluetoothServiceImpl::OnStopNotifySessionComplete(
1028 const std::string& characteristic_instance_id, 1027 const std::string& characteristic_instance_id,
1029 const RemoteCharacteristicStopNotificationsCallback& callback) { 1028 const RemoteCharacteristicStopNotificationsCallback& callback) {
1030 characteristic_id_to_notify_session_.erase(characteristic_instance_id); 1029 characteristic_id_to_notify_session_.erase(characteristic_instance_id);
1031 callback.Run(); 1030 callback.Run();
1032 } 1031 }
1033 1032
1034 void WebBluetoothServiceImpl::OnDescriptorReadValueSuccess( 1033 void WebBluetoothServiceImpl::OnDescriptorReadValueSuccess(
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 descriptor_id_to_characteristic_id_.clear(); 1206 descriptor_id_to_characteristic_id_.clear();
1208 characteristic_id_to_service_id_.clear(); 1207 characteristic_id_to_service_id_.clear();
1209 service_id_to_device_address_.clear(); 1208 service_id_to_device_address_.clear();
1210 connected_devices_.reset( 1209 connected_devices_.reset(
1211 new FrameConnectedBluetoothDevices(render_frame_host_)); 1210 new FrameConnectedBluetoothDevices(render_frame_host_));
1212 device_chooser_controller_.reset(); 1211 device_chooser_controller_.reset();
1213 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); 1212 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this);
1214 } 1213 }
1215 1214
1216 } // namespace content 1215 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698