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

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

Issue 2718583002: Refactor WebBluetoothServiceClient in the web_bluetooth.mojom (Closed)
Patch Set: rebase 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
11 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" 11 #include "content/browser/bluetooth/web_bluetooth_service_impl.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 14
15 #include "base/memory/ptr_util.h"
15 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread_task_runner_handle.h" 17 #include "base/threading/thread_task_runner_handle.h"
17 #include "content/browser/bluetooth/bluetooth_blocklist.h" 18 #include "content/browser/bluetooth/bluetooth_blocklist.h"
18 #include "content/browser/bluetooth/bluetooth_device_chooser_controller.h" 19 #include "content/browser/bluetooth/bluetooth_device_chooser_controller.h"
19 #include "content/browser/bluetooth/bluetooth_metrics.h" 20 #include "content/browser/bluetooth/bluetooth_metrics.h"
20 #include "content/browser/bluetooth/frame_connected_bluetooth_devices.h" 21 #include "content/browser/bluetooth/frame_connected_bluetooth_devices.h"
21 #include "content/browser/renderer_host/render_process_host_impl.h" 22 #include "content/browser/renderer_host/render_process_host_impl.h"
22 #include "content/browser/storage_partition_impl.h" 23 #include "content/browser/storage_partition_impl.h"
23 #include "content/common/bluetooth/web_bluetooth_device_id.h" 24 #include "content/common/bluetooth/web_bluetooth_device_id.h"
24 #include "content/public/browser/browser_context.h" 25 #include "content/public/browser/browser_context.h"
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 return blink::mojom::WebBluetoothResult::DEVICE_NO_LONGER_IN_RANGE; 141 return blink::mojom::WebBluetoothResult::DEVICE_NO_LONGER_IN_RANGE;
141 } 142 }
142 143
143 device::BluetoothDevice* device = nullptr; 144 device::BluetoothDevice* device = nullptr;
144 device::BluetoothRemoteGattService* service = nullptr; 145 device::BluetoothRemoteGattService* service = nullptr;
145 device::BluetoothRemoteGattCharacteristic* characteristic = nullptr; 146 device::BluetoothRemoteGattCharacteristic* characteristic = nullptr;
146 device::BluetoothRemoteGattDescriptor* descriptor = nullptr; 147 device::BluetoothRemoteGattDescriptor* descriptor = nullptr;
147 CacheQueryOutcome outcome; 148 CacheQueryOutcome outcome;
148 }; 149 };
149 150
151 struct GATTNotifySessionAndCharacteristicClient {
152 GATTNotifySessionAndCharacteristicClient(
153 std::unique_ptr<device::BluetoothGattNotifySession> session,
154 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtr client)
155 : gatt_notify_session(std::move(session)),
156 characteristic_client(std::move(client)) {}
157
158 std::unique_ptr<device::BluetoothGattNotifySession> gatt_notify_session;
159 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtr
160 characteristic_client;
161 };
162
150 WebBluetoothServiceImpl::WebBluetoothServiceImpl( 163 WebBluetoothServiceImpl::WebBluetoothServiceImpl(
151 RenderFrameHost* render_frame_host, 164 RenderFrameHost* render_frame_host,
152 blink::mojom::WebBluetoothServiceRequest request) 165 blink::mojom::WebBluetoothServiceRequest request)
153 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)), 166 : WebContentsObserver(WebContents::FromRenderFrameHost(render_frame_host)),
154 connected_devices_(new FrameConnectedBluetoothDevices(render_frame_host)), 167 connected_devices_(new FrameConnectedBluetoothDevices(render_frame_host)),
155 render_frame_host_(render_frame_host), 168 render_frame_host_(render_frame_host),
156 binding_(this, std::move(request)), 169 binding_(this, std::move(request)),
157 weak_ptr_factory_(this) { 170 weak_ptr_factory_(this) {
158 DCHECK_CURRENTLY_ON(BrowserThread::UI); 171 DCHECK_CURRENTLY_ON(BrowserThread::UI);
159 CHECK(web_contents()); 172 CHECK(web_contents());
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 DCHECK_CURRENTLY_ON(BrowserThread::UI); 218 DCHECK_CURRENTLY_ON(BrowserThread::UI);
206 219
207 if (device_chooser_controller_.get()) { 220 if (device_chooser_controller_.get()) {
208 device_chooser_controller_->AddFilteredDevice(*device); 221 device_chooser_controller_->AddFilteredDevice(*device);
209 } 222 }
210 223
211 if (!device->IsGattConnected()) { 224 if (!device->IsGattConnected()) {
212 base::Optional<WebBluetoothDeviceId> device_id = 225 base::Optional<WebBluetoothDeviceId> device_id =
213 connected_devices_->CloseConnectionToDeviceWithAddress( 226 connected_devices_->CloseConnectionToDeviceWithAddress(
214 device->GetAddress()); 227 device->GetAddress());
215 if (device_id && client_) {
216 client_->GattServerDisconnected(device_id.value());
217 }
218 } 228 }
219 } 229 }
220 230
221 void WebBluetoothServiceImpl::GattServicesDiscovered( 231 void WebBluetoothServiceImpl::GattServicesDiscovered(
222 device::BluetoothAdapter* adapter, 232 device::BluetoothAdapter* adapter,
223 device::BluetoothDevice* device) { 233 device::BluetoothDevice* device) {
224 if (device_chooser_controller_.get()) { 234 if (device_chooser_controller_.get()) {
225 device_chooser_controller_->AddFilteredDevice(*device); 235 device_chooser_controller_->AddFilteredDevice(*device);
226 } 236 }
227 237
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 base::Bind(&WebBluetoothServiceImpl::NotifyCharacteristicValueChanged, 274 base::Bind(&WebBluetoothServiceImpl::NotifyCharacteristicValueChanged,
265 weak_ptr_factory_.GetWeakPtr(), 275 weak_ptr_factory_.GetWeakPtr(),
266 characteristic->GetIdentifier(), value))) { 276 characteristic->GetIdentifier(), value))) {
267 LOG(WARNING) << "No TaskRunner."; 277 LOG(WARNING) << "No TaskRunner.";
268 } 278 }
269 } 279 }
270 280
271 void WebBluetoothServiceImpl::NotifyCharacteristicValueChanged( 281 void WebBluetoothServiceImpl::NotifyCharacteristicValueChanged(
272 const std::string& characteristic_instance_id, 282 const std::string& characteristic_instance_id,
273 const std::vector<uint8_t>& value) { 283 const std::vector<uint8_t>& value) {
274 if (client_) { 284 auto iter =
275 client_->RemoteCharacteristicValueChanged(characteristic_instance_id, 285 characteristic_id_to_notify_session_.find(characteristic_instance_id);
276 value); 286 if (iter != characteristic_id_to_notify_session_.end()) {
287 iter->second->characteristic_client->RemoteCharacteristicValueChanged(
288 value);
277 } 289 }
278 } 290 }
279 291
280 void WebBluetoothServiceImpl::SetClient(
281 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo client) {
282 DCHECK(!client_.get());
283 client_.Bind(std::move(client));
284 }
285
286 void WebBluetoothServiceImpl::RequestDevice( 292 void WebBluetoothServiceImpl::RequestDevice(
287 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options, 293 blink::mojom::WebBluetoothRequestDeviceOptionsPtr options,
288 const RequestDeviceCallback& callback) { 294 const RequestDeviceCallback& callback) {
289 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); 295 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE);
290 RecordRequestDeviceOptions(options); 296 RecordRequestDeviceOptions(options);
291 297
292 if (!GetAdapter()) { 298 if (!GetAdapter()) {
293 if (BluetoothAdapterFactoryWrapper::Get().IsLowEnergyAvailable()) { 299 if (BluetoothAdapterFactoryWrapper::Get().IsLowEnergyAvailable()) {
294 BluetoothAdapterFactoryWrapper::Get().AcquireAdapter( 300 BluetoothAdapterFactoryWrapper::Get().AcquireAdapter(
295 this, base::Bind(&WebBluetoothServiceImpl::RequestDeviceImpl, 301 this, base::Bind(&WebBluetoothServiceImpl::RequestDeviceImpl,
296 weak_ptr_factory_.GetWeakPtr(), 302 weak_ptr_factory_.GetWeakPtr(),
297 base::Passed(std::move(options)), callback)); 303 base::Passed(std::move(options)), callback));
298 return; 304 return;
299 } 305 }
300 RecordRequestDeviceOutcome( 306 RecordRequestDeviceOutcome(
301 UMARequestDeviceOutcome::BLUETOOTH_LOW_ENERGY_NOT_AVAILABLE); 307 UMARequestDeviceOutcome::BLUETOOTH_LOW_ENERGY_NOT_AVAILABLE);
302 callback.Run( 308 callback.Run(
303 blink::mojom::WebBluetoothResult::BLUETOOTH_LOW_ENERGY_NOT_AVAILABLE, 309 blink::mojom::WebBluetoothResult::BLUETOOTH_LOW_ENERGY_NOT_AVAILABLE,
304 nullptr /* device */); 310 nullptr /* device */);
305 return; 311 return;
306 } 312 }
307 RequestDeviceImpl(std::move(options), callback, GetAdapter()); 313 RequestDeviceImpl(std::move(options), callback, GetAdapter());
308 } 314 }
309 315
310 void WebBluetoothServiceImpl::RemoteServerConnect( 316 void WebBluetoothServiceImpl::RemoteServerConnect(
311 const WebBluetoothDeviceId& device_id, 317 const WebBluetoothDeviceId& device_id,
318 blink::mojom::WebBluetoothServerClientAssociatedPtrInfo client,
312 const RemoteServerConnectCallback& callback) { 319 const RemoteServerConnectCallback& callback) {
313 DCHECK_CURRENTLY_ON(BrowserThread::UI); 320 DCHECK_CURRENTLY_ON(BrowserThread::UI);
314 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT); 321 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT);
315 322
316 const CacheQueryResult query_result = QueryCacheForDevice(device_id); 323 const CacheQueryResult query_result = QueryCacheForDevice(device_id);
317 324
318 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { 325 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
319 RecordConnectGATTOutcome(query_result.outcome); 326 RecordConnectGATTOutcome(query_result.outcome);
320 callback.Run(query_result.GetWebResult()); 327 callback.Run(query_result.GetWebResult());
321 return; 328 return;
322 } 329 }
323 330
324 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) { 331 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) {
325 DVLOG(1) << "Already connected."; 332 DVLOG(1) << "Already connected.";
326 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 333 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
327 return; 334 return;
328 } 335 }
329 336
330 // It's possible for WebBluetoothServiceImpl to issue two successive 337 // It's possible for WebBluetoothServiceImpl to issue two successive
331 // connection requests for which it would get two successive responses 338 // connection requests for which it would get two successive responses
332 // and consequently try to insert two BluetoothGattConnections for the 339 // and consequently try to insert two BluetoothGattConnections for the
333 // same device. WebBluetoothServiceImpl should reject or queue connection 340 // same device. WebBluetoothServiceImpl should reject or queue connection
334 // requests if there is a pending connection already, but the platform 341 // requests if there is a pending connection already, but the platform
335 // abstraction doesn't currently support checking for pending connections. 342 // abstraction doesn't currently support checking for pending connections.
336 // TODO(ortuno): CHECK that this never happens once the platform 343 // TODO(ortuno): CHECK that this never happens once the platform
337 // abstraction allows to check for pending connections. 344 // abstraction allows to check for pending connections.
338 // http://crbug.com/583544 345 // http://crbug.com/583544
339 const base::TimeTicks start_time = base::TimeTicks::Now(); 346 const base::TimeTicks start_time = base::TimeTicks::Now();
347 blink::mojom::WebBluetoothServerClientAssociatedPtr
348 web_bluetooth_server_client;
349 web_bluetooth_server_client.Bind(std::move(client));
340 query_result.device->CreateGattConnection( 350 query_result.device->CreateGattConnection(
341 base::Bind(&WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess, 351 base::Bind(&WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess,
342 weak_ptr_factory_.GetWeakPtr(), device_id, start_time, 352 weak_ptr_factory_.GetWeakPtr(), device_id, start_time,
343 callback), 353 base::Passed(&web_bluetooth_server_client), callback),
344 base::Bind(&WebBluetoothServiceImpl::OnCreateGATTConnectionFailed, 354 base::Bind(&WebBluetoothServiceImpl::OnCreateGATTConnectionFailed,
345 weak_ptr_factory_.GetWeakPtr(), start_time, callback)); 355 weak_ptr_factory_.GetWeakPtr(), start_time, callback));
346 } 356 }
347 357
348 void WebBluetoothServiceImpl::RemoteServerDisconnect( 358 void WebBluetoothServiceImpl::RemoteServerDisconnect(
349 const WebBluetoothDeviceId& device_id) { 359 const WebBluetoothDeviceId& device_id) {
350 DCHECK_CURRENTLY_ON(BrowserThread::UI); 360 DCHECK_CURRENTLY_ON(BrowserThread::UI);
351 RecordWebBluetoothFunctionCall( 361 RecordWebBluetoothFunctionCall(
352 UMAWebBluetoothFunction::REMOTE_GATT_SERVER_DISCONNECT); 362 UMAWebBluetoothFunction::REMOTE_GATT_SERVER_DISCONNECT);
353 363
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 query_result.characteristic->WriteRemoteCharacteristic( 663 query_result.characteristic->WriteRemoteCharacteristic(
654 value, 664 value,
655 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueSuccess, 665 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueSuccess,
656 weak_ptr_factory_.GetWeakPtr(), callback), 666 weak_ptr_factory_.GetWeakPtr(), callback),
657 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed, 667 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed,
658 weak_ptr_factory_.GetWeakPtr(), callback)); 668 weak_ptr_factory_.GetWeakPtr(), callback));
659 } 669 }
660 670
661 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications( 671 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications(
662 const std::string& characteristic_instance_id, 672 const std::string& characteristic_instance_id,
673 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtrInfo client,
663 const RemoteCharacteristicStartNotificationsCallback& callback) { 674 const RemoteCharacteristicStartNotificationsCallback& callback) {
664 DCHECK_CURRENTLY_ON(BrowserThread::UI); 675 DCHECK_CURRENTLY_ON(BrowserThread::UI);
665 RecordWebBluetoothFunctionCall( 676 RecordWebBluetoothFunctionCall(
666 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); 677 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS);
667 678
668 auto iter = 679 auto iter =
669 characteristic_id_to_notify_session_.find(characteristic_instance_id); 680 characteristic_id_to_notify_session_.find(characteristic_instance_id);
670 if (iter != characteristic_id_to_notify_session_.end() && 681 if (iter != characteristic_id_to_notify_session_.end() &&
671 iter->second->IsActive()) { 682 iter->second->gatt_notify_session->IsActive()) {
672 // If the frame has already started notifications and the notifications 683 // If the frame has already started notifications and the notifications
673 // are active we return SUCCESS. 684 // are active we return SUCCESS.
674 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 685 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
675 return; 686 return;
676 } 687 }
677 688
678 const CacheQueryResult query_result = 689 const CacheQueryResult query_result =
679 QueryCacheForCharacteristic(characteristic_instance_id); 690 QueryCacheForCharacteristic(characteristic_instance_id);
680 691
681 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { 692 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
682 return; 693 return;
683 } 694 }
684 695
685 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { 696 if (query_result.outcome != CacheQueryOutcome::SUCCESS) {
686 RecordStartNotificationsOutcome(query_result.outcome); 697 RecordStartNotificationsOutcome(query_result.outcome);
687 callback.Run(query_result.GetWebResult()); 698 callback.Run(query_result.GetWebResult());
688 return; 699 return;
689 } 700 }
690 701
691 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate = 702 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate =
692 query_result.characteristic->GetProperties() & 703 query_result.characteristic->GetProperties() &
693 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY | 704 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY |
694 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE); 705 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE);
695 if (!notify_or_indicate) { 706 if (!notify_or_indicate) {
696 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED); 707 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED);
697 return; 708 return;
698 } 709 }
699 710
711 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtr
712 characteristic_client;
713 characteristic_client.Bind(std::move(client));
714
700 query_result.characteristic->StartNotifySession( 715 query_result.characteristic->StartNotifySession(
701 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, 716 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess,
702 weak_ptr_factory_.GetWeakPtr(), callback), 717 weak_ptr_factory_.GetWeakPtr(),
718 base::Passed(&characteristic_client), callback),
703 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, 719 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed,
704 weak_ptr_factory_.GetWeakPtr(), callback)); 720 weak_ptr_factory_.GetWeakPtr(), callback));
705 } 721 }
706 722
707 void WebBluetoothServiceImpl::RemoteCharacteristicStopNotifications( 723 void WebBluetoothServiceImpl::RemoteCharacteristicStopNotifications(
708 const std::string& characteristic_instance_id, 724 const std::string& characteristic_instance_id,
709 const RemoteCharacteristicStopNotificationsCallback& callback) { 725 const RemoteCharacteristicStopNotificationsCallback& callback) {
710 DCHECK_CURRENTLY_ON(BrowserThread::UI); 726 DCHECK_CURRENTLY_ON(BrowserThread::UI);
711 RecordWebBluetoothFunctionCall( 727 RecordWebBluetoothFunctionCall(
712 UMAWebBluetoothFunction::CHARACTERISTIC_STOP_NOTIFICATIONS); 728 UMAWebBluetoothFunction::CHARACTERISTIC_STOP_NOTIFICATIONS);
713 729
714 const CacheQueryResult query_result = 730 const CacheQueryResult query_result =
715 QueryCacheForCharacteristic(characteristic_instance_id); 731 QueryCacheForCharacteristic(characteristic_instance_id);
716 732
717 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { 733 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) {
718 return; 734 return;
719 } 735 }
720 736
721 auto notify_session_iter = 737 auto notify_session_iter =
722 characteristic_id_to_notify_session_.find(characteristic_instance_id); 738 characteristic_id_to_notify_session_.find(characteristic_instance_id);
723 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { 739 if (notify_session_iter == characteristic_id_to_notify_session_.end()) {
724 // If the frame hasn't subscribed to notifications before we just 740 // If the frame hasn't subscribed to notifications before we just
725 // run the callback. 741 // run the callback.
726 callback.Run(); 742 callback.Run();
727 return; 743 return;
728 } 744 }
729 notify_session_iter->second->Stop(base::Bind( 745 notify_session_iter->second->gatt_notify_session->Stop(base::Bind(
730 &WebBluetoothServiceImpl::OnStopNotifySessionComplete, 746 &WebBluetoothServiceImpl::OnStopNotifySessionComplete,
731 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback)); 747 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback));
732 } 748 }
733 749
734 void WebBluetoothServiceImpl::RemoteDescriptorReadValue( 750 void WebBluetoothServiceImpl::RemoteDescriptorReadValue(
735 const std::string& descriptor_instance_id, 751 const std::string& descriptor_instance_id,
736 const RemoteDescriptorReadValueCallback& callback) { 752 const RemoteDescriptorReadValueCallback& callback) {
737 DCHECK_CURRENTLY_ON(BrowserThread::UI); 753 DCHECK_CURRENTLY_ON(BrowserThread::UI);
738 RecordWebBluetoothFunctionCall( 754 RecordWebBluetoothFunctionCall(
739 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE); 755 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE);
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 const RequestDeviceCallback& callback, 939 const RequestDeviceCallback& callback,
924 blink::mojom::WebBluetoothResult result) { 940 blink::mojom::WebBluetoothResult result) {
925 // Errors are recorded by the *device_chooser_controller_. 941 // Errors are recorded by the *device_chooser_controller_.
926 callback.Run(result, nullptr /* device */); 942 callback.Run(result, nullptr /* device */);
927 device_chooser_controller_.reset(); 943 device_chooser_controller_.reset();
928 } 944 }
929 945
930 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess( 946 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess(
931 const WebBluetoothDeviceId& device_id, 947 const WebBluetoothDeviceId& device_id,
932 base::TimeTicks start_time, 948 base::TimeTicks start_time,
949 blink::mojom::WebBluetoothServerClientAssociatedPtr client,
933 const RemoteServerConnectCallback& callback, 950 const RemoteServerConnectCallback& callback,
934 std::unique_ptr<device::BluetoothGattConnection> connection) { 951 std::unique_ptr<device::BluetoothGattConnection> connection) {
935 DCHECK_CURRENTLY_ON(BrowserThread::UI); 952 DCHECK_CURRENTLY_ON(BrowserThread::UI);
936 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); 953 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time);
937 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); 954 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS);
938 955
939 connected_devices_->Insert(device_id, std::move(connection)); 956 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) {
957 DVLOG(1) << "Already connected.";
958 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
959 return;
960 }
961
940 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); 962 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
963 connected_devices_->Insert(device_id, std::move(connection),
964 std::move(client));
941 } 965 }
942 966
943 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed( 967 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed(
944 base::TimeTicks start_time, 968 base::TimeTicks start_time,
945 const RemoteServerConnectCallback& callback, 969 const RemoteServerConnectCallback& callback,
946 device::BluetoothDevice::ConnectErrorCode error_code) { 970 device::BluetoothDevice::ConnectErrorCode error_code) {
947 DCHECK_CURRENTLY_ON(BrowserThread::UI); 971 DCHECK_CURRENTLY_ON(BrowserThread::UI);
948 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time); 972 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time);
949 callback.Run(TranslateConnectErrorAndRecord(error_code)); 973 callback.Run(TranslateConnectErrorAndRecord(error_code));
950 } 974 }
(...skipping 24 matching lines...) Expand all
975 999
976 void WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed( 1000 void WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed(
977 const RemoteCharacteristicWriteValueCallback& callback, 1001 const RemoteCharacteristicWriteValueCallback& callback,
978 device::BluetoothRemoteGattService::GattErrorCode error_code) { 1002 device::BluetoothRemoteGattService::GattErrorCode error_code) {
979 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1003 DCHECK_CURRENTLY_ON(BrowserThread::UI);
980 callback.Run(TranslateGATTErrorAndRecord( 1004 callback.Run(TranslateGATTErrorAndRecord(
981 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE)); 1005 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE));
982 } 1006 }
983 1007
984 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( 1008 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess(
1009 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtr client,
985 const RemoteCharacteristicStartNotificationsCallback& callback, 1010 const RemoteCharacteristicStartNotificationsCallback& callback,
986 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { 1011 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) {
987 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1012 DCHECK_CURRENTLY_ON(BrowserThread::UI);
988 // Copy Characteristic Instance ID before passing a unique pointer because 1013 // Copy Characteristic Instance ID before passing a unique pointer because
989 // compilers may evaluate arguments in any order. 1014 // compilers may evaluate arguments in any order.
990 std::string characteristic_instance_id = 1015 std::string characteristic_instance_id =
991 notify_session->GetCharacteristicIdentifier(); 1016 notify_session->GetCharacteristicIdentifier();
1017
1018 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
992 // Saving the BluetoothGattNotifySession keeps notifications active. 1019 // Saving the BluetoothGattNotifySession keeps notifications active.
1020 auto gatt_notify_session_and_client =
1021 base::MakeUnique<GATTNotifySessionAndCharacteristicClient>(
1022 std::move(notify_session), std::move(client));
993 characteristic_id_to_notify_session_[characteristic_instance_id] = 1023 characteristic_id_to_notify_session_[characteristic_instance_id] =
994 std::move(notify_session); 1024 std::move(gatt_notify_session_and_client);
995 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS);
996 } 1025 }
997 1026
998 void WebBluetoothServiceImpl::OnStartNotifySessionFailed( 1027 void WebBluetoothServiceImpl::OnStartNotifySessionFailed(
999 const RemoteCharacteristicStartNotificationsCallback& callback, 1028 const RemoteCharacteristicStartNotificationsCallback& callback,
1000 device::BluetoothRemoteGattService::GattErrorCode error_code) { 1029 device::BluetoothRemoteGattService::GattErrorCode error_code) {
1001 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1030 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1002 callback.Run(TranslateGATTErrorAndRecord( 1031 callback.Run(TranslateGATTErrorAndRecord(
1003 error_code, UMAGATTOperation::START_NOTIFICATIONS)); 1032 error_code, UMAGATTOperation::START_NOTIFICATIONS));
1004 } 1033 }
1005 1034
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1186 descriptor_id_to_characteristic_id_.clear(); 1215 descriptor_id_to_characteristic_id_.clear();
1187 characteristic_id_to_service_id_.clear(); 1216 characteristic_id_to_service_id_.clear();
1188 service_id_to_device_address_.clear(); 1217 service_id_to_device_address_.clear();
1189 connected_devices_.reset( 1218 connected_devices_.reset(
1190 new FrameConnectedBluetoothDevices(render_frame_host_)); 1219 new FrameConnectedBluetoothDevices(render_frame_host_));
1191 device_chooser_controller_.reset(); 1220 device_chooser_controller_.reset();
1192 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); 1221 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this);
1193 } 1222 }
1194 1223
1195 } // namespace content 1224 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698