Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: content/browser/bluetooth/bluetooth_device_chooser_controller.cc

Issue 2496833002: bluetooth: Cache device name and uuids when iterating over filters (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "content/browser/bluetooth/bluetooth_blacklist.h" 15 #include "content/browser/bluetooth/bluetooth_blacklist.h"
16 #include "content/browser/bluetooth/bluetooth_metrics.h" 16 #include "content/browser/bluetooth/bluetooth_metrics.h"
17 #include "content/browser/bluetooth/web_bluetooth_service_impl.h" 17 #include "content/browser/bluetooth/web_bluetooth_service_impl.h"
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/content_browser_client.h" 19 #include "content/public/browser/content_browser_client.h"
20 #include "content/public/browser/render_frame_host.h" 20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_contents_delegate.h" 22 #include "content/public/browser/web_contents_delegate.h"
23 #include "device/bluetooth/bluetooth_adapter.h" 23 #include "device/bluetooth/bluetooth_adapter.h"
24 #include "device/bluetooth/bluetooth_common.h" 24 #include "device/bluetooth/bluetooth_common.h"
25 #include "device/bluetooth/bluetooth_discovery_session.h" 25 #include "device/bluetooth/bluetooth_discovery_session.h"
26 26
27 using device::BluetoothUUID; 27 using device::BluetoothUUID;
28 using UUIDSet = device::BluetoothDevice::UUIDSet;
28 29
29 namespace { 30 namespace {
30 31
31 // Anything worse than or equal to this will show 0 bars. 32 // Anything worse than or equal to this will show 0 bars.
32 const int kMinRSSI = -100; 33 const int kMinRSSI = -100;
33 // Anything better than or equal to this will show the maximum bars. 34 // Anything better than or equal to this will show the maximum bars.
34 const int kMaxRSSI = -55; 35 const int kMaxRSSI = -55;
35 // Number of RSSI levels used in the signal strength image. 36 // Number of RSSI levels used in the signal strength image.
36 const int kNumSignalStrengthLevels = 5; 37 const int kNumSignalStrengthLevels = 5;
37 38
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 } 100 }
100 101
101 bool HasEmptyOrInvalidFilter( 102 bool HasEmptyOrInvalidFilter(
102 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { 103 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) {
103 return filters.empty() 104 return filters.empty()
104 ? true 105 ? true
105 : filters.end() != std::find_if(filters.begin(), filters.end(), 106 : filters.end() != std::find_if(filters.begin(), filters.end(),
106 IsEmptyOrInvalidFilter); 107 IsEmptyOrInvalidFilter);
107 } 108 }
108 109
109 bool MatchesFilter(const device::BluetoothDevice& device, 110 bool MatchesFilter(const std::string* device_name,
111 const UUIDSet& device_uuids,
110 const blink::mojom::WebBluetoothScanFilterPtr& filter) { 112 const blink::mojom::WebBluetoothScanFilterPtr& filter) {
111 if (!filter->name.is_null()) { 113 if (!filter->name.is_null()) {
112 if (!device.GetName()) 114 if (device_name == nullptr)
113 return false; 115 return false;
114 if (filter->name != device.GetName().value()) 116 if (filter->name != *device_name)
115 return false; 117 return false;
116 } 118 }
117 119
118 if (!filter->name_prefix.is_null() && filter->name_prefix.size()) { 120 if (!filter->name_prefix.is_null() && filter->name_prefix.size()) {
119 if (!device.GetName()) 121 if (device_name == nullptr)
120 return false; 122 return false;
121 if (!base::StartsWith(device.GetName().value(), filter->name_prefix.get(), 123 if (!base::StartsWith(*device_name, filter->name_prefix.get(),
122 base::CompareCase::SENSITIVE)) 124 base::CompareCase::SENSITIVE))
123 return false; 125 return false;
124 } 126 }
125 127
126 if (!filter->services.is_null()) { 128 if (!filter->services.is_null()) {
127 const device::BluetoothDevice::UUIDSet& device_uuids = device.GetUUIDs();
128 for (const base::Optional<BluetoothUUID>& service : filter->services) { 129 for (const base::Optional<BluetoothUUID>& service : filter->services) {
129 if (!base::ContainsKey(device_uuids, service.value())) { 130 if (!base::ContainsKey(device_uuids, service.value())) {
130 return false; 131 return false;
131 } 132 }
132 } 133 }
133 } 134 }
134 135
135 return true; 136 return true;
136 } 137 }
137 138
138 bool MatchesFilters( 139 bool MatchesFilters(
139 const device::BluetoothDevice& device, 140 const std::string* device_name,
141 const UUIDSet& device_uuids,
140 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { 142 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) {
141 DCHECK(!HasEmptyOrInvalidFilter(filters)); 143 DCHECK(!HasEmptyOrInvalidFilter(filters));
142 for (const auto& filter : filters) { 144 for (const auto& filter : filters) {
143 if (MatchesFilter(device, filter)) { 145 if (MatchesFilter(device_name, device_uuids, filter)) {
144 return true; 146 return true;
145 } 147 }
146 } 148 }
147 return false; 149 return false;
148 } 150 }
149 151
150 std::unique_ptr<device::BluetoothDiscoveryFilter> ComputeScanFilter( 152 std::unique_ptr<device::BluetoothDiscoveryFilter> ComputeScanFilter(
151 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) { 153 const mojo::Array<blink::mojom::WebBluetoothScanFilterPtr>& filters) {
152 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash> services; 154 std::unordered_set<BluetoothUUID, device::BluetoothUUIDHash> services;
153 for (const auto& filter : filters) { 155 for (const auto& filter : filters) {
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 chooser_->SetAdapterPresence( 354 chooser_->SetAdapterPresence(
353 BluetoothChooser::AdapterPresence::POWERED_OFF); 355 BluetoothChooser::AdapterPresence::POWERED_OFF);
354 return; 356 return;
355 } 357 }
356 358
357 StartDeviceDiscovery(); 359 StartDeviceDiscovery();
358 } 360 }
359 361
360 void BluetoothDeviceChooserController::AddFilteredDevice( 362 void BluetoothDeviceChooserController::AddFilteredDevice(
361 const device::BluetoothDevice& device) { 363 const device::BluetoothDevice& device) {
362 if (chooser_.get() && MatchesFilters(device, options_->filters)) { 364 base::Optional<std::string> device_name = device.GetName();
365 if (chooser_.get() &&
366 MatchesFilters(device_name ? &device_name.value() : nullptr,
367 device.GetUUIDs(), options_->filters)) {
363 base::Optional<int8_t> rssi = device.GetInquiryRSSI(); 368 base::Optional<int8_t> rssi = device.GetInquiryRSSI();
364 chooser_->AddOrUpdateDevice( 369 chooser_->AddOrUpdateDevice(
365 device.GetAddress(), !!device.GetName() /* should_update_name */, 370 device.GetAddress(), !!device.GetName() /* should_update_name */,
366 device.GetNameForDisplay(), device.IsGattConnected(), 371 device.GetNameForDisplay(), device.IsGattConnected(),
367 web_bluetooth_service_->IsDevicePaired(device.GetAddress()), 372 web_bluetooth_service_->IsDevicePaired(device.GetAddress()),
368 rssi ? CalculateSignalStrengthLevel(rssi.value()) : -1); 373 rssi ? CalculateSignalStrengthLevel(rssi.value()) : -1);
369 } 374 }
370 } 375 }
371 376
372 void BluetoothDeviceChooserController::AdapterPoweredChanged(bool powered) { 377 void BluetoothDeviceChooserController::AdapterPoweredChanged(bool powered) {
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 535
531 void BluetoothDeviceChooserController::PostErrorCallback( 536 void BluetoothDeviceChooserController::PostErrorCallback(
532 blink::mojom::WebBluetoothResult error) { 537 blink::mojom::WebBluetoothResult error) {
533 if (!base::ThreadTaskRunnerHandle::Get()->PostTask( 538 if (!base::ThreadTaskRunnerHandle::Get()->PostTask(
534 FROM_HERE, base::Bind(error_callback_, error))) { 539 FROM_HERE, base::Bind(error_callback_, error))) {
535 LOG(WARNING) << "No TaskRunner."; 540 LOG(WARNING) << "No TaskRunner.";
536 } 541 }
537 } 542 }
538 543
539 } // namespace content 544 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698