| 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/bluetooth_device_chooser_controller.h" | 5 #include "content/browser/bluetooth/bluetooth_device_chooser_controller.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <unordered_set> | 9 #include <unordered_set> |
| 10 | 10 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 // The duration of a Bluetooth Scan in seconds. | 57 // The duration of a Bluetooth Scan in seconds. |
| 58 constexpr int kScanDuration = 60; | 58 constexpr int kScanDuration = 60; |
| 59 constexpr int kTestScanDuration = 0; | 59 constexpr int kTestScanDuration = 0; |
| 60 | 60 |
| 61 void LogRequestDeviceOptions( | 61 void LogRequestDeviceOptions( |
| 62 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { | 62 const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options) { |
| 63 VLOG(1) << "requestDevice called with the following filters: "; | 63 VLOG(1) << "requestDevice called with the following filters: "; |
| 64 int i = 0; | 64 int i = 0; |
| 65 for (const auto& filter : options->filters) { | 65 for (const auto& filter : options->filters) { |
| 66 VLOG(1) << "Filter #" << ++i; | 66 VLOG(1) << "Filter #" << ++i; |
| 67 if (!filter->name.is_null()) | 67 if (filter->name) |
| 68 VLOG(1) << "Name: " << filter->name; | 68 VLOG(1) << "Name: " << filter->name.value(); |
| 69 | 69 |
| 70 if (!filter->name_prefix.is_null()) | 70 if (filter->name_prefix) |
| 71 VLOG(1) << "Name Prefix: " << filter->name_prefix; | 71 VLOG(1) << "Name Prefix: " << filter->name_prefix.value(); |
| 72 | 72 |
| 73 if (!filter->services.is_null()) { | 73 if (filter->services) { |
| 74 VLOG(1) << "Services: "; | 74 VLOG(1) << "Services: "; |
| 75 VLOG(1) << "\t["; | 75 VLOG(1) << "\t["; |
| 76 for (const auto& service : filter->services) | 76 for (const auto& service : filter->services.value()) |
| 77 VLOG(1) << "\t\t" << service->canonical_value(); | 77 VLOG(1) << "\t\t" << service.canonical_value(); |
| 78 VLOG(1) << "\t]"; | 78 VLOG(1) << "\t]"; |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 bool IsEmptyOrInvalidFilter( | 83 bool IsEmptyOrInvalidFilter( |
| 84 const blink::mojom::WebBluetoothScanFilterPtr& filter) { | 84 const blink::mojom::WebBluetoothScanFilterPtr& filter) { |
| 85 // At least one member needs to be present. | 85 // At least one member needs to be present. |
| 86 if (filter->name.is_null() && filter->name_prefix.is_null() && | 86 if (!filter->name && !filter->name_prefix && !filter->services) |
| 87 filter->services.is_null()) | |
| 88 return true; | 87 return true; |
| 89 | 88 |
| 90 // The renderer will never send a name or a name_prefix longer than | 89 // The renderer will never send a name or a name_prefix longer than |
| 91 // kMaxLengthForDeviceName. | 90 // kMaxLengthForDeviceName. |
| 92 if (!filter->name.is_null() && filter->name.size() > kMaxLengthForDeviceName) | 91 if (filter->name && filter->name->size() > kMaxLengthForDeviceName) |
| 93 return true; | 92 return true; |
| 94 if (!filter->name_prefix.is_null() && filter->name_prefix.size() == 0) | 93 if (filter->name_prefix && filter->name_prefix->size() == 0) |
| 95 return true; | 94 return true; |
| 96 if (!filter->name_prefix.is_null() && | 95 if (filter->name_prefix && |
| 97 filter->name_prefix.size() > kMaxLengthForDeviceName) | 96 filter->name_prefix->size() > kMaxLengthForDeviceName) |
| 98 return true; | 97 return true; |
| 99 | 98 |
| 100 return false; | 99 return false; |
| 101 } | 100 } |
| 102 | 101 |
| 103 bool HasEmptyOrInvalidFilter( | 102 bool HasEmptyOrInvalidFilter( |
| 104 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { | 103 const std::vector<blink::mojom::WebBluetoothScanFilterPtr>& filters) { |
| 105 return filters.empty() | 104 return filters.empty() |
| 106 ? true | 105 ? true |
| 107 : filters.end() != std::find_if(filters.begin(), filters.end(), | 106 : filters.end() != std::find_if(filters.begin(), filters.end(), |
| 108 IsEmptyOrInvalidFilter); | 107 IsEmptyOrInvalidFilter); |
| 109 } | 108 } |
| 110 | 109 |
| 111 bool MatchesFilter(const std::string* device_name, | 110 bool MatchesFilter(const std::string* device_name, |
| 112 const UUIDSet& device_uuids, | 111 const UUIDSet& device_uuids, |
| 113 const blink::mojom::WebBluetoothScanFilterPtr& filter) { | 112 const blink::mojom::WebBluetoothScanFilterPtr& filter) { |
| 114 if (!filter->name.is_null()) { | 113 if (filter->name) { |
| 115 if (device_name == nullptr) | 114 if (device_name == nullptr) |
| 116 return false; | 115 return false; |
| 117 if (filter->name != *device_name) | 116 if (filter->name.value() != *device_name) |
| 118 return false; | 117 return false; |
| 119 } | 118 } |
| 120 | 119 |
| 121 if (!filter->name_prefix.is_null() && filter->name_prefix.size()) { | 120 if (filter->name_prefix && filter->name_prefix->size()) { |
| 122 if (device_name == nullptr) | 121 if (device_name == nullptr) |
| 123 return false; | 122 return false; |
| 124 if (!base::StartsWith(*device_name, filter->name_prefix.get(), | 123 if (!base::StartsWith(*device_name, filter->name_prefix.value(), |
| 125 base::CompareCase::SENSITIVE)) | 124 base::CompareCase::SENSITIVE)) |
| 126 return false; | 125 return false; |
| 127 } | 126 } |
| 128 | 127 |
| 129 if (!filter->services.is_null()) { | 128 if (filter->services) { |
| 130 for (const base::Optional<BluetoothUUID>& service : filter->services) { | 129 for (const auto& service : filter->services.value()) { |
| 131 if (!base::ContainsKey(device_uuids, service.value())) { | 130 if (!base::ContainsKey(device_uuids, service)) { |
| 132 return false; | 131 return false; |
| 133 } | 132 } |
| 134 } | 133 } |
| 135 } | 134 } |
| 136 | 135 |
| 137 return true; | 136 return true; |
| 138 } | 137 } |
| 139 | 138 |
| 140 bool MatchesFilters( | 139 bool MatchesFilters( |
| 141 const std::string* device_name, | 140 const std::string* device_name, |
| 142 const UUIDSet& device_uuids, | 141 const UUIDSet& device_uuids, |
| 143 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { | 142 const std::vector<blink::mojom::WebBluetoothScanFilterPtr>& filters) { |
| 144 DCHECK(!HasEmptyOrInvalidFilter(filters)); | 143 DCHECK(!HasEmptyOrInvalidFilter(filters)); |
| 145 for (const auto& filter : filters) { | 144 for (const auto& filter : filters) { |
| 146 if (MatchesFilter(device_name, device_uuids, filter)) { | 145 if (MatchesFilter(device_name, device_uuids, filter)) { |
| 147 return true; | 146 return true; |
| 148 } | 147 } |
| 149 } | 148 } |
| 150 return false; | 149 return false; |
| 151 } | 150 } |
| 152 | 151 |
| 153 std::unique_ptr<device::BluetoothDiscoveryFilter> ComputeScanFilter( | 152 std::unique_ptr<device::BluetoothDiscoveryFilter> ComputeScanFilter( |
| 154 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { | 153 const std::vector<blink::mojom::WebBluetoothScanFilterPtr>& filters) { |
| 155 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash> services; | 154 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash> services; |
| 156 for (const auto& filter : filters) { | 155 for (const auto& filter : filters) { |
| 157 for (const base::Optional<BluetoothUUID>& service : filter->services) { | 156 if (!filter->services) { |
| 158 services.insert(service.value()); | 157 continue; |
| 158 } |
| 159 for (const auto& service : filter->services.value()) { |
| 160 services.insert(service); |
| 159 } | 161 } |
| 160 } | 162 } |
| 161 // There isn't much support for GATT over BR/EDR from neither platforms nor | 163 // There isn't much support for GATT over BR/EDR from neither platforms nor |
| 162 // devices so performing a Dual scan will find devices that the API is not | 164 // devices so performing a Dual scan will find devices that the API is not |
| 163 // able to interact with. To avoid wasting power and confusing users with | 165 // able to interact with. To avoid wasting power and confusing users with |
| 164 // devices they are not able to interact with, we only perform an LE Scan. | 166 // devices they are not able to interact with, we only perform an LE Scan. |
| 165 auto discovery_filter = base::MakeUnique<device::BluetoothDiscoveryFilter>( | 167 auto discovery_filter = base::MakeUnique<device::BluetoothDiscoveryFilter>( |
| 166 device::BLUETOOTH_TRANSPORT_LE); | 168 device::BLUETOOTH_TRANSPORT_LE); |
| 167 for (const BluetoothUUID& service : services) { | 169 for (const BluetoothUUID& service : services) { |
| 168 discovery_filter->AddUUID(service); | 170 discovery_filter->AddUUID(service); |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 | 557 |
| 556 void BluetoothDeviceChooserController::PostErrorCallback( | 558 void BluetoothDeviceChooserController::PostErrorCallback( |
| 557 blink::mojom::WebBluetoothResult error) { | 559 blink::mojom::WebBluetoothResult error) { |
| 558 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 560 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 559 FROM_HERE, base::Bind(error_callback_, error))) { | 561 FROM_HERE, base::Bind(error_callback_, error))) { |
| 560 LOG(WARNING) << "No TaskRunner."; | 562 LOG(WARNING) << "No TaskRunner."; |
| 561 } | 563 } |
| 562 } | 564 } |
| 563 | 565 |
| 564 } // namespace content | 566 } // namespace content |
| OLD | NEW |