Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeHealth] Migrate constants to use string_view #26710

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions chromium_src/net/base/lookup_string_in_fixed_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,21 @@ int LookupSuffixInReversedSet(base::span<const uint8_t> graph,
// omnibox, it will be parsed as OmniboxInputType::URL input type instead of
// OmniboxInputType::UNKNOWN, The first entry in the autocomplete list will be
// URL instead of search.
for (auto* unstoppable_domain : decentralized_dns::kUnstoppableDomains) {
if (host.ends_with(unstoppable_domain)) {
*suffix_length = strlen(unstoppable_domain) - 1;
return kDafsaFound;
}
if (auto domain = decentralized_dns::GetUnstoppableDomainSuffix(host)) {
*suffix_length = domain->size() - 1;
return kDafsaFound;
}
if (host.ends_with(decentralized_dns::kEthDomain)) {
*suffix_length = strlen(decentralized_dns::kEthDomain) - 1;
*suffix_length = decentralized_dns::kEthDomain.size() - 1;
return kDafsaFound;
}
if (host.ends_with(decentralized_dns::kSolDomain)) {
*suffix_length = strlen(decentralized_dns::kSolDomain) - 1;
*suffix_length = decentralized_dns::kSolDomain.size() - 1;
return kDafsaFound;
}

if (include_private && host.ends_with(decentralized_dns::kDNSForEthDomain)) {
*suffix_length = strlen(decentralized_dns::kDNSForEthDomain) - 1;
*suffix_length = decentralized_dns::kDNSForEthDomain.size() - 1;
return kDafsaFound;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ void DecentralizedDnsOptInPage::PopulateInterstitialStrings(
const std::u16string sol_domain = base::ASCIIToUTF16(std::string(kSolDomain));
const std::u16string eth_domain = base::ASCIIToUTF16(std::string(kEthDomain));

std::u16string unstoppable_domains;
for (auto* const domain : kUnstoppableDomains) {
unstoppable_domains =
base::StrCat({unstoppable_domains, base::ASCIIToUTF16(domain), u", "});
}
if (!unstoppable_domains.empty()) {
unstoppable_domains.resize(unstoppable_domains.size() - 2);
}

if (IsUnstoppableDomainsTLD(request_url_.host_piece())) {
load_time_data.Set("tabTitle", brave_l10n::GetLocalizedResourceUTF16String(
IDS_UNSTOPPABLE_DOMAINS_OPT_IN_TITLE));
Expand All @@ -99,7 +90,7 @@ void DecentralizedDnsOptInPage::PopulateInterstitialStrings(
base::ReplaceStringPlaceholders(
brave_l10n::GetLocalizedResourceUTF16String(
IDS_UNSTOPPABLE_DOMAINS_AND_ENS_OPT_IN_PRIMARY_PARAGRAPH),
{infura, unstoppable_domains,
{infura, base::ASCIIToUTF16(GetUnstoppableDomainSuffixFullList()),
brave_l10n::GetLocalizedResourceUTF16String(
IDS_UNSTOPPABLE_DOMAINS_OPT_IN_TITLE),
infura_tou, infura_privacy_policy},
Expand Down
7 changes: 1 addition & 6 deletions components/decentralized_dns/core/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ void MigrateObsoleteLocalStatePrefs(PrefService* local_state) {
}

bool IsUnstoppableDomainsTLD(std::string_view host) {
for (auto* domain : kUnstoppableDomains) {
if (host.ends_with(domain)) {
return true;
}
}
return false;
return GetUnstoppableDomainSuffix(host).has_value();
}

void SetUnstoppableDomainsResolveMethod(PrefService* local_state,
Expand Down
11 changes: 7 additions & 4 deletions components/p3a/histograms_braveizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include "brave/components/p3a/histograms_braveizer.h"

#include <array>
#include <string>

#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/histogram_macros.h"
Expand All @@ -17,13 +20,13 @@ namespace {

// Please keep this list sorted and synced with |DoHistogramBravezation|.
// clang-format off
constexpr const char* kBravezationHistograms[] = {
constexpr auto kBravezationHistograms = std::to_array<std::string_view>({
goodov marked this conversation as resolved.
Show resolved Hide resolved
"DefaultBrowser.State",
"Extensions.LoadExtension",
"Tabs.TabCount",
"Tabs.TabCountPerLoad",
"Tabs.WindowCount",
};
});
// clang-format on

} // namespace
Expand All @@ -40,11 +43,11 @@ HistogramsBraveizer::HistogramsBraveizer() = default;
HistogramsBraveizer::~HistogramsBraveizer() = default;

void HistogramsBraveizer::InitCallbacks() {
for (const char* histogram_name : kBravezationHistograms) {
for (std::string_view histogram_name : kBravezationHistograms) {
histogram_sample_callbacks_.push_back(
std::make_unique<
base::StatisticsRecorder::ScopedHistogramSampleObserver>(
histogram_name,
std::string(histogram_name),
base::BindRepeating(&HistogramsBraveizer::DoHistogramBravetization,
this)));
}
Expand Down
1 change: 1 addition & 0 deletions net/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ source_set("unit_tests") {
testonly = true
sources = [
"base/brave_host_port_pair_unittest.cc",
"decentralized_dns/constants_unittest.cc",
"http/partitioned_host_state_map_unittest.cc",
"http/transport_security_state_unittest.cc",
]
Expand Down
96 changes: 96 additions & 0 deletions net/decentralized_dns/constants.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* 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 "brave/net/decentralized_dns/constants.h"

#include "base/containers/fixed_flat_set.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_util.h"

namespace decentralized_dns {

namespace {

// A struct to be used to guide the lookup of domain suffixes in the fixed flat
// set below. Lookups using this wrapper will match based on the suffix after
// the last '.' in the domain. Lookups for suffix can be done like:
//
// kUnstoppableDomains.contains(DomainSuffixLookup("https://foo.crypto"))
//
struct DomainSuffixLookup {
goodov marked this conversation as resolved.
Show resolved Hide resolved
constexpr explicit DomainSuffixLookup(std::string_view domain)
: domain(ExtractSuffix(domain)) {}

// Used to initialise the view from the last '.' in the domain, if any exist.
// Otherwise, passes the whole string for comparison.
static std::string_view ExtractSuffix(std::string_view domain) {
size_t pos = domain.find_last_of('.');
return pos != std::string_view::npos ? domain.substr(pos) : domain;
}

std::string_view domain;
};

// A custom comparator with strict weak ordering, to allow the lookup of a
// domain with `DomainSuffixLookup`.
struct SuffixComparator {
using is_transparent = void;

constexpr bool operator()(const std::string_view& lhs,
const std::string_view& rhs) const {
return lhs < rhs;
}

constexpr bool operator()(const std::string_view& lhs,
DomainSuffixLookup rhs) const {
return (*this)(lhs, rhs.domain);
}

constexpr bool operator()(DomainSuffixLookup lhs,
const std::string_view& rhs) const {
return (*this)(lhs.domain, rhs);
}
};

inline constexpr auto kUnstoppableDomains =
base::MakeFixedFlatSet<std::string_view>(
{".crypto", ".x", ".nft", ".dao", ".wallet",
".blockchain", ".bitcoin", ".zil", ".altimist", ".anime",
".klever", ".manga", ".polygon", ".unstoppable", ".pudgy",
".tball", ".stepn", ".secret", ".raiin", ".pog",
".clay", ".metropolis", ".witg", ".ubu", ".kryptic",
".farms", ".dfz", ".kresus", ".binanceus", ".austin",
".bitget", ".wrkx"},
SuffixComparator());

// Ensure all domain suffixes start with `.`
constexpr bool CheckAllDomainSuffixesStartWithDot() {
for (const auto& domain : kUnstoppableDomains) {
if (domain[0] != '.' && base::ranges::count(domain, '.') == 1) {
return false;
}
}
return true;
}
static_assert(CheckAllDomainSuffixesStartWithDot(),
"kUnstoppableDomains must start with a '.', and only one "
"occurence of the '.'");

} // namespace

std::optional<std::string_view> GetUnstoppableDomainSuffix(
std::string_view host) {
auto domain = kUnstoppableDomains.find(DomainSuffixLookup(host));
if (domain == kUnstoppableDomains.end()) {
return std::nullopt;
}
return *domain;
}

std::string GetUnstoppableDomainSuffixFullList() {
return base::JoinString(kUnstoppableDomains, ", ");
}

} // namespace decentralized_dns
26 changes: 15 additions & 11 deletions net/decentralized_dns/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@
#ifndef BRAVE_NET_DECENTRALIZED_DNS_CONSTANTS_H_
#define BRAVE_NET_DECENTRALIZED_DNS_CONSTANTS_H_

#include <optional>
#include <string>
#include <string_view>

#include "net/base/net_export.h"

namespace decentralized_dns {

inline constexpr const char* kUnstoppableDomains[] = {
".crypto", ".x", ".nft", ".dao", ".wallet",
".blockchain", ".bitcoin", ".zil", ".altimist", ".anime",
".klever", ".manga", ".polygon", ".unstoppable", ".pudgy",
".tball", ".stepn", ".secret", ".raiin", ".pog",
".clay", ".metropolis", ".witg", ".ubu", ".kryptic",
".farms", ".dfz", ".kresus", ".binanceus", ".austin",
".bitget", ".wrkx"};
inline constexpr std::string_view kEthDomain = ".eth";
inline constexpr std::string_view kDNSForEthDomain = ".eth.link";
inline constexpr std::string_view kSolDomain = ".sol";
cdesouza-chromium marked this conversation as resolved.
Show resolved Hide resolved

inline constexpr char kEthDomain[] = ".eth";
inline constexpr char kDNSForEthDomain[] = ".eth.link";
// Checks if a given host ends with the suffix of an unstoppable domain, e.g.
// foo.crypto. Returns a reference to the domain entry.
NET_EXPORT std::optional<std::string_view> GetUnstoppableDomainSuffix(
std::string_view host);

inline constexpr char kSolDomain[] = ".sol";
// Returns a full list of unstoppable domain suffixes separated by commas.
NET_EXPORT std::string GetUnstoppableDomainSuffixFullList();

} // namespace decentralized_dns

Expand Down
27 changes: 27 additions & 0 deletions net/decentralized_dns/constants_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* 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 "brave/net/decentralized_dns/constants.h"

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::Optional;

namespace decentralized_dns {

TEST(DecentralisedDnsConstantsTest, UnstoppableDomainsSuffixLookup) {
EXPECT_THAT(GetUnstoppableDomainSuffix("https://foo.crypto"),
Optional(std::string_view(".crypto")));
EXPECT_THAT(GetUnstoppableDomainSuffix("https://foo.bar.crypto"),
Optional(std::string_view(".crypto")));
EXPECT_FALSE(
GetUnstoppableDomainSuffix("https://foo.bar.crypto.unknown").has_value());
EXPECT_THAT(GetUnstoppableDomainSuffix("https://foo.unstoppable"),
Optional(std::string_view(".unstoppable")));
EXPECT_FALSE(GetUnstoppableDomainSuffix("https://unstoppable").has_value());
}

} // namespace decentralized_dns
1 change: 1 addition & 0 deletions net/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# You can obtain one at http://mozilla.org/MPL/2.0/.

brave_net_sources = [
"//brave/net/decentralized_dns/constants.cc",
"//brave/net/decentralized_dns/constants.h",
"//brave/net/http/partitioned_host_state_map.cc",
"//brave/net/http/partitioned_host_state_map.h",
Expand Down
Loading