Skip to content

Commit

Permalink
[spanify] Avoid base::make_span (#26813)
Browse files Browse the repository at this point in the history
This PR removes various uses of `base::make_span` across the codebase,
in favour of the existing constructors that can be deduced with the use
of CTAD.

Resolves brave/brave-browser#42624
  • Loading branch information
cdesouza-chromium authored Dec 3, 2024
1 parent deb39c7 commit 6aae0b2
Show file tree
Hide file tree
Showing 32 changed files with 79 additions and 117 deletions.
3 changes: 1 addition & 2 deletions browser/brave_ads/device_id/device_id_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ std::optional<std::string> ComputeHmacSha256(std::string_view key,
std::string_view text) {
crypto::HMAC hmac(crypto::HMAC::SHA256);
std::array<uint8_t, 32u> digest;
if (!hmac.Init(key) ||
!hmac.Sign(base::as_bytes(base::make_span(text)), digest)) {
if (!hmac.Init(key) || !hmac.Sign(base::as_byte_span(text), digest)) {
return std::nullopt;
}

Expand Down
4 changes: 2 additions & 2 deletions browser/brave_ads/device_id/device_id_impl_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ class MacAddressProcessor {
}

constexpr size_t kMacLength = 6u;
auto mac_address_bytes = base::as_bytes(
UNSAFE_TODO(base::make_span(ifinfo.ifr_hwaddr.sa_data, kMacLength)));
auto mac_address_bytes =
base::as_byte_span(ifinfo.ifr_hwaddr.sa_data).first<kMacLength>();
if (!is_valid_mac_address_callback_.Run(mac_address_bytes)) {
return keep_going;
}
Expand Down
4 changes: 1 addition & 3 deletions browser/brave_ads/device_id/device_id_impl_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ class MacAddressProcessor {
return keep_going;
}

auto mac_address_bytes = base::as_bytes(UNSAFE_TODO(base::make_span(
CFDataGetBytePtr(mac_address_data.get()),
base::checked_cast<size_t>(CFDataGetLength(mac_address_data.get())))));
auto mac_address_bytes = base::apple::CFDataToSpan(mac_address_data.get());
if (!is_valid_mac_address_callback_.Run(mac_address_bytes)) {
return keep_going;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ bool WireguardGenerateKeypair(std::string* public_key,
return false;
}

*public_key = base::Base64Encode(base::make_span(public_key_bytes));
*private_key = base::Base64Encode(base::make_span(private_key_bytes));
*public_key = base::Base64Encode(public_key_bytes);
*private_key = base::Base64Encode(private_key_bytes);
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions browser/default_protocol_handler_utils_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ std::wstring HashString(base::wcstring_view input) {
}

uint32_t hash[2] = {h0 ^ h1, h0_acc ^ h1_acc};
return base::UTF8ToWide(
base::Base64Encode(base::as_bytes(base::make_span(hash))));
return base::UTF8ToWide(base::Base64Encode(base::as_byte_span(hash)));
}

std::wstring FormatUserChoiceString(std::wstring_view ext,
Expand Down
6 changes: 2 additions & 4 deletions browser/ui/webui/ai_chat/ai_chat_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ AIChatUI::AIChatUI(content::WebUI* web_ui)
content::WebUIDataSource::CreateAndAdd(
web_ui->GetWebContents()->GetBrowserContext(), kChatUIURL);

webui::SetupWebUIDataSource(
untrusted_source,
UNSAFE_TODO(base::make_span(kAiChatUiGenerated, kAiChatUiGeneratedSize)),
IDR_CHAT_UI_HTML);
webui::SetupWebUIDataSource(untrusted_source, kAiChatUiGenerated,
IDR_CHAT_UI_HTML);

untrusted_source->AddResourcePath("styles.css", IDR_CHAT_UI_CSS);

Expand Down
7 changes: 2 additions & 5 deletions browser/ui/webui/brave_rewards/rewards_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,8 @@ RewardsPanelUI::RewardsPanelUI(content::WebUI* web_ui)
web_ui->GetWebContents()->GetBrowserContext(), kBraveRewardsPanelHost);
source->AddLocalizedStrings(kStrings);

webui::SetupWebUIDataSource(
source,
UNSAFE_TODO(base::make_span(kBraveRewardsPanelGenerated,
kBraveRewardsPanelGeneratedSize)),
IDR_BRAVE_REWARDS_PANEL_HTML);
webui::SetupWebUIDataSource(source, kBraveRewardsPanelGenerated,
IDR_BRAVE_REWARDS_PANEL_HTML);

// Adaptive captcha challenges are displayed in an iframe on the Rewards
// panel. In order to display these challenges we need to specify in CSP that
Expand Down
5 changes: 1 addition & 4 deletions browser/ui/webui/brave_rewards/tip_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ TipPanelUI::TipPanelUI(content::WebUI* web_ui)

source->AddLocalizedStrings(kStrings);

webui::SetupWebUIDataSource(
source,
UNSAFE_TODO(base::make_span(kTipPanelGenerated, kTipPanelGeneratedSize)),
IDR_TIP_PANEL_HTML);
webui::SetupWebUIDataSource(source, kTipPanelGenerated, IDR_TIP_PANEL_HTML);

source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ImgSrc,
Expand Down
7 changes: 2 additions & 5 deletions browser/ui/webui/brave_shields/cookie_list_opt_in_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ CookieListOptInUI::CookieListOptInUI(content::WebUI* web_ui)
web_ui->GetWebContents()->GetBrowserContext(), kCookieListOptInHost);
source->AddLocalizedStrings(kStrings);

webui::SetupWebUIDataSource(
source,
UNSAFE_TODO(base::make_span(kCookieListOptInGenerated,
kCookieListOptInGeneratedSize)),
IDR_COOKIE_LIST_OPT_IN_HTML);
webui::SetupWebUIDataSource(source, kCookieListOptInGenerated,
IDR_COOKIE_LIST_OPT_IN_HTML);

content::URLDataSource::Add(
profile, std::make_unique<FaviconSource>(
Expand Down
7 changes: 2 additions & 5 deletions browser/ui/webui/brave_shields/shields_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ ShieldsPanelUI::ShieldsPanelUI(content::WebUI* web_ui)
profile_, std::make_unique<FaviconSource>(
profile_, chrome::FaviconUrlFormat::kFavicon2));

webui::SetupWebUIDataSource(
source,
UNSAFE_TODO(base::make_span(kBraveShieldsPanelGenerated,
kBraveShieldsPanelGeneratedSize)),
IDR_SHIELDS_PANEL_HTML);
webui::SetupWebUIDataSource(source, kBraveShieldsPanelGenerated,
IDR_SHIELDS_PANEL_HTML);
}

ShieldsPanelUI::~ShieldsPanelUI() = default;
Expand Down
7 changes: 2 additions & 5 deletions browser/ui/webui/brave_vpn/vpn_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ VPNPanelUI::VPNPanelUI(content::WebUI* web_ui)
web_ui->GetWebContents()->GetBrowserContext(), kVPNPanelURL);

brave_vpn::AddLocalizedStrings(source);
webui::SetupWebUIDataSource(
source,
UNSAFE_TODO(base::make_span(kBraveVpnPanelGenerated,
kBraveVpnPanelGeneratedSize)),
IDR_VPN_PANEL_HTML);
webui::SetupWebUIDataSource(source, kBraveVpnPanelGenerated,
IDR_VPN_PANEL_HTML);

source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::StyleSrc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void RecentTabsSubMenuModelTest::VerifyModel(

// The first two commands are History and History Clusters, but we disable
// History Clusters and upstream won't show it, so we should skip one command.
::VerifyModel(model, base::make_span(data).subspan(1));
::VerifyModel(model, base::span(data).subspan(1));
}

void RecentTabsSubMenuModelTest::VerifyModel(const ui::MenuModel* model,
Expand Down
2 changes: 1 addition & 1 deletion components/ai_chat/core/browser/conversation_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ void ConversationHandler::RateMessage(bool is_liked,

if (current_turn_id <= history.size()) {
base::span<const mojom::ConversationTurnPtr> history_slice =
base::make_span(history).first(current_turn_id);
base::span(history).first(current_turn_id);

feedback_api_->SendRating(
is_liked, ai_chat_service_->IsPremiumStatus(), history_slice,
Expand Down
2 changes: 1 addition & 1 deletion components/ai_chat/core/browser/text_embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void TextEmbedder::GetTopSimilarityWithPromptTilContextLimitInternal(
base::unexpected("TextEmbedder is not initialized."));
return;
}
auto text_hash = base::FastHash(base::as_bytes(base::make_span(text)));
auto text_hash = base::FastHash(base::as_byte_span(text));
if (text_hash != text_hash_) {
text_hash_ = text_hash;
segments_ = SplitSegments(text);
Expand Down
4 changes: 2 additions & 2 deletions components/ai_chat/renderer/page_content_extractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void PageContentExtractor::ExtractPageContent(
ai_chat::mojom::PageContentType::VideoTranscriptYouTube);
// Main world so that we can access a global variable
main_frame->RequestExecuteScript(
global_world_id_, base::make_span(&source, 1u),
global_world_id_, base::span_from_ref(source),
blink::mojom::UserActivationOption::kDoNotActivate,
blink::mojom::EvaluationTiming::kAsynchronous,
blink::mojom::LoadEventBlockingOption::kDoNotBlock,
Expand All @@ -208,7 +208,7 @@ void PageContentExtractor::ExtractPageContent(
ai_chat::mojom::PageContentType::VideoTranscriptYouTube);
// Main world so that we can access a global variable
main_frame->RequestExecuteScript(
isolated_world_id_, base::make_span(&source, 1u),
isolated_world_id_, base::span_from_ref(source),
blink::mojom::UserActivationOption::kDoNotActivate,
blink::mojom::EvaluationTiming::kAsynchronous,
blink::mojom::LoadEventBlockingOption::kDoNotBlock,
Expand Down
2 changes: 1 addition & 1 deletion components/ai_chat/renderer/page_text_distilling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void DistillPageText(
};

render_frame->GetWebFrame()->RequestExecuteScript(
isolated_world_id, UNSAFE_TODO(base::make_span(&source, 1u)),
isolated_world_id, base::span_from_ref(source),
blink::mojom::UserActivationOption::kDoNotActivate,
blink::mojom::EvaluationTiming::kAsynchronous,
blink::mojom::LoadEventBlockingOption::kDoNotBlock,
Expand Down
4 changes: 2 additions & 2 deletions components/brave_news/browser/feed_generation_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ FeedGenerationInfo::FeedGenerationInfo(
locale_(locale),
channels_(std::move(channels)),
suggested_publisher_ids_(suggested_publisher_ids),
suggested_publisher_ids_span_(base::make_span(suggested_publisher_ids_)) {
suggested_publisher_ids_span_(base::span(suggested_publisher_ids_)) {
this->feed_items_.reserve(feed_items.size());
for (const auto& item : feed_items) {
this->feed_items_.push_back(item->Clone());
Expand All @@ -210,7 +210,7 @@ FeedGenerationInfo::FeedGenerationInfo(
}
this->topics_.emplace_back(topic.first.Clone(), std::move(articles));
}
this->topics_span_ = base::make_span(this->topics_);
this->topics_span_ = base::span(this->topics_);
}

FeedGenerationInfo::FeedGenerationInfo(FeedGenerationInfo&&) = default;
Expand Down
6 changes: 2 additions & 4 deletions components/brave_rewards/core/common/request_signer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ std::vector<std::string> RequestSigner::GetSignedHeaders(
const std::string& request_content) {
CHECK(!request_target.empty());

std::string digest =
GetDigest(base::as_bytes(base::make_span(request_content)));
std::string digest = GetDigest(base::as_byte_span(request_content));

std::vector<std::pair<std::string, std::string>> headers = {
{"digest", digest}, {"(request-target)", request_target}};
Expand All @@ -105,8 +104,7 @@ std::string RequestSigner::SignHeaders(
message += key + ": " + value;
}

auto signed_message =
signer_.SignMessage(base::as_bytes(base::make_span(message)));
auto signed_message = signer_.SignMessage(base::as_byte_span(message));

return base::StrCat(
{"keyId=\"", key_id_, "\",algorithm=\"ed25519\",headers=\"", header_names,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class RewardsRequestSignerTest : public testing::Test {

TEST_F(RewardsRequestSignerTest, GetDigest) {
auto seed = Signer::GenerateRecoverySeed();
auto digest =
RequestSigner::GetDigest(base::as_bytes(base::make_span("hello world")));
auto digest = RequestSigner::GetDigest(
base::byte_span_with_nul_from_cstring("hello world"));
EXPECT_EQ(digest, "SHA-256=QwZGhH5wNEwJ9Yc56Z1byW6sjV/nKVzxlrmGJ5h2v5s=");
}

Expand Down
4 changes: 2 additions & 2 deletions components/brave_rewards/core/common/signer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ TEST_F(RewardsSignerTest, SignMessage) {
"vasRmhvzIy1J8ij2pSBPvSHPk5DI5l3a08fGSj5JPex+"
"ZsQSLeH43VfOCuzGBuFunm1cTEMafWN4zTY++3Oz6A==");

auto signed_message = base::Base64Encode(
signer->SignMessage(base::as_bytes(base::make_span("hello world"))));
auto signed_message = base::Base64Encode(signer->SignMessage(
base::byte_span_with_nul_from_cstring("hello world")));

EXPECT_EQ(signed_message,
"Yskxukdvz9rLYytvkpsvn2QztIhSbEd9GyUhQ/dX18z/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ std::optional<std::string> PostConnectUphold::Content() const {
return std::nullopt;
}

std::string digest =
RequestSigner::GetDigest(base::as_bytes(base::make_span(octets)));
std::string digest = RequestSigner::GetDigest(base::as_byte_span(octets));

signer->set_key_id("primary");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ void SolanaWalletProvider::OnPostChallengesResponse(

std::string message =
base::StrCat({base::ToLowerASCII(wallet->payment_id), ".", challenge_id});
auto signed_message =
signer->SignMessage(base::as_bytes(base::make_span(message)));
auto signed_message = signer->SignMessage(base::as_byte_span(message));
std::string signature;
base::Base64UrlEncode(
signed_message, base::Base64UrlEncodePolicy::INCLUDE_PADDING, &signature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ TEST(HDKeyUnitTest, SignDer) {
HDKey::GenerateFromPrivateKey(private_key_bytes);

std::string message = "Very deterministic message";
auto hashed = DoubleSHA256Hash(base::as_bytes(base::make_span(message)));
auto hashed = DoubleSHA256Hash(base::as_byte_span(message));

auto signature = hdkey->SignDer(hashed);
// https://github.com/bitcoin/bitcoin/blob/v24.0/src/test/key_tests.cpp#L141
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ TEST(BtcLikeSerializerStreamTest, PushSizeAndBytes) {
BtcLikeSerializerStream stream(&data);
stream.PushSizeAndBytes(bytes);
EXPECT_EQ(data.size(), 1u + 10u);
EXPECT_EQ(base::HexEncode(base::make_span(data).first<1>()), "0A");
EXPECT_TRUE(base::ranges::all_of(base::make_span(data).last<10>(),
EXPECT_EQ(base::HexEncode(base::span(data).first<1>()), "0A");
EXPECT_TRUE(base::ranges::all_of(base::span(data).last<10>(),
[](auto c) { return c == 0xab; }));
EXPECT_EQ(stream.serialized_bytes(), 11u);
}
Expand All @@ -92,8 +92,8 @@ TEST(BtcLikeSerializerStreamTest, PushSizeAndBytes) {
BtcLikeSerializerStream stream(&data);
stream.PushSizeAndBytes(bytes);
EXPECT_EQ(data.size(), 3u + 300u);
EXPECT_EQ(base::HexEncode(base::make_span(data).first<3>()), "FD2C01");
EXPECT_TRUE(base::ranges::all_of(base::make_span(data).last<300>(),
EXPECT_EQ(base::HexEncode(base::span(data).first<3>()), "FD2C01");
EXPECT_TRUE(base::ranges::all_of(base::span(data).last<300>(),
[](auto c) { return c == 0xcd; }));
EXPECT_EQ(stream.serialized_bytes(), 303u);
}
Expand All @@ -104,8 +104,8 @@ TEST(BtcLikeSerializerStreamTest, PushSizeAndBytes) {
BtcLikeSerializerStream stream(&data);
stream.PushSizeAndBytes(bytes);
EXPECT_EQ(data.size(), 5u + 0x10000);
EXPECT_EQ(base::HexEncode(base::make_span(data).first<5>()), "FE00000100");
EXPECT_TRUE(base::ranges::all_of(base::make_span(data).last<0x10000>(),
EXPECT_EQ(base::HexEncode(base::span(data).first<5>()), "FE00000100");
EXPECT_TRUE(base::ranges::all_of(base::span(data).last<0x10000>(),
[](auto c) { return c == 0xef; }));
EXPECT_EQ(stream.serialized_bytes(), 65541u);
}
Expand Down
6 changes: 3 additions & 3 deletions components/brave_wallet/common/eth_abi_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ TEST(EthAbiUtilsTest, ExtractAddress) {
{
auto bytes = ToBytes(
"000000000000000000000000c1735677a60884abbcf72295e88d47764beda282");
EXPECT_EQ(ExtractAddress(base::make_span(bytes)).ToHex(),
EXPECT_EQ(ExtractAddress(bytes).ToHex(),
"0xc1735677a60884abbcf72295e88d47764beda282");
}

Expand All @@ -197,13 +197,13 @@ TEST(EthAbiUtilsTest, ExtractAddress) {
// Zero address.
auto bytes = ToBytes(
"0000000000000000000000000000000000000000000000000000000000000000");
EXPECT_EQ(ExtractAddress(base::make_span(bytes)).ToHex(),
EXPECT_EQ(ExtractAddress(base::span(bytes)).ToHex(),
"0x0000000000000000000000000000000000000000");
}

{ // Empty.
auto bytes = std::vector<uint8_t>{};
EXPECT_TRUE(ExtractAddress(base::make_span(bytes)).IsEmpty());
EXPECT_TRUE(ExtractAddress(base::span(bytes)).IsEmpty());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void ScriptInjectorRenderFrameObserver::RequestAsyncExecuteScript(
auto want_result = CheckIfWantResult(callback);

render_frame()->GetWebFrame()->RequestExecuteScript(
world_id, UNSAFE_TODO(base::make_span(&web_script_source, 1u)),
user_activation, blink::mojom::EvaluationTiming::kAsynchronous,
world_id, base::span_from_ref(web_script_source), user_activation,
blink::mojom::EvaluationTiming::kAsynchronous,
blink::mojom::LoadEventBlockingOption::kDoNotBlock,
base::BindOnce(
[](RequestAsyncExecuteScriptCallback callback,
Expand Down
3 changes: 1 addition & 2 deletions components/speedreader/speedreader_rewriter_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ std::string WrapStylesheetWithCSP(const std::string& stylesheet,

const auto make_style_data = [](std::string_view id, std::string_view data) {
const std::string& hash = crypto::SHA256HashString(data);
const std::string& sha256 =
base::Base64Encode(base::as_bytes(base::make_span(hash)));
const std::string& sha256 = base::Base64Encode(base::as_byte_span(hash));

return base::StrCat({"<script type=\"brave-style-data\" id=\"", id,
"\" integrity=\"", sha256, "\">", data, "</script>"});
Expand Down
3 changes: 1 addition & 2 deletions ios/browser/api/bookmarks/exporter/bookmark_html_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,7 @@ class Writer : public base::RefCountedThreadSafe<Writer> {
auto itr = favicons_map_->find(*url_string);
if (itr != favicons_map_->end()) {
scoped_refptr<base::RefCountedMemory> data(itr->second.get());
GURL favicon_url("data:image/png;base64," +
base::Base64Encode(base::make_span(*data)));
GURL favicon_url("data:image/png;base64," + base::Base64Encode(*data));
favicon_string = favicon_url.spec();
}

Expand Down
6 changes: 3 additions & 3 deletions ios/browser/api/certificate/brave_certificate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/ios/browser/api/certificate/brave_certificate.h"

#include "base/apple/foundation_util.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/sys_string_conversions.h"
Expand Down Expand Up @@ -54,9 +55,8 @@ - (nullable instancetype)initWithCertificate:(SecCertificateRef)certificate {
SecCertificateCopyKey(certificate));

bssl::UniquePtr<CRYPTO_BUFFER> cert_buffer(
net::x509_util::CreateCryptoBuffer(UNSAFE_TODO(base::make_span(
CFDataGetBytePtr(cert_data_.get()),
base::checked_cast<size_t>(CFDataGetLength(cert_data_.get()))))));
net::x509_util::CreateCryptoBuffer(
base::apple::CFDataToSpan(cert_data_.get())));

if (!cert_buffer) {
return nullptr;
Expand Down
Loading

0 comments on commit 6aae0b2

Please sign in to comment.