0

[device] Use base::Contains() instead of std or container::find()

Use base::Contains() instead of std or <container>::find() where
appropriate in //device.

Bug: 561800
Change-Id: I69b1e78bc8bfa5dcafa009b30eb155c83874fe56
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4842088
Reviewed-by: Matt Reynolds <mattreynolds@chromium.org>
Commit-Queue: Ho Cheung <uioptt24@gmail.com>
Cr-Commit-Position: refs/heads/main@{#1193838}
This commit is contained in:
Ho Cheung
2023-09-07 23:29:23 +00:00
committed by Chromium LUCI CQ
parent 1631506c15
commit 52dc93f2cf
20 changed files with 74 additions and 69 deletions

@ -31,6 +31,7 @@
#include "device/bluetooth/test/bluetooth_test.h"
#include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
#include "device/bluetooth/test/test_bluetooth_advertisement_observer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_ANDROID)
@ -674,15 +675,11 @@ TEST_F(BluetoothAdapterTest, GetMergedDiscoveryFilterAllFields) {
EXPECT_TRUE(resulting_filter->GetTransport());
EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, resulting_filter->GetTransport());
EXPECT_EQ(-85, resulting_rssi);
EXPECT_EQ(4UL, resulting_uuids.size());
EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1000")) !=
resulting_uuids.end());
EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1001")) !=
resulting_uuids.end());
EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1003")) !=
resulting_uuids.end());
EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1020")) !=
resulting_uuids.end());
EXPECT_THAT(resulting_uuids,
testing::UnorderedElementsAre(device::BluetoothUUID("1000"),
device::BluetoothUUID("1001"),
device::BluetoothUUID("1003"),
device::BluetoothUUID("1020")));
adapter_->CleanupSessions();
}

