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 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" | 5 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" |
6 | 6 |
7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
8 #include "content/browser/bluetooth/bluetooth_blacklist.h" | 8 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
9 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" | 9 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" |
10 #include "content/browser/renderer_host/render_process_host_impl.h" | 10 #include "content/browser/renderer_host/render_process_host_impl.h" |
11 #include "content/public/browser/navigation_handle.h" | 11 #include "content/public/browser/navigation_handle.h" |
12 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
14 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 14 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
15 | 15 |
16 using device::BluetoothGattService; | 16 using device::BluetoothRemoteGattService; |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 blink::mojom::WebBluetoothError TranslateGATTErrorAndRecord( | 22 blink::mojom::WebBluetoothError TranslateGATTErrorAndRecord( |
23 BluetoothGattService::GattErrorCode error_code, | 23 BluetoothRemoteGattService::GattErrorCode error_code, |
24 UMAGATTOperation operation) { | 24 UMAGATTOperation operation) { |
25 switch (error_code) { | 25 switch (error_code) { |
26 case BluetoothGattService::GATT_ERROR_UNKNOWN: | 26 case BluetoothRemoteGattService::GATT_ERROR_UNKNOWN: |
27 RecordGATTOperationOutcome(operation, UMAGATTOperationOutcome::UNKNOWN); | 27 RecordGATTOperationOutcome(operation, UMAGATTOperationOutcome::UNKNOWN); |
28 return blink::mojom::WebBluetoothError::GATT_UNKNOWN_ERROR; | 28 return blink::mojom::WebBluetoothError::GATT_UNKNOWN_ERROR; |
29 case BluetoothGattService::GATT_ERROR_FAILED: | 29 case BluetoothRemoteGattService::GATT_ERROR_FAILED: |
30 RecordGATTOperationOutcome(operation, UMAGATTOperationOutcome::FAILED); | 30 RecordGATTOperationOutcome(operation, UMAGATTOperationOutcome::FAILED); |
31 return blink::mojom::WebBluetoothError::GATT_UNKNOWN_FAILURE; | 31 return blink::mojom::WebBluetoothError::GATT_UNKNOWN_FAILURE; |
32 case BluetoothGattService::GATT_ERROR_IN_PROGRESS: | 32 case BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS: |
33 RecordGATTOperationOutcome(operation, | 33 RecordGATTOperationOutcome(operation, |
34 UMAGATTOperationOutcome::IN_PROGRESS); | 34 UMAGATTOperationOutcome::IN_PROGRESS); |
35 return blink::mojom::WebBluetoothError::GATT_OPERATION_IN_PROGRESS; | 35 return blink::mojom::WebBluetoothError::GATT_OPERATION_IN_PROGRESS; |
36 case BluetoothGattService::GATT_ERROR_INVALID_LENGTH: | 36 case BluetoothRemoteGattService::GATT_ERROR_INVALID_LENGTH: |
37 RecordGATTOperationOutcome(operation, | 37 RecordGATTOperationOutcome(operation, |
38 UMAGATTOperationOutcome::INVALID_LENGTH); | 38 UMAGATTOperationOutcome::INVALID_LENGTH); |
39 return blink::mojom::WebBluetoothError::GATT_INVALID_ATTRIBUTE_LENGTH; | 39 return blink::mojom::WebBluetoothError::GATT_INVALID_ATTRIBUTE_LENGTH; |
40 case BluetoothGattService::GATT_ERROR_NOT_PERMITTED: | 40 case BluetoothRemoteGattService::GATT_ERROR_NOT_PERMITTED: |
41 RecordGATTOperationOutcome(operation, | 41 RecordGATTOperationOutcome(operation, |
42 UMAGATTOperationOutcome::NOT_PERMITTED); | 42 UMAGATTOperationOutcome::NOT_PERMITTED); |
43 return blink::mojom::WebBluetoothError::GATT_NOT_PERMITTED; | 43 return blink::mojom::WebBluetoothError::GATT_NOT_PERMITTED; |
44 case BluetoothGattService::GATT_ERROR_NOT_AUTHORIZED: | 44 case BluetoothRemoteGattService::GATT_ERROR_NOT_AUTHORIZED: |
45 RecordGATTOperationOutcome(operation, | 45 RecordGATTOperationOutcome(operation, |
46 UMAGATTOperationOutcome::NOT_AUTHORIZED); | 46 UMAGATTOperationOutcome::NOT_AUTHORIZED); |
47 return blink::mojom::WebBluetoothError::GATT_NOT_AUTHORIZED; | 47 return blink::mojom::WebBluetoothError::GATT_NOT_AUTHORIZED; |
48 case BluetoothGattService::GATT_ERROR_NOT_PAIRED: | 48 case BluetoothRemoteGattService::GATT_ERROR_NOT_PAIRED: |
49 RecordGATTOperationOutcome(operation, | 49 RecordGATTOperationOutcome(operation, |
50 UMAGATTOperationOutcome::NOT_PAIRED); | 50 UMAGATTOperationOutcome::NOT_PAIRED); |
51 return blink::mojom::WebBluetoothError::GATT_NOT_PAIRED; | 51 return blink::mojom::WebBluetoothError::GATT_NOT_PAIRED; |
52 case BluetoothGattService::GATT_ERROR_NOT_SUPPORTED: | 52 case BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED: |
53 RecordGATTOperationOutcome(operation, | 53 RecordGATTOperationOutcome(operation, |
54 UMAGATTOperationOutcome::NOT_SUPPORTED); | 54 UMAGATTOperationOutcome::NOT_SUPPORTED); |
55 return blink::mojom::WebBluetoothError::GATT_NOT_SUPPORTED; | 55 return blink::mojom::WebBluetoothError::GATT_NOT_SUPPORTED; |
56 } | 56 } |
57 NOTREACHED(); | 57 NOTREACHED(); |
58 return blink::mojom::WebBluetoothError::GATT_UNTRANSLATED_ERROR_CODE; | 58 return blink::mojom::WebBluetoothError::GATT_UNTRANSLATED_ERROR_CODE; |
59 } | 59 } |
60 | 60 |
61 } // namespace | 61 } // namespace |
62 | 62 |
(...skipping 27 matching lines...) Expand all Loading... |
90 if (navigation_handle->HasCommitted() && | 90 if (navigation_handle->HasCommitted() && |
91 navigation_handle->GetRenderFrameHost() == render_frame_host_ && | 91 navigation_handle->GetRenderFrameHost() == render_frame_host_ && |
92 !navigation_handle->IsSamePage()) { | 92 !navigation_handle->IsSamePage()) { |
93 // After navigation we need to clear the frame's state. | 93 // After navigation we need to clear the frame's state. |
94 characteristic_id_to_notify_session_.clear(); | 94 characteristic_id_to_notify_session_.clear(); |
95 } | 95 } |
96 } | 96 } |
97 | 97 |
98 void WebBluetoothServiceImpl::GattCharacteristicValueChanged( | 98 void WebBluetoothServiceImpl::GattCharacteristicValueChanged( |
99 device::BluetoothAdapter* adapter, | 99 device::BluetoothAdapter* adapter, |
100 device::BluetoothGattCharacteristic* characteristic, | 100 device::BluetoothRemoteGattCharacteristic* characteristic, |
101 const std::vector<uint8_t>& value) { | 101 const std::vector<uint8_t>& value) { |
102 // TODO(ortuno): Only send characteristic value changed events for | 102 // TODO(ortuno): Only send characteristic value changed events for |
103 // characteristics that we've returned in the past. We can't yet do | 103 // characteristics that we've returned in the past. We can't yet do |
104 // this because WebBluetoothServiceImpl doesn't have direct access | 104 // this because WebBluetoothServiceImpl doesn't have direct access |
105 // to the list of returned characteristics. | 105 // to the list of returned characteristics. |
106 // https://crbug.com/508771 | 106 // https://crbug.com/508771 |
107 if (BluetoothBlacklist::Get().IsExcluded(characteristic->GetUUID())) { | 107 if (BluetoothBlacklist::Get().IsExcluded(characteristic->GetUUID())) { |
108 return; | 108 return; |
109 } | 109 } |
110 | 110 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 binding_.Close(); | 242 binding_.Close(); |
243 return; | 243 return; |
244 } | 244 } |
245 | 245 |
246 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { | 246 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { |
247 RecordStartNotificationsOutcome(query_result.outcome); | 247 RecordStartNotificationsOutcome(query_result.outcome); |
248 callback.Run(query_result.GetWebError()); | 248 callback.Run(query_result.GetWebError()); |
249 return; | 249 return; |
250 } | 250 } |
251 | 251 |
252 device::BluetoothGattCharacteristic::Properties notify_or_indicate = | 252 device::BluetoothRemoteGattCharacteristic::Properties notify_or_indicate = |
253 query_result.characteristic->GetProperties() & | 253 query_result.characteristic->GetProperties() & |
254 (device::BluetoothGattCharacteristic::PROPERTY_NOTIFY | | 254 (device::BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY | |
255 device::BluetoothGattCharacteristic::PROPERTY_INDICATE); | 255 device::BluetoothRemoteGattCharacteristic::PROPERTY_INDICATE); |
256 if (!notify_or_indicate) { | 256 if (!notify_or_indicate) { |
257 callback.Run(blink::mojom::WebBluetoothError::GATT_NOT_SUPPORTED); | 257 callback.Run(blink::mojom::WebBluetoothError::GATT_NOT_SUPPORTED); |
258 return; | 258 return; |
259 } | 259 } |
260 | 260 |
261 query_result.characteristic->StartNotifySession( | 261 query_result.characteristic->StartNotifySession( |
262 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, | 262 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionSuccess, |
263 weak_ptr_factory_.GetWeakPtr(), callback), | 263 weak_ptr_factory_.GetWeakPtr(), callback), |
264 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, | 264 base::Bind(&WebBluetoothServiceImpl::OnStartNotifySessionFailed, |
265 weak_ptr_factory_.GetWeakPtr(), callback)); | 265 weak_ptr_factory_.GetWeakPtr(), callback)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 const RemoteCharacteristicReadValueCallback& callback, | 298 const RemoteCharacteristicReadValueCallback& callback, |
299 const std::vector<uint8_t>& value) { | 299 const std::vector<uint8_t>& value) { |
300 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 300 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
301 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS); | 301 RecordCharacteristicReadValueOutcome(UMAGATTOperationOutcome::SUCCESS); |
302 callback.Run(blink::mojom::WebBluetoothError::SUCCESS, | 302 callback.Run(blink::mojom::WebBluetoothError::SUCCESS, |
303 mojo::Array<uint8_t>::From(value)); | 303 mojo::Array<uint8_t>::From(value)); |
304 } | 304 } |
305 | 305 |
306 void WebBluetoothServiceImpl::OnReadValueFailed( | 306 void WebBluetoothServiceImpl::OnReadValueFailed( |
307 const RemoteCharacteristicReadValueCallback& callback, | 307 const RemoteCharacteristicReadValueCallback& callback, |
308 device::BluetoothGattService::GattErrorCode error_code) { | 308 device::BluetoothRemoteGattService::GattErrorCode error_code) { |
309 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 309 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
310 callback.Run(TranslateGATTErrorAndRecord( | 310 callback.Run(TranslateGATTErrorAndRecord( |
311 error_code, UMAGATTOperation::CHARACTERISTIC_READ), | 311 error_code, UMAGATTOperation::CHARACTERISTIC_READ), |
312 nullptr /* value */); | 312 nullptr /* value */); |
313 } | 313 } |
314 | 314 |
315 void WebBluetoothServiceImpl::OnWriteValueSuccess( | 315 void WebBluetoothServiceImpl::OnWriteValueSuccess( |
316 const RemoteCharacteristicWriteValueCallback& callback) { | 316 const RemoteCharacteristicWriteValueCallback& callback) { |
317 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 317 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
318 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS); | 318 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS); |
319 callback.Run(blink::mojom::WebBluetoothError::SUCCESS); | 319 callback.Run(blink::mojom::WebBluetoothError::SUCCESS); |
320 } | 320 } |
321 | 321 |
322 void WebBluetoothServiceImpl::OnWriteValueFailed( | 322 void WebBluetoothServiceImpl::OnWriteValueFailed( |
323 const RemoteCharacteristicWriteValueCallback& callback, | 323 const RemoteCharacteristicWriteValueCallback& callback, |
324 device::BluetoothGattService::GattErrorCode error_code) { | 324 device::BluetoothRemoteGattService::GattErrorCode error_code) { |
325 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 325 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
326 callback.Run(TranslateGATTErrorAndRecord( | 326 callback.Run(TranslateGATTErrorAndRecord( |
327 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE)); | 327 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE)); |
328 } | 328 } |
329 | 329 |
330 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( | 330 void WebBluetoothServiceImpl::OnStartNotifySessionSuccess( |
331 const RemoteCharacteristicStartNotificationsCallback& callback, | 331 const RemoteCharacteristicStartNotificationsCallback& callback, |
332 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { | 332 std::unique_ptr<device::BluetoothGattNotifySession> notify_session) { |
333 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 333 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
334 // Copy Characteristic Instance ID before passing a unique pointer because | 334 // Copy Characteristic Instance ID before passing a unique pointer because |
335 // compilers may evaluate arguments in any order. | 335 // compilers may evaluate arguments in any order. |
336 std::string characteristic_instance_id = | 336 std::string characteristic_instance_id = |
337 notify_session->GetCharacteristicIdentifier(); | 337 notify_session->GetCharacteristicIdentifier(); |
338 // Saving the BluetoothGattNotifySession keeps notifications active. | 338 // Saving the BluetoothGattNotifySession keeps notifications active. |
339 characteristic_id_to_notify_session_[characteristic_instance_id] = | 339 characteristic_id_to_notify_session_[characteristic_instance_id] = |
340 std::move(notify_session); | 340 std::move(notify_session); |
341 callback.Run(blink::mojom::WebBluetoothError::SUCCESS); | 341 callback.Run(blink::mojom::WebBluetoothError::SUCCESS); |
342 } | 342 } |
343 | 343 |
344 void WebBluetoothServiceImpl::OnStartNotifySessionFailed( | 344 void WebBluetoothServiceImpl::OnStartNotifySessionFailed( |
345 const RemoteCharacteristicStartNotificationsCallback& callback, | 345 const RemoteCharacteristicStartNotificationsCallback& callback, |
346 device::BluetoothGattService::GattErrorCode error_code) { | 346 device::BluetoothRemoteGattService::GattErrorCode error_code) { |
347 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 347 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
348 callback.Run(TranslateGATTErrorAndRecord( | 348 callback.Run(TranslateGATTErrorAndRecord( |
349 error_code, UMAGATTOperation::START_NOTIFICATIONS)); | 349 error_code, UMAGATTOperation::START_NOTIFICATIONS)); |
350 } | 350 } |
351 | 351 |
352 void WebBluetoothServiceImpl::OnStopNotifySessionComplete( | 352 void WebBluetoothServiceImpl::OnStopNotifySessionComplete( |
353 const std::string& characteristic_instance_id, | 353 const std::string& characteristic_instance_id, |
354 const RemoteCharacteristicStopNotificationsCallback& callback) { | 354 const RemoteCharacteristicStopNotificationsCallback& callback) { |
355 characteristic_id_to_notify_session_.erase(characteristic_instance_id); | 355 characteristic_id_to_notify_session_.erase(characteristic_instance_id); |
356 callback.Run(); | 356 callback.Run(); |
(...skipping 13 matching lines...) Expand all Loading... |
370 bad_message::BadMessageReason reason) { | 370 bad_message::BadMessageReason reason) { |
371 bad_message::ReceivedBadMessage(GetRenderProcessHost(), reason); | 371 bad_message::ReceivedBadMessage(GetRenderProcessHost(), reason); |
372 binding_.Close(); | 372 binding_.Close(); |
373 } | 373 } |
374 | 374 |
375 url::Origin WebBluetoothServiceImpl::GetOrigin() { | 375 url::Origin WebBluetoothServiceImpl::GetOrigin() { |
376 return render_frame_host_->GetLastCommittedOrigin(); | 376 return render_frame_host_->GetLastCommittedOrigin(); |
377 } | 377 } |
378 | 378 |
379 } // namespace content | 379 } // namespace content |
OLD | NEW |