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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 const blink::mojom::WebBluetoothScanFilterPtr& filter) { | 64 const blink::mojom::WebBluetoothScanFilterPtr& filter) { |
65 // At least one member needs to be present. | 65 // At least one member needs to be present. |
66 if (filter->name.is_null() && filter->name_prefix.is_null() && | 66 if (filter->name.is_null() && filter->name_prefix.is_null() && |
67 filter->services.is_null()) | 67 filter->services.is_null()) |
68 return true; | 68 return true; |
69 | 69 |
70 // The renderer will never send a name or a name_prefix longer than | 70 // The renderer will never send a name or a name_prefix longer than |
71 // kMaxLengthForDeviceName. | 71 // kMaxLengthForDeviceName. |
72 if (!filter->name.is_null() && filter->name.size() > kMaxLengthForDeviceName) | 72 if (!filter->name.is_null() && filter->name.size() > kMaxLengthForDeviceName) |
73 return true; | 73 return true; |
| 74 if (!filter->name_prefix.is_null() && filter->name_prefix.size() == 0) |
| 75 return true; |
74 if (!filter->name_prefix.is_null() && | 76 if (!filter->name_prefix.is_null() && |
75 filter->name_prefix.size() > kMaxLengthForDeviceName) | 77 filter->name_prefix.size() > kMaxLengthForDeviceName) |
76 return true; | 78 return true; |
77 | 79 |
78 return false; | 80 return false; |
79 } | 81 } |
80 | 82 |
81 bool HasEmptyOrInvalidFilter( | 83 bool HasEmptyOrInvalidFilter( |
82 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { | 84 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { |
83 return filters.empty() | 85 return filters.empty() |
84 ? true | 86 ? true |
85 : filters.end() != std::find_if(filters.begin(), filters.end(), | 87 : filters.end() != std::find_if(filters.begin(), filters.end(), |
86 IsEmptyOrInvalidFilter); | 88 IsEmptyOrInvalidFilter); |
87 } | 89 } |
88 | 90 |
89 bool MatchesFilter(const device::BluetoothDevice& device, | 91 bool MatchesFilter(const device::BluetoothDevice& device, |
90 const blink::mojom::WebBluetoothScanFilterPtr& filter) { | 92 const blink::mojom::WebBluetoothScanFilterPtr& filter) { |
91 DCHECK(!IsEmptyOrInvalidFilter(filter)); | 93 if (!filter->name.is_null()) { |
92 | 94 if (!device.GetName()) |
93 // TODO(615720): Use the upcoming GetName (was GetDeviceName). | 95 return false; |
94 const std::string device_name = base::UTF16ToUTF8(device.GetNameForDisplay()); | 96 if (filter->name != device.GetName().value()) |
95 | 97 return false; |
96 if (!filter->name.is_null() && (device_name != filter->name)) { | |
97 return false; | |
98 } | 98 } |
99 | 99 |
100 if (!filter->name_prefix.is_null() && | 100 if (!filter->name_prefix.is_null() && filter->name_prefix.size()) { |
101 (!base::StartsWith(device_name, filter->name_prefix.get(), | 101 if (!device.GetName()) |
102 base::CompareCase::SENSITIVE))) { | 102 return false; |
103 return false; | 103 if (!base::StartsWith(device.GetName().value(), filter->name_prefix.get(), |
| 104 base::CompareCase::SENSITIVE)) |
| 105 return false; |
104 } | 106 } |
105 | 107 |
106 if (!filter->services.is_null()) { | 108 if (!filter->services.is_null()) { |
107 const auto& device_uuid_list = device.GetUUIDs(); | 109 const auto& device_uuid_list = device.GetUUIDs(); |
108 const std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash> | 110 const std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash> |
109 device_uuids(device_uuid_list.begin(), device_uuid_list.end()); | 111 device_uuids(device_uuid_list.begin(), device_uuid_list.end()); |
110 for (const base::Optional<BluetoothUUID>& service : filter->services) { | 112 for (const base::Optional<BluetoothUUID>& service : filter->services) { |
111 if (!ContainsKey(device_uuids, service.value())) { | 113 if (!ContainsKey(device_uuids, service.value())) { |
112 return false; | 114 return false; |
113 } | 115 } |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 | 481 |
480 void BluetoothDeviceChooserController::PostErrorCallback( | 482 void BluetoothDeviceChooserController::PostErrorCallback( |
481 blink::mojom::WebBluetoothError error) { | 483 blink::mojom::WebBluetoothError error) { |
482 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( | 484 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( |
483 FROM_HERE, base::Bind(error_callback_, error))) { | 485 FROM_HERE, base::Bind(error_callback_, error))) { |
484 LOG(WARNING) << "No TaskRunner."; | 486 LOG(WARNING) << "No TaskRunner."; |
485 } | 487 } |
486 } | 488 } |
487 | 489 |
488 } // namespace content | 490 } // namespace content |
OLD | NEW |