OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "content/browser/bluetooth/bluetooth_blacklist.h" |
| 10 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" |
| 11 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 12 #include "content/public/browser/render_frame_host.h" |
| 13 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 14 |
| 15 using device::BluetoothGattService; |
| 16 |
| 17 namespace content { |
| 18 |
| 19 namespace { |
| 20 |
| 21 blink::mojom::WebBluetoothError TranslateGATTErrorAndRecord( |
| 22 BluetoothGattService::GattErrorCode error_code, |
| 23 UMAGATTOperation operation) { |
| 24 switch (error_code) { |
| 25 case BluetoothGattService::GATT_ERROR_UNKNOWN: |
| 26 RecordGATTOperationOutcome(operation, UMAGATTOperationOutcome::UNKNOWN); |
| 27 return blink::mojom::WebBluetoothError::GATT_UNKNOWN_ERROR; |
| 28 case BluetoothGattService::GATT_ERROR_FAILED: |
| 29 RecordGATTOperationOutcome(operation, UMAGATTOperationOutcome::FAILED); |
| 30 return blink::mojom::WebBluetoothError::GATT_UNKNOWN_FAILURE; |
| 31 case BluetoothGattService::GATT_ERROR_IN_PROGRESS: |
| 32 RecordGATTOperationOutcome(operation, |
| 33 UMAGATTOperationOutcome::IN_PROGRESS); |
| 34 return blink::mojom::WebBluetoothError::GATT_OPERATION_IN_PROGRESS; |
| 35 case BluetoothGattService::GATT_ERROR_INVALID_LENGTH: |
| 36 RecordGATTOperationOutcome(operation, |
| 37 UMAGATTOperationOutcome::INVALID_LENGTH); |
| 38 return blink::mojom::WebBluetoothError::GATT_INVALID_ATTRIBUTE_LENGTH; |
| 39 case BluetoothGattService::GATT_ERROR_NOT_PERMITTED: |
| 40 RecordGATTOperationOutcome(operation, |
| 41 UMAGATTOperationOutcome::NOT_PERMITTED); |
| 42 return blink::mojom::WebBluetoothError::GATT_NOT_PERMITTED; |
| 43 case BluetoothGattService::GATT_ERROR_NOT_AUTHORIZED: |
| 44 RecordGATTOperationOutcome(operation, |
| 45 UMAGATTOperationOutcome::NOT_AUTHORIZED); |
| 46 return blink::mojom::WebBluetoothError::GATT_NOT_AUTHORIZED; |
| 47 case BluetoothGattService::GATT_ERROR_NOT_PAIRED: |
| 48 RecordGATTOperationOutcome(operation, |
| 49 UMAGATTOperationOutcome::NOT_PAIRED); |
| 50 return blink::mojom::WebBluetoothError::GATT_NOT_PAIRED; |
| 51 case BluetoothGattService::GATT_ERROR_NOT_SUPPORTED: |
| 52 RecordGATTOperationOutcome(operation, |
| 53 UMAGATTOperationOutcome::NOT_SUPPORTED); |
| 54 return blink::mojom::WebBluetoothError::GATT_NOT_SUPPORTED; |
| 55 } |
| 56 NOTREACHED(); |
| 57 return blink::mojom::WebBluetoothError::GATT_UNTRANSLATED_ERROR_CODE; |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
| 62 using CacheQueryResult = BluetoothDispatcherHost::CacheQueryResult; |
| 63 |
| 64 WebBluetoothServiceImpl::WebBluetoothServiceImpl( |
| 65 RenderFrameHost* render_frame_host, |
| 66 blink::mojom::WebBluetoothServiceRequest request) |
| 67 : render_frame_host_(render_frame_host), |
| 68 binding_(this, std::move(request)), |
| 69 weak_ptr_factory_(this) { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 71 } |
| 72 |
| 73 WebBluetoothServiceImpl::~WebBluetoothServiceImpl() {} |
| 74 |
| 75 void WebBluetoothServiceImpl::RemoteCharacteristicWriteValue( |
| 76 const mojo::String& characteristic_instance_id, |
| 77 mojo::Array<uint8_t> value, |
| 78 const RemoteCharacteristicWriteValueCallback& callback) { |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 80 RecordWebBluetoothFunctionCall( |
| 81 UMAWebBluetoothFunction::CHARACTERISTIC_WRITE_VALUE); |
| 82 |
| 83 // We perform the length check on the renderer side. So if we |
| 84 // get a value with length > 512, we can assume it's a hostile |
| 85 // renderer and kill it. |
| 86 if (value.size() > 512) { |
| 87 CrashRendererAndClosePipe(bad_message::BDH_INVALID_WRITE_VALUE_LENGTH); |
| 88 return; |
| 89 } |
| 90 |
| 91 const CacheQueryResult query_result = |
| 92 GetBluetoothDispatcherHost()->QueryCacheForCharacteristic( |
| 93 GetOrigin(), characteristic_instance_id); |
| 94 |
| 95 if (query_result.outcome == CacheQueryOutcome::BAD_RENDERER) { |
| 96 binding_.Close(); |
| 97 return; |
| 98 } |
| 99 |
| 100 if (query_result.outcome != CacheQueryOutcome::SUCCESS) { |
| 101 RecordCharacteristicWriteValueOutcome(query_result.outcome); |
| 102 callback.Run(query_result.GetWebError()); |
| 103 return; |
| 104 } |
| 105 |
| 106 if (BluetoothBlacklist::Get().IsExcludedFromWrites( |
| 107 query_result.characteristic->GetUUID())) { |
| 108 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::BLACKLISTED); |
| 109 callback.Run(blink::mojom::WebBluetoothError::BLACKLISTED_WRITE); |
| 110 return; |
| 111 } |
| 112 |
| 113 query_result.characteristic->WriteRemoteCharacteristic( |
| 114 value.To<std::vector<uint8_t>>(), |
| 115 base::Bind(&WebBluetoothServiceImpl::OnWriteValueSuccess, |
| 116 weak_ptr_factory_.GetWeakPtr(), callback), |
| 117 base::Bind(&WebBluetoothServiceImpl::OnWriteValueFailed, |
| 118 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 119 } |
| 120 |
| 121 void WebBluetoothServiceImpl::OnWriteValueSuccess( |
| 122 const RemoteCharacteristicWriteValueCallback& callback) { |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 124 RecordCharacteristicWriteValueOutcome(UMAGATTOperationOutcome::SUCCESS); |
| 125 callback.Run(blink::mojom::WebBluetoothError::SUCCESS); |
| 126 } |
| 127 |
| 128 void WebBluetoothServiceImpl::OnWriteValueFailed( |
| 129 const RemoteCharacteristicWriteValueCallback& callback, |
| 130 device::BluetoothGattService::GattErrorCode error_code) { |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 132 callback.Run(TranslateGATTErrorAndRecord( |
| 133 error_code, UMAGATTOperation::CHARACTERISTIC_WRITE)); |
| 134 } |
| 135 |
| 136 RenderProcessHost* WebBluetoothServiceImpl::GetRenderProcessHost() { |
| 137 return render_frame_host_->GetProcess(); |
| 138 } |
| 139 |
| 140 BluetoothDispatcherHost* WebBluetoothServiceImpl::GetBluetoothDispatcherHost() { |
| 141 RenderProcessHostImpl* render_process_host_impl = |
| 142 static_cast<RenderProcessHostImpl*>(GetRenderProcessHost()); |
| 143 return render_process_host_impl->GetBluetoothDispatcherHost(); |
| 144 } |
| 145 |
| 146 void WebBluetoothServiceImpl::CrashRendererAndClosePipe( |
| 147 bad_message::BadMessageReason reason) { |
| 148 bad_message::ReceivedBadMessage(GetRenderProcessHost(), reason); |
| 149 binding_.Close(); |
| 150 } |
| 151 |
| 152 url::Origin WebBluetoothServiceImpl::GetOrigin() { |
| 153 return render_frame_host_->GetLastCommittedOrigin(); |
| 154 } |
| 155 |
| 156 } // namespace content |
OLD | NEW |