Skip to content

Commit 5dba5a2

Browse files
[CodeHealth] Use cxx20's T::contains pt.2
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
1 parent e858e32 commit 5dba5a2

25 files changed

+131
-135
lines changed

browser/permissions/permission_origin_lifetime_monitor_impl.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include <utility>
99

10-
#include "base/containers/contains.h"
1110
#include "brave/browser/ephemeral_storage/ephemeral_storage_service_factory.h"
1211
#include "brave/components/brave_wallet/browser/permission_utils.h"
1312
#include "brave/components/ephemeral_storage/ephemeral_storage_service.h"
@@ -54,7 +53,7 @@ PermissionOriginLifetimeMonitorImpl::SubscribeToPermissionOriginDestruction(
5453
}
5554
std::string storage_domain = net::URLToEphemeralStorageDomain(
5655
is_sub_request_origin ? sub_request_origin.GetURL() : requesting_origin);
57-
if (!base::Contains(active_subscriptions_, storage_domain)) {
56+
if (!active_subscriptions_.contains(storage_domain)) {
5857
active_subscriptions_.insert(storage_domain);
5958
}
6059
return storage_domain;

browser/ui/brave_browser_command_controller.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
#include "brave/browser/ui/brave_browser_command_controller.h"
77

88
#include <optional>
9-
#include <vector>
109

11-
#include "base/containers/contains.h"
10+
#include "base/containers/fixed_flat_set.h"
1211
#include "base/feature_list.h"
1312
#include "base/types/to_address.h"
1413
#include "brave/app/brave_command_ids.h"
@@ -76,11 +75,11 @@ bool IsBraveCommands(int id) {
7675
}
7776

7877
bool IsBraveOverrideCommands(int id) {
79-
static std::vector<int> override_commands({
78+
static constexpr auto kOverrideCommands = base::MakeFixedFlatSet<int>({
8079
IDC_NEW_WINDOW,
8180
IDC_NEW_INCOGNITO_WINDOW,
8281
});
83-
return base::Contains(override_commands, id);
82+
return kOverrideCommands.contains(id);
8483
}
8584

8685
} // namespace

chromium_src/components/sessions/content/content_serialized_navigation_driver.cc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
* You can obtain one at http://mozilla.org/MPL/2.0/. */
55

6+
#include "components/sessions/content/content_serialized_navigation_driver.h"
7+
68
#include <string>
79

8-
#include "base/containers/contains.h"
9-
#include "components/sessions/content/content_serialized_navigation_driver.h"
10+
#include "base/containers/fixed_flat_set.h"
1011
#include "components/sessions/core/serialized_navigation_entry.h"
1112
#include "content/public/common/url_constants.h"
1213

@@ -17,18 +18,22 @@
1718

1819
namespace sessions {
1920

21+
namespace {
22+
23+
// Extension can override below three chrome urls.
24+
// https://source.chromium.org/chromium/chromium/src/+/main:chrome/common/extensions/api/chrome_url_overrides.idl
25+
constexpr auto kAllowedChromeUrlsOverridingHostList =
26+
base::MakeFixedFlatSet<std::string_view>(
27+
{"newtab", "history", "bookmarks"});
28+
29+
} // namespace
30+
2031
std::string ContentSerializedNavigationDriver::GetSanitizedPageStateForPickle(
2132
const sessions::SerializedNavigationEntry* navigation) const {
22-
// Extension can override below three chrome urls.
23-
// https://source.chromium.org/chromium/chromium/src/+/main:chrome/common/extensions/api/chrome_url_overrides.idl
24-
constexpr std::array<const char*, 3> kAllowedChromeUrlsOverridingHostList = {
25-
"newtab", "history", "bookmarks"};
26-
2733
const auto& virtual_url = navigation->virtual_url();
2834
if (virtual_url.SchemeIs(content::kChromeUIScheme)) {
2935
// If empty string is returned, chrome url overriding is ignored.
30-
if (base::Contains(kAllowedChromeUrlsOverridingHostList,
31-
virtual_url.host())) {
36+
if (kAllowedChromeUrlsOverridingHostList.contains(virtual_url.host())) {
3237
// chrome url can be re-written when it's restored during the tab but
3338
// re-written url is ignored when encoded page state is empty.
3439
// In ContentSerializedNavigationBuilder::ToNavigationEntry(), re-written

chromium_src/components/sync_device_info/device_info_sync_bridge.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void DeviceInfoSyncBridge::RefreshLocalDeviceInfoIfNeeded() {
130130
return;
131131
}
132132

133-
if (!base::Contains(all_data_, current_info->guid())) {
133+
if (!all_data_.contains(current_info->guid())) {
134134
// After initiating leave the sync chain `DeleteSpecifics` cleans
135135
// `all_data_` map.
136136
// It is possible that user close sync settings page or change the data type

chromium_src/components/webapps/browser/banners/app_banner_manager.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
44
// You can obtain one at https://mozilla.org/MPL/2.0/.
55

6-
#include "base/containers/contains.h"
76
#include "brave/components/constants/webui_url_constants.h"
87

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

1817
#include "src/components/webapps/browser/banners/app_banner_manager.cc"
1918
#undef kChromeUIPasswordManagerHost

chromium_src/third_party/blink/common/origin_trials/origin_trials.cc

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include <string_view>
99

10-
#include "base/containers/contains.h"
10+
#include "base/containers/fixed_flat_set.h"
1111

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

2020
namespace blink::origin_trials {
2121

22+
namespace {
23+
24+
// When updating also update the array in the overload below.
25+
constexpr auto kBraveDisabledTrialNames =
26+
base::MakeFixedFlatSet<std::string_view>({
27+
"AdInterestGroupAPI",
28+
"DeviceAttributes",
29+
"DigitalGoodsV2",
30+
"InterestCohortAPI",
31+
"FencedFrames",
32+
"Fledge",
33+
"Parakeet",
34+
"SignedExchangeSubresourcePrefetch",
35+
"SubresourceWebBundles",
36+
});
37+
38+
constexpr auto kBraveDisabledTrialFeatures =
39+
base::MakeFixedFlatSet<blink::mojom::OriginTrialFeature>({
40+
blink::mojom::OriginTrialFeature::kAdInterestGroupAPI,
41+
blink::mojom::OriginTrialFeature::kDigitalGoods,
42+
blink::mojom::OriginTrialFeature::kParakeet,
43+
});
44+
45+
} // namespace
46+
2247
bool IsTrialDisabledInBrave(std::string_view trial_name) {
23-
// When updating also update the array in the overload below.
24-
static const char* const kBraveDisabledTrialNames[] = {
25-
"AdInterestGroupAPI",
26-
"DeviceAttributes",
27-
"DigitalGoodsV2",
28-
"InterestCohortAPI",
29-
"FencedFrames",
30-
"Fledge",
31-
"Parakeet",
32-
"SignedExchangeSubresourcePrefetch",
33-
"SubresourceWebBundles",
34-
};
35-
36-
if (base::Contains(kBraveDisabledTrialNames, trial_name)) {
48+
if (kBraveDisabledTrialNames.contains(trial_name)) {
3749
// Check if this is still a valid trial name in Chromium. If not, it needs
3850
// to be changed as in Chromium or removed.
3951
DCHECK(IsTrialValid_ChromiumImpl(trial_name));
@@ -45,14 +57,7 @@ bool IsTrialDisabledInBrave(std::string_view trial_name) {
4557

4658
bool IsTrialDisabledInBrave(blink::mojom::OriginTrialFeature feature) {
4759
// When updating also update the array in the overload above.
48-
static const blink::mojom::OriginTrialFeature kBraveDisabledTrialFeatures[] =
49-
{
50-
blink::mojom::OriginTrialFeature::kAdInterestGroupAPI,
51-
blink::mojom::OriginTrialFeature::kDigitalGoods,
52-
blink::mojom::OriginTrialFeature::kParakeet,
53-
};
54-
55-
return base::Contains(kBraveDisabledTrialFeatures, feature);
60+
return kBraveDisabledTrialFeatures.contains(feature);
5661
}
5762

5863
bool IsTrialValid(std::string_view trial_name) {

components/ai_chat/content/browser/page_content_fetcher.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "base/check.h"
2020
#include "base/containers/checked_iterators.h"
21-
#include "base/containers/contains.h"
2221
#include "base/containers/fixed_flat_set.h"
2322
#include "base/functional/bind.h"
2423
#include "base/functional/callback.h"
@@ -203,7 +202,7 @@ class PageContentFetcherInternal {
203202
return;
204203
}
205204
DVLOG(1) << "OnTabContentResult: " << data.get();
206-
const bool is_video = base::Contains(kVideoPageContentTypes, data->type);
205+
const bool is_video = kVideoPageContentTypes.contains(data->type);
207206
DVLOG(1) << "Is video? " << is_video;
208207
// Handle text mode response
209208
if (!is_video) {
@@ -456,7 +455,7 @@ void PageContentFetcher::FetchPageContent(std::string_view invalidation_token,
456455

457456
auto url = web_contents_->GetLastCommittedURL();
458457
#if BUILDFLAG(ENABLE_TEXT_RECOGNITION)
459-
if (base::Contains(kScreenshotRetrievalHosts, url.host_piece())) {
458+
if (kScreenshotRetrievalHosts.contains(url.host_piece())) {
460459
content::RenderWidgetHostView* view =
461460
web_contents_->GetRenderWidgetHostView();
462461
if (view) {

components/ai_chat/core/browser/tab_tracker_service.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void TabTrackerService::Bind(
2424

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

2929
auto it = base::ranges::find_if(
3030
tabs_, [tab_id](const auto& tab) { return tab->id == tab_id; });

components/ai_chat/renderer/ai_chat_resource_sniffer.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <utility>
1313

1414
#include "base/check.h"
15-
#include "base/containers/contains.h"
1615
#include "base/containers/fixed_flat_set.h"
1716
#include "base/functional/callback.h"
1817
#include "base/macros/if.h"
@@ -51,8 +50,7 @@ std::unique_ptr<AIChatResourceSniffer> AIChatResourceSniffer::MaybeCreate(
5150
// TODO(petemill): Allow some kind of config to be passed in to determine
5251
// which hosts and paths to sniff, and how to parse it to a
5352
// |mojom::PageContent|.
54-
if (url.SchemeIsHTTPOrHTTPS() &&
55-
base::Contains(kYouTubeHosts, url.host_piece()) &&
53+
if (url.SchemeIsHTTPOrHTTPS() && kYouTubeHosts.contains(url.host_piece()) &&
5654
base::EqualsCaseInsensitiveASCII(url.path_piece(),
5755
kYouTubePlayerAPIPath)) {
5856
return base::WrapUnique(new AIChatResourceSniffer(std::move(delegate)));

components/ai_chat/renderer/page_content_extractor.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
#include "base/check.h"
2525
#include "base/check_op.h"
26-
#include "base/containers/contains.h"
2726
#include "base/containers/fixed_flat_set.h"
2827
#include "base/containers/flat_tree.h"
2928
#include "base/containers/span.h"
@@ -173,7 +172,7 @@ void PageContentExtractor::ExtractPageContent(
173172

174173
if (origin.is_valid()) {
175174
std::string host = origin.host();
176-
if (base::Contains(kYouTubeHosts, host)) {
175+
if (kYouTubeHosts.contains(host)) {
177176
VLOG(1) << "YouTube transcript type";
178177
// Do Youtube extraction
179178
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
@@ -193,7 +192,7 @@ void PageContentExtractor::ExtractPageContent(
193192
blink::mojom::WantResultOption::kWantResult,
194193
blink::mojom::PromiseResultOption::kAwait);
195194
return;
196-
} else if (base::Contains(kVideoTrackHosts, host)) {
195+
} else if (kVideoTrackHosts.contains(host)) {
197196
VLOG(1) << "Video track transcript type";
198197
// Video <track> extraction
199198
// TODO(petemill): Use heuristics to determine if page's main focus is

0 commit comments

Comments
 (0)