@ -9,6 +9,7 @@
#include <utility>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/location.h"
@ -227,15 +228,14 @@ void BluetoothAdapterWin::DevicesPolled(
DeviceAddressSet changed_devices =
base::STLSetIntersection<DeviceAddressSet>(known_devices, new_devices);
for (const auto& device_state : devices) {
if (added_devices.find(device_state->address) != added_devices.end()) {
if (base::Contains(added_devices, device_state->address)) {
auto device_win = std::make_unique<BluetoothDeviceWin>(
this, *device_state, ui_task_runner_, socket_thread_);
BluetoothDeviceWin* device_win_raw = device_win.get();
devices_[device_state->address] = std::move(device_win);
for (auto& observer : observers_)
observer.DeviceAdded(this, device_win_raw);
} else if (changed_devices.find(device_state->address) !=
changed_devices.end()) {
} else if (base::Contains(changed_devices, device_state->address)) {
auto iter = devices_.find(device_state->address);
DCHECK(iter != devices_.end());
BluetoothDeviceWin* device_win =

@ -6,6 +6,7 @@
#include <memory>
#include "base/containers/contains.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "testing/gmock/include/gmock/gmock.h"
@ -187,9 +188,9 @@ TEST(BluetoothDiscoveryFilterTest, MergeUUIDs) {
// df3 should contain all uuids from df1 and df2
std::set<device::BluetoothUUID> out_uuids;
df3->GetUUIDs(out_uuids);
EXPECT_TRUE(out_uuids.find(uuid1020) != out_uuids.end());
EXPECT_TRUE(out_uuids.find(uuid1003) != out_uuids.end());
EXPECT_TRUE(out_uuids.find(uuid1004) != out_uuids.end());
EXPECT_TRUE(base::Contains(out_uuids, uuid1020));
EXPECT_TRUE(base::Contains(out_uuids, uuid1003));
EXPECT_TRUE(base::Contains(out_uuids, uuid1004));
// Merging with empty filter would return empty filter
df3 = BluetoothDiscoveryFilter::Merge(&df1, nullptr);

@ -8,6 +8,7 @@
#include <limits>
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
@ -281,7 +282,7 @@ void BluetoothRemoteGattCharacteristicBlueZ::UnsubscribeFromNotifications(
void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded(
const dbus::ObjectPath& object_path) {
if (descriptors_.find(object_path.value()) != descriptors_.end()) {
if (base::Contains(descriptors_, object_path.value())) {
DVLOG(1) << "Remote GATT characteristic descriptor already exists: "
<< object_path.value();
return;

@ -5,6 +5,7 @@
#include <iterator>
#include <utility>
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "dbus/property.h"
@ -146,7 +147,7 @@ void BluetoothRemoteGattServiceBlueZ::GattServicePropertyChanged(
void BluetoothRemoteGattServiceBlueZ::GattCharacteristicAdded(
const dbus::ObjectPath& object_path) {
if (characteristics_.find(object_path.value()) != characteristics_.end()) {
if (base::Contains(characteristics_, object_path.value())) {
DVLOG(1) << "Remote GATT characteristic already exists: "
<< object_path.value();
return;

@ -6,6 +6,7 @@
#include <utility>
#include "base/containers/contains.h"
#include "base/values.h"
namespace bluez {
@ -46,7 +47,7 @@ void BluetoothServiceRecordBlueZ::AddRecordEntry(
}
bool BluetoothServiceRecordBlueZ::IsAttributePresented(uint16_t id) const {
return attributes_.find(id) != attributes_.end();
return base::Contains(attributes_, id);
}
} // namespace bluez

@ -6,6 +6,7 @@
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
@ -284,8 +285,9 @@ void BluetoothAdapterCast::OnConnectChanged(
// This method could be called before this device is detected in a scan and
// GetDevice() is called. Add it if needed.
if (devices_.find(address) == devices_.end())
if (!base::Contains(devices_, address)) {
AddDevice(std::move(device));
}
BluetoothDeviceCast* cast_device = GetCastDevice(address);
cast_device->SetConnected(connected);
@ -328,9 +330,8 @@ void BluetoothAdapterCast::OnNewScanResult(
// If we haven't created a BluetoothDeviceCast for this address yet, we need
// to send an async request to |gatt_client_manager_| for a handle to the
// device.
if (devices_.find(address) == devices_.end()) {
bool first_time_seen =
pending_scan_results_.find(address) == pending_scan_results_.end();
if (!base::Contains(devices_, address)) {
bool first_time_seen = !base::Contains(pending_scan_results_, address);
// These results will be used to construct the BluetoothDeviceCast.
pending_scan_results_[address].push_back(result);
@ -376,7 +377,7 @@ void BluetoothAdapterCast::AddDevice(
// This method should not be called if we already have a BluetoothDeviceCast
// registered for this device.
std::string address = GetCanonicalBluetoothAddress(remote_device->addr());
DCHECK(devices_.find(address) == devices_.end());
DCHECK(!base::Contains(devices_, address));
devices_[address] =
std::make_unique<BluetoothDeviceCast>(this, remote_device);
@ -440,14 +441,14 @@ void BluetoothAdapterCast::OnGetDevice(
// OnConnectChanged() is called for a particular device. If that happened,
// |remote_device| already has a handle. In this case, there should be no
// |pending_scan_results_| and we should fast-return.
if (devices_.find(address) != devices_.end()) {
DCHECK(pending_scan_results_.find(address) == pending_scan_results_.end());
if (base::Contains(devices_, address)) {
DCHECK(!base::Contains(pending_scan_results_, address));
return;
}
// If there is not a device already, there should be at least one ScanResult
// which triggered the GetDevice() call.
DCHECK(pending_scan_results_.find(address) != pending_scan_results_.end());
DCHECK(!base::Contains(pending_scan_results_, address));
AddDevice(std::move(remote_device));
}

@ -6,6 +6,7 @@
#include <memory>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
@ -170,8 +171,7 @@ void FakeBluetoothGattCharacteristicClient::ReadValue(
return;
}
if (action_extra_requests_.find("ReadValue") !=
action_extra_requests_.end()) {
if (base::Contains(action_extra_requests_, "ReadValue")) {
DelayedCallback* delayed = action_extra_requests_["ReadValue"];
delayed->delay_--;
std::move(error_callback)
@ -244,8 +244,7 @@ void FakeBluetoothGattCharacteristicClient::WriteValue(
}
DCHECK(heart_rate_control_point_properties_.get());
if (action_extra_requests_.find("WriteValue") !=
action_extra_requests_.end()) {
if (base::Contains(action_extra_requests_, "WriteValue")) {
DelayedCallback* delayed = action_extra_requests_["WriteValue"];
delayed->delay_--;
std::move(error_callback)

@ -6,6 +6,7 @@
#include <vector>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
@ -130,7 +131,7 @@ void FakeBluetoothGattDescriptorClient::WriteValue(
const std::vector<uint8_t>& value,
base::OnceClosure callback,
ErrorCallback error_callback) {
if (properties_.find(object_path) == properties_.end()) {
if (!base::Contains(properties_, object_path)) {
std::move(error_callback).Run(kUnknownDescriptorError, "");
return;
}

@ -6,6 +6,7 @@
#include <map>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
@ -72,8 +73,9 @@ FakeBluetoothInputClient::Properties* FakeBluetoothInputClient::GetProperties(
void FakeBluetoothInputClient::AddInputDevice(
const dbus::ObjectPath& object_path) {
if (properties_map_.find(object_path) != properties_map_.end())
if (base::Contains(properties_map_, object_path)) {
return;
}
std::unique_ptr<Properties> properties = std::make_unique<Properties>(
base::BindRepeating(&FakeBluetoothInputClient::OnPropertyChanged,

@ -4,6 +4,7 @@
#include "device/bluetooth/floss/fake_floss_gatt_manager_client.h"
#include "base/containers/contains.h"
#include "base/rand_util.h"
#include "base/task/single_thread_task_runner.h"
#include "device/bluetooth/floss/floss_dbus_client.h"
@ -41,7 +42,7 @@ void FakeFlossGattManagerClient::AddService(ResponseCallback<Void> callback,
}
int32_t instance_id = added_service.instance_id;
if (services_.find(instance_id) != services_.end()) {
if (base::Contains(services_, instance_id)) {
GattServerServiceAdded(GattStatus::kError, added_service);
return;
}
@ -55,7 +56,7 @@ void FakeFlossGattManagerClient::RemoveService(ResponseCallback<Void> callback,
int32_t handle) {
std::move(callback).Run(DBusResult<Void>({}));
if (services_.find(handle) == services_.end()) {
if (!base::Contains(services_, handle)) {
GattServerServiceRemoved(GattStatus::kError, handle);
return;
}

@ -10,6 +10,7 @@
#include <sys/types.h>
#include <unistd.h>
#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
@ -212,8 +213,7 @@ void FakeFlossSocketManager::Close(const SocketId id,
void FakeFlossSocketManager::SendSocketReady(const SocketId id,
const device::BluetoothUUID& uuid,
const BtifStatus status) {
if (listening_sockets_to_callbacks_.find(id) !=
listening_sockets_to_callbacks_.end()) {
if (base::Contains(listening_sockets_to_callbacks_, id)) {
FlossListeningSocket socket;
socket.id = id;
socket.type = SocketType::kRfcomm;
@ -226,8 +226,7 @@ void FakeFlossSocketManager::SendSocketReady(const SocketId id,
void FakeFlossSocketManager::SendSocketClosed(const SocketId id,
const BtifStatus status) {
if (listening_sockets_to_callbacks_.find(id) !=
listening_sockets_to_callbacks_.end()) {
if (base::Contains(listening_sockets_to_callbacks_, id)) {
FlossListeningSocket socket;
socket.id = id;
socket.type = SocketType::kRfcomm;
@ -242,8 +241,7 @@ void FakeFlossSocketManager::SendIncomingConnection(
const SocketId listener_id,
const FlossDeviceId& remote_device,
const device::BluetoothUUID& uuid) {
if (listening_sockets_to_callbacks_.find(listener_id) !=
listening_sockets_to_callbacks_.end()) {
if (base::Contains(listening_sockets_to_callbacks_, listener_id)) {
// Create a fake socket and send a new connection callback.
FlossSocket socket;
socket.id = listener_id;

@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "device/bluetooth/floss/floss_socket_manager.h"
#include "base/containers/contains.h"
#include "base/types/expected.h"
namespace floss {
@ -80,7 +81,7 @@ bool FlossDBusClient::ReadDBusParam(
std::string key;
dict.PopString(&key);
if (required_keys.find(key) != required_keys.end()) {
if (base::Contains(required_keys, key)) {
if (key == kListeningPropId) {
required_keys[key] = ReadDBusParamFromVariant(&dict, &socket->id);
} else if (key == kListeningPropSockType) {
@ -159,7 +160,7 @@ bool FlossDBusClient::ReadDBusParam(dbus::MessageReader* reader,
std::string key;
dict.PopString(&key);
if (required_keys.find(key) != required_keys.end()) {
if (base::Contains(required_keys, key)) {
if (key == kConnectingPropId) {
required_keys[key] = ReadDBusParamFromVariant(&dict, &socket->id);
} else if (key == kConnectingPropRemoteDevice) {
@ -244,7 +245,7 @@ bool FlossDBusClient::ReadDBusParam(
std::string key;
dict.PopString(&key);
if (required_keys.find(key) != required_keys.end()) {
if (base::Contains(required_keys, key)) {
if (key == kResultPropStatus) {
required_keys[key] =
ReadDBusParamFromVariant(&dict, &socket_result->status);

@ -4,6 +4,7 @@
#include "device/fido/cable/v2_handshake.h"
#include "base/containers/contains.h"
#include "base/rand_util.h"
#include "base/ranges/algorithm.h"
#include "components/cbor/reader.h"
@ -27,16 +28,17 @@ TEST(CableV2Encoding, TunnelServerURLs) {
// Tunnel ID zero should map to Google's tunnel server.
const tunnelserver::KnownDomainID kGoogleDomain(0);
const GURL url = tunnelserver::GetNewTunnelURL(kGoogleDomain, tunnel_id);
EXPECT_TRUE(url.spec().find("//cable.ua5v.com/") != std::string::npos) << url;
EXPECT_TRUE(base::Contains(url.spec(), "//cable.ua5v.com/")) << url;
// The hash function shouldn't change across releases, so test a hashed
// domain.
const tunnelserver::KnownDomainID kHashedDomain(266);
const GURL hashed_url =
tunnelserver::GetNewTunnelURL(kHashedDomain, tunnel_id);
EXPECT_TRUE(hashed_url.spec().find("//cable.wufkweyy3uaxb.com/") !=
std::string::npos)
<< url;
EXPECT_TRUE(base::Contains(hashed_url.spec(), "//cable.wufkweyy3uaxb.com/"))
<< hashed_url;
}
TEST(CableV2Encoding, EIDToFromComponents) {

@ -7,6 +7,7 @@
#import <AuthenticationServices/AuthenticationServices.h>
#import <Foundation/Foundation.h>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
@ -375,8 +376,7 @@ class API_AVAILABLE(macos(13.3)) Authenticator : public FidoAuthenticator {
// please have macOS show its own error dialog.
CtapDeviceResponseCode response;
if (error.code == 1001 &&
description.find("No credentials available for login") !=
std::string::npos) {
base::Contains(description, "No credentials available for login")) {
response = CtapDeviceResponseCode::kCtap2ErrNoCredentials;
} else {
// All other errors are currently mapped to `kCtap2ErrOperationDenied`

@ -1599,7 +1599,7 @@ absl::optional<CtapDeviceResponseCode> VirtualCtap2Device::OnGetAssertion(
// technically permissible to send an empty allow_list when asking for
// discoverable credentials, but some authenticators in practice don't take it
// that way. Thus this code mirrors that to better reflect reality.
if (request_map.find(cbor::Value(3)) == request_map.end()) {
if (!base::Contains(request_map, cbor::Value(3))) {
DCHECK(config_.resident_key_support);
for (auto& registration : mutable_state()->registrations) {
if (registration.second.is_resident &&
@ -2112,10 +2112,11 @@ absl::optional<CtapDeviceResponseCode> VirtualCtap2Device::OnPINCommand(
PinUvAuthTokenPermissions permissions;
if (subcommand ==
static_cast<int>(device::pin::Subcommand::kGetPINToken)) {
if (request_map.find(cbor::Value(static_cast<int>(
pin::RequestKey::kPermissions))) != request_map.end() ||
request_map.find(cbor::Value(static_cast<int>(
pin::RequestKey::kPermissionsRPID))) != request_map.end()) {
if (base::Contains(request_map, cbor::Value(static_cast<int>(
pin::RequestKey::kPermissions))) ||
base::Contains(request_map,
cbor::Value(static_cast<int>(
pin::RequestKey::kPermissionsRPID)))) {
return CtapDeviceResponseCode::kCtap1ErrInvalidParameter;
}
// Set default PinUvAuthToken permissions.

@ -9,6 +9,7 @@
#include "base/apple/bridging.h"
#include "base/apple/foundation_util.h"
#include "base/containers/contains.h"
#include "base/strings/sys_string_conversions.h"
#import "base/task/sequenced_task_runner.h"
#include "base/task/sequenced_task_runner.h"
@ -171,8 +172,9 @@ void GamepadPlatformDataFetcherMac::DeviceAdd(IOHIDDeviceRef device) {
DCHECK_EQ(kXInputTypeNone,
gamepad_id_list.GetXInputType(vendor_int, product_int));
if (devices_.find(location_int) != devices_.end())
if (base::Contains(devices_, location_int)) {
return;
}
const GamepadId gamepad_id =
gamepad_id_list.GetGamepadId(product_name, vendor_int, product_int);

@ -8,6 +8,7 @@
#include <memory>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/strings/sys_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
@ -199,7 +200,7 @@ void RawInputDataFetcher::EnumerateDevices() {
// path handle it.
// http://msdn.microsoft.com/en-us/library/windows/desktop/ee417014.aspx
const std::wstring device_name = new_device->GetDeviceName();
if (filter_xinput_ && device_name.find(L"IG_") != std::wstring::npos) {
if (filter_xinput_ && base::Contains(device_name, L"IG_")) {
new_device->Shutdown();
continue;
}

@ -847,8 +847,8 @@ XrResult OpenXrApiWrapper::UpdateSecondaryViewConfigStates(
bool state_changed = false;
for (const XrSecondaryViewConfigurationStateMSFT& state : states) {
DCHECK(secondary_view_configs_.find(state.viewConfigurationType) !=
secondary_view_configs_.end());
DCHECK(
base::Contains(secondary_view_configs_, state.viewConfigurationType));
OpenXrViewConfiguration& view_config =
secondary_view_configs_.at(state.viewConfigurationType);

@ -19,7 +19,7 @@
namespace {
bool PathContainsString(const std::string& path, const std::string& s) {
return path.find(s) != std::string::npos;
return base::Contains(path, s);
}
device::XrEye GetEyeForIndex(uint32_t index, uint32_t num_views) {
@ -504,8 +504,7 @@ XrResult OpenXrTestHelper::BeginSession(
// xrBeginSession in the fake OpenXR runtime should have added the primary
// view configuration first.
if (primary_configs_supported_.find(view_configs[0]) ==
primary_configs_supported_.end()) {
if (!base::Contains(primary_configs_supported_, view_configs[0])) {
return XR_ERROR_VIEW_CONFIGURATION_TYPE_UNSUPPORTED;
}
@ -515,14 +514,12 @@ XrResult OpenXrTestHelper::BeginSession(
// Process the rest of the view configurations, which should all be secondary.
for (uint32_t i = 1; i < view_configs.size(); i++) {
if (secondary_configs_supported_.find(view_configs[i]) ==
secondary_configs_supported_.end()) {
if (!base::Contains(secondary_configs_supported_, view_configs[i])) {
return XR_ERROR_VIEW_CONFIGURATION_TYPE_UNSUPPORTED;
}
// Check for additional primary view configuration.
if (primary_configs_supported_.find(view_configs[i]) !=
primary_configs_supported_.end()) {
if (base::Contains(primary_configs_supported_, view_configs[i])) {
return XR_ERROR_VALIDATION_FAILURE;
}
@ -1337,10 +1334,8 @@ XrResult OpenXrTestHelper::ValidateViews(uint32_t view_capacity_input,
XrResult OpenXrTestHelper::ValidateViewConfigType(
XrViewConfigurationType view_config) const {
RETURN_IF(primary_configs_supported_.find(view_config) ==
primary_configs_supported_.end() &&
secondary_configs_supported_.find(view_config) ==
secondary_configs_supported_.end(),
RETURN_IF(!base::Contains(primary_configs_supported_, view_config) &&
!base::Contains(secondary_configs_supported_, view_config),
XR_ERROR_VIEW_CONFIGURATION_TYPE_UNSUPPORTED,
"XrViewConfigurationType unsupported");