| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // NETWORK_ERROR Note: | 5 // NETWORK_ERROR Note: |
| 6 // When a device can't be found in the BluetoothAdapter, that generally | 6 // When a device can't be found in the BluetoothAdapter, that generally |
| 7 // indicates that it's gone out of range. We reject with a NetworkError in that | 7 // indicates that it's gone out of range. We reject with a NetworkError in that |
| 8 // case. | 8 // case. |
| 9 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothdevice-conne
ctgatt | 9 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothdevice-conne
ctgatt |
| 10 | 10 |
| 11 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" | 11 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" |
| 12 | 12 |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/hash.h" |
| 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/metrics/sparse_histogram.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 15 #include "content/browser/bad_message.h" | 17 #include "content/browser/bad_message.h" |
| 16 #include "content/browser/frame_host/render_frame_host_impl.h" | 18 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 17 #include "content/common/bluetooth/bluetooth_messages.h" | 19 #include "content/common/bluetooth/bluetooth_messages.h" |
| 18 #include "device/bluetooth/bluetooth_adapter.h" | 20 #include "device/bluetooth/bluetooth_adapter.h" |
| 19 #include "device/bluetooth/bluetooth_adapter_factory.h" | 21 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 20 #include "device/bluetooth/bluetooth_device.h" | 22 #include "device/bluetooth/bluetooth_device.h" |
| 21 #include "device/bluetooth/bluetooth_discovery_session.h" | 23 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 22 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 24 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 23 #include "device/bluetooth/bluetooth_gatt_service.h" | 25 #include "device/bluetooth/bluetooth_gatt_service.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 51 NO_MATCHING_DEVICES_FOUND = 5, | 53 NO_MATCHING_DEVICES_FOUND = 5, |
| 52 BLUETOOTH_ADAPTER_NOT_PRESENT = 6, | 54 BLUETOOTH_ADAPTER_NOT_PRESENT = 6, |
| 53 BLUETOOTH_ADAPTER_OFF = 7, | 55 BLUETOOTH_ADAPTER_OFF = 7, |
| 54 // NOTE: Add new requestDevice() outcomes immediately above this line. Make | 56 // NOTE: Add new requestDevice() outcomes immediately above this line. Make |
| 55 // sure to update the enum list in | 57 // sure to update the enum list in |
| 56 // tools/metrics/histogram/histograms.xml accordingly. | 58 // tools/metrics/histogram/histograms.xml accordingly. |
| 57 COUNT | 59 COUNT |
| 58 }; | 60 }; |
| 59 | 61 |
| 60 void RecordRequestDeviceOutcome(UMARequestDeviceOutcome outcome) { | 62 void RecordRequestDeviceOutcome(UMARequestDeviceOutcome outcome) { |
| 61 UMA_HISTOGRAM_ENUMERATION("Bluetooth.RequestDevice.Outcome", | 63 UMA_HISTOGRAM_ENUMERATION("Bluetooth.Web.RequestDevice.Outcome", |
| 62 static_cast<int>(outcome), | 64 static_cast<int>(outcome), |
| 63 static_cast<int>(UMARequestDeviceOutcome::COUNT)); | 65 static_cast<int>(UMARequestDeviceOutcome::COUNT)); |
| 64 } | 66 } |
| 67 // TODO(ortuno): Remove once we have a macro to histogram strings. |
| 68 // http://crbug.com/520284 |
| 69 int HashUUID(const std::string& uuid) { |
| 70 uint32 data = base::SuperFastHash(uuid.data(), uuid.size()); |
| 71 |
| 72 // Strip off the signed bit because UMA doesn't support negative values, |
| 73 // but takes a signed int as input. |
| 74 return static_cast<int>(data & 0x7fffffff); |
| 75 } |
| 76 |
| 77 void RecordRequestDeviceFilters( |
| 78 const std::vector<content::BluetoothScanFilter>& filters) { |
| 79 UMA_HISTOGRAM_COUNTS_100("Bluetooth.Web.RequestDevice.Filters.Count", |
| 80 filters.size()); |
| 81 for (const content::BluetoothScanFilter& filter : filters) { |
| 82 UMA_HISTOGRAM_COUNTS_100("Bluetooth.Web.RequestDevice.FilterSize", |
| 83 filter.services.size()); |
| 84 for (const BluetoothUUID& service : filter.services) { |
| 85 // TODO(ortuno): Use a macro to histogram strings. |
| 86 // http://crbug.com/520284 |
| 87 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 88 "Bluetooth.Web.RequestDevice.Filters.Services", |
| 89 HashUUID(service.canonical_value())); |
| 90 } |
| 91 } |
| 92 } |
| 93 |
| 94 void RecordRequestDeviceOptionalServices( |
| 95 const std::vector<BluetoothUUID>& optional_services) { |
| 96 UMA_HISTOGRAM_COUNTS_100("Bluetooth.Web.RequestDevice.OptionalServices.Count", |
| 97 optional_services.size()); |
| 98 for (const BluetoothUUID& service : optional_services) { |
| 99 // TODO(ortuno): Use a macro to histogram strings. |
| 100 // http://crbug.com/520284 |
| 101 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 102 "Bluetooth.Web.RequestDevice.OptionalServices.Services", |
| 103 HashUUID(service.canonical_value())); |
| 104 } |
| 105 } |
| 106 |
| 107 void RecordUnionOfServices( |
| 108 const std::vector<content::BluetoothScanFilter>& filters, |
| 109 const std::vector<BluetoothUUID>& optional_services) { |
| 110 std::set<BluetoothUUID> union_of_services(optional_services.begin(), |
| 111 optional_services.end()); |
| 112 |
| 113 for (const content::BluetoothScanFilter& filter : filters) |
| 114 union_of_services.insert(filter.services.begin(), filter.services.end()); |
| 115 |
| 116 UMA_HISTOGRAM_COUNTS_100("Bluetooth.Web.RequestDevice.UnionOfServices.Count", |
| 117 union_of_services.size()); |
| 118 } |
| 65 | 119 |
| 66 enum class UMAWebBluetoothFunction { | 120 enum class UMAWebBluetoothFunction { |
| 67 REQUEST_DEVICE, | 121 REQUEST_DEVICE, |
| 68 CONNECT_GATT, | 122 CONNECT_GATT, |
| 69 GET_PRIMARY_SERVICE, | 123 GET_PRIMARY_SERVICE, |
| 70 GET_CHARACTERISTIC, | 124 GET_CHARACTERISTIC, |
| 71 CHARACTERISTIC_READ_VALUE, | 125 CHARACTERISTIC_READ_VALUE, |
| 72 CHARACTERISTIC_WRITE_VALUE, | 126 CHARACTERISTIC_WRITE_VALUE, |
| 73 // NOTE: Add new actions immediately above this line. Make sure to update the | 127 // NOTE: Add new actions immediately above this line. Make sure to update the |
| 74 // enum list in tools/metrics/histogram/histograms.xml accordingly. | 128 // enum list in tools/metrics/histogram/histograms.xml accordingly. |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 } | 318 } |
| 265 | 319 |
| 266 void BluetoothDispatcherHost::OnRequestDevice( | 320 void BluetoothDispatcherHost::OnRequestDevice( |
| 267 int thread_id, | 321 int thread_id, |
| 268 int request_id, | 322 int request_id, |
| 269 int frame_routing_id, | 323 int frame_routing_id, |
| 270 const std::vector<BluetoothScanFilter>& filters, | 324 const std::vector<BluetoothScanFilter>& filters, |
| 271 const std::vector<BluetoothUUID>& optional_services) { | 325 const std::vector<BluetoothUUID>& optional_services) { |
| 272 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 326 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 273 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); | 327 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); |
| 328 RecordRequestDeviceFilters(filters); |
| 329 RecordRequestDeviceOptionalServices(optional_services); |
| 330 RecordUnionOfServices(filters, optional_services); |
| 331 |
| 332 VLOG(1) << "requestDevice called with the following filters: "; |
| 333 for (const BluetoothScanFilter& filter : filters) { |
| 334 VLOG(1) << "\t["; |
| 335 for (const BluetoothUUID& service : filter.services) |
| 336 VLOG(1) << "\t\t" << service.value(); |
| 337 VLOG(1) << "\t]"; |
| 338 } |
| 339 |
| 340 VLOG(1) << "requestDevice called with the following optional services: "; |
| 341 for (const BluetoothUUID& service : optional_services) |
| 342 VLOG(1) << "\t" << service.value(); |
| 274 | 343 |
| 275 RenderFrameHostImpl* render_frame_host = | 344 RenderFrameHostImpl* render_frame_host = |
| 276 RenderFrameHostImpl::FromID(render_process_id_, frame_routing_id); | 345 RenderFrameHostImpl::FromID(render_process_id_, frame_routing_id); |
| 277 | 346 |
| 278 if (!render_frame_host) { | 347 if (!render_frame_host) { |
| 279 DLOG(WARNING) | 348 DLOG(WARNING) |
| 280 << "Got a requestDevice IPC without a matching RenderFrameHost: " | 349 << "Got a requestDevice IPC without a matching RenderFrameHost: " |
| 281 << render_process_id_ << ", " << frame_routing_id; | 350 << render_process_id_ << ", " << frame_routing_id; |
| 282 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::NO_RENDER_FRAME); | 351 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::NO_RENDER_FRAME); |
| 283 Send(new BluetoothMsg_RequestDeviceError( | 352 Send(new BluetoothMsg_RequestDeviceError( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 request_device_sessions_.erase(std::make_pair(thread_id, request_id)); | 391 request_device_sessions_.erase(std::make_pair(thread_id, request_id)); |
| 323 return; | 392 return; |
| 324 } | 393 } |
| 325 adapter_->StartDiscoverySessionWithFilter( | 394 adapter_->StartDiscoverySessionWithFilter( |
| 326 ComputeScanFilter(filters), | 395 ComputeScanFilter(filters), |
| 327 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, | 396 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, |
| 328 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), | 397 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
| 329 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, | 398 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, |
| 330 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | 399 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
| 331 } else { | 400 } else { |
| 332 DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice."; | 401 VLOG(1) << "No BluetoothAdapter. Can't serve requestDevice."; |
| 333 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::NO_BLUETOOTH_ADAPTER); | 402 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::NO_BLUETOOTH_ADAPTER); |
| 334 Send(new BluetoothMsg_RequestDeviceError( | 403 Send(new BluetoothMsg_RequestDeviceError( |
| 335 thread_id, request_id, WebBluetoothError::NoBluetoothAdapter)); | 404 thread_id, request_id, WebBluetoothError::NoBluetoothAdapter)); |
| 336 } | 405 } |
| 337 return; | 406 return; |
| 338 } | 407 } |
| 339 | 408 |
| 340 void BluetoothDispatcherHost::OnConnectGATT( | 409 void BluetoothDispatcherHost::OnConnectGATT( |
| 341 int thread_id, | 410 int thread_id, |
| 342 int request_id, | 411 int request_id, |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 device->GetUUIDs())); // uuids | 701 device->GetUUIDs())); // uuids |
| 633 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::SUCCESS); | 702 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::SUCCESS); |
| 634 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, | 703 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, |
| 635 device_ipc)); | 704 device_ipc)); |
| 636 request_device_sessions_.erase(session); | 705 request_device_sessions_.erase(session); |
| 637 return; | 706 return; |
| 638 } | 707 } |
| 639 } | 708 } |
| 640 RecordRequestDeviceOutcome( | 709 RecordRequestDeviceOutcome( |
| 641 UMARequestDeviceOutcome::NO_MATCHING_DEVICES_FOUND); | 710 UMARequestDeviceOutcome::NO_MATCHING_DEVICES_FOUND); |
| 711 VLOG(1) << "No matching Bluetooth Devices found"; |
| 642 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 712 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
| 643 WebBluetoothError::NoDevicesFound)); | 713 WebBluetoothError::NoDevicesFound)); |
| 644 request_device_sessions_.erase(session); | 714 request_device_sessions_.erase(session); |
| 645 } | 715 } |
| 646 | 716 |
| 647 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, | 717 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, |
| 648 int request_id) { | 718 int request_id) { |
| 649 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 719 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 650 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; | 720 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; |
| 651 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::DISCOVERY_STOP_FAILED); | 721 RecordRequestDeviceOutcome(UMARequestDeviceOutcome::DISCOVERY_STOP_FAILED); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 | 804 |
| 735 void BluetoothDispatcherHost::OnWriteValueFailed( | 805 void BluetoothDispatcherHost::OnWriteValueFailed( |
| 736 int thread_id, | 806 int thread_id, |
| 737 int request_id, | 807 int request_id, |
| 738 device::BluetoothGattService::GattErrorCode error_code) { | 808 device::BluetoothGattService::GattErrorCode error_code) { |
| 739 Send(new BluetoothMsg_WriteCharacteristicValueError( | 809 Send(new BluetoothMsg_WriteCharacteristicValueError( |
| 740 thread_id, request_id, TranslateGATTError(error_code))); | 810 thread_id, request_id, TranslateGATTError(error_code))); |
| 741 } | 811 } |
| 742 | 812 |
| 743 } // namespace content | 813 } // namespace content |
| OLD | NEW |