Skip to content

Commit

Permalink
[chromeos] Use base::Contains() instead of std::find() in //chromeos
Browse files Browse the repository at this point in the history
1. Use base::Contains() instead of std::find() where appropriate in
//chromeos.

2. Improved some codes to avoid performance loss.

TEST=autoninja -C out/os chrome chromeos_unittests unit_tests
browser_tests && ./chromeos_unittests && ./unit_tests &&
./browser_tests

In addition to the above tests, also tested with tryjobs.

Bug: 561800
Change-Id: I72d69fb512fc291d2aad57a5743cfaf2c03c1682
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4801128
Reviewed-by: James Cook <jamescook@chromium.org>
Commit-Queue: Ho Cheung <uioptt24@gmail.com>
Cr-Commit-Position: refs/heads/main@{#1188377}
  • Loading branch information
gz83 authored and Chromium LUCI CQ committed Aug 25, 2023
1 parent 1725880 commit 4a68c5d
Show file tree
Hide file tree
Showing 28 changed files with 135 additions and 107 deletions.
40 changes: 22 additions & 18 deletions chromeos/ash/components/audio/audio_device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "chromeos/ash/components/audio/audio_device.h"

#include <stdint.h>

#include "ash/constants/ash_features.h"
#include "base/containers/contains.h"
#include "base/format_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
Expand Down Expand Up @@ -94,44 +96,46 @@ std::string AudioDevice::GetTypeString(AudioDeviceType type) {

// static
AudioDeviceType AudioDevice::GetAudioType(const std::string& node_type) {
if (node_type.find("HEADPHONE") != std::string::npos)
if (base::Contains(node_type, "HEADPHONE")) {
return AudioDeviceType::kHeadphone;
else if (node_type.find("INTERNAL_MIC") != std::string::npos)
} else if (base::Contains(node_type, "INTERNAL_MIC")) {
return AudioDeviceType::kInternalMic;
else if (node_type.find("FRONT_MIC") != std::string::npos)
} else if (base::Contains(node_type, "FRONT_MIC")) {
return AudioDeviceType::kFrontMic;
else if (node_type.find("REAR_MIC") != std::string::npos)
} else if (base::Contains(node_type, "REAR_MIC")) {
return AudioDeviceType::kRearMic;
else if (node_type.find("KEYBOARD_MIC") != std::string::npos)
} else if (base::Contains(node_type, "KEYBOARD_MIC")) {
return AudioDeviceType::kKeyboardMic;
else if (node_type.find("BLUETOOTH_NB_MIC") != std::string::npos)
} else if (base::Contains(node_type, "BLUETOOTH_NB_MIC")) {
return AudioDeviceType::kBluetoothNbMic;
else if (node_type.find("MIC") != std::string::npos)
} else if (base::Contains(node_type, "MIC")) {
return AudioDeviceType::kMic;
else if (node_type.find("USB") != std::string::npos)
} else if (base::Contains(node_type, "USB")) {
return AudioDeviceType::kUsb;
else if (node_type.find("BLUETOOTH") != std::string::npos)
} else if (base::Contains(node_type, "BLUETOOTH")) {
return AudioDeviceType::kBluetooth;
else if (node_type.find("HDMI") != std::string::npos)
} else if (base::Contains(node_type, "HDMI")) {
return AudioDeviceType::kHdmi;
else if (node_type.find("INTERNAL_SPEAKER") != std::string::npos)
} else if (base::Contains(node_type, "INTERNAL_SPEAKER")) {
return AudioDeviceType::kInternalSpeaker;
}
// TODO(hychao): Remove the 'AOKR' matching line after CRAS switches
// node type naming to 'HOTWORD'.
else if (node_type.find("AOKR") != std::string::npos)
else if (base::Contains(node_type, "AOKR")) {
return AudioDeviceType::kHotword;
else if (node_type.find("HOTWORD") != std::string::npos)
} else if (base::Contains(node_type, "HOTWORD")) {
return AudioDeviceType::kHotword;
else if (node_type.find("LINEOUT") != std::string::npos)
} else if (base::Contains(node_type, "LINEOUT")) {
return AudioDeviceType::kLineout;
else if (node_type.find("POST_MIX_LOOPBACK") != std::string::npos)
} else if (base::Contains(node_type, "POST_MIX_LOOPBACK")) {
return AudioDeviceType::kPostMixLoopback;
else if (node_type.find("POST_DSP_LOOPBACK") != std::string::npos)
} else if (base::Contains(node_type, "POST_DSP_LOOPBACK")) {
return AudioDeviceType::kPostDspLoopback;
else if (node_type.find("ALSA_LOOPBACK") != std::string::npos)
} else if (base::Contains(node_type, "ALSA_LOOPBACK")) {
return AudioDeviceType::kAlsaLoopback;
else
} else {
return AudioDeviceType::kOther;
}
}

AudioDevice::AudioDevice() = default;
Expand Down
12 changes: 8 additions & 4 deletions chromeos/ash/components/dbus/biod/fake_biod_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,10 @@ void FakeBiodClient::EndAuthSession(chromeos::VoidDBusMethodCallback callback) {
void FakeBiodClient::SetRecordLabel(const dbus::ObjectPath& record_path,
const std::string& label,
chromeos::VoidDBusMethodCallback callback) {
if (records_.find(record_path) != records_.end())
records_[record_path].label = label;
auto it = records_.find(record_path);
if (it != records_.end()) {
it->second.label = label;
}
SaveRecords();
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), true));
Expand All @@ -332,8 +334,10 @@ void FakeBiodClient::RemoveRecord(const dbus::ObjectPath& record_path,
void FakeBiodClient::RequestRecordLabel(const dbus::ObjectPath& record_path,
LabelCallback callback) {
std::string record_label;
if (records_.find(record_path) != records_.end())
record_label = records_[record_path].label;
auto it = records_.find(record_path);
if (it != records_.end()) {
record_label = it->second.label;
}

base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), record_label));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ const char* const kBlocklistedConfigOptions[] = {
// non-allowlisted keywords. Returns true if no blocklisted items are contained.
bool ValidateConfigLine(const std::string& line) {
for (const char* option : kBlocklistedConfigOptions) {
if (line.find(option) != std::string::npos)
if (base::Contains(line, option)) {
return false;
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <map>
#include <set>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
Expand Down Expand Up @@ -218,7 +219,7 @@ void ShillThirdPartyVpnDriverClientImpl::SetParameters(
dbus::MessageWriter array_writer(nullptr);
writer.OpenArray("{ss}", &array_writer);
for (auto it : parameters) {
if (valid_keys_.find(it.first) == valid_keys_.end()) {
if (!base::Contains(valid_keys_, it.first)) {
LOG(WARNING) << "Unknown key " << it.first;
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ std::vector<FresnelImportData> DeviceActiveUseCase::GetImportData() const {

base::Time DeviceActiveUseCase::RetrievePsmIdDate(
private_membership::rlwe::RlwePlaintextId id) {
if (psm_id_to_date_.find(id.sensitive_id()) == psm_id_to_date_.end())
auto it = psm_id_to_date_.find(id.sensitive_id());
if (it == psm_id_to_date_.end()) {
return base::Time::UnixEpoch();
return psm_id_to_date_.at(id.sensitive_id());
}
return it->second;
}

std::string DeviceActiveUseCase::GetDigestString(
Expand Down
4 changes: 2 additions & 2 deletions chromeos/ash/components/disks/disk_mount_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <vector>

#include "base/barrier_closure.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
Expand Down Expand Up @@ -1072,8 +1073,7 @@ class DiskMountManagerImpl : public DiskMountManager,
}

bool IsPendingPartitioningDisk(const std::string& device_path) {
if (pending_partitioning_disks_.find(device_path) !=
pending_partitioning_disks_.end()) {
if (base::Contains(pending_partitioning_disks_, device_path)) {
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion chromeos/ash/components/disks/fake_disk_mount_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <utility>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "chromeos/ash/components/disks/disk.h"

Expand Down Expand Up @@ -166,7 +167,7 @@ bool FakeDiskMountManager::AddDiskForTest(std::unique_ptr<Disk> disk) {

bool FakeDiskMountManager::AddMountPointForTest(const MountPoint& mount_point) {
if (mount_point.mount_type == MountType::kDevice &&
disks_.find(mount_point.source_path) == disks_.end()) {
!base::Contains(disks_, mount_point.source_path)) {
// Device mount point must have a disk entry.
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <memory>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
Expand Down Expand Up @@ -286,13 +287,12 @@ TEST_F(SimpleGeolocationTest, InvalidResponse) {
receiver.WaitUntilRequestDone();

std::string receiver_position = receiver.position().ToString();
EXPECT_NE(
std::string::npos,
receiver_position.find(
"latitude=200.000000, longitude=200.000000, accuracy=-1.000000, "
"error_code=0, error_message='SimpleGeolocation provider at "
"'https://localhost/' : JSONReader failed:"));
EXPECT_NE(std::string::npos, receiver_position.find("status=4 (TIMEOUT)"));
EXPECT_TRUE(base::Contains(
receiver_position,
"latitude=200.000000, longitude=200.000000, accuracy=-1.000000, "
"error_code=0, error_message='SimpleGeolocation provider at "
"'https://localhost/' : JSONReader failed:"));
EXPECT_TRUE(base::Contains(receiver_position, "status=4 (TIMEOUT)"));
EXPECT_TRUE(receiver.server_error());
EXPECT_GE(url_factory.attempts(), 2U);
if (url_factory.attempts() > expected_retries + 1) {
Expand Down
11 changes: 7 additions & 4 deletions chromeos/ash/components/local_search_service/inverted_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ InvertedIndex::InvertedIndex() {
InvertedIndex::~InvertedIndex() = default;

PostingList InvertedIndex::FindTerm(const std::u16string& term) const {
if (dictionary_.find(term) != dictionary_.end())
return dictionary_.at(term);
auto it = dictionary_.find(term);
if (it != dictionary_.end()) {
return it->second;
}

return {};
}
Expand Down Expand Up @@ -281,8 +283,9 @@ void InvertedIndex::UpdateDocuments(

std::vector<TfidfResult> InvertedIndex::GetTfidf(
const std::u16string& term) const {
if (tfidf_cache_.find(term) != tfidf_cache_.end()) {
return tfidf_cache_.at(term);
auto it = tfidf_cache_.find(term);
if (it != tfidf_cache_.end()) {
return it->second;
}

return {};
Expand Down
3 changes: 2 additions & 1 deletion chromeos/ash/components/login/hibernate/hibernate_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "chromeos/ash/components/login/hibernate/hibernate_manager.h"

#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
Expand Down Expand Up @@ -47,7 +48,7 @@ bool HasAESKL() {
return false;
}

return (crypto_info.find("aeskl") != std::string::npos);
return base::Contains(crypto_info, "aeskl");
}();
return hasKL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <map>
#include <set>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
Expand Down Expand Up @@ -94,15 +95,13 @@ class TestNetworkConfigurationObserver : public NetworkConfigurationObserver {

void OnBeforeConfigurationRemoved(const std::string& service_path,
const std::string& guid) override {
ASSERT_EQ(before_remove_configurations_.end(),
before_remove_configurations_.find(service_path));
ASSERT_FALSE(base::Contains(before_remove_configurations_, service_path));
before_remove_configurations_[service_path] = guid;
}

void OnConfigurationRemoved(const std::string& service_path,
const std::string& guid) override {
ASSERT_EQ(removed_configurations_.end(),
removed_configurations_.find(service_path));
ASSERT_FALSE(base::Contains(removed_configurations_, service_path));
removed_configurations_[service_path] = guid;
}

Expand All @@ -114,23 +113,19 @@ class TestNetworkConfigurationObserver : public NetworkConfigurationObserver {
}

bool HasCreatedConfiguration(const std::string& service_path) {
return created_configurations_.find(service_path) !=
created_configurations_.end();
return base::Contains(created_configurations_, service_path);
}

bool HasCalledBeforeRemoveConfiguration(const std::string& service_path) {
return before_remove_configurations_.find(service_path) !=
before_remove_configurations_.end();
return base::Contains(before_remove_configurations_, service_path);
}

bool HasRemovedConfiguration(const std::string& service_path) {
return removed_configurations_.find(service_path) !=
removed_configurations_.end();
return base::Contains(removed_configurations_, service_path);
}

bool HasUpdatedConfiguration(const std::string& service_path) {
return updated_configurations_.find(service_path) !=
updated_configurations_.end();
return base::Contains(updated_configurations_, service_path);
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>

#include "ash/constants/ash_features.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
Expand Down Expand Up @@ -506,8 +507,8 @@ bool NetworkDeviceHandlerImpl::IsUsbEnabledDevice(
return device_state && device_state->link_up() &&
device_state->Matches(NetworkTypePattern::Ethernet()) &&
device_state->device_bus_type() == shill::kDeviceBusTypeUsb &&
mac_address_change_not_supported_.find(device_state->mac_address()) ==
mac_address_change_not_supported_.end();
!base::Contains(mac_address_change_not_supported_,
device_state->mac_address());
}

void NetworkDeviceHandlerImpl::UpdatePrimaryEnabledUsbEthernetDevice() {
Expand Down

0 comments on commit 4a68c5d

Please sign in to comment.