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