Skip to content

Commit

Permalink
[CodeHealth] Use cxx20's T::contains pt.2
Browse files Browse the repository at this point in the history
In `cxx20`, there is a new function in associative containers called
`contains`, which should be used as a replacement to `base::Contains`.

Resolves brave/brave-browser#44063
  • Loading branch information
cdesouza-chromium committed Feb 18, 2025
1 parent e858e32 commit 5dba5a2
Show file tree
Hide file tree
Showing 25 changed files with 131 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <utility>

#include "base/containers/contains.h"
#include "brave/browser/ephemeral_storage/ephemeral_storage_service_factory.h"
#include "brave/components/brave_wallet/browser/permission_utils.h"
#include "brave/components/ephemeral_storage/ephemeral_storage_service.h"
Expand Down Expand Up @@ -54,7 +53,7 @@ PermissionOriginLifetimeMonitorImpl::SubscribeToPermissionOriginDestruction(
}
std::string storage_domain = net::URLToEphemeralStorageDomain(
is_sub_request_origin ? sub_request_origin.GetURL() : requesting_origin);
if (!base::Contains(active_subscriptions_, storage_domain)) {
if (!active_subscriptions_.contains(storage_domain)) {
active_subscriptions_.insert(storage_domain);
}
return storage_domain;
Expand Down
7 changes: 3 additions & 4 deletions browser/ui/brave_browser_command_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include "brave/browser/ui/brave_browser_command_controller.h"

#include <optional>
#include <vector>

#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/feature_list.h"
#include "base/types/to_address.h"
#include "brave/app/brave_command_ids.h"
Expand Down Expand Up @@ -76,11 +75,11 @@ bool IsBraveCommands(int id) {
}

bool IsBraveOverrideCommands(int id) {
static std::vector<int> override_commands({
static constexpr auto kOverrideCommands = base::MakeFixedFlatSet<int>({
IDC_NEW_WINDOW,
IDC_NEW_INCOGNITO_WINDOW,
});
return base::Contains(override_commands, id);
return kOverrideCommands.contains(id);
}

} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "components/sessions/content/content_serialized_navigation_driver.h"

#include <string>

#include "base/containers/contains.h"
#include "components/sessions/content/content_serialized_navigation_driver.h"
#include "base/containers/fixed_flat_set.h"
#include "components/sessions/core/serialized_navigation_entry.h"
#include "content/public/common/url_constants.h"

Expand All @@ -17,18 +18,22 @@

namespace sessions {

namespace {

// Extension can override below three chrome urls.
// https://source.chromium.org/chromium/chromium/src/+/main:chrome/common/extensions/api/chrome_url_overrides.idl
constexpr auto kAllowedChromeUrlsOverridingHostList =
base::MakeFixedFlatSet<std::string_view>(
{"newtab", "history", "bookmarks"});

} // namespace

std::string ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle(
const sessions::SerializedNavigationEntry* navigation) const {
// Extension can override below three chrome urls.
// https://source.chromium.org/chromium/chromium/src/+/main:chrome/common/extensions/api/chrome_url_overrides.idl
constexpr std::array<const char*, 3> kAllowedChromeUrlsOverridingHostList = {
"newtab", "history", "bookmarks"};

const auto& virtual_url = navigation->virtual_url();
if (virtual_url.SchemeIs(content::kChromeUIScheme)) {
// If empty string is returned, chrome url overriding is ignored.
if (base::Contains(kAllowedChromeUrlsOverridingHostList,
virtual_url.host())) {
if (kAllowedChromeUrlsOverridingHostList.contains(virtual_url.host())) {
// chrome url can be re-written when it's restored during the tab but
// re-written url is ignored when encoded page state is empty.
// In ContentSerializedNavigationBuilder::ToNavigationEntry(), re-written
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void DeviceInfoSyncBridge::RefreshLocalDeviceInfoIfNeeded() {
return;
}

if (!base::Contains(all_data_, current_info->guid())) {
if (!all_data_.contains(current_info->guid())) {
// After initiating leave the sync chain `DeleteSpecifics` cleans
// `all_data_` map.
// It is possible that user close sync settings page or change the data type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

#include "base/containers/contains.h"
#include "brave/components/constants/webui_url_constants.h"

// Include password_manager constants before override so we don't override the
Expand All @@ -13,7 +12,7 @@
// Add some extra items to WebUI hosts considered valid for PWAs
#define kChromeUIPasswordManagerHost \
kChromeUIPasswordManagerHost && \
!base::Contains(kInstallablePWAWebUIHosts, url.host_piece())
!kInstallablePWAWebUIHosts.contains(url.host_piece())

#include "src/components/webapps/browser/banners/app_banner_manager.cc"
#undef kChromeUIPasswordManagerHost
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <string_view>

#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"

namespace blink::origin_trials {
bool IsTrialValid_ChromiumImpl(std::string_view trial_name);
Expand All @@ -19,21 +19,33 @@ bool IsTrialValid_ChromiumImpl(std::string_view trial_name);

namespace blink::origin_trials {

namespace {

// When updating also update the array in the overload below.
constexpr auto kBraveDisabledTrialNames =
base::MakeFixedFlatSet<std::string_view>({
"AdInterestGroupAPI",
"DeviceAttributes",
"DigitalGoodsV2",
"InterestCohortAPI",
"FencedFrames",
"Fledge",
"Parakeet",
"SignedExchangeSubresourcePrefetch",
"SubresourceWebBundles",
});

constexpr auto kBraveDisabledTrialFeatures =
base::MakeFixedFlatSet<blink::mojom::OriginTrialFeature>({
blink::mojom::OriginTrialFeature::kAdInterestGroupAPI,
blink::mojom::OriginTrialFeature::kDigitalGoods,
blink::mojom::OriginTrialFeature::kParakeet,
});

} // namespace

bool IsTrialDisabledInBrave(std::string_view trial_name) {
// When updating also update the array in the overload below.
static const char* const kBraveDisabledTrialNames[] = {
"AdInterestGroupAPI",
"DeviceAttributes",
"DigitalGoodsV2",
"InterestCohortAPI",
"FencedFrames",
"Fledge",
"Parakeet",
"SignedExchangeSubresourcePrefetch",
"SubresourceWebBundles",
};

if (base::Contains(kBraveDisabledTrialNames, trial_name)) {
if (kBraveDisabledTrialNames.contains(trial_name)) {
// Check if this is still a valid trial name in Chromium. If not, it needs
// to be changed as in Chromium or removed.
DCHECK(IsTrialValid_ChromiumImpl(trial_name));
Expand All @@ -45,14 +57,7 @@ bool IsTrialDisabledInBrave(std::string_view trial_name) {

bool IsTrialDisabledInBrave(blink::mojom::OriginTrialFeature feature) {
// When updating also update the array in the overload above.
static const blink::mojom::OriginTrialFeature kBraveDisabledTrialFeatures[] =
{
blink::mojom::OriginTrialFeature::kAdInterestGroupAPI,
blink::mojom::OriginTrialFeature::kDigitalGoods,
blink::mojom::OriginTrialFeature::kParakeet,
};

return base::Contains(kBraveDisabledTrialFeatures, feature);
return kBraveDisabledTrialFeatures.contains(feature);
}

bool IsTrialValid(std::string_view trial_name) {
Expand Down
5 changes: 2 additions & 3 deletions components/ai_chat/content/browser/page_content_fetcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "base/check.h"
#include "base/containers/checked_iterators.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
Expand Down Expand Up @@ -203,7 +202,7 @@ class PageContentFetcherInternal {
return;
}
DVLOG(1) << "OnTabContentResult: " << data.get();
const bool is_video = base::Contains(kVideoPageContentTypes, data->type);
const bool is_video = kVideoPageContentTypes.contains(data->type);
DVLOG(1) << "Is video? " << is_video;
// Handle text mode response
if (!is_video) {
Expand Down Expand Up @@ -456,7 +455,7 @@ void PageContentFetcher::FetchPageContent(std::string_view invalidation_token,

auto url = web_contents_->GetLastCommittedURL();
#if BUILDFLAG(ENABLE_TEXT_RECOGNITION)
if (base::Contains(kScreenshotRetrievalHosts, url.host_piece())) {
if (kScreenshotRetrievalHosts.contains(url.host_piece())) {
content::RenderWidgetHostView* view =
web_contents_->GetRenderWidgetHostView();
if (view) {
Expand Down
2 changes: 1 addition & 1 deletion components/ai_chat/core/browser/tab_tracker_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void TabTrackerService::Bind(

void TabTrackerService::UpdateTab(int32_t tab_id, mojom::TabDataPtr tab) {
auto allowed_scheme =
tab && base::Contains(kAllowedContentSchemes, tab->url.scheme());
tab && kAllowedContentSchemes.contains(tab->url.scheme());

auto it = base::ranges::find_if(
tabs_, [tab_id](const auto& tab) { return tab->id == tab_id; });
Expand Down
4 changes: 1 addition & 3 deletions components/ai_chat/renderer/ai_chat_resource_sniffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <utility>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/functional/callback.h"
#include "base/macros/if.h"
Expand Down Expand Up @@ -51,8 +50,7 @@ std::unique_ptr<AIChatResourceSniffer> AIChatResourceSniffer::MaybeCreate(
// TODO(petemill): Allow some kind of config to be passed in to determine
// which hosts and paths to sniff, and how to parse it to a
// |mojom::PageContent|.
if (url.SchemeIsHTTPOrHTTPS() &&
base::Contains(kYouTubeHosts, url.host_piece()) &&
if (url.SchemeIsHTTPOrHTTPS() && kYouTubeHosts.contains(url.host_piece()) &&
base::EqualsCaseInsensitiveASCII(url.path_piece(),
kYouTubePlayerAPIPath)) {
return base::WrapUnique(new AIChatResourceSniffer(std::move(delegate)));
Expand Down
5 changes: 2 additions & 3 deletions components/ai_chat/renderer/page_content_extractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/containers/flat_tree.h"
#include "base/containers/span.h"
Expand Down Expand Up @@ -173,7 +172,7 @@ void PageContentExtractor::ExtractPageContent(

if (origin.is_valid()) {
std::string host = origin.host();
if (base::Contains(kYouTubeHosts, host)) {
if (kYouTubeHosts.contains(host)) {
VLOG(1) << "YouTube transcript type";
// Do Youtube extraction
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
Expand All @@ -193,7 +192,7 @@ void PageContentExtractor::ExtractPageContent(
blink::mojom::WantResultOption::kWantResult,
blink::mojom::PromiseResultOption::kAwait);
return;
} else if (base::Contains(kVideoTrackHosts, host)) {
} else if (kVideoTrackHosts.contains(host)) {
VLOG(1) << "Video track transcript type";
// Video <track> extraction
// TODO(petemill): Use heuristics to determine if page's main focus is
Expand Down
22 changes: 11 additions & 11 deletions components/ai_chat/renderer/page_text_distilling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include <vector>

#include "base/compiler_specific.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_map.h"
#include "base/containers/fixed_flat_set.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
Expand Down Expand Up @@ -50,18 +50,18 @@ namespace ai_chat {

namespace {

static const ax::mojom::Role kContentParentRoles[]{
constexpr auto kContentParentRoles = base::MakeFixedFlatSet<ax::mojom::Role>({
ax::mojom::Role::kMain,
ax::mojom::Role::kArticle,
};
});

static const ax::mojom::Role kContentRoles[]{
constexpr auto kContentRoles = base::MakeFixedFlatSet<ax::mojom::Role>({
ax::mojom::Role::kHeading,
ax::mojom::Role::kParagraph,
ax::mojom::Role::kNote,
};
});

static const ax::mojom::Role kRolesToSkip[]{
constexpr auto kRolesToSkip = base::MakeFixedFlatSet<ax::mojom::Role>({
ax::mojom::Role::kAudio,
ax::mojom::Role::kBanner,
ax::mojom::Role::kButton,
Expand All @@ -83,7 +83,7 @@ static const ax::mojom::Role kRolesToSkip[]{
ax::mojom::Role::kSlider,
ax::mojom::Role::kSpinButton,
ax::mojom::Role::kSearchBox,
};
});

void GetContentRootNodes(const ui::AXNode* root,
std::vector<const ui::AXNode*>* content_root_nodes) {
Expand All @@ -94,7 +94,7 @@ void GetContentRootNodes(const ui::AXNode* root,
queue.pop();
// If a main or article node is found, add it to the list of content root
// nodes and continue. Do not explore children for nested article nodes.
if (base::Contains(kContentParentRoles, node->GetRole())) {
if (kContentParentRoles.contains(node->GetRole())) {
content_root_nodes->push_back(node);
continue;
}
Expand All @@ -107,11 +107,11 @@ void GetContentRootNodes(const ui::AXNode* root,

void AddContentNodesToVector(const ui::AXNode* node,
std::vector<const ui::AXNode*>* content_nodes) {
if (base::Contains(kContentRoles, node->GetRole())) {
if (kContentRoles.contains(node->GetRole())) {
content_nodes->emplace_back(node);
return;
}
if (base::Contains(kRolesToSkip, node->GetRole())) {
if (kRolesToSkip.contains(node->GetRole())) {
return;
}
for (auto iter = node->UnignoredChildrenBegin();
Expand All @@ -124,7 +124,7 @@ void AddTextNodesToVector(const ui::AXNode* node,
std::vector<std::u16string>* strings) {
const ui::AXNodeData& node_data = node->data();

if (base::Contains(kRolesToSkip, node_data.role)) {
if (kRolesToSkip.contains(node_data.role)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <utility>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/containers/flat_set.h"
#include "base/logging.h"
Expand Down Expand Up @@ -261,8 +260,7 @@ mojom::CreativeSearchResultAdInfoPtr ExtractMojomEntity(
return {};
}

if (base::Contains(kRequiredCreativeAdPropertyNames,
mojom_property->name)) {
if (kRequiredCreativeAdPropertyNames.contains(mojom_property->name)) {
if (!ExtractCreativeAdMojomProperty(mojom_property,
mojom_creative_ad.get())) {
VLOG(6) << "Failed to extract creative search result ad property "
Expand All @@ -271,8 +269,8 @@ mojom::CreativeSearchResultAdInfoPtr ExtractMojomEntity(
}

creative_ad_property_names.insert(mojom_property->name);
} else if (base::Contains(kCreativeSetConversionPropertyNames,
mojom_property->name)) {
} else if (kCreativeSetConversionPropertyNames.contains(
mojom_property->name)) {
if (!ExtractCreativeSetConversionMojomProperty(
mojom_property, mojom_creative_set_conversion.get())) {
VLOG(6) << "Failed to extract creative set conversion property "
Expand Down
Loading

0 comments on commit 5dba5a2

Please sign in to comment.