OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 server_client; | |
dcheng
2017/03/16 22:20:29
server_client is kind of a confusing name here. *s
juncai
2017/03/17 00:53:56
Changed it to |web_bluetooth_server_client|.
Done
| |
348 server_client.Bind(std::move(client)); | |
340 query_result.device->CreateGattConnection( | 349 query_result.device->CreateGattConnection( |
341 base::Bind(&WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess, | 350 base::Bind(&WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess, |
342 weak_ptr_factory_.GetWeakPtr(), device_id, start_time, | 351 weak_ptr_factory_.GetWeakPtr(), device_id, start_time, |
343 callback), | 352 base::Passed(std::move(server_client)), callback), |
dcheng
2017/03/16 22:20:29
Nit: base::Passed(&server_client)
juncai
2017/03/17 00:53:56
Done.
| |
344 base::Bind(&WebBluetoothServiceImpl::OnCreateGATTConnectionFailed, | 353 base::Bind(&WebBluetoothServiceImpl::OnCreateGATTConnectionFailed, |
345 weak_ptr_factory_.GetWeakPtr(), start_time, callback)); | 354 weak_ptr_factory_.GetWeakPtr(), start_time, callback)); |
346 } | 355 } |
347 | 356 |
348 void WebBluetoothServiceImpl::RemoteServerDisconnect( | 357 void WebBluetoothServiceImpl::RemoteServerDisconnect( |
349 const WebBluetoothDeviceId& device_id) { | 358 const WebBluetoothDeviceId& device_id) { |
350 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 359 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
351 RecordWebBluetoothFunctionCall( | 360 RecordWebBluetoothFunctionCall( |
352 UMAWebBluetoothFunction::REMOTE_GATT_SERVER_DISCONNECT); | 361 UMAWebBluetoothFunction::REMOTE_GATT_SERVER_DISCONNECT); |
353 | 362 |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
653 query_result.characteristic->WriteRemoteCharacteristic( | 662 query_result.characteristic->WriteRemoteCharacteristic( |
654 value, | 663 value, |
655 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueSuccess, | 664 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueSuccess, |
656 weak_ptr_factory_.GetWeakPtr(), callback), | 665 weak_ptr_factory_.GetWeakPtr(), callback), |
657 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed, | 666 base::Bind(&WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed, |
658 weak_ptr_factory_.GetWeakPtr(), callback)); | 667 weak_ptr_factory_.GetWeakPtr(), callback)); |
659 } | 668 } |
660 | 669 |
661 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications( | 670 void WebBluetoothServiceImpl::RemoteCharacteristicStartNotifications( |
662 const std::string& characteristic_instance_id, | 671 const std::string& characteristic_instance_id, |
672 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtrInfo client, | |
663 const RemoteCharacteristicStartNotificationsCallback& callback) { | 673 const RemoteCharacteristicStartNotificationsCallback& callback) { |
664 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 674 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
665 RecordWebBluetoothFunctionCall( | 675 RecordWebBluetoothFunctionCall( |
666 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); | 676 UMAWebBluetoothFunction::CHARACTERISTIC_START_NOTIFICATIONS); |
667 | 677 |
668 auto iter = | 678 auto iter = |
669 characteristic_id_to_notify_session_.find(characteristic_instance_id); | 679 characteristic_id_to_notify_session_.find(characteristic_instance_id); |
670 if (iter != characteristic_id_to_notify_session_.end() && | 680 if (iter != characteristic_id_to_notify_session_.end() && |
671 iter->second->IsActive()) { | 681 iter->second->gatt_notify_session->IsActive()) { |
672 // If the frame has already started notifications and the notifications | 682 // If the frame has already started notifications and the notifications |
673 // are active we return SUCCESS. | 683 // are active we return SUCCESS. |
674 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); | 684 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); |
675 return; | 685 return; |
676 } | 686 } |
677 | 687 |
678 const CacheQueryResult query_result = | 688 const CacheQueryResult query_result = |
679 QueryCacheForCharacteristic(characteristic_instance_id); | 689 QueryCacheForCharacteristic(characteristic_instance_id); |
680 | 690 |
681 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { | 691 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { |
682 return; | 692 return; |
683 } | 693 } |
684 | 694 |
685 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | 695 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { |
686 RecordStartNotificationsOutcome(query_result.outcome); | 696 RecordStartNotificationsOutcome(query_result.outcome); |
687 callback.Run(query_result.GetWebResult()); | 697 callback.Run(query_result.GetWebResult()); |
688 return; | 698 return; |
689 } | 699 } |
690 | 700 |
691 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate = | 701 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate = |
692 query_result.characteristic->GetProperties() & | 702 query_result.characteristic->GetProperties() & |
693 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY | | 703 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY | |
694 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE); | 704 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE); |
695 if (!notify_or_indicate) { | 705 if (!notify_or_indicate) { |
696 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED); | 706 callback.Run(blink::mojom::WebBluetoothResult::GATT_NOT_SUPPORTED); |
697 return; | 707 return; |
698 } | 708 } |
699 | 709 |
710 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtr | |
711 characteristic_client; | |
712 characteristic_client.Bind(std::move(client)); | |
713 | |
700 query_result.characteristic->StartNotifySession( | 714 query_result.characteristic->StartNotifySession( |
701 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, | 715 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, |
702 weak_ptr_factory_.GetWeakPtr(), callback), | 716 weak_ptr_factory_.GetWeakPtr(), |
717 base::Passed(std::move(characteristic_client)), callback), | |
dcheng
2017/03/16 22:20:29
Ditto
juncai
2017/03/17 00:53:56
Done.
| |
703 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, | 718 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, |
704 weak_ptr_factory_.GetWeakPtr(), callback)); | 719 weak_ptr_factory_.GetWeakPtr(), callback)); |
705 } | 720 } |
706 | 721 |
707 void WebBluetoothServiceImpl::RemoteCharacteristicStopNotifications( | 722 void WebBluetoothServiceImpl::RemoteCharacteristicStopNotifications( |
708 const std::string& characteristic_instance_id, | 723 const std::string& characteristic_instance_id, |
709 const RemoteCharacteristicStopNotificationsCallback& callback) { | 724 const RemoteCharacteristicStopNotificationsCallback& callback) { |
710 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 725 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
711 RecordWebBluetoothFunctionCall( | 726 RecordWebBluetoothFunctionCall( |
712 UMAWebBluetoothFunction::CHARACTERISTIC_STOP_NOTIFICATIONS); | 727 UMAWebBluetoothFunction::CHARACTERISTIC_STOP_NOTIFICATIONS); |
713 | 728 |
714 const CacheQueryResult query_result = | 729 const CacheQueryResult query_result = |
715 QueryCacheForCharacteristic(characteristic_instance_id); | 730 QueryCacheForCharacteristic(characteristic_instance_id); |
716 | 731 |
717 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { | 732 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { |
718 return; | 733 return; |
719 } | 734 } |
720 | 735 |
721 auto notify_session_iter = | 736 auto notify_session_iter = |
722 characteristic_id_to_notify_session_.find(characteristic_instance_id); | 737 characteristic_id_to_notify_session_.find(characteristic_instance_id); |
723 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { | 738 if (notify_session_iter == characteristic_id_to_notify_session_.end()) { |
724 // If the frame hasn't subscribed to notifications before we just | 739 // If the frame hasn't subscribed to notifications before we just |
725 // run the callback. | 740 // run the callback. |
726 callback.Run(); | 741 callback.Run(); |
727 return; | 742 return; |
728 } | 743 } |
729 notify_session_iter->second->Stop(base::Bind( | 744 notify_session_iter->second->gatt_notify_session->Stop(base::Bind( |
730 &WebBluetoothServiceImpl::OnStopNotifySessionComplete, | 745 &WebBluetoothServiceImpl::OnStopNotifySessionComplete, |
731 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback)); | 746 weak_ptr_factory_.GetWeakPtr(), characteristic_instance_id, callback)); |
732 } | 747 } |
733 | 748 |
734 void WebBluetoothServiceImpl::RemoteDescriptorReadValue( | 749 void WebBluetoothServiceImpl::RemoteDescriptorReadValue( |
735 const std::string& descriptor_instance_id, | 750 const std::string& descriptor_instance_id, |
736 const RemoteDescriptorReadValueCallback& callback) { | 751 const RemoteDescriptorReadValueCallback& callback) { |
737 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 752 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
738 RecordWebBluetoothFunctionCall( | 753 RecordWebBluetoothFunctionCall( |
739 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE); | 754 UMAWebBluetoothFunction::DESCRIPTOR_READ_VALUE); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
923 const RequestDeviceCallback& callback, | 938 const RequestDeviceCallback& callback, |
924 blink::mojom::WebBluetoothResult result) { | 939 blink::mojom::WebBluetoothResult result) { |
925 // Errors are recorded by the *device_chooser_controller_. | 940 // Errors are recorded by the *device_chooser_controller_. |
926 callback.Run(result, nullptr /* device */); | 941 callback.Run(result, nullptr /* device */); |
927 device_chooser_controller_.reset(); | 942 device_chooser_controller_.reset(); |
928 } | 943 } |
929 | 944 |
930 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess( | 945 void WebBluetoothServiceImpl::OnCreateGATTConnectionSuccess( |
931 const WebBluetoothDeviceId& device_id, | 946 const WebBluetoothDeviceId& device_id, |
932 base::TimeTicks start_time, | 947 base::TimeTicks start_time, |
948 blink::mojom::WebBluetoothServerClientAssociatedPtr client, | |
933 const RemoteServerConnectCallback& callback, | 949 const RemoteServerConnectCallback& callback, |
934 std::unique_ptr<device::BluetoothGattConnection> connection) { | 950 std::unique_ptr<device::BluetoothGattConnection> connection) { |
935 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 951 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
936 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); | 952 RecordConnectGATTTimeSuccess(base::TimeTicks::Now() - start_time); |
937 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); | 953 RecordConnectGATTOutcome(UMAConnectGATTOutcome::SUCCESS); |
938 | 954 |
939 connected_devices_->Insert(device_id, std::move(connection)); | 955 if (connected_devices_->IsConnectedToDeviceWithId(device_id)) { |
956 DVLOG(1) << "Already connected."; | |
957 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); | |
958 return; | |
959 } | |
960 | |
940 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); | 961 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); |
962 connected_devices_->Insert(device_id, std::move(connection), | |
963 std::move(client)); | |
941 } | 964 } |
942 | 965 |
943 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed( | 966 void WebBluetoothServiceImpl::OnCreateGATTConnectionFailed( |
944 base::TimeTicks start_time, | 967 base::TimeTicks start_time, |
945 const RemoteServerConnectCallback& callback, | 968 const RemoteServerConnectCallback& callback, |
946 device::BluetoothDevice::ConnectErrorCode error_code) { | 969 device::BluetoothDevice::ConnectErrorCode error_code) { |
947 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 970 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
948 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time); | 971 RecordConnectGATTTimeFailed(base::TimeTicks::Now() - start_time); |
949 callback.Run(TranslateConnectErrorAndRecord(error_code)); | 972 callback.Run(TranslateConnectErrorAndRecord(error_code)); |
950 } | 973 } |
(...skipping 24 matching lines...) Expand all Loading... | |
975 | 998 |
976 void WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed( | 999 void WebBluetoothServiceImpl::OnCharacteristicWriteValueFailed( |
977 const RemoteCharacteristicWriteValueCallback& callback, | 1000 const RemoteCharacteristicWriteValueCallback& callback, |
978 device::BluetoothRemoteGattService::GattErrorCode error_code) { | 1001 device::BluetoothRemoteGattService::GattErrorCode error_code) { |
979 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1002 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
980 callback.Run(TranslateGATTErrorAndRecord( | 1003 callback.Run(TranslateGATTErrorAndRecord( |
981 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE)); | 1004 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE)); |
982 } | 1005 } |
983 | 1006 |
984 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( | 1007 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( |
1008 blink::mojom::WebBluetoothCharacteristicClientAssociatedPtr client, | |
985 const RemoteCharacteristicStartNotificationsCallback& callback, | 1009 const RemoteCharacteristicStartNotificationsCallback& callback, |
986 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { | 1010 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { |
987 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1011 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
988 // Copy Characteristic Instance ID before passing a unique pointer because | 1012 // Copy Characteristic Instance ID before passing a unique pointer because |
989 // compilers may evaluate arguments in any order. | 1013 // compilers may evaluate arguments in any order. |
990 std::string characteristic_instance_id = | 1014 std::string characteristic_instance_id = |
991 notify_session->GetCharacteristicIdentifier(); | 1015 notify_session->GetCharacteristicIdentifier(); |
1016 | |
1017 auto iter = | |
1018 characteristic_id_to_notify_session_.find(characteristic_instance_id); | |
1019 if (iter != characteristic_id_to_notify_session_.end() && | |
1020 iter->second->gatt_notify_session->IsActive()) { | |
dcheng
2017/03/16 22:20:29
Just to help me understand... what is IsActive(),
juncai
2017/03/17 00:53:56
This is similar to:
https://cs.chromium.org/chromi
scheib
2017/03/17 01:25:15
Yes, the description for why IsActive may be false
juncai
2017/03/17 17:52:59
Done.
| |
1021 // If the frame has already started notifications and the notifications | |
1022 // are active we return SUCCESS. | |
1023 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); | |
1024 return; | |
1025 } | |
1026 | |
1027 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); | |
992 // Saving the BluetoothGattNotifySession keeps notifications active. | 1028 // Saving the BluetoothGattNotifySession keeps notifications active. |
1029 auto gatt_notify_session_and_client = | |
1030 base::MakeUnique<GATTNotifySessionAndCharacteristicClient>( | |
1031 std::move(notify_session), std::move(client)); | |
993 characteristic_id_to_notify_session_[characteristic_instance_id] = | 1032 characteristic_id_to_notify_session_[characteristic_instance_id] = |
994 std::move(notify_session); | 1033 std::move(gatt_notify_session_and_client); |
995 callback.Run(blink::mojom::WebBluetoothResult::SUCCESS); | |
996 } | 1034 } |
997 | 1035 |
998 void WebBluetoothServiceImpl::OnStartNotifySessionFailed( | 1036 void WebBluetoothServiceImpl::OnStartNotifySessionFailed( |
999 const RemoteCharacteristicStartNotificationsCallback& callback, | 1037 const RemoteCharacteristicStartNotificationsCallback& callback, |
1000 device::BluetoothRemoteGattService::GattErrorCode error_code) { | 1038 device::BluetoothRemoteGattService::GattErrorCode error_code) { |
1001 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 1039 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
1002 callback.Run(TranslateGATTErrorAndRecord( | 1040 callback.Run(TranslateGATTErrorAndRecord( |
1003 error_code, UMAGATTOperation::START_NOTIFICATIONS)); | 1041 error_code, UMAGATTOperation::START_NOTIFICATIONS)); |
1004 } | 1042 } |
1005 | 1043 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1186 descriptor_id_to_characteristic_id_.clear(); | 1224 descriptor_id_to_characteristic_id_.clear(); |
1187 characteristic_id_to_service_id_.clear(); | 1225 characteristic_id_to_service_id_.clear(); |
1188 service_id_to_device_address_.clear(); | 1226 service_id_to_device_address_.clear(); |
1189 connected_devices_.reset( | 1227 connected_devices_.reset( |
1190 new FrameConnectedBluetoothDevices(render_frame_host_)); | 1228 new FrameConnectedBluetoothDevices(render_frame_host_)); |
1191 device_chooser_controller_.reset(); | 1229 device_chooser_controller_.reset(); |
1192 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); | 1230 BluetoothAdapterFactoryWrapper::Get().ReleaseAdapter(this); |
1193 } | 1231 } |
1194 | 1232 |
1195 } // namespace content | 1233 } // namespace content |
OLD | NEW